forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.go
148 lines (132 loc) · 3.11 KB
/
robot.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package gobot
import (
"fmt"
"log"
"math/rand"
"time"
)
type Robot struct {
Connections []Connection `json:"connections"`
Devices []Device `json:"devices"`
Name string `json:"name"`
Commands map[string]interface{} `json:"-"`
RobotCommands []string `json:"commands"`
Work func() `json:"-"`
connections []*connection `json:"-"`
devices []*device `json:"-"`
}
func (r *Robot) Start() {
m := GobotMaster()
m.Robots = []*Robot{r}
m.Start()
}
func (r *Robot) startRobot() {
r.initName()
r.initCommands()
r.initConnections()
if r.startConnections() != true {
panic("Could not start connections")
}
if r.initDevices() != true {
panic("Could not initialize devices")
}
if r.startDevices() != true {
panic("Could not start devices")
}
if r.Work != nil {
r.Work()
}
}
func (r *Robot) initName() {
if r.Name == "" {
rand.Seed(time.Now().UTC().UnixNano())
i := rand.Int()
r.Name = fmt.Sprintf("Robot%v", i)
}
}
func (r *Robot) initCommands() {
r.RobotCommands = make([]string, 0)
for k, _ := range r.Commands {
r.RobotCommands = append(r.RobotCommands, k)
}
}
func (r *Robot) initConnections() {
r.connections = make([]*connection, len(r.Connections))
log.Println("Initializing connections...")
for i, connection := range r.Connections {
log.Println("Initializing connection ", FieldByNamePtr(connection, "Name"), "...")
r.connections[i] = NewConnection(connection, r)
}
}
func (r *Robot) initDevices() bool {
r.devices = make([]*device, len(r.Devices))
log.Println("Initializing devices...")
for i, device := range r.Devices {
r.devices[i] = NewDevice(device, r)
}
success := true
for _, device := range r.devices {
log.Println("Initializing device " + device.Name + "...")
if device.Init() == false {
success = false
break
}
}
return success
}
func (r *Robot) startConnections() bool {
log.Println("Starting connections...")
success := true
for _, connection := range r.connections {
log.Println("Starting connection " + connection.Name + "...")
if connection.Connect() == false {
success = false
break
}
}
return success
}
func (r *Robot) startDevices() bool {
log.Println("Starting devices...")
success := true
for _, device := range r.devices {
log.Println("Starting device " + device.Name + "...")
if device.Start() == false {
success = false
break
}
}
return success
}
func (r *Robot) haltDevices() {
for _, device := range r.devices {
device.Halt()
}
}
func (r *Robot) finalizeConnections() {
for _, connection := range r.connections {
connection.Finalize()
}
}
func (r *Robot) GetDevices() []*device {
return r.devices
}
func (r *Robot) GetDevice(name string) *device {
for _, device := range r.devices {
if device.Name == name {
return device
}
}
return nil
}
func (r *Robot) GetConnections() []*connection {
return r.connections
}
func (r *Robot) GetConnection(name string) *connection {
for _, connection := range r.connections {
if connection.Name == name {
return connection
}
}
return nil
}