forked from DheerajKhajuria/pimatic-mysensors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySensors.coffee
executable file
·2290 lines (2005 loc) · 71.6 KB
/
MySensors.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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module.exports = (env) ->
Promise = env.require 'bluebird'
assert = env.require 'cassert'
_ = env.require 'lodash'
events = env.require 'events'
M = env.matcher
V_TEMP = 0
V_HUM = 1
V_STATUS = 2
V_PERCENTAGE = 3
V_PRESSURE = 4
V_FORECAST = 5
V_RAIN = 6
V_RAINRATE = 7
V_WIND = 8
V_GUST = 9
V_DIRECTION = 10
V_UV = 11
V_WEIGHT = 12
V_DISTANCE = 13
V_IMPEDANCE = 14
V_ARMED = 15
V_TRIPPED = 16
V_WATT = 17
V_KWH = 18
V_SCENE_ON = 19
V_SCENE_OFF = 20
V_HEATER = 21
V_HEATER_SW = 22
V_LIGHT_LEVEL = 23
V_VAR1 = 24
V_VAR2 = 25
V_VAR3 = 26
V_VAR4 = 27
V_VAR5 = 28
V_UP = 29
V_DOWN = 30
V_STOP = 31
V_IR_SEND = 32
V_IR_RECEIVE = 33
V_FLOW = 34
V_VOLUME = 35
V_LOCK_STATUS = 36
V_LEVEL = 37
V_VOLTAGE = 38
V_CURRENT = 39
V_RGB = 40
V_RGBW = 41
V_ID = 42
V_UNIT_PREFIX = 43
V_HVAC_SETPOINT_COOL = 44
V_HVAC_SETPOINT_HEAT = 45
V_HVAC_FLOW_MODE = 46
V_TEXT = 47
V_CUSTOM = 48
V_POSITION = 49
V_IR_RECORD = 50
V_PH = 51
V_ORP = 52
V_EC = 53
ZERO_VALUE = "0"
FIRMWARE_BLOCK_SIZE = 16
BROADCAST_ADDRESS = 255
NODE_SENSOR_ID = 255
C_PRESENTATION = 0
C_SET = 1
C_REQ = 2
C_INTERNAL = 3
C_STREAM = 4
I_BATTERY_LEVEL = 0
I_TIME = 1
I_VERSION = 2
I_ID_REQUEST = 3
I_ID_RESPONSE = 4
I_INCLUSION_MODE = 5
I_CONFIG = 6
I_PING = 7
I_PING_ACK = 8
I_LOG_MESSAGE = 9
I_CHILDREN = 10
I_SKETCH_NAME = 11
I_SKETCH_VERSION = 12
I_REBOOT = 13
I_GATEWAY_READY = 14
I_REQUEST_SIGNING = 15
I_GET_NONCE = 16
I_GET_NONCE_RESPONSE = 17
I_HEARTBEAT = 18
I_PRESENTATION = 19
I_DISCOVER = 20
I_DISCOVER_RESPONSE = 21
I_HEARTBEAT_RESPONSE = 22
I_LOCKED = 23
S_DOOR = 0
S_MOTION = 1
S_SMOKE = 2
S_LIGHT = 3
S_DIMMER = 4
S_COVER = 5
S_TEMP = 6
S_HUM = 7
S_BARO = 8
S_WIND = 9
S_RAIN = 10
S_UV = 11
S_WEIGHT = 12
S_POWER = 13
S_HEATER = 14
S_DISTANCE = 15
S_LIGHT_LEVEL = 16
S_ARDUINO_NODE = 17
S_ARDUINO_REPEATER_NODE = 18
S_LOCK = 19
S_IR = 20
S_WATER = 21
S_AIR_QUALITY = 22
S_CUSTOM = 23
S_DUST = 24
S_SCENE_CONTROLLER = 25
S_RGB_LIGHT = 26
S_RGBW_LIGHT = 27
S_COLOR_SENSOR = 28
S_HVAC = 29
S_MULTIMETER = 30
S_SPRINKLER = 31
S_WATER_LEAK = 32
S_SOUND = 33
S_VIBRATION = 34
S_MOISTURE = 35
S_INFO = 36
S_GAS = 37
S_GPS = 38
S_WATER_QUALITY = 39
ST_FIRMWARE_CONFIG_REQUEST = 0
ST_FIRMWARE_CONFIG_RESPONSE = 1
ST_FIRMWARE_REQUEST = 2
ST_FIRMWARE_RESPONSE = 3
ST_SOUND = 4
ST_IMAGE = 5
P_STRING = 0
P_BYTE = 1
P_INT16 = 2
P_UINT16 = 3
P_LONG32 = 4
P_ULONG32 = 5
P_CUSTOM = 6
P_FLOAT32 = 7
class Board extends events.EventEmitter
constructor: (framework,config) ->
@config = config
@framework = framework
assert @config.time in ["utc", "local"]
assert @config.driver in ["serialport", "ethernet"]
@timeOffset = 0
if @config.time is "local"
@timeOffset = ((new Date()).getTimezoneOffset() * 60 )
env.logger.debug "<- TimeOffset ", @timeOffset
# setup a new driver
switch @config.driver
when "serialport"
SerialPortDriver = require('./serialport')(env)
@driver = new SerialPortDriver(@config.driverOptions)
when "ethernet"
EthernetDriver = require './ethernet'
@driver = new EthernetDriver(@config.driverOptions)
@driver.on('error', (error) =>
env.logger.error('Disconnected from gateway')
@emit('error', error)
)
@driver.on('reconnect', (error) =>
@emit('reconnect', error)
)
@driver.on('close', =>
@emit('close')
)
@driver.on("data", (data) =>
@emit "data", data
)
@driver.on("line", (line) =>
@emit "line", line
@_rfReceived(line)
)
connect: (timeout = 2500, retries = 3) ->
return @pendingConnect = @driver.connect(timeout, retries)
disconnect: ->
return @driver.disconnect()
_rfReceived: (data) ->
# decoding message
datas = data.toString().split(";")
sender = parseInt datas[0]
sensor = parseInt datas[1]
command = parseInt datas[2]
ack = parseInt datas[3]
type = parseInt datas[4]
rawpayload = ""
if (datas[5])
rawpayload = datas[5].trim()
switch command
when C_PRESENTATION
if @config.debug
env.logger.debug "<- Presented Node ", datas
@_rfpresent(sender,sensor,type)
when C_SET
@_rfsendtoboard(sender,sensor,type,rawpayload)
when C_REQ
if @config.debug
env.logger.debug "<- request from ", sender, rawpayload
@_rfrequest(sender,sensor,type)
when C_INTERNAL
switch type
when I_BATTERY_LEVEL
if @config.debug
env.logger.debug "<- I_BATTERY_LEVEL ", sender, rawpayload
@_rfsendbatterystat(sender,rawpayload)
when I_TIME
if @config.debug
env.logger.debug "<- I_TIME ", data
@_rfsendTime(sender, sensor)
when I_VERSION
if @config.debug
env.logger.debug "<- I_VERSION ", rawpayload
when I_ID_REQUEST
if @config.debug
env.logger.debug "<- I_ID_REQUEST ", data
@_rfsendNextAvailableSensorId()
when I_ID_RESPONSE
if @config.debug
env.logger.debug "<- I_ID_RESPONSE ", data
when I_INCLUSION_MODE
if @config.debug
env.logger.debug "<- I_INCLUSION_MODE ", data
when I_CONFIG
if @config.debug
env.logger.debug "<- I_CONFIG ", data
@_rfsendConfig(sender)
when I_PING
if @config.debug
env.logger.debug "<- I_PING ", data
when I_PING_ACK
if @config.debug
env.logger.debug "<- I_PING_ACK ", data
when I_LOG_MESSAGE
if @config.debug
env.logger.debug "<- I_LOG_MESSAGE ", data
when I_CHILDREN
if @config.debug
env.logger.debug "<- I_CHILDREN ", data
when I_SKETCH_NAME
#saveSketchName(sender, payload, db);
if @config.debug
env.logger.debug "<- I_SKETCH_NAME ", data
when I_SKETCH_VERSION
#saveSketchVersion(sender, payload, db);
if @config.debug
env.logger.debug "<- I_SKETCH_VERSION ", data
_rfsendTime: (destination,sensor) ->
date = new Date()
payload = Math.floor( ( date.getTime() ) / 1000 ) - @timeOffset
datas =
{
"destination": destination,
"sensor": sensor,
"type" : I_TIME,
"ack" : 0,
"command" : C_INTERNAL,
"value" : payload
}
@_rfWrite( datas)
_rfsendNextAvailableSensorId: ->
newid = false
nextnodeid = @config.startingNodeId
if nextnodeid is null
nextnodeid = 1
else
nextnodeid +=1
while newid is false
newid = not @framework.deviceManager.devicesConfig.some (device, iterator) =>
device.nodeid is nextnodeid
if newid is false
nextnodeid +=1
if newid is true and nextnodeid < 255
datas =
{
"destination": BROADCAST_ADDRESS,
"sensor": NODE_SENSOR_ID,
"type" : I_ID_RESPONSE,
"ack" : 0,
"command" : C_INTERNAL,
"value" : nextnodeid
}
@config.startingNodeId = nextnodeid
@_rfWrite(datas)
@framework.saveConfig()
else
env.logger.error "-> Error assigning next node ID"
_rfrequest: (sender,sensor,type) ->
result = {
"sender": sender,
"sensor": sensor,
"type": type
}
@emit "rfRequest", result
_rfpresent: (sender,sensor,type) ->
result = {
"sender": sender,
"sensor": sensor,
"type" : type
}
@emit "rfPresent", result
_rfsendtoboard: (sender,sensor,type,rawpayload) ->
result = {
"sender": sender,
"sensor": sensor,
"type" : type,
"value" : rawpayload
}
@emit "rfValue", result
_rfsendbatterystat: (sender,rawpayload) ->
result = {
"sender": sender,
"value" : rawpayload
}
@emit "rfbattery", result
_rfsendConfig: (destination) ->
datas = {
"destination": destination,
"sensor": NODE_SENSOR_ID,
"type" : I_CONFIG,
"ack" : 0,
"command" : C_INTERNAL,
"value" : @config.metric
}
@_rfWrite(datas)
_rfWrite: (datas) ->
datas.command ?= C_SET
data = @_rfencode(datas.destination,datas.sensor,datas.command,datas.ack,datas.type,datas.value)
if @config.debug
env.logger.debug "-> Sending ", data
@driver.write(data)
_rfencode: (destination, sensor, command, acknowledge, type, payload) ->
msg = destination.toString(10) + ";" + sensor.toString(10) + ";" + command.toString(10) + ";" + acknowledge.toString(10) + ";" + type.toString(10) + ";";
msg += payload
msg += '\n'
return msg.toString()
Promise.promisifyAll(Board.prototype)
## MySensors class.
class MySensors extends env.plugins.Plugin
init: (app, @framework, @config) =>
@board = new Board(@framework, @config)
@board.connect().then( =>
env.logger.info("Connected to MySensors Gateway.")
)
@board.on('error', (error) =>
env.logger.info("Trying to reconnect in 10s")
setTimeout(( =>
@board.connect()
), 10000)
)
deviceConfigDef = require("./device-config-schema")
# Discover MySensor nodes
@framework.deviceManager.on('discover', (eventData) =>
@framework.deviceManager.discoverMessage(
'pimatic-mysensors', "Searching for nodes"
)
# Stop searching after configured time
setTimeout(( =>
@board.removeListener("rfPresent", discoverListener)
), eventData.time)
# Received presentation message from the gateway
@board.on("rfPresent", discoverListener = (result) =>
newdevice = true
nodeid = result.sender
sensorid = result.sensor
sensortype = result.type
# Check if device already exists in pimatic
newdevice = not @framework.deviceManager.devicesConfig.some (device, iterator) =>
device.sensorid is sensorid and device.nodeid is nodeid
# Device is a new device and not a battery device
if newdevice
# Temp sensor found
if sensortype is S_TEMP
config = {
class: 'MySensorsDST',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Temp Sensor #{nodeid}.#{sensorid}", config
)
# PIR sensor found
if sensortype is S_MOTION
config = {
class: 'MySensorsPIR',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Motion Sensor #{nodeid}.#{sensorid}", config
)
# Smoke sensor found
if sensortype is S_SMOKE
config = {
class: 'MySensorsPIR',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Smoke Sensor #{nodeid}.#{sensorid}", config
)
# Moisture sensor found
if sensortype is S_MOISTURE
config = {
class: 'MySensorsPIR',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Moisture Sensor #{nodeid}.#{sensorid}", config
)
# Leak sensor found
if sensortype is S_WATER_LEAK
config = {
class: 'MySensorsPIR',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Leak Sensor #{nodeid}.#{sensorid}", config
)
# Contact sensor found
if sensortype is S_DOOR
config = {
class: 'MySensorsButton',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Contact Sensor #{nodeid}.#{sensorid}", config
)
# Shutter found
if sensortype is S_COVER
config = {
class: 'MySensorsShutter',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Shutter #{nodeid}.#{sensorid}", config
)
# Light sensor found
if sensortype is S_LIGHT_LEVEL
config = {
class: 'MySensorsLight',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Light Sensor #{nodeid}.#{sensorid}", config
)
# Lux sensor found
if sensortype is S_LIGHT_LEVEL
config = {
class: 'MySensorsLux',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Lux Sensor #{nodeid}.#{sensorid}", config
)
# kWh sensor found
if sensortype is S_POWER
config = {
class: 'MySensorsPulseMeter',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "kWh Sensor #{nodeid}.#{sensorid}", config
)
# Water sensor found
if sensortype is S_WATER
config = {
class: 'MySensorsWaterMeter',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Water Sensor #{nodeid}.#{sensorid}", config
)
# pH sensor found
if sensortype is S_WATER_QUALITY
config = {
class: 'MySensorsPH',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "pH Sensor #{nodeid}.#{sensorid}", config
)
# Switch found
if sensortype is S_LIGHT or sensortype is S_SPRINKLER
config = {
class: 'MySensorsSwitch',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Switch #{nodeid}.#{sensorid}", config
)
# Dimmer found
if sensortype is S_DIMMER
config = {
class: 'MySensorsDimmer',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Dimmer #{nodeid}.#{sensorid}", config
)
# Distance sensor found
if sensortype is S_DISTANCE
config = {
class: 'MySensorsDistance',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Distance Sensor #{nodeid}.#{sensorid}", config
)
# Gas sensor found
if sensortype is S_AIR_QUALITY
config = {
class: 'MySensorsGas',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "Gas sensor #{nodeid}.#{sensorid}", config
)
# IR sensor found
if sensortype is S_IR
config = {
class: 'MySensorsIR',
nodeid: nodeid,
sensorid: sensorid
}
@framework.deviceManager.discoveredDevice(
'pimatic-mysensors', "IR sensor #{nodeid}.#{sensorid}", config
)
)
)
@framework.ruleManager.addActionProvider(new MySensorsActionProvider @framework,@board, @config)
deviceClasses = [
MySensorsDHT
MySensorsDST
MySensorsBMP
MySensorsPIR
MySensorsSwitch
MySensorsDimmer
MySensorsPulseMeter
MySensorsEnergyMeter
MySensorsWaterMeter
MySensorsPH
MySensorsButton
MySensorsLight
MySensorsLux
MySensorsDistance
MySensorsGas
MySensorsShutter
MySensorsMulti
MySensorsIR
]
for Cl in deviceClasses
do (Cl) =>
@framework.deviceManager.registerDeviceClass(Cl.name, {
configDef: deviceConfigDef[Cl.name]
createCallback: (config,lastState) =>
device = new Cl(config,lastState, @board)
return device
})
# registerDevice for MySensorsBattery device
@framework.deviceManager.registerDeviceClass(MySensorsBattery.name, {
configDef: deviceConfigDef[MySensorsBattery.name]
createCallback: (config,lastState) =>
device = new MySensorsBattery(config,lastState, @board,@framework)
return device
})
class MySensorsDHT extends env.devices.TemperatureSensor
constructor: (@config,lastState, @board) ->
@id = @config.id
@name = @config.name
@_temperatue = lastState?.temperature?.value
@_humidity = lastState?.humidity?.value
@_battery = lastState?.battery?.value
if mySensors.config.debug
env.logger.debug "MySensorsDHT ", @id, @name
@attributes = {}
@attributes.temperature = {
description: "the measured temperature"
type: "number"
unit: '°C'
acronym: 'T'
}
@attributes.humidity = {
description: "the measured humidity"
type: "number"
unit: '%'
acronym: 'RH'
}
@attributes.battery = {
description: "Display the battery level of sensor"
type: "number"
displaySparkline: false
unit: "%"
icon:
noText: true
mapping: {
'icon-battery-empty': 0
'icon-battery-fuel-1': [0, 20]
'icon-battery-fuel-2': [20, 40]
'icon-battery-fuel-3': [40, 60]
'icon-battery-fuel-4': [60, 80]
'icon-battery-fuel-5': [80, 100]
'icon-battery-filled': 100
}
hidden: [email protected]
}
@rfbatteryEventHandler = ( (result) =>
if result.sender is @config.nodeid
unless result.value is null or undefined
# When the battery is to low, battery percentages higher then 100 could be send
if result.value > 100
result.value = 0
@_battery = parseInt(result.value)
@emit "battery", @_battery
)
@rfValueEventHandler = ( (result) =>
if result.sender is @config.nodeid
for sensorid in @config.sensorid
if result.sensor is sensorid
if mySensors.config.debug
env.logger.debug "<- MySensorDHT ", result
if result.type is V_TEMP
#env.logger.debug "temp", result.value
@_temperatue = parseFloat(result.value)
@emit "temperature", @_temperatue
if result.type is V_HUM
#env.logger.debug "humidity", result.value
@_humidity = Math.round(parseFloat(result.value))
@emit "humidity", @_humidity
)
@board.on("rfbattery", @rfbatteryEventHandler)
@board.on("rfValue", @rfValueEventHandler)
super()
destroy: ->
@board.removeListener "rfbattery", @rfbatteryEventHandler
@board.removeListener "rfValue", @rfValueEventHandler
super()
getTemperature: -> Promise.resolve @_temperatue
getHumidity: -> Promise.resolve @_humidity
getBattery: -> Promise.resolve @_battery
class MySensorsDST extends env.devices.TemperatureSensor
constructor: (@config,lastState, @board) ->
@id = @config.id
@name = @config.name
@_temperatue = lastState?.temperature?.value
@_battery = lastState?.battery?.value
if mySensors.config.debug
env.logger.debug "MySensorsDST ", @id, @name
@attributes = {}
@attributes.temperature = {
description: "the measured temperature"
type: "number"
unit: '°C'
acronym: 'T'
}
@attributes.battery = {
description: "Display the battery level of sensor"
type: "number"
displaySparkline: false
unit: "%"
icon:
noText: true
mapping: {
'icon-battery-empty': 0
'icon-battery-fuel-1': [0, 20]
'icon-battery-fuel-2': [20, 40]
'icon-battery-fuel-3': [40, 60]
'icon-battery-fuel-4': [60, 80]
'icon-battery-fuel-5': [80, 100]
'icon-battery-filled': 100
}
hidden: [email protected]
}
@rfbatteryEventHandler = ( (result) =>
if result.sender is @config.nodeid
unless result.value is null or undefined
# When the battery is to low, battery percentages higher then 100 could be send
if result.value > 100
result.value = 0
@_battery = parseInt(result.value)
@emit "battery", @_battery
)
@rfValueEventHandler = ( (result) =>
if result.sender is @config.nodeid and result.type is V_TEMP and result.sensor is @config.sensorid
if mySensors.config.debug
env.logger.debug "<- MySensorDST ", result
@_temperatue = parseFloat(result.value)
@emit "temperature", @_temperatue
)
@board.on("rfbattery", @rfbatteryEventHandler)
@board.on("rfValue", @rfValueEventHandler)
super()
destroy: ->
@board.removeListener "rfbattery", @rfbatteryEventHandler
@board.removeListener "rfValue", @rfValueEventHandler
super()
getTemperature: -> Promise.resolve @_temperatue
getBattery: -> Promise.resolve @_battery
class MySensorsBMP extends env.devices.TemperatureSensor
constructor: (@config,lastState, @board) ->
@id = @config.id
@name = @config.name
@_temperatue = lastState?.temperature?.value
@_pressure = lastState?.pressure?.value
@_forecast = lastState?.forecast?.value
@_battery = lastState?.battery?.value
if mySensors.config.debug
env.logger.debug "MySensorsBMP ", @id, @name
@attributes = {}
@attributes.temperature = {
description: "the measured temperature"
type: "number"
unit: '°C'
acronym: 'T'
}
@attributes.pressure = {
description: "the measured pressure"
type: "number"
unit: 'hPa'
acronym: 'mbar'
}
@attributes.forecast = {
description: "the forecast"
type: "string"
}
@attributes.battery = {
description: "Display the battery level of sensor"
type: "number"
displaySparkline: false
unit: "%"
icon:
noText: true
mapping: {
'icon-battery-empty': 0
'icon-battery-fuel-1': [0, 20]
'icon-battery-fuel-2': [20, 40]
'icon-battery-fuel-3': [40, 60]
'icon-battery-fuel-4': [60, 80]
'icon-battery-fuel-5': [80, 100]
'icon-battery-filled': 100
}
hidden: [email protected]
}
@rfbatteryEventHandler = ( (result) =>
if result.sender is @config.nodeid
unless result.value is null or undefined
# When the battery is to low, battery percentages higher then 100 could be send
if result.value > 100
result.value = 0
@_battery = parseInt(result.value)
@emit "battery", @_battery
)
@rfValueEventHandler = ( (result) =>
if result.sender is @config.nodeid
for sensorid in @config.sensorid
if result.sensor is sensorid
if mySensors.config.debug
env.logger.debug "<- MySensorBMP ", result
if result.type is V_TEMP
#env.logger.debug "temp", result.value
@_temperatue = parseInt(result.value)
@emit "temperature", @_temperatue
if result.type is V_PRESSURE
#env.logger.debug "pressure", result.value
@_pressure = parseInt(result.value)
@emit "pressure", @_pressure
if result.type is V_FORECAST
#env.logger.debug "forecast", result.value
@_forecast = result.value
@emit "forecast", @_forecast
)
@board.on("rfbattery", @rfbatteryEventHandler)
@board.on("rfValue", @rfValueEventHandler)
super()
destroy: ->
@board.removeListener "rfbattery", @rfbatteryEventHandler
@board.removeListener "rfValue", @rfValueEventHandler
super()
getTemperature: -> Promise.resolve @_temperatue
getPressure: -> Promise.resolve @_pressure
getForecast: -> Promise.resolve @_forecast
getBattery: -> Promise.resolve @_battery
class MySensorsPulseMeter extends env.devices.Device
constructor: (@config,lastState, @board) ->
@id = @config.id
@name = @config.name
@voltage = @config.appliedVoltage
@_watt = lastState?.watt?.value
@_ampere = lastState?.ampere?.value
@_kwh = lastState?.kWh?.value
@_pulsecount = lastState?.pulsecount?.value
@_battery = lastState?.battery?.value
if mySensors.config.debug
env.logger.debug "MySensorsPulseMeter ", @id, @name
@attributes = {}
@attributes.watt = {
description: "the measured Wattage"
type: "number"
unit: 'W'
acronym: 'Watt'
}
@attributes.pulsecount = {
description: "Measure the Pulse Count"
type: "number"
#unit: ''
hidden: yes
}
@attributes.kWh = {
description: "the measured kWh"
type: "number"
unit: 'kWh'
acronym: 'kWh'
}
@attributes.battery = {
description: "Display the battery level of sensor"
type: "number"
displaySparkline: false
unit: "%"
icon:
noText: true
mapping: {
'icon-battery-empty': 0
'icon-battery-fuel-1': [0, 20]
'icon-battery-fuel-2': [20, 40]
'icon-battery-fuel-3': [40, 60]
'icon-battery-fuel-4': [60, 80]
'icon-battery-fuel-5': [80, 100]
'icon-battery-filled': 100
}
hidden: [email protected]
}
@attributes.ampere = {
description: "the measured Ampere"
type: "number",
unit: "A"
acronym: 'Ampere'
}
@rfRequestEventHandler = ( (result) =>
if result.sender is @config.nodeid
datas =
{
"destination": @config.nodeid,
"sensor": @config.sensorid,
"type" : V_VAR1,
"value" : @_pulsecount,
"ack" : 1
}
@board._rfWrite(datas)
)
@rfbatteryEventHandler = ( (result) =>
if result.sender is @config.nodeid
unless result.value is null or undefined
# When the battery is to low, battery percentages higher then 100 could be send
if result.value > 100
result.value = 0
@_battery = parseInt(result.value)
@emit "battery", @_battery
)
@rfValueEventHandler = ( (result) =>
if result.sender is @config.nodeid and result.sensor is @config.sensorid
if mySensors.config.debug
env.logger.debug "<- MySensorsPulseMeter", result
if result.type is V_VAR1
if mySensors.config.debug