1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00

118 lines
2.6 KiB
Go
Raw Normal View History

2014-04-29 13:20:32 -07:00
package gobot
import (
2014-04-30 08:10:44 -07:00
"log"
"os"
"os/signal"
2014-04-29 13:20:32 -07:00
)
// JSONGobot is a JSON representation of a Gobot.
2014-06-12 20:58:54 -07:00
type JSONGobot struct {
Robots []*JSONRobot `json:"robots"`
Commands []string `json:"commands"`
}
// NewJSONGobot returns a JSONGobt given a Gobot.
func NewJSONGobot(gobot *Gobot) *JSONGobot {
jsonGobot := &JSONGobot{
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
}
// Gobot is the main type of your Gobot application and contains a collection of
// Robots, API commands and Events.
2014-04-29 13:20:32 -07:00
type Gobot struct {
robots *Robots
trap func(chan os.Signal)
AutoStop bool
Commander
Eventer
2014-04-29 13:20:32 -07:00
}
2014-11-13 11:06:57 -08:00
// NewGobot returns a new Gobot
2014-04-29 13:20:32 -07:00
func NewGobot() *Gobot {
return &Gobot{
robots: &Robots{},
trap: func(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
},
AutoStop: true,
Commander: NewCommander(),
Eventer: NewEventer(),
2014-04-29 13:20:32 -07: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 *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-12 11:21:50 -08:00
}
2014-04-29 13:20:32 -07:00
if g.AutoStop {
c := make(chan os.Signal, 1)
g.trap(c)
if len(errs) > 0 {
2016-07-13 10:44:47 -06:00
// 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
2016-07-13 09:32:22 -06:00
<-c
// Stop calls the Stop method on each robot in its collection of robots.
g.Stop()
}
return errs
}
2014-04-29 13:20:32 -07:00
// Stop calls the Stop method on each robot in its collection of robots.
func (g *Gobot) 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
2014-04-29 13:20:32 -07:00
}
// Robots returns all robots associated with this Gobot.
func (g *Gobot) Robots() *Robots {
2014-06-23 20:33:59 -07:00
return g.robots
}
2014-11-13 11:06:57 -08:00
// AddRobot adds a new robot to the internal collection of robots. Returns the
// added robot
2014-07-07 21:45:36 -07:00
func (g *Gobot) AddRobot(r *Robot) *Robot {
2014-07-09 09:38:43 -07:00
*g.robots = append(*g.robots, r)
return r
2014-07-07 21:45:36 -07:00
}
// Robot returns a robot given name. Returns nil if the Robot does not exist.
2014-04-30 08:10:44 -07:00
func (g *Gobot) Robot(name string) *Robot {
2014-07-09 09:38:43 -07:00
for _, robot := range *g.Robots() {
2014-06-23 20:33:59 -07:00
if robot.Name == name {
return robot
2014-04-29 13:20:32 -07:00
}
}
return nil
}