-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgadgets.go
434 lines (396 loc) · 9.08 KB
/
gadgets.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package gogadgets
import (
"crypto/rand"
"fmt"
"io"
"log"
"math"
"os"
"strconv"
"strings"
"time"
)
var (
units = map[string]string{
"liters": "volume",
"gallons": "volume",
"liter": "volume",
"gallon": "volume",
"c": "temperature",
"f": "temperature",
"C": "temperature",
"F": "temperature",
"celcius": "temperature",
"fahrenheit": "temperature",
"seconds": "time",
"minutes": "time",
"hours": "time",
"second": "time",
"minute": "time",
"hour": "time",
"%": "power",
}
)
type Comparitor func(msg *Message) bool
type Gadgeter interface {
GetUID() string
GetDirection() string
Start(in <-chan Message, out chan<- Message)
}
// Each part of a Gadgets system that controls a single
// piece of hardware (for example: a gpio pin) is represented
// by Gadget. A Gadget must have either an InputDevice or
// an OutputDevice. Gadget fulfills the GoGaget interface.
type Gadget struct {
Type string
Location string
Name string
Output OutputDevice
Input InputDevice
Direction string
OnCommands []string
OffCommands []string
inputLookup map[bool]string
InitialValue string
targetValue *Value
UID string
lastCmd string
status bool
compare Comparitor
shutdown bool
filterMessages bool
units string
Operator string
out chan<- Message
devIn chan Message
timerIn chan bool
timerOut chan bool
}
// All gadgets respond to Robot Command Language (RCL) messages. isMyCommand
// reads an RCL message and decides if it was meant for this instance
// of Gadget.
func (g *Gadget) isMyCommand(msg *Message) (bool, string, string) {
if msg.Type != COMMAND {
return false, "", ""
}
if msg.Body == "update" || msg.Body == "shutdown" {
return true, "", ""
}
for _, cmd := range g.OnCommands {
if strings.Index(msg.Body, cmd) == 0 {
return true, "on", cmd
}
}
for _, cmd := range g.OffCommands {
if strings.Index(msg.Body, cmd) == 0 {
return true, "off", cmd
}
}
return false, "", ""
}
func (g *Gadget) GetDirection() string {
if g.Output != nil {
return "output"
}
return "input"
}
// Start is one of the two interface methods of GoGadget. Start takes
// in in and out chan and is meant to be called as a goroutine.
func (g *Gadget) Start(in <-chan Message, out chan<- Message) {
g.out = out
g.timerIn = make(chan bool)
g.timerOut = make(chan bool)
if g.Output != nil {
if len(g.InitialValue) > 0 {
g.readInitialValue()
} else {
g.off()
}
g.doOutputLoop(in)
} else if g.Input != nil {
g.doInputLoop(in)
}
}
// Once a gadget is started as a goroutine, this loop collects
// all the messages that are sent to this particular Gadget and
// responds accordingly. This is the loop that is executed if
// this Gadget is an input Gadget
func (g *Gadget) doInputLoop(in <-chan Message) {
devOut := make(chan Value, 10)
g.devIn = make(chan Message, 10)
go g.Input.Start(g.devIn, devOut)
if g.Name != "n/a" {
g.sendUpdate()
}
for !g.shutdown {
select {
case msg := <-in:
g.readMessage(&msg)
case val := <-devOut:
val = g.translateBool(val)
location := g.Location
name := g.Name
sender := g.UID
if val.location != "" && val.name != "" {
location = val.location
name = val.name
sender = fmt.Sprintf("%s %s", location, name)
}
g.out <- Message{
UUID: GetUUID(),
Sender: sender,
Type: "update",
Location: location,
Name: name,
Value: val,
Timestamp: time.Now().UTC(),
Info: Info{
Direction: g.Direction,
Type: g.Type,
},
}
}
}
}
func (g *Gadget) translateBool(val Value) Value {
if len(g.inputLookup) == 0 {
return val
}
t, ok := val.Value.(bool)
if ok {
v, ok := g.inputLookup[t]
if ok {
val.Value = v
}
}
return val
}
func (g *Gadget) readInitialValue() {
msg := &Message{
UUID: GetUUID(),
Body: g.InitialValue,
}
g.readCommand(msg, "on", g.InitialValue)
}
func (g *Gadget) doOutputLoop(in <-chan Message) {
for !g.shutdown {
select {
case msg := <-in:
g.readMessage(&msg)
case <-g.timerOut:
g.off()
}
}
}
func (g *Gadget) on(val *Value) {
err := g.Output.On(val)
if err != nil {
log.Println("on err", err)
return
}
g.targetValue = val
g.status = true
g.sendUpdate()
}
func (g *Gadget) off() {
g.status = false
g.targetValue = nil
g.Output.Off()
g.compare = nil
g.sendUpdate()
}
func (g *Gadget) readMessage(msg *Message) {
if g.devIn != nil {
g.devIn <- *msg
}
mine, onoff, matched := g.isMyCommand(msg)
if mine && msg.Type == COMMAND {
g.lastCmd = matched
g.readCommand(msg, onoff, matched)
} else if g.status && msg.Type == UPDATE {
g.readUpdate(msg)
}
}
func (g *Gadget) readUpdate(msg *Message) {
if g.status && g.compare != nil && g.compare(msg) {
g.off()
} else if g.status && (msg.Location == g.Location || !g.filterMessages) {
if g.Output.Update(msg) {
g.sendUpdate()
}
}
}
func (g *Gadget) readCommand(msg *Message, onoff, matched string) {
if msg.Body == "shutdown" {
g.shutdown = true
g.off()
} else if msg.Body == "update" {
g.sendUpdate()
} else if onoff == "on" {
g.readOnCommand(msg, matched)
} else if onoff == "off" {
g.readOffCommand(msg)
}
}
func (g *Gadget) readOnCommand(msg *Message, matched string) {
var val *Value
if len(strings.Trim(msg.Body, " ")) > len(matched) {
val, err := g.readOnArguments(msg.Body)
if err == nil {
g.on(val)
}
} else {
g.compare = nil
g.on(val)
}
}
func (g *Gadget) readOnArguments(cmd string) (*Value, error) {
var val *Value
value, unit, err := ParseCommand(cmd)
if err != nil {
return val, fmt.Errorf("could not parse %s", cmd)
}
gadget, ok := units[unit]
if !ok {
return nil, nil
}
val = &Value{
Value: value,
Units: unit,
Cmd: cmd,
}
if gadget == "time" {
go g.startTimer(value, unit, g.timerIn, g.timerOut)
} else if gadget == "volume" {
g.setCompare(value, unit, gadget)
}
return val, nil
}
func (g *Gadget) setCompare(value float64, unit string, gadget string) {
if g.Operator == "<=" {
g.compare = func(msg *Message) bool {
val, ok := msg.Value.Value.(float64)
return msg.Location == g.Location &&
ok &&
msg.Name == gadget &&
(math.Abs(val-value) < 0.001 || val <= value)
}
} else if g.Operator == ">=" {
g.compare = func(msg *Message) bool {
val, ok := msg.Value.Value.(float64)
return msg.Location == g.Location &&
ok &&
msg.Name == gadget &&
(math.Abs(val-value) < 0.001 || val >= value)
}
}
}
func (g *Gadget) getDuration(value float64, unit string) time.Duration {
if unit == "minute" || unit == "minutes" {
value *= 60.0
} else if unit == "hour" || unit == "hours" {
value *= 3600.0
}
return time.Duration(value * float64(time.Second))
}
func (g *Gadget) startTimer(value float64, unit string, in <-chan bool, out chan<- bool) {
d := g.getDuration(value, unit)
keepGoing := true
for keepGoing {
select {
case <-in:
keepGoing = false
case <-time.After(d):
keepGoing = false
out <- true
}
}
}
func (g *Gadget) readOffCommand(msg *Message) {
if g.status {
g.off()
}
}
func (g *Gadget) GetUID() string {
if g.UID == "" {
g.UID = fmt.Sprintf("%s %s", g.Location, g.Name)
}
return g.UID
}
func (g *Gadget) sendUpdate() {
var value Value
if g.Input != nil {
value = *(g.Input.GetValue())
} else {
value = Value{
Units: g.units,
Value: g.status,
Output: g.Output.Status(),
Cmd: g.lastCmd,
}
}
g.out <- Message{
UUID: GetUUID(),
Sender: g.UID,
Type: UPDATE,
Location: g.Location,
Name: g.Name,
Value: value,
TargetValue: g.targetValue,
Timestamp: time.Now().UTC(),
Info: Info{
Type: g.Type,
Direction: g.Direction,
On: g.OnCommands,
Off: g.OffCommands,
},
}
}
func ParseCommand(cmd string) (float64, string, error) {
cmd = stripCommand(cmd)
value, unit, err := splitCommand(cmd)
var v float64
if err == nil {
v, err = strconv.ParseFloat(value, 64)
}
return v, unit, err
}
func splitCommand(cmd string) (string, string, error) {
parts := strings.Split(cmd, " ")
if len(parts) != 2 {
return "", "", fmt.Errorf("invalid command: %s", cmd)
}
return parts[0], parts[1], nil
}
func stripCommand(cmd string) string {
cmd = strings.Trim(cmd, " ")
i := strings.Index(cmd, " for ")
if i != -1 {
return cmd[i+5:]
}
i = strings.Index(cmd, " to ")
if i != -1 {
return cmd[i+4:]
}
return ""
}
// GetUUID generates a random UUID according to RFC 4122
func GetUUID() string {
uuid := make([]byte, 16)
n, err := io.ReadFull(rand.Reader, uuid)
if n != len(uuid) || err != nil {
return ""
}
// variant bits; see section 4.1.1
uuid[8] = uuid[8]&^0xc0 | 0x80
// version 4 (pseudo-random); see section 4.1.3
uuid[6] = uuid[6]&^0xf0 | 0x40
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
}
// FileExists tells you if the file exists.
func FileExists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
}
return false
}