2013-11-24 16:17:47 -08:00
|
|
|
package gobot
|
|
|
|
|
2013-12-30 20:38:14 -08:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"runtime"
|
|
|
|
)
|
2013-12-18 23:50:42 -08:00
|
|
|
|
2013-11-24 16:17:47 -08:00
|
|
|
type Master struct {
|
|
|
|
Robots []Robot
|
2013-12-18 23:50:42 -08:00
|
|
|
NumCPU int
|
2013-11-24 16:17:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GobotMaster() *Master {
|
|
|
|
m := new(Master)
|
2013-12-18 23:50:42 -08:00
|
|
|
m.NumCPU = runtime.NumCPU()
|
2013-11-24 16:17:47 -08:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Master) Start() {
|
2013-12-18 23:50:42 -08:00
|
|
|
runtime.GOMAXPROCS(m.NumCPU)
|
2013-12-30 20:38:14 -08:00
|
|
|
|
2013-11-24 16:17:47 -08:00
|
|
|
for s := range m.Robots {
|
2014-01-02 15:12:41 -08:00
|
|
|
m.Robots[s].startRobot()
|
2013-11-24 16:17:47 -08:00
|
|
|
}
|
2013-12-30 20:38:14 -08:00
|
|
|
|
|
|
|
c := make(chan os.Signal)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
|
|
|
|
for _ = range c {
|
|
|
|
for r := range m.Robots {
|
|
|
|
m.Robots[r].finalizeConnections()
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2013-11-24 16:17:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Master) FindRobot(name string) *Robot {
|
2014-01-02 15:12:41 -08:00
|
|
|
for _, robot := range m.Robots {
|
|
|
|
if robot.Name == name {
|
|
|
|
return &robot
|
2013-11-24 16:17:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-11-27 20:05:45 -08:00
|
|
|
|
2013-12-18 15:25:07 -08:00
|
|
|
func (m *Master) FindRobotDevice(name string, device string) *device {
|
2014-01-02 15:12:41 -08:00
|
|
|
robot := m.FindRobot(name)
|
|
|
|
if robot != nil {
|
|
|
|
return robot.GetDevice(device)
|
2013-11-24 16:17:47 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-11-27 20:05:45 -08:00
|
|
|
|
2013-12-18 15:25:07 -08:00
|
|
|
func (m *Master) FindRobotConnection(name string, connection string) *connection {
|
2014-01-02 15:12:41 -08:00
|
|
|
robot := m.FindRobot(name)
|
|
|
|
if robot != nil {
|
|
|
|
return robot.GetConnection(connection)
|
2013-11-24 16:17:47 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|