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 {
|
|
|
|
Robots []Robot
|
2013-12-19 15:50:42 +08:00
|
|
|
NumCPU int
|
2013-11-25 08:17:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GobotMaster() *Master {
|
|
|
|
m := new(Master)
|
2013-12-19 15:50:42 +08:00
|
|
|
m.NumCPU = runtime.NumCPU()
|
2013-11-25 08:17:47 +08:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Master) Start() {
|
2013-12-19 15:50:42 +08:00
|
|
|
runtime.GOMAXPROCS(m.NumCPU)
|
2013-12-31 12:38:14 +08:00
|
|
|
|
2013-11-25 08:17:47 +08:00
|
|
|
for s := range m.Robots {
|
2014-01-03 07:12:41 +08:00
|
|
|
m.Robots[s].startRobot()
|
2013-11-25 08:17:47 +08:00
|
|
|
}
|
2013-12-31 12: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-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 {
|
|
|
|
return &robot
|
2013-11-25 08:17:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-11-28 12:05:45 +08:00
|
|
|
|
2013-12-19 07:25:07 +08:00
|
|
|
func (m *Master) FindRobotDevice(name string, device string) *device {
|
2014-01-03 07:12:41 +08:00
|
|
|
robot := m.FindRobot(name)
|
|
|
|
if robot != nil {
|
|
|
|
return robot.GetDevice(device)
|
2013-11-25 08:17:47 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-11-28 12:05:45 +08:00
|
|
|
|
2013-12-19 07:25:07 +08:00
|
|
|
func (m *Master) FindRobotConnection(name string, connection string) *connection {
|
2014-01-03 07:12:41 +08:00
|
|
|
robot := m.FindRobot(name)
|
|
|
|
if robot != nil {
|
|
|
|
return robot.GetConnection(connection)
|
2013-11-25 08:17:47 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|