This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaction.coffee
486 lines (412 loc) · 14.8 KB
/
action.coffee
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
module.exports = (env) ->
_ = env.require 'lodash'
M = env.matcher
Promise = env.require 'bluebird'
assert = env.require 'cassert'
t = env.require('decl-api').types
matchTransitionExpression = (match, callback, optional=yes) ->
matcher = ( (next) =>
next.match(" with", optional: yes)
.match([" transition time "])
.matchTimeDuration(wildcard: "{duration}", type: "text", callback)
)
return if optional then match.optional(matcher) else matcher(match)
################################################################################
## SCENE ACTIONS
################################################################################
class RaspBeeSceneActionProvider extends env.actions.ActionProvider
constructor: (framework) ->
super()
@framework=framework
parseAction: (input, context) =>
matchingScene = null
matchingDevice = null
scenes = []
onSceneMatch = (m, {device,scene}) =>
matchingDevice = device
matchingScene = scene
RaspBeeDevices = _(@framework.deviceManager.devices).values().filter(
(device) => _.includes [
'RaspBeeGroupScenes',
], device.config.class
).value()
if RaspBeeDevices.length is 0 then return
for id, d of RaspBeeDevices
for s in d.config.buttons
scenes.push [{device: d, scene: s.name}, s.name]
m = M(input, context)
.match('activate group scene ')
.match(
scenes,
onSceneMatch
)
match = m.getFullMatch()
if match?
assert matchingScene?
assert matchingDevice?
assert typeof match is "string"
return {
token: match
nextInput: input.substring(match.length)
actionHandler: new SceneActionHandler(matchingDevice, matchingScene)
}
else
return null
class SceneActionHandler extends env.actions.ActionHandler
constructor: (device, scene) ->
super()
@device=device
@scene=scene
assert @device?
assert @scene? and typeof @scene is "string"
setup: ->
@dependOnDevice(@device)
super()
_doExecuteAction: (simulate) =>
return (
if simulate
Promise.resolve __("would activate scene %s of device %s", @scene, @device.id)
else
@device.buttonPressed(@scene)
.then( =>__("activate scene %s of device %s", @scene, @device.id) )
)
executeAction: (simulate) => @_doExecuteAction(simulate)
hasRestoreAction: -> no
################################################################################
## Color temperature ACTIONS
################################################################################
class RaspBeeTempActionHandler extends env.actions.ActionHandler
constructor: (framework, device, valueTokens, @transitionTime=null) ->
super()
@framework=framework
@device=device
@valueTokens=valueTokens
assert @device?
assert @valueTokens?
setup: ->
@dependOnDevice(@device)
super()
_clampVal: (value) ->
assert(not isNaN(value))
return (switch
when value > 100 then 100
when value < 0 then 0
else value
)
_doExecuteAction: (simulate, value) =>
return (
if simulate
__("would change %s to %s%%", @device.name, value)
else
@device.setCT(value,@transitionTime).then( => __("change color temp from %s to %s%%", @device.name, value) )
)
executeAction: (simulate) =>
@device.getCt().then( (lastValue) =>
@lastValue = lastValue or 0
return @framework.variableManager.evaluateNumericExpression(@valueTokens).then( (value) =>
value = @_clampVal value
return @_doExecuteAction(simulate, value)
)
)
hasRestoreAction: -> yes
executeRestoreAction: (simulate) => Promise.resolve(@_doExecuteAction(simulate, @lastValue))
class RaspBeeTempActionProvider extends env.actions.ActionProvider
constructor: (framework) ->
super()
@framework=framework
parseAction: (input, context) =>
retVar = null
RaspBeeDevices = _(@framework.deviceManager.devices).values().filter(
(device) => _.includes [
'RaspBeeCT',
'RaspBeeRGB',
'RaspBeeRGBCT',
'RaspBeeRGBCTGroup'
], device.config.class
).value()
device = null
valueTokens = null
match = null
transitionMs = null
if RaspBeeDevices.length is 0 then return
M(input, context)
.match('set color temp ')
.matchDevice(RaspBeeDevices, (next, d) =>
next.match(' to ')
.matchNumericExpression( (next, ts) =>
m = next.match('%', optional: yes)
if device? and device.id isnt d.id
context?.addError(""""#{input.trim()}" is ambiguous.""")
return
device = d
valueTokens = ts
m = matchTransitionExpression(m, ( (m, {time, unit, timeMs}) =>
transitionMs = timeMs/100
), yes)
match = m.getFullMatch()
)
)
if match?
if valueTokens.length is 1 and not isNaN(valueTokens[0])
value = valueTokens[0]
assert(not isNaN(value))
value = parseFloat(value)
if value < 0.0
context?.addError("Can't dim to a negative dimlevel.")
return
if value > 100.0
context?.addError("Can't dim to greater than 100%.")
return
return {
token: match
nextInput: input.substring(match.length)
actionHandler: new RaspBeeTempActionHandler(@framework, device, valueTokens, transitionMs)
}
else
return null
################################################################################
## Color RGB ACTIONS
################################################################################
class RaspBeeRGBActionHandler extends env.actions.ActionHandler
constructor: (framework, device, hex, @transitionTime=null) ->
super()
@framework=framework
@device=device
@hex=hex
assert @device?
assert @hex?
@result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(@hex)
@r = parseInt(@result[1], 16)
@g = parseInt(@result[2], 16)
@b = parseInt(@result[3], 16)
setup: ->
@dependOnDevice(@device)
super()
_doExecuteAction: (simulate) =>
return (
if simulate
Promise.resolve __("would set color %s to %s%", @device.name)
else
@device.setRGB(@r,@g,@b,@transitionTime).then( => __("set color %s to %s", @device.name, @hex) )
)
executeAction: (simulate) =>
return @_doExecuteAction(simulate)
hasRestoreAction: -> no
class RaspBeeRGBActionProvider extends env.actions.ActionProvider
constructor: (framework) ->
super()
@framework=framework
parseAction: (input, context) =>
RaspBeeDevices = _(@framework.deviceManager.devices).values().filter(
(device) => _.includes [
'RaspBeeRGB'
'RaspBeeRGBCT',
'RaspBeeRGBCTGroup'
], device.config.class
).value()
m = M(input, context).match(['set color rgb '])
device = null
hex = null
match = null
r = null
g = null
b = null
transitionMs = null
m.matchDevice RaspBeeDevices, (m, d) ->
if device? and device.id isnt d.id
context?.addError(""""#{input.trim()}" is ambiguous.""")
return
device = d
m.match [' to '], (m) ->
m.or [
(m) -> m.match [/(#[a-fA-F\d]{6})(.*)/], (m, s) ->
hex = s.trim()
m = matchTransitionExpression(m, ( (m, {time, unit, timeMs}) =>
transitionMs = timeMs/100
), yes)
match = m.getFullMatch()
]
if match?
assert hex?
return {
token : match
nextInput: input.substring(match.length)
actionHandler: new RaspBeeRGBActionHandler(@framework, device, hex, transitionMs)
}
else
return null
class RaspBeeHueSatActionHandler extends env.actions.ActionHandler
constructor: (@framework, @device, @hueExpr, @satExpr, @transitionTime=null) ->
assert @device?
assert @hueExpr? or @satExpr?
executeAction: (@simulate) =>
if @hueExpr? and isNaN(@hueExpr)
huePromise = @framework.variableManager.evaluateExpression(@hueExpr)
else
huePromise = Promise.resolve @hueExpr
if @satExpr? and isNaN(@satExpr)
satPromise = @framework.variableManager.evaluateExpression(@satExpr)
else
satPromise = Promise.resolve @satExpr
return Promise.join huePromise, satPromise, @_changeHueSat
_changeHueSat: (hueValue, satValue) =>
if hueValue? and satValue?
f = (hue, sat) => @device.changeHueSatTo hue, sat, @transitionTime
msg = "changed color to hue #{hueValue}% and sat #{satValue}%"
else if hueValue?
f = (hue, sat) => @device.changeHueTo hue, @transitionTime
msg = "changed color to hue #{hueValue}%"
else if satValue?
f = (hue, sat) => @device.changeSatTo sat, @transitionTime
msg = "changed color to sat #{satValue}%"
msg += " transition time #{@transitionTime}ms" if @transitionTime?
if @simulate
return Promise.resolve "would have #{msg}"
else
return f(hueValue, satValue).then( => msg )
class RaspBeeHueSatActionProvider extends env.actions.ActionProvider
constructor: (framework) ->
super()
@framework=framework
parseAction: (input, context) =>
RaspBeeDevices = _(@framework.deviceManager.devices).values().filter(
(device) => _.includes [
'RaspBeeRGB'
'RaspBeeRGBCT',
'RaspBeeRGBCTGroup'
], device.config.class
).value()
hueValueTokens = null
satValueTokens = null
device = null
hueMatcher = (next) =>
next.match(" hue ")
.matchNumericExpression( (next, ts) => hueValueTokens = ts )
.match('%', optional: yes)
satMatcher = (next) =>
next.match([" sat ", " saturation "])
.matchNumericExpression( (next, ts) => satValueTokens = ts )
.match('%', optional: yes)
match = M(input, context)
.match("set color ")
.matchDevice(RaspBeeDevices, (next, d) =>
if device? and device.id isnt d.id
context?.addError(""""#{input.trim()}" is ambiguous.""")
return
device = d
)
.match(" to")
.or([
( (next) =>
hueMatcher(next)
.optional( (next) =>
satMatcher(next.match(" and", optional: yes))
)
),
( (next) =>
satMatcher(next)
.optional( (next) =>
hueMatcher(next.match(" and", optional: yes))
)
)
])
transitionMs = null
match = matchTransitionExpression(match, (m, {time, unit, timeMs}) =>
transitionMs = timeMs
)
if not (match? and (hueValueTokens? or satValueTokens?))
return null
if hueValueTokens?.length is 1 and not isNaN(hueValueTokens[0])
hueExpr = parseFloat(hueValueTokens[0])
unless hueExpr? and (0.0 <= hueExpr <= 100.0)
context?.addError("Hue value should be between 0% and 100%")
return null
else if hueValueTokens?.length > 0
hueExpr = hueValueTokens
if satValueTokens?.length is 1 and not isNaN(satValueTokens[0])
satExpr = parseFloat(satValueTokens[0])
unless satExpr? and (0.0 <= satExpr <= 100.0)
context?.addError("Saturation value should be between 0% and 100%")
return null
else if satValueTokens?.length > 0
satExpr = satValueTokens
return {
token: match.getFullMatch()
nextInput: input.substring(match.getFullMatch().length)
actionHandler: new RaspBeeHueSatActionHandler(@framework, device, hueExpr, satExpr, transitionMs)
}
class RaspbeeDimmerActionProvider extends env.actions.ActionProvider
constructor: (framework) ->
super()
@framework=framework
parseAction: (input, context) =>
retVar = null
RaspBeeDevices = _(@framework.deviceManager.devices).values().filter(
(device) => _.includes [
'RaspBeeDimmer',
'RaspBeeCT',
'RaspBeeRGB',
'RaspBeeDimmerGroup',
'RaspBeeRGBCT',
'RaspBeeRGBCTGroup'
], device.config.class
).value()
if RaspBeeDevices.length is 0 then return
device = null
valueTokens = null
match = M(input, context)
.match("dim raspbee ")
.matchDevice(RaspBeeDevices, (next, d) =>
if device? and device.id isnt d.id
context?.addError(""""#{input.trim()}" is ambiguous (device).""")
return
device = d
)
.match(" to ")
.matchNumericExpression( (next, ts) => valueTokens = ts )
.match('%', optional: yes)
transitionMs = null
match = matchTransitionExpression(match, ( (m, {time, unit, timeMs}) =>
transitionMs = timeMs/100
), yes)
unless match? and valueTokens? then return null
if valueTokens.length is 1 and not isNaN(valueTokens[0])
unless 0.0 <= parseFloat(valueTokens[0]) <= 100.0
context?.addError("Dimlevel must be between 0% and 100%")
return null
return {
token: match.getFullMatch()
nextInput: input.substring(match.getFullMatch().length)
actionHandler: new RaspbeeDimmerActionHandler(@framework, device, valueTokens, transitionMs)
}
class RaspbeeDimmerActionHandler extends env.actions.ActionHandler
constructor: (framework, device, valueTokens, @transitionTime=null) ->
super()
@framework=framework
@device=device
@valueTokens=valueTokens
assert @device?
assert @valueTokens?
setup: ->
@dependOnDevice(@device)
super()
_doExecuteAction: (simulate, value, transtime) =>
return (
if simulate
__("would dim %s to %s%%", @device.name, value)
else
@device.changeDimlevelTo(value, @transitionTime).then( => __("dimmed %s to %s%%", @device.name, value) )
)
executeAction: (simulate) =>
return @framework.variableManager.evaluateNumericExpression(@valueTokens).then( (value) =>
return @_doExecuteAction(simulate, value )
)
hasRestoreAction: -> yes
executeRestoreAction: (simulate) => Promise.resolve(@_doExecuteAction(simulate, @lastValue))
return exports = {
RaspBeeSceneActionProvider
RaspBeeRGBActionProvider
RaspBeeTempActionProvider
RaspbeeDimmerActionProvider
RaspBeeHueSatActionProvider
}