1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/master.go

72 lines
1.1 KiB
Go
Raw Normal View History

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 {
2014-04-14 23:15:54 -07:00
Robots []*Robot
2013-12-18 23:50:42 -08:00
NumCPU int
2014-04-18 14:39:17 -07:00
Api *api
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
}
2014-04-14 23:15:54 -07:00
var trap = func(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
}
func (m *Master) Start() {
2013-12-18 23:50:42 -08:00
runtime.GOMAXPROCS(m.NumCPU)
2013-12-30 20:38:14 -08:00
2014-04-18 14:39:17 -07:00
if m.Api != nil {
m.Api.StartApi()
}
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
2014-04-14 23:15:54 -07:00
var c = make(chan os.Signal, 1)
trap(c)
2013-12-30 20:38:14 -08:00
for _ = range c {
for r := range m.Robots {
2014-03-31 14:25:20 -07:00
m.Robots[r].haltDevices()
2013-12-30 20:38:14 -08:00
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 {
2014-04-14 23:15:54 -07:00
return robot
2013-11-24 16:17:47 -08:00
}
}
return nil
}
2013-11-27 20:05:45 -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
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
}