-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmethod_runner.go
More file actions
306 lines (282 loc) · 7.01 KB
/
method_runner.go
File metadata and controls
306 lines (282 loc) · 7.01 KB
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
package gogadgets
import (
"errors"
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"
)
var (
timeExp = regexp.MustCompile(`for (\d*\.?\d*) (seconds?|minutes?|hours?)`)
stepExp = regexp.MustCompile(`for (.+) (>=|>|==|<=|<) ([\w\.]+) ?(.+)?`)
)
type stepChecker func(msg *Message) bool
type comparitor func(value interface{}) bool
// Gadgets respond to the Robot Command Language (RCL) and a
// list of RCL messages can be run to form a method. Runner
// takes a method as input and runs it.
//
// If a RCL message starts with 'wait' runner pauses the method and
// waits for the condition if the wait to be fulfilled. For example,
// if the RCL message
// 'wait for 5 seconds'
// is recieved, Runner waits for 5 seconds and continues with the the
// rest of the message.
//
// Another example would be
// 'wait for boiler temperature >= 200 F'.
// MethodRunner would then wait for a message from the boiler that says
// its temperature is 200 F (or more). It then sends the next
// message of the method
type MethodRunner struct {
method Method
stepChecker stepChecker
step int
uid string
out chan<- Message
timeOut chan bool
}
func (m *MethodRunner) GetUID() string {
return "method runner"
}
func (m *MethodRunner) GetDirection() string {
return "na"
}
func (m *MethodRunner) Start(in <-chan Message, out chan<- Message) {
m.uid = m.GetUID()
m.out = out
shutdown := false
m.timeOut = make(chan bool)
for !shutdown {
select {
case msg := <-in:
shutdown = m.readMessage(&msg)
case <-m.timeOut:
m.runNextStep()
}
}
m.out <- Message{}
}
func (m *MethodRunner) readMessage(msg *Message) (shutdown bool) {
if msg.Type == METHOD {
m.startMethod(msg)
shutdown = false
} else if msg.Type == COMMAND && msg.Body == "update" {
m.sendUpdate()
} else if msg.Type == COMMAND && msg.Body == "clear method" {
m.clear()
m.sendUpdate()
} else if len(m.method.Steps) != 0 && (msg.Type == UPDATE || msg.Type == METHODUPDATE) {
m.checkUpdate(msg)
shutdown = false
} else if msg.Type == COMMAND && msg.Body == "shutdown" {
shutdown = true
} else {
shutdown = false
}
return shutdown
}
func (m *MethodRunner) startMethod(msg *Message) {
if msg.Method.Step > 0 {
m.resumeMethod(msg)
} else {
m.method = msg.Method
m.step = -1
m.runNextStep()
}
}
func (m *MethodRunner) resumeMethod(msg *Message) {
m.method = msg.Method
m.step = msg.Method.Step
if m.step >= len(m.method.Steps) {
return
}
cmd := m.method.Steps[m.step]
if strings.Index(cmd, "wait") == -1 {
m.runNextStep()
} else {
m.readWaitCommand(cmd, msg.Method.Time)
}
}
func (m *MethodRunner) sendUpdate() {
m.method.Step = m.step
msg := Message{
Sender: m.GetUID(),
Type: UPDATE,
Method: m.method,
}
m.out <- msg
}
func (m *MethodRunner) checkUpdate(msg *Message) {
if m.stepChecker != nil && m.stepChecker(msg) {
m.stepChecker = nil
m.runNextStep()
}
}
func (m *MethodRunner) clear() {
m.method = Method{}
m.step = -1
}
func (m *MethodRunner) runNextStep() {
m.step += 1
m.method.Step = m.step
m.out <- Message{
Sender: m.uid,
Type: METHODUPDATE,
Method: m.method,
}
if len(m.method.Steps) <= m.step {
m.clear()
return
}
cmd := m.method.Steps[m.step]
if strings.Index(cmd, "wait") == 0 {
m.readWaitCommand(cmd, 0)
} else {
m.sendCommand(cmd)
m.runNextStep()
}
}
func (m *MethodRunner) sendCommand(cmd string) {
msg := Message{
Sender: m.uid,
Type: COMMAND,
Body: cmd,
}
m.out <- msg
}
func (m *MethodRunner) readWaitCommand(cmd string, resumeTime int) {
waitTime, err := m.getWaitTime(cmd, time.Duration(resumeTime)*time.Second)
if strings.Index(cmd, "wait for user") == 0 {
m.setUserStepChecker(cmd)
} else if err == nil {
go m.doCountdown(waitTime)
} else {
m.setStepChecker(cmd)
}
}
func (m *MethodRunner) setUserStepChecker(cmd string) {
m.stepChecker = func(msg *Message) bool {
return msg.Body == cmd
}
}
func (m *MethodRunner) setStepChecker(cmd string) {
uid, operator, value, err := m.parseWaitCommand(cmd)
if err == nil {
compare, err := m.getCompare(operator, value)
if err == nil {
m.stepChecker = func(msg *Message) bool {
return msg.Sender == uid &&
compare(msg.Value.Value)
}
} else {
log.Println(err)
m.stepChecker = func(msg *Message) bool {
return false
}
}
} else {
log.Println(err)
}
}
func (m *MethodRunner) getCompare(operator string, value interface{}) (cmp comparitor, err error) {
switch v := value.(type) {
case float64:
return m.getFloatCompare(operator, v)
default:
return func(x interface{}) bool { return x == v }, nil
}
}
func (m *MethodRunner) getFloatCompare(operator string, value float64) (cmp comparitor, err error) {
if operator == "<=" {
cmp = func(x interface{}) bool { return x.(float64) <= value }
} else if operator == "<" {
cmp = func(x interface{}) bool { return x.(float64) < value }
} else if operator == "==" {
cmp = func(x interface{}) bool { return x.(float64) == value }
} else if operator == ">=" {
cmp = func(x interface{}) bool { return x.(float64) >= value }
} else if operator == ">" {
cmp = func(x interface{}) bool { return x.(float64) > value }
} else {
err = errors.New(fmt.Sprintf("invalid operator: %s", operator))
}
return cmp, err
}
func (m *MethodRunner) parseWaitCommand(cmd string) (uid, operator string, value interface{}, err error) {
result := stepExp.FindStringSubmatch(cmd)
if len(result) == 5 {
uid = result[1]
operator = result[2]
v := result[3]
if v == "true" {
value = true
} else if v == "false" {
value = false
} else {
value, err = strconv.ParseFloat(v, 64)
}
}
return uid, operator, value, err
}
func (m *MethodRunner) getWaitTime(cmd string, resumeTime time.Duration) (waitTime time.Duration, err error) {
result := timeExp.FindStringSubmatch(cmd)
if len(result) != 3 {
err = errors.New(fmt.Sprintf("could not parse command %s", cmd))
return waitTime, err
}
units := result[2]
t, err := strconv.ParseFloat(result[1], 64)
if err != nil {
err = errors.New(fmt.Sprintf("could not parse command %s", cmd))
return waitTime, err
} else {
if units == "minutes" || units == "minute" {
t *= 60.0
} else if units == "hours" || units == "hour" {
t *= 3600.0
}
if resumeTime > 0 {
waitTime = resumeTime
} else {
waitTime = time.Duration(t * float64(time.Second))
}
}
return waitTime, err
}
func (m *MethodRunner) doCountdown(waitTime time.Duration) {
t1 := time.Now()
sleepTime := time.Duration(1 * time.Second)
i := 0.0
m.out <- Message{
Sender: m.uid,
Type: METHODUPDATE,
Method: Method{
Time: int(waitTime.Seconds()),
Step: m.step,
Steps: m.method.Steps,
},
}
for {
time.Sleep(sleepTime)
i += 1.0
t2 := time.Now()
d := t2.Sub(t1)
sleepTime = time.Duration((1 - (d.Seconds() - i)) * float64(time.Second))
m.out <- Message{
Sender: m.uid,
Type: METHODUPDATE,
Method: Method{
Time: int(1 + waitTime.Seconds() - d.Seconds()),
Step: m.step,
Steps: m.method.Steps,
},
}
if d > waitTime {
m.timeOut <- true
return
}
}
}