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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Gobot struct {
|
2014-04-30 23:10:44 +08:00
|
|
|
Robots []*Robot
|
2014-04-30 04:20:32 +08:00
|
|
|
trap func(chan os.Signal)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGobot() *Gobot {
|
|
|
|
return &Gobot{
|
|
|
|
trap: func(c chan os.Signal) {
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gobot) Start() {
|
2014-04-30 23:10:44 +08:00
|
|
|
Robots(g.Robots).Start()
|
2014-04-30 04:20:32 +08:00
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
g.trap(c)
|
|
|
|
|
|
|
|
// waiting for interrupt coming on the channel
|
|
|
|
_ = <-c
|
2014-04-30 23:10:44 +08:00
|
|
|
Robots(g.Robots).Each(func(r *Robot) {
|
|
|
|
log.Println("Stopping Robot", r.Name, "...")
|
|
|
|
r.Devices().Halt()
|
|
|
|
r.Connections().Finalize()
|
2014-04-30 04:20:32 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-04-30 23:10:44 +08:00
|
|
|
func (g *Gobot) Robot(name string) *Robot {
|
2014-04-30 04:20:32 +08:00
|
|
|
for _, r := range g.Robots {
|
|
|
|
if r.Name == name {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|