-
Notifications
You must be signed in to change notification settings - Fork 5
/
rflink.coffee
875 lines (798 loc) · 27.5 KB
/
rflink.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
module.exports = (env) ->
# Require the bluebird promise library
Promise = env.require 'bluebird'
# Require the [cassert library](https://github.com/rhoot/cassert).
assert = env.require 'cassert'
_ = env.require 'lodash'
M = env.matcher
Board = require './lib/board'
Protocol = require './lib/protocol'
class RFLinkPlugin extends env.plugins.Plugin
init: (app, @framework, @config) =>
#disable logginf og rfdebug info when generic debug is on to prevent redundant debug logging
@rfdebug = [email protected] && (@config.rfdebug || @config.rfudebug || @config.qrfdebug)
@protocol = new Protocol
@protocol.on("warning", (warning) =>
env.logger.warn warning
)
@board = new Board(@config.driverOptions, @protocol, @config.connectionTimeout, @config.reconnectInterval, @config.pingInterval)
@board.on("data", (data) =>
if @config.debug
env.logger.debug "data: \"#{data}\""
)
@board.on("send", (data) =>
if @config.debug
env.logger.debug "send: \"#{data.trim()}\""
)
@board.on("rfdebug", (data) =>
if @rfdebug
env.logger.debug "rfdebug: \" #{data}\""
)
@board.on("connected", () =>
env.logger.info("Connected to rflink device.")
if @config.rfdebug
env.logger.info("Enabling RFDEBUG...")
@board.enableRfDebug()
if @config.rfudebug
env.logger.info("Enabling RFUDEBUG...")
@board.enableRfuDebug()
if @config.qrfdebug
env.logger.info("Enabling QRFDEBUG...")
@board.enableQrfDebug()
)
@board.on("debug", (message) =>
if @config.debug
env.logger.debug message
)
@board.on("warning", (warning) =>
env.logger.warn warning
)
@board.on("error", (error) =>
env.logger.error("Error with connection to RFLink device: #{error.message}.")
env.logger.error(error.stack)
)
@framework.on "after init", ( =>
@board.connect(@config.connectionTimeout)
)
@framework.on "destroy", ( =>
@board.destroy()
)
deviceConfigDef = require("./device-config-schema")
deviceClasses = [
RFLinkSwitch
RFLinkDimmer
RFLinkData
RFLinkPir
RFLinkContactSensor
# RFLinkShutter
# RFLinkGenericSensor
]
for Cl in deviceClasses
do (Cl) =>
dcd = deviceConfigDef[Cl.name]
@framework.deviceManager.registerDeviceClass(Cl.name, {
configDef: dcd
createCallback: (deviceConfig, lastState) =>
device = new Cl(deviceConfig, lastState, @board, @config, @protocol)
return device
})
@framework.ruleManager.addPredicateProvider(new RFEventPredicateProvider(@framework))
# if @config.apikey? and @config.apikey.length > 0
# @framework.userManager.addAllowPublicAccessCallback( (req) =>
# return req.url.match(/^\/rflink\/received.*$/)?
# )
# app.get('/rflink/received', (req, res) =>
# if req.query.apikey isnt @config.apikey
# res.end('Invalid apikey')
# return
# buckets = JSON.parse(req.query.buckets)
# pulses = req.query.pulses
# @board.provessExternalReceive(buckets, pulses)
# res.end('ACK')
# )
rflinkPlugin = new RFLinkPlugin()
logDebug = (config, protocol, options) ->
message = "Sending Protocol: #{protocol.name}"
for field, content of options
message += " #{field}: #{content}"
env.logger.debug(message)
sendToSwitchesMixin = (protocols, state = false) ->
pending = []
for p in protocols
do (p) =>
unless p.send is false
event = _.clone(p)
event.id = @protocol.decodeAttribute('id',event.id)
delete event.send
delete event.receive
event.action= @protocol.encodeAttribute('cmd', {'state': state})
pending.push @board.connectionReady.then( =>
if @_pluginConfig.debug
logDebug(@_pluginConfig, p, event)
return @board.encodeAndWriteEvent(event)
)
return Promise.all(pending)
sendToDimmersMixin = (protocols, level = 0) ->
pending = []
for p in protocols
do (p) =>
unless p.send is false
event = _.clone(p)
event.id = @protocol.decodeAttribute('id',event.id)
delete event.send
delete event.receive
if level is 0
event.action= @protocol.encodeAttribute('cmd', {'state': false})
else
event.action= @protocol.encodeAttribute('set_level', level)
pending.push @board.connectionReady.then( =>
if @_pluginConfig.debug
logDebug(@_pluginConfig, p, event)
return @board.encodeAndWriteEvent(event)
)
return Promise.all(pending)
class RFLinkSwitch extends env.devices.PowerSwitch
constructor: (@config, lastState, @board, @_pluginConfig, @protocol) ->
@id = @config.id
@name = @config.name
@_state = lastState?.state?.value
@board.on('rf', @rfListener = (event) =>
for p in @config.protocols
unless p.receive is false
if @protocol.switchEventMatches(event, p)
@emit('rf', event.cmd.state) # used by the RFEventPredicateHandler
@_setState(event.cmd.state)
)
super()
destroy: () ->
@board.removeListener 'rf', @rfListener
super()
_sendStateToSwitches: sendToSwitchesMixin
changeStateTo: (state) ->
unless @config.forceSend
if @_state is state then return Promise.resolve true
@_sendStateToSwitches(@config.protocols, state).then( =>
@_setState(state)
)
class RFLinkDimmer extends env.devices.DimmerActuator
_lastdimlevel: null
constructor: (@config, lastState, @board, @_pluginConfig, @protocol) ->
@id = @config.id
@name = @config.name
@_dimlevel = lastState?.dimlevel?.value or 0
@_lastdimlevel = lastState?.lastdimlevel?.value or 100
@_state = lastState?.state?.value or off
@board.on('rf', @rfListener = (event) =>
for p in @config.protocols
unless p.receive is false
if @protocol.switchEventMatches(event, p)
if event.cmd.level?
@_setDimlevel(event.cmd.level)
else
if event.cmd.state is false
unless @_dimlevel is 0
@_lastdimlevel = @_dimlevel
@_setDimlevel(0)
else
@_setDimlevel(@_lastdimlevel)
)
super()
destroy: () ->
@board.removeListener 'rf', @rfListener
super()
_sendLevelToDimmers: sendToDimmersMixin
turnOn: -> @changeDimlevelTo(@_lastdimlevel)
changeDimlevelTo: (level) ->
unless @config.forceSend
if @_dimlevel is level then return Promise.resolve true
unless @_dimlevel is 0
@_lastdimlevel = @_dimlevel
@_sendLevelToDimmers(@config.protocols, level).then( =>
@_setDimlevel(level)
)
class RFLinkContactSensor extends env.devices.ContactSensor
constructor: (@config, lastState, @board, @_pluginConfig, @protocol) ->
@id = @config.id
@name = @config.name
@_contact = lastState?.contact?.value or false
@board.on('rf', @rfListener = (event) =>
for p in @config.protocols
if @protocol.switchEventMatches(event, p)
if @config.invert is false
@_setContact(event.cmd.state)
else
@_setContact(!event.cmd.state)
if @config.autoReset is true
clearTimeout(@_resetContactTimeout)
@_resetContactTimeout = setTimeout(( =>
@_setContact(!event.cmd.state)
), @config.resetTime)
)
super()
destroy: () ->
@board.removeListener 'rf', @rfListener
super()
# class RFLinkShutter extends env.devices.ShutterController
#
# constructor: (@config, lastState, @board, @_pluginConfig) ->
# @id = @config.id
# @name = @config.name
# @_position = lastState?.position?.value or 'stopped'
#
# for p in @config.protocols
# _protocol = Board.getRfProtocol(p.name)
# unless _protocol?
# throw new Error("Could not find a protocol with the name \"#{p.name}\".")
#
# @board.on('rf', (event) =>
# for p in @config.protocols
# match = doesProtocolMatch(event, p)
# unless match
# return
# now = new Date().getTime()
# # ignore own send messages
# if (now - @_lastSendTime) < 3000
# return
# if @_position is 'stopped'
# @_setPosition(if event.values.state then 'up' else 'down')
# else
# @_setPosition('stopped')
# )
# super()
#
# _sendStateToSwitches: sendToSwitchesMixin
#
# stop: ->
# unless @config.forceSend
# if @_position is 'stopped' then return Promise.resolve()
# @_sendStateToSwitches(@config.protocols, @_position is 'up').then( =>
# @_setPosition('stopped')
# )
#
# return Promise.resolve()
#
# # Returns a promise that is fulfilled when done.
# moveToPosition: (position) ->
# unless @config.forceSend
# if position is @_position then return Promise.resolve()
# if position is 'stopped' then return @stop()
# else return @_sendStateToSwitches(@config.protocols, position is 'up').then( =>
# @_lastSendTime = new Date().getTime()
# @_setPosition(position)
# )
#
#
class RFLinkPir extends env.devices.PresenceSensor
constructor: (@config, lastState, @board, @_pluginConfig, @protocol) ->
@id = @config.id
@name = @config.name
@_presence = lastState?.presence?.value or false
resetPresence = ( =>
@_setPresence(no)
)
@board.on('rf', @rfListener = (event) =>
for p in @config.protocols
if @protocol.switchEventMatches(event, p)
if @config.invert is false
@_setPresence(event.cmd.state)
else
@_setPresence(!event.cmd.state)
if @config.autoReset is true
@_resetPresenceTimeout = setTimeout(resetPresence, @config.resetTime)
)
super()
destroy: () ->
@board.removeListener 'rf', @rfListener
super()
getPresence: -> Promise.resolve @_presence
class RFLinkData extends env.devices.Sensor
constructor: (@config, lastState, @board, @_pluginConfig, @protocol) ->
@id = @config.id
@name = @config.name
@_temp = lastState?.temp?.value or 0
@_hum = lastState?.hum?.value or 0
@_baro = lastState?.baro?.value or 0
@_hstatus = lastState?.hstatus?.value or 0
@_bforecast = lastState?.bforecast?.value or 0
@_uv = lastState?.uv?.value or 0
@_lux = lastState?.lux?.value or 0
@_bat = lastState?.bat?.value or ""
@_rain = lastState?.rain?.value or 0
@_rainrate = lastState?.rainrate?.value or 0
@_raintot = lastState?.raintot?.value or 0
@_winsp = lastState?.winsp?.value or 0
@_awinsp = lastState?.awinsp?.value or 0
@_wings = lastState?.wings?.value or 0
@_windir = lastState?.windir?.value or 0
@_winchl = lastState?.winchl?.value or 0
@_wintmp = lastState?.wintmp?.value or 0
@_co2 = lastState?.co2?.value or 0
@_sound = lastState?.sound?.value or 0
@_kwatt = lastState?.kwatt?.value or 0
@_watt = lastState?.watt?.value or 0
@_current = lastState?.current?.value or 0
@_current2 = lastState?.current2?.value or 0
@_current3 = lastState?.current3?.value or 0
@_dist = lastState?.dist?.value or 0
@_meter = lastState?.meter?.value or 0
@_volt = lastState?.volt?.value or 0
@attributes = {}
for s in @config.values
switch s
when "temp"
@attributes.temp = {
description: "the measured temperature"
type: "number"
unit: '°C'
acronym: 'T'
}
when "hum"
@attributes.hum = {
description: "the measured humidity"
type: "number"
unit: '%'
acronym: 'RH'
}
when "baro"
@attributes.baro = {
description: "the measured barometric pressure"
type: "number"
unit: 'mbar'
acronym: 'PB'
}
when "hstatus"
@attributes.hstatus = {
description: "indication of the weather 0=Normal, 1=Comfortable, 2=Dry, 3=Wet"
type: "number"
unit: ''
acronym: 'ACTUAL'
}
when "bforecast"
@attributes.bforecast = {
description: "prediction of the weather 0=No Info/Unknown, 1=Sunny, 2=Partly Cloudy, 3=Cloudy, 4=Rain"
type: "number"
unit: ''
acronym: 'FORECAST'
}
when "uv"
@attributes.uv = {
description: "the measured UV intensity"
type: "number"
unit: ''
acronym: 'UV'
}
when "lux"
@attributes.lux = {
description: "the measured light intensity"
type: "number"
unit: 'lx'
acronym: 'LUM'
}
when "bat"
@attributes.bat = {
description: "the battery status"
type: "string"
unit: ''
acronym: 'BAT'
}
when "rain"
@attributes.rain = {
description: "the measured rain rate"
type: "number"
unit: 'mm'
acronym: 'RAIN'
}
when "rainrate"
@attributes.rainrate = {
description: "the measured rain rate"
type: "number"
unit: 'mm'
acronym: 'RAINR'
}
when "raintot"
@attributes.raintot = {
description: "the measured total rain per 24 hours"
type: "number"
unit: 'mm'
acronym: 'RAINT'
}
when "winsp"
@attributes.winsp = {
description: "the measured wind speed"
type: "number"
unit: 'km/h'
acronym: 'WIND'
}
when "awinsp"
@attributes.winsp = {
description: "the measured average wind speed"
type: "number"
unit: 'km/h'
acronym: 'MWIND'
}
when "wings"
@attributes.wings = {
description: "the measured wind gust"
type: "number"
unit: 'km/h'
acronym: 'GUST'
}
when "windir"
@attributes.windir = {
description: "the measured wind direction 0-360"
type: "number"
unit: ''
acronym: 'WDIR'
}
when "winchil"
@attributes.winchil = {
description: "the measured wind chill"
type: "number"
unit: '°C'
acronym: 'CHILL'
}
when "wintmp"
@attributes.wintmp = {
description: "the measured wind temperature"
type: "number"
unit: '°C'
acronym: 'WTEMP'
}
when "co2"
@attributes.co2 = {
description: "the measured CO2 air quality"
type: "number"
unit: ''
acronym: 'CO2'
}
when "sound"
@attributes.sound = {
description: "the measured noise level"
type: "number"
unit: ''
acronym: 'SOUND'
}
when "kwatt"
@attributes.kwatt = {
description: "the measured power value"
type: "number"
unit: 'kW'
acronym: 'POWER'
}
when "watt"
@attributes.watt = {
description: "the measured power value"
type: "number"
unit: 'W'
acronym: 'POWER'
}
when "current"
@attributes.current = {
description: "Current phase 1"
type: "number"
unit: 'A'
acronym: 'I'
}
when "current2"
@attributes.current2 = {
description: "Current phase 2"
type: "number"
unit: 'A'
acronym: 'I2'
}
when "current3"
@attributes.current3 = {
description: "Current phase 3"
type: "number"
unit: 'A'
acronym: 'I3'
}
when "dist"
@attributes.dist = {
description: "the measured distance value"
type: "number"
unit: 'mm'
acronym: 'DISTANCE'
}
when "meter"
@attributes.meter = {
description: "the measured meter value"
type: "number"
unit: ''
acronym: 'METER'
}
when "volt"
@attributes.volt = {
description: "the measured voltage value"
type: "number"
unit: 'V'
acronym: 'U'
}
else
throw new Error(
"Values should be one of: temp, hum, baro, hstatus, bforecast, uv, lux, bat, rain, raintot, winsp, awinsp, wings, windir, winchil, wintmp, co2, sound, kwatt, watt, dist, meter, volt, current"
)
@board.on('rf', @rfListener = (event) =>
for p in @config.protocols
if @protocol.eventMatches(event, p)
if event.temp?
@_temp = event.temp
@emit "temp", @_temp
if event.hum?
@_hum = event.hum
@emit "hum", @_hum
if event.baro?
@_baro = event.baro
@emit "baro", @_baro
if event.hstatus?
@_hstatus = event.hstatus
@emit "hstatus", @_hstatus
if event.bforecast?
@_bforecast = event.bforecast
@emit "bforecast", @_bforecast
if event.uv?
@_uv = event.uv
@emit "uv", @_uv
if event.lux?
@_lux = event.lux
@emit "lux", @_lux
if event.bat?
@_bat = event.bat
@emit "bat", @_bat
if event.rain?
@_rain = event.rain
@emit "rain", @_rain
if event.rainrate?
@_rain = event.rainrate
@emit "rainrate", @_rainrate
if event.raintot?
@_raintot = event.raintot
@emit "raintot", @_raintot
if event.winsp?
@_winsp = event.winsp
@emit "winsp", @_winsp
if event.awinsp?
@_awinsp = event.awinsp
@emit "awinsp", @_awinsp
if event.wings?
@_wings = event.wings
@emit "wings", @_wings
if event.windir?
@_windir = event.windir
@emit "windir", @_windir
if event.winchl?
@_winchl = event.winchl
@emit "winchl", @_winchl
if event.wintmp?
@_wintmp = event.wintmp
@emit "wintmp", @_wintmp
if event.co2?
@_co2 = event.co2
@emit "co2", @_co2
if event.sound?
@_sound = event.sound
@emit "sound", @_sound
if event.kwatt?
@_kwatt = event.kwatt
@emit "kwatt", @_kwatt
if event.watt?
@_watt = event.watt
@emit "watt", @_watt
if event.current?
@_current = event.current
@emit "current", @_current
if event.current2?
@_current2 = event.current2
@emit "current2", @_current2
if event.current3?
@_current3 = event.current3
@emit "current3", @_current3
if event.dist?
@_dist = event.dist
@emit "dist", @_dist
if event.meter?
@_meter = event.meter
@emit "meter", @_meter
if event.volt?
@_volt = event.volt
@emit "volt", @_volt
)
super()
destroy: () ->
@board.removeListener 'rf', @rfListener
super()
getTemp: -> Promise.resolve @_temp
getHum: -> Promise.resolve @_hum
getBaro: -> Promise.resolve @_baro
getHstatus: -> Promise.resolve @_hstatus
getBforecast: -> Promise.resolve @_bforecast
getUv: -> Promise.resolve @_uv
getLux: -> Promise.resolve @_lux
getBat: -> Promise.resolve @_bat
getRain: -> Promise.resolve @_rain
getRainrate: -> Promise.resolve @_rainrate
getRaintot: -> Promise.resolve @_raintot
getWinsp: -> Promise.resolve @_winsp
getAwinsp: -> Promise.resolve @_awinsp
getWings: -> Promise.resolve @_wings
getWindir: -> Promise.resolve @_windir
getWinchl: -> Promise.resolve @_winchl
getWintmp: -> Promise.resolve @_wintmp
getCo2: -> Promise.resolve @_co2
getSound: -> Promise.resolve @_sound
getKwatt: -> Promise.resolve @_kwatt
getWatt: -> Promise.resolve @_watt
getCurrent: -> Promise.resolve @_current
getCurrent2: -> Promise.resolve @_current2
getCurrent3: -> Promise.resolve @_current3
getDist: -> Promise.resolve @_dist
getMeter: -> Promise.resolve @_meter
getVolt: -> Promise.resolve @_volt
# class RFLinkGenericSensor extends env.devices.Sensor
#
# constructor: (@config, lastState, @board) ->
# @id = @config.id
# @name = @config.name
#
# for p in @config.protocols
# _protocol = Board.getRfProtocol(p.name)
# unless _protocol?
# throw new Error("Could not find a protocol with the name \"#{p.name}\".")
# unless _protocol.type is "generic"
# throw new Error("\"#{p.name}\" is not a generic protocol.")
#
# @attributes = {}
# for attributeConfig in @config.attributes
# @_createAttribute(attributeConfig)
#
# @_lastReceiveTimes = {}
# @board.on('rf', (event) =>
# for p in @config.protocols
# match = doesProtocolMatch(event, p)
# if match
# for attributeConfig in @config.attributes
# @_updateAttribute(attributeConfig, event)
# )
# super()
#
# _createAttribute: (attributeConfig) ->
# name = attributeConfig.name
# if @attributes[name]?
# throw new Error(
# "Two attributes with the same name in RFLinkGenericSensor config \"#{name}\""
# )
# # Set description and label
# @attributes[name] = {
# description: name
# label: (
# if attributeConfig.label? and attributeConfig.label.length > 0 then attributeConfig.label
# else name
# )
# type: "number"
# }
# # Set unit
# if attributeConfig.unit? and attributeConfig.unit.length > 0
# @attributes[name].unit = attributeConfig.unit
#
# if attributeConfig.discrete?
# @attributes[name].discrete = attributeConfig.discrete
#
# if attributeConfig.acronym?
# @attributes[name].acronym = attributeConfig.acronym
#
# # generate getter:
# @_createGetter(name, => Promise.resolve(@_attributesMeta[name].value))
#
# _updateAttribute: (attributeConfig, event) ->
# name = attributeConfig.name
# now = (new Date()).getTime()
# timeDelta = (
# if @_lastReceiveTimes[name]? then (now - @_lastReceiveTimes[name])
# else 9999999
# )
# if timeDelta < 2000
# return
#
# unless event.values.value?
# return
#
# unless event.values.type is attributeConfig.type
# return
#
# baseValue = attributeConfig.baseValue
# decimalsDivider = Math.pow(10, attributeConfig.decimals)
# value = event.values.value / decimalsDivider
# value = -value if event.values.positive is false
# value += baseValue
# @emit name, value
# @_lastReceiveTimes[name] = now
###
The RF-Event Predicate Provider
----------------
Provides predicates for the state of switch devices like:
* _device_ receives on|off
####
class RFEventPredicateProvider extends env.predicates.PredicateProvider
constructor: (@framework) ->
# ### parsePredicate()
parsePredicate: (input, context) ->
rfSwitchDevices = _(@framework.deviceManager.devices)
.filter( (device) => device instanceof RFLinkSwitch ).value()
device = null
state = null
match = null
M(input, context)
.matchDevice(rfSwitchDevices, (next, d) =>
next.match([' receives'])
.match([' on', ' off'], (next, s) =>
# Already had a match with another device?
if device? and device.id isnt d.id
context?.addError(""""#{input.trim()}" is ambiguous.""")
return
assert d?
assert s in [' on', ' off']
device = d
state = s.trim() is 'on'
match = next.getFullMatch()
)
)
# If we have a match
if match?
assert device?
assert state?
assert typeof match is "string"
# and state as boolean.
return {
token: match
nextInput: input.substring(match.length)
predicateHandler: new RFEventPredicateHandler(device, state)
}
else
return null
class RFEventPredicateHandler extends env.predicates.PredicateHandler
constructor: (@device, @state) ->
setup: ->
lastTime = 0
@rfListener = (newState) =>
if @state is newState
now = new Date().getTime()
# suppress same values within 200ms
if now - lastTime <= 200
return
lastTime = now
@emit 'change', 'event'
@device.on 'rf', @rfListener
super()
getValue: -> Promise.resolve(false)
destroy: ->
@device.removeListener "rf", @rfListener
super()
getType: -> 'event'
return rflinkPlugin