hybridgroup.gobot/master.go

59 lines
910 B
Go
Raw Normal View History

2013-11-25 08:17:47 +08:00
package gobot
2013-12-31 12:38:14 +08:00
import (
"os"
"os/signal"
"runtime"
)
2013-12-19 15:50:42 +08:00
2013-11-25 08:17:47 +08:00
type Master struct {
2014-04-15 14:15:54 +08:00
Robots []*Robot
2013-12-19 15:50:42 +08:00
NumCPU int
2014-04-19 07:04:49 +08:00
Api *api
2014-04-27 00:13:33 +08:00
trap func(chan os.Signal)
2013-11-25 08:17:47 +08:00
}
2014-04-27 00:13:33 +08:00
// used to be GobotMaster()
func NewMaster() *Master {
return &Master{
NumCPU: runtime.NumCPU(),
trap: func(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
},
}
2014-04-15 14:15:54 +08:00
}
func (m *Master) Start() {
2014-04-27 00:13:33 +08:00
// this changes the amount of cores used by the program
// to match the amount of CPUs set on master.
2013-12-19 15:50:42 +08:00
runtime.GOMAXPROCS(m.NumCPU)
2013-12-31 12:38:14 +08:00
2014-04-19 05:39:17 +08:00
if m.Api != nil {
2014-04-27 00:13:33 +08:00
m.Api.start()
2014-04-19 05:39:17 +08:00
}
2014-04-27 00:13:33 +08:00
for _, r := range m.Robots {
r.startRobot()
2013-11-25 08:17:47 +08:00
}
2013-12-31 12:38:14 +08:00
2014-04-15 14:15:54 +08:00
var c = make(chan os.Signal, 1)
2014-04-27 00:13:33 +08:00
m.trap(c)
2013-12-31 12:38:14 +08:00
2014-04-27 00:13:33 +08:00
// waiting on something coming on the channel
_ = <-c
for _, r := range m.Robots {
r.haltDevices()
r.finalizeConnections()
2013-12-31 12:38:14 +08:00
}
2014-04-27 00:13:33 +08:00
2013-11-25 08:17:47 +08:00
}
func (m *Master) FindRobot(name string) *Robot {
2014-01-03 07:12:41 +08:00
for _, robot := range m.Robots {
if robot.Name == name {
2014-04-15 14:15:54 +08:00
return robot
2013-11-25 08:17:47 +08:00
}
}
return nil
}