2014-04-30 04:20:32 +08:00
|
|
|
package gobot
|
|
|
|
|
|
|
|
import (
|
2015-10-22 15:12:00 +08:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2017-05-07 19:28:11 +08:00
|
|
|
"sync/atomic"
|
2014-04-30 04:20:32 +08:00
|
|
|
)
|
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
// JSONManager is a JSON representation of a Gobot Manager.
|
|
|
|
type JSONManager struct {
|
2014-06-13 11:58:54 +08:00
|
|
|
Robots []*JSONRobot `json:"robots"`
|
|
|
|
Commands []string `json:"commands"`
|
|
|
|
}
|
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
// NewJSONManager returns a JSONManager given a Gobot Manager.
|
|
|
|
func NewJSONManager(gobot *Manager) *JSONManager {
|
|
|
|
jsonGobot := &JSONManager{
|
2014-11-22 03:57:26 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
// Manager is the main type of your Gobot application and contains a collection of
|
|
|
|
// Robots, API commands that apply to the Manager, and Events that apply to the Manager.
|
|
|
|
type Manager struct {
|
2016-10-18 22:34:29 +08:00
|
|
|
robots *Robots
|
|
|
|
trap func(chan os.Signal)
|
|
|
|
AutoRun bool
|
2017-05-07 19:28:11 +08:00
|
|
|
running atomic.Value
|
2014-11-21 10:00:32 +08:00
|
|
|
Commander
|
|
|
|
Eventer
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
// NewManager returns a new Gobot Manager
|
|
|
|
func NewManager() *Manager {
|
|
|
|
m := &Manager{
|
2015-10-22 15:12:00 +08:00
|
|
|
robots: &Robots{},
|
|
|
|
trap: func(c chan os.Signal) {
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
},
|
2016-10-18 22:34:29 +08:00
|
|
|
AutoRun: true,
|
2014-11-21 10:00:32 +08:00
|
|
|
Commander: NewCommander(),
|
|
|
|
Eventer: NewEventer(),
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
2017-05-07 19:28:11 +08:00
|
|
|
m.running.Store(false)
|
|
|
|
return m
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
|
|
|
|
2015-07-22 04:20:02 +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.
|
2024-02-13 23:30:25 +08:00
|
|
|
func (g *Manager) Start() error {
|
2023-06-13 01:51:25 +08:00
|
|
|
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
|
|
|
|
2017-05-07 19:28:11 +08:00
|
|
|
g.running.Store(true)
|
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
if !g.AutoRun {
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-22 15:12:00 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
g.trap(c)
|
2015-10-22 15:12:00 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
// waiting for interrupt coming on the channel
|
|
|
|
<-c
|
2015-10-22 15:12:00 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
// Stop calls the Stop method on each robot in its collection of robots.
|
|
|
|
return g.Stop()
|
2015-07-22 04:20:02 +08:00
|
|
|
}
|
2014-04-30 04:20:32 +08:00
|
|
|
|
2015-07-22 04:20:02 +08:00
|
|
|
// Stop calls the Stop method on each robot in its collection of robots.
|
2024-02-13 23:30:25 +08:00
|
|
|
func (g *Manager) Stop() error {
|
2023-06-13 01:51:25 +08:00
|
|
|
err := g.robots.Stop()
|
2017-05-07 19:28:11 +08:00
|
|
|
g.running.Store(false)
|
2023-06-13 01:51:25 +08:00
|
|
|
return err
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
// Running returns if the Manager is currently started or not
|
|
|
|
func (g *Manager) Running() bool {
|
2023-11-16 03:51:52 +08:00
|
|
|
return g.running.Load().(bool) //nolint:forcetypeassert // no error return value, so there is no better way
|
2017-05-07 19:28:11 +08:00
|
|
|
}
|
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
// Robots returns all robots associated with this Gobot Manager.
|
|
|
|
func (g *Manager) 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
|
2024-02-13 23:30:25 +08:00
|
|
|
func (g *Manager) 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-12-31 21:15:52 +08:00
|
|
|
// Robot returns a robot given name. Returns nil if the Robot does not exist.
|
2024-02-13 23:30:25 +08:00
|
|
|
func (g *Manager) 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
|
|
|
|
}
|