hybridgroup.gobot/master.go

115 lines
2.6 KiB
Go
Raw Normal View History

2014-04-30 04:20:32 +08:00
package gobot
import (
"os"
"os/signal"
"sync/atomic"
2014-04-30 04:20:32 +08:00
)
// JSONMaster is a JSON representation of a Gobot Master.
type JSONMaster struct {
2014-06-13 11:58:54 +08:00
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
running atomic.Value
Commander
Eventer
2014-04-30 04:20:32 +08:00
}
// NewMaster returns a new Gobot Master
func NewMaster() *Master {
m := &Master{
robots: &Robots{},
trap: func(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
},
AutoRun: true,
Commander: NewCommander(),
Eventer: NewEventer(),
2014-04-30 04:20:32 +08:00
}
m.running.Store(false)
return m
2014-04-30 04:20:32 +08:00
}
// 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() error {
if err := g.robots.Start(!g.AutoRun); err != nil {
return err
2014-11-13 03:21:50 +08:00
}
2014-04-30 04:20:32 +08:00
g.running.Store(true)
if !g.AutoRun {
return nil
}
c := make(chan os.Signal, 1)
g.trap(c)
// waiting for interrupt coming on the channel
<-c
// Stop calls the Stop method on each robot in its collection of robots.
return g.Stop()
}
2014-04-30 04:20:32 +08:00
// Stop calls the Stop method on each robot in its collection of robots.
func (g *Master) Stop() error {
err := g.robots.Stop()
g.running.Store(false)
return err
2014-04-30 04:20:32 +08:00
}
// Running returns if the Master is currently started or not
func (g *Master) Running() bool {
return g.running.Load().(bool) //nolint:forcetypeassert // no error return value, so there is no better way
}
// Robots returns all robots associated with this Gobot Master.
func (g *Master) Robots() *Robots {
2014-06-24 11:33:59 +08:00
return g.robots
}
2014-11-14 03:06:57 +08:00
// AddRobot adds a new robot to the internal collection of robots. Returns the
// added robot
func (g *Master) 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 returns a robot given name. Returns nil if the Robot does not exist.
func (g *Master) 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
}