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 {
|
2013-12-19 15:50:42 +08:00
|
|
|
go 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 {
|
|
|
|
for s := range m.Robots {
|
|
|
|
if m.Robots[s].Name == name {
|
|
|
|
return &m.Robots[s]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2013-11-25 08:17:47 +08:00
|
|
|
for r := range m.Robots {
|
|
|
|
if m.Robots[r].Name == name {
|
|
|
|
for d := range m.Robots[r].devices {
|
|
|
|
if m.Robots[r].devices[d].Name == device {
|
|
|
|
return m.Robots[r].devices[d]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2013-11-25 08:17:47 +08:00
|
|
|
for r := range m.Robots {
|
|
|
|
if m.Robots[r].Name == name {
|
|
|
|
for c := range m.Robots[r].connections {
|
|
|
|
if m.Robots[r].connections[c].Name == connection {
|
|
|
|
return m.Robots[r].connections[c]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|