forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.go
71 lines (59 loc) · 1.09 KB
/
master.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package gobot
import (
"os"
"os/signal"
"runtime"
)
type Master struct {
Robots []*Robot
NumCPU int
Api *api
}
func GobotMaster() *Master {
m := new(Master)
m.NumCPU = runtime.NumCPU()
return m
}
var trap = func(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
}
func (m *Master) Start() {
runtime.GOMAXPROCS(m.NumCPU)
if m.Api != nil {
m.Api.startApi()
}
for s := range m.Robots {
m.Robots[s].startRobot()
}
var c = make(chan os.Signal, 1)
trap(c)
for _ = range c {
for r := range m.Robots {
m.Robots[r].haltDevices()
m.Robots[r].finalizeConnections()
}
break
}
}
func (m *Master) FindRobot(name string) *Robot {
for _, robot := range m.Robots {
if robot.Name == name {
return robot
}
}
return nil
}
func (m *Master) FindRobotDevice(name string, device string) *device {
robot := m.FindRobot(name)
if robot != nil {
return robot.GetDevice(device)
}
return nil
}
func (m *Master) FindRobotConnection(name string, connection string) *connection {
robot := m.FindRobot(name)
if robot != nil {
return robot.GetConnection(connection)
}
return nil
}