119 lines
2.7 KiB
Go
119 lines
2.7 KiB
Go
package gobot
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
)
|
|
|
|
// JSONMaster is a JSON representation of a Gobot Master.
|
|
type JSONMaster struct {
|
|
Robots []*JSONRobot `json:"robots"`
|
|
Commands []string `json:"commands"`
|
|
}
|
|
|
|
// NewJSONMaster returns a JSONMaster given a Gobot Master.
|
|
func NewJSONMaster(gobot *Master) *JSONMaster {
|
|
jsonGobot := &JSONMaster{
|
|
Robots: []*JSONRobot{},
|
|
Commands: []string{},
|
|
}
|
|
|
|
for command := range gobot.Commands() {
|
|
jsonGobot.Commands = append(jsonGobot.Commands, command)
|
|
}
|
|
|
|
gobot.robots.Each(func(r *Robot) {
|
|
jsonGobot.Robots = append(jsonGobot.Robots, NewJSONRobot(r))
|
|
})
|
|
return jsonGobot
|
|
}
|
|
|
|
// Master is the main type of your Gobot application and contains a collection of
|
|
// Robots, API commands that apply to the Master, and Events that apply to the Master.
|
|
type Master struct {
|
|
robots *Robots
|
|
trap func(chan os.Signal)
|
|
AutoRun bool
|
|
Commander
|
|
Eventer
|
|
}
|
|
|
|
// NewMaster returns a new Gobot Master
|
|
func NewMaster() *Master {
|
|
return &Master{
|
|
robots: &Robots{},
|
|
trap: func(c chan os.Signal) {
|
|
signal.Notify(c, os.Interrupt)
|
|
},
|
|
AutoRun: true,
|
|
Commander: NewCommander(),
|
|
Eventer: NewEventer(),
|
|
}
|
|
}
|
|
|
|
// Start calls the Start method on each robot in its collection of robots. On
|
|
// error, call Stop to ensure that all robots are returned to a sane, stopped
|
|
// state.
|
|
func (g *Master) Start() (errs []error) {
|
|
if rerrs := g.robots.Start(!g.AutoRun); len(rerrs) > 0 {
|
|
for _, err := range rerrs {
|
|
log.Println("Error:", err)
|
|
errs = append(errs, err)
|
|
}
|
|
return
|
|
}
|
|
|
|
if g.AutoRun {
|
|
c := make(chan os.Signal, 1)
|
|
g.trap(c)
|
|
if len(errs) > 0 {
|
|
// there was an error during start, so we immediately pass the interrupt
|
|
// in order to disconnect the initialized robots, connections and devices
|
|
c <- os.Interrupt
|
|
}
|
|
|
|
// waiting for interrupt coming on the channel
|
|
<-c
|
|
|
|
// Stop calls the Stop method on each robot in its collection of robots.
|
|
g.Stop()
|
|
}
|
|
|
|
return errs
|
|
}
|
|
|
|
// Stop calls the Stop method on each robot in its collection of robots.
|
|
func (g *Master) Stop() (errs []error) {
|
|
if rerrs := g.robots.Stop(); len(rerrs) > 0 {
|
|
for _, err := range rerrs {
|
|
log.Println("Error:", err)
|
|
errs = append(errs, err)
|
|
}
|
|
}
|
|
|
|
return errs
|
|
}
|
|
|
|
// Robots returns all robots associated with this Gobot Master.
|
|
func (g *Master) Robots() *Robots {
|
|
return g.robots
|
|
}
|
|
|
|
// AddRobot adds a new robot to the internal collection of robots. Returns the
|
|
// added robot
|
|
func (g *Master) AddRobot(r *Robot) *Robot {
|
|
*g.robots = append(*g.robots, r)
|
|
return r
|
|
}
|
|
|
|
// Robot returns a robot given name. Returns nil if the Robot does not exist.
|
|
func (g *Master) Robot(name string) *Robot {
|
|
for _, robot := range *g.Robots() {
|
|
if robot.Name == name {
|
|
return robot
|
|
}
|
|
}
|
|
return nil
|
|
}
|