2014-04-30 04:20:32 +08:00
|
|
|
package gobot
|
|
|
|
|
|
|
|
import (
|
2014-04-30 23:10:44 +08:00
|
|
|
"log"
|
2014-04-30 04:20:32 +08:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
)
|
|
|
|
|
2014-07-19 04:18:01 +08:00
|
|
|
// JSONGobot holds a JSON representation of a Gobot.
|
2014-06-13 11:58:54 +08:00
|
|
|
type JSONGobot struct {
|
|
|
|
Robots []*JSONRobot `json:"robots"`
|
|
|
|
Commands []string `json:"commands"`
|
|
|
|
}
|
|
|
|
|
2014-07-19 04:18:01 +08:00
|
|
|
// Gobot is a container composed of one or more robots
|
2014-04-30 04:20:32 +08:00
|
|
|
type Gobot struct {
|
2014-06-24 11:33:59 +08:00
|
|
|
robots *robots
|
2014-07-03 09:08:44 +08:00
|
|
|
commands map[string]func(map[string]interface{}) interface{}
|
2014-06-13 11:58:54 +08:00
|
|
|
trap func(chan os.Signal)
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
|
|
|
|
2014-07-19 04:18:01 +08:00
|
|
|
// NewGobot instantiates a new Gobot
|
2014-04-30 04:20:32 +08:00
|
|
|
func NewGobot() *Gobot {
|
|
|
|
return &Gobot{
|
2014-06-24 11:33:59 +08:00
|
|
|
robots: &robots{},
|
2014-07-03 09:08:44 +08:00
|
|
|
commands: make(map[string]func(map[string]interface{}) interface{}),
|
2014-04-30 04:20:32 +08:00
|
|
|
trap: func(c chan os.Signal) {
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-19 04:18:01 +08:00
|
|
|
/*
|
|
|
|
AddCommand creates a new command and adds it to the Gobot. This command
|
|
|
|
will be available via HTTP using '/commands/name'
|
|
|
|
|
|
|
|
Example:
|
|
|
|
gbot.AddCommand( 'rollover', func( params map[string]interface{}) interface{} {
|
|
|
|
fmt.Println( "Rolling over - Stand by...")
|
|
|
|
})
|
|
|
|
|
|
|
|
With the api package setup, you can now get your Gobot to rollover using: http://localhost:3000/commands/rollover
|
|
|
|
*/
|
2014-07-04 10:14:04 +08:00
|
|
|
func (g *Gobot) AddCommand(name string, f func(map[string]interface{}) interface{}) {
|
|
|
|
g.commands[name] = f
|
|
|
|
}
|
|
|
|
|
2014-07-19 04:18:01 +08:00
|
|
|
// Commands lists all available commands on this Gobot instance.
|
2014-07-03 09:08:44 +08:00
|
|
|
func (g *Gobot) Commands() map[string]func(map[string]interface{}) interface{} {
|
2014-06-24 11:33:59 +08:00
|
|
|
return g.commands
|
2014-06-13 11:58:54 +08:00
|
|
|
}
|
|
|
|
|
2014-07-19 04:18:01 +08:00
|
|
|
// Command fetch the associated command using the given command name
|
2014-07-04 10:14:04 +08:00
|
|
|
func (g *Gobot) Command(name string) func(map[string]interface{}) interface{} {
|
|
|
|
return g.commands[name]
|
|
|
|
}
|
|
|
|
|
2014-07-19 04:18:01 +08:00
|
|
|
// Start runs the main Gobot event loop
|
2014-04-30 04:20:32 +08:00
|
|
|
func (g *Gobot) Start() {
|
2014-06-24 11:33:59 +08:00
|
|
|
g.robots.Start()
|
2014-04-30 04:20:32 +08:00
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
g.trap(c)
|
|
|
|
|
|
|
|
// waiting for interrupt coming on the channel
|
|
|
|
_ = <-c
|
2014-06-24 11:33:59 +08:00
|
|
|
g.robots.Each(func(r *Robot) {
|
2014-04-30 23:10:44 +08:00
|
|
|
log.Println("Stopping Robot", r.Name, "...")
|
|
|
|
r.Devices().Halt()
|
|
|
|
r.Connections().Finalize()
|
2014-04-30 04:20:32 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-19 04:18:01 +08:00
|
|
|
// Robots fetch all robots associated with this Gobot instance.
|
2014-06-24 11:33:59 +08:00
|
|
|
func (g *Gobot) Robots() *robots {
|
|
|
|
return g.robots
|
|
|
|
}
|
|
|
|
|
2014-07-19 04:18:01 +08:00
|
|
|
// AddRobot adds a new robot to our Gobot instance.
|
2014-07-08 12:45:36 +08:00
|
|
|
func (g *Gobot) AddRobot(r *Robot) *Robot {
|
2014-07-10 00:38:43 +08:00
|
|
|
*g.robots = append(*g.robots, r)
|
|
|
|
return r
|
2014-07-08 12:45:36 +08:00
|
|
|
}
|
|
|
|
|
2014-07-19 04:18:01 +08:00
|
|
|
// Robot find a robot with a given name.
|
2014-04-30 23:10:44 +08:00
|
|
|
func (g *Gobot) Robot(name string) *Robot {
|
2014-07-10 00:38:43 +08:00
|
|
|
for _, robot := range *g.Robots() {
|
2014-06-24 11:33:59 +08:00
|
|
|
if robot.Name == name {
|
|
|
|
return robot
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-06-13 11:58:54 +08:00
|
|
|
|
2014-07-19 04:18:01 +08:00
|
|
|
// ToJSON retrieves a JSON representation of this Gobot.
|
2014-06-13 11:58:54 +08:00
|
|
|
func (g *Gobot) ToJSON() *JSONGobot {
|
|
|
|
jsonGobot := &JSONGobot{
|
|
|
|
Robots: []*JSONRobot{},
|
|
|
|
Commands: []string{},
|
|
|
|
}
|
2014-06-24 11:33:59 +08:00
|
|
|
|
2014-07-03 09:08:44 +08:00
|
|
|
for command := range g.Commands() {
|
|
|
|
jsonGobot.Commands = append(jsonGobot.Commands, command)
|
|
|
|
}
|
2014-06-24 11:33:59 +08:00
|
|
|
|
|
|
|
g.robots.Each(func(r *Robot) {
|
|
|
|
jsonGobot.Robots = append(jsonGobot.Robots, r.ToJSON())
|
|
|
|
})
|
2014-06-13 11:58:54 +08:00
|
|
|
return jsonGobot
|
|
|
|
}
|