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"
|
|
|
|
)
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// JSONGobot is 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"`
|
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// NewJSONGobot returns a JSONGobt given a Gobot.
|
2014-11-22 03:57:26 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Gobot is the main type of your Gobot application and contains a collection of
|
|
|
|
// Robots, API commands and Events.
|
2014-04-30 04:20:32 +08:00
|
|
|
type Gobot struct {
|
2014-12-31 21:15:52 +08:00
|
|
|
robots *Robots
|
2014-11-21 10:00:32 +08:00
|
|
|
trap func(chan os.Signal)
|
|
|
|
Commander
|
|
|
|
Eventer
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
|
|
|
|
2014-11-14 03:06:57 +08:00
|
|
|
// NewGobot returns a new Gobot
|
2014-04-30 04:20:32 +08:00
|
|
|
func NewGobot() *Gobot {
|
|
|
|
return &Gobot{
|
2014-12-31 21:15:52 +08:00
|
|
|
robots: &Robots{},
|
2014-04-30 04:20:32 +08:00
|
|
|
trap: func(c chan os.Signal) {
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
},
|
2014-11-21 10:00:32 +08:00
|
|
|
Commander: NewCommander(),
|
|
|
|
Eventer: NewEventer(),
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 03:06:57 +08:00
|
|
|
// Start calls the Start method on each robot in it's collection of robots, and
|
|
|
|
// stops all robots on reception of a SIGINT. Start will block the execution of
|
|
|
|
// your main function until it receives the SIGINT.
|
2014-11-20 15:21:19 +08:00
|
|
|
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)
|
2014-11-20 15:21:19 +08:00
|
|
|
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, "...")
|
2014-11-20 15:21:19 +08:00
|
|
|
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
|
|
|
})
|
2014-11-20 15:21:19 +08:00
|
|
|
return errs
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Robots returns all robots associated with this Gobot.
|
|
|
|
func (g *Gobot) 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
|
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
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Robot returns a robot given name. Returns nil if the Robot does not exist.
|
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
|
|
|
|
}
|