hybridgroup.gobot/gobot.go

132 lines
3.2 KiB
Go
Raw Normal View History

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"
)
// 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"`
}
// 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
}
// 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)
},
}
}
/*
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
*/
func (g *Gobot) AddCommand(name string, f func(map[string]interface{}) interface{}) {
g.commands[name] = f
}
// 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
}
// Command fetch the associated command using the given command name
func (g *Gobot) Command(name string) func(map[string]interface{}) interface{} {
return g.commands[name]
}
// Start runs the main Gobot event loop
func (g *Gobot) Start() (errs []error) {
if rerrs := g.robots.Start(); len(rerrs) > 0 {
for _, err := range rerrs {
log.Println("Error:", err)
errs = append(errs, err)
}
2014-11-13 03:21:50 +08:00
}
2014-04-30 04:20:32 +08:00
c := make(chan os.Signal, 1)
g.trap(c)
if len(errs) > 0 {
// there was an error during start, so we immediatly pass the interrupt
// in order to disconnect the initialized robots, connections and devices
2014-11-18 08:23:19 +08:00
c <- os.Interrupt
}
2014-04-30 04:20:32 +08:00
// 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, "...")
if herrs := r.Devices().Halt(); len(herrs) > 0 {
for _, err := range herrs {
log.Println("Error:", err)
errs = append(errs, err)
}
}
if cerrs := r.Connections().Finalize(); len(cerrs) > 0 {
for _, err := range cerrs {
log.Println("Error:", err)
errs = append(errs, err)
}
}
2014-04-30 04:20:32 +08:00
})
return errs
2014-04-30 04:20:32 +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
}
// 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
}
// 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
// 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
}