forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.go
272 lines (236 loc) · 6.64 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package gobot
import (
"fmt"
"log"
"os"
"os/signal"
"sync/atomic"
"sync"
multierror "github.com/hashicorp/go-multierror"
)
// JSONRobot a JSON representation of a Robot.
type JSONRobot struct {
Name string `json:"name"`
Commands []string `json:"commands"`
Connections []*JSONConnection `json:"connections"`
Devices []*JSONDevice `json:"devices"`
}
// NewJSONRobot returns a JSONRobot given a Robot.
func NewJSONRobot(robot *Robot) *JSONRobot {
jsonRobot := &JSONRobot{
Name: robot.Name,
Commands: []string{},
Connections: []*JSONConnection{},
Devices: []*JSONDevice{},
}
for command := range robot.Commands() {
jsonRobot.Commands = append(jsonRobot.Commands, command)
}
robot.Devices().Each(func(device Device) {
jsonDevice := NewJSONDevice(device)
jsonRobot.Connections = append(jsonRobot.Connections, NewJSONConnection(robot.Connection(jsonDevice.Connection)))
jsonRobot.Devices = append(jsonRobot.Devices, jsonDevice)
})
return jsonRobot
}
// Robot is a named entity that manages a collection of connections and devices.
// It contains its own work routine and a collection of
// custom commands to control a robot remotely via the Gobot api.
type Robot struct {
Name string
Work func()
connections *Connections
devices *Devices
trap func(chan os.Signal)
AutoRun bool
running atomic.Value
done chan bool
workRegistry *RobotWorkRegistry
WorkEveryWaitGroup *sync.WaitGroup
WorkAfterWaitGroup *sync.WaitGroup
Commander
Eventer
}
// Robots is a collection of Robot
type Robots []*Robot
// Len returns the amount of Robots in the collection.
func (r *Robots) Len() int {
return len(*r)
}
// Start calls the Start method of each Robot in the collection. We return on first error.
func (r *Robots) Start(args ...interface{}) error {
autoRun := true
if args[0] != nil {
autoRun = args[0].(bool)
}
for _, robot := range *r {
if err := robot.Start(autoRun); err != nil {
return err
}
}
return nil
}
// Stop calls the Stop method of each Robot in the collection. We try to stop all robots and
// collect the errors.
func (r *Robots) Stop() error {
var err error
for _, robot := range *r {
if e := robot.Stop(); e != nil {
err = multierror.Append(err, e)
}
}
return err
}
// Each enumerates through the Robots and calls specified callback function.
func (r *Robots) Each(f func(*Robot)) {
for _, robot := range *r {
f(robot)
}
}
// NewRobot returns a new Robot. It supports the following optional params:
//
// name: string with the name of the Robot. A name will be automatically generated if no name is supplied.
// []Connection: Connections which are automatically started and stopped with the robot
// []Device: Devices which are automatically started and stopped with the robot
// func(): The work routine the robot will execute once all devices and connections have been initialized and started
func NewRobot(v ...interface{}) *Robot {
r := &Robot{
Name: fmt.Sprintf("%X", Rand(int(^uint(0)>>1))),
connections: &Connections{},
devices: &Devices{},
done: make(chan bool, 1),
trap: func(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
},
AutoRun: true,
Work: nil,
Eventer: NewEventer(),
Commander: NewCommander(),
}
for i := range v {
switch v[i].(type) {
case string:
r.Name = v[i].(string)
case []Connection:
log.Println("Initializing connections...")
for _, connection := range v[i].([]Connection) {
c := r.AddConnection(connection)
log.Println("Initializing connection", c.Name(), "...")
}
case []Device:
log.Println("Initializing devices...")
for _, device := range v[i].([]Device) {
d := r.AddDevice(device)
log.Println("Initializing device", d.Name(), "...")
}
case func():
r.Work = v[i].(func())
}
}
r.workRegistry = &RobotWorkRegistry{
r: make(map[string]*RobotWork),
}
r.WorkAfterWaitGroup = &sync.WaitGroup{}
r.WorkEveryWaitGroup = &sync.WaitGroup{}
r.running.Store(false)
log.Println("Robot", r.Name, "initialized.")
return r
}
// Start a Robot's Connections, Devices, and work. We stop initialization of
// connections and devices on first error.
func (r *Robot) Start(args ...interface{}) error {
if len(args) > 0 && args[0] != nil {
r.AutoRun = args[0].(bool)
}
log.Println("Starting Robot", r.Name, "...")
if err := r.Connections().Start(); err != nil {
log.Println(err)
return err
}
if err := r.Devices().Start(); err != nil {
log.Println(err)
return err
}
if r.Work == nil {
r.Work = func() {}
}
log.Println("Starting work...")
go func() {
r.Work()
<-r.done
}()
r.running.Store(true)
if !r.AutoRun {
return nil
}
c := make(chan os.Signal, 1)
r.trap(c)
// waiting for interrupt coming on the channel
<-c
// Stop calls the Stop method on itself, if we are "auto-running".
return r.Stop()
}
// Stop stops a Robot's connections and devices. We try to stop all items and
// collect all errors.
func (r *Robot) Stop() error {
var err error
log.Println("Stopping Robot", r.Name, "...")
if e := r.Devices().Halt(); e != nil {
err = multierror.Append(err, e)
}
if e := r.Connections().Finalize(); e != nil {
err = multierror.Append(err, e)
}
r.done <- true
r.running.Store(false)
return err
}
// Running returns if the Robot is currently started or not
func (r *Robot) Running() bool {
return r.running.Load().(bool)
}
// Devices returns all devices associated with this Robot.
func (r *Robot) Devices() *Devices {
return r.devices
}
// AddDevice adds a new Device to the robots collection of devices. Returns the
// added device.
func (r *Robot) AddDevice(d Device) Device {
*r.devices = append(*r.Devices(), d)
return d
}
// Device returns a device given a name. Returns nil if the Device does not exist.
func (r *Robot) Device(name string) Device {
if r == nil {
return nil
}
for _, device := range *r.devices {
if device.Name() == name {
return device
}
}
return nil
}
// Connections returns all connections associated with this robot.
func (r *Robot) Connections() *Connections {
return r.connections
}
// AddConnection adds a new connection to the robots collection of connections.
// Returns the added connection.
func (r *Robot) AddConnection(c Connection) Connection {
*r.connections = append(*r.Connections(), c)
return c
}
// Connection returns a connection given a name. Returns nil if the Connection
// does not exist.
func (r *Robot) Connection(name string) Connection {
if r == nil {
return nil
}
for _, connection := range *r.connections {
if connection.Name() == name {
return connection
}
}
return nil
}