-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
3812 lines (3585 loc) · 168 KB
/
index.js
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
/*
* (c) 2020-2023 Feilner
*/
var PlatformAccessory, Service, Characteristic, UUIDGen;
var snap7 = require('node-snap7');
// Exports
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
PlatformAccessory = homebridge.platformAccessory;
homebridge.registerPlatform('homebridge-plc', 'PLC', PLC_Platform);
};
function PLC_Platform(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.s7PlatformAccessories = [];
this.S7Client = new snap7.S7Client();
this.isConnectOngoing = false;
this.S7ClientConnect();
}
PLC_Platform.prototype = {
accessories: function(callback) {
var log = this.log;
if ( typeof(this.config.defaultPollInterval) === 'undefined' || this.config.defaultPollInterval === null || this.config.defaultPollInterval < 1) {
this.config.defaultPollInterval = 10;
}
log.info("Add PLC accessories...");
//create accessory for each configuration
this.config.accessories.forEach((config, index) => {
var accessoryNumber = index +1;
var numberOfAccessories = this.config.accessories.length;
var removedOptions = ['minValue', 'maxValue', 'minStep', 'minHumidityValue', 'maxHumidityValue', 'minHumidityStep', 'mapGetCurrent', 'mapGetTarget', 'mapSetTarget', 'invert', 'set_Secured', 'set_Unsecured', 'forceCurrentState', 'set_Deactivate', 'set_Off', 'mapSet', 'mapGet'];
var removedOptionsLockMechanismBool = ['get_LockCurrentState', 'get_LockTargetState', 'set_LockTargetState', 'set_Secured', 'set_Unsecured'];
var hasRemovedOption = false;
removedOptions.forEach((item) => {
if (item in config) {
log.warn("[" + config.name + "] Parameter " + item + " was renamed please update your config");
hasRemovedOption = true;
}
});
if (config.accessory == 'PLC_LockMechanismBool') {
removedOptionsLockMechanismBool.forEach((item) => {
if (item in config) {
log.warn("[" + config.name + "] Parameter " + item + " was renamed please update your config");
hasRemovedOption = true;
}
});
}
if (hasRemovedOption) {
log.error("[" + String(accessoryNumber) + "/" + String(numberOfAccessories) + "] " + config.name + " (" + config.accessory + ") needs update of config and was not added!" );
}
else {
log.info("[" + String(accessoryNumber) + "/" + String(numberOfAccessories) + "] " + config.name + " (" + config.accessory + ")" );
//call accessory construction
var accessory = new GenericPLCAccessory(this, config, accessoryNumber);
this.s7PlatformAccessories.push(accessory);
}
});
callback(this.s7PlatformAccessories);
if (this.config.enablePolling) {
log.info("Enable polling...");
setInterval(function(param) {this.pollLoop( this.s7PlatformAccessories);}.bind(this),1000);
}
if (this.config.enablePush || this.config.enableControl) {
this.port = this.config.port || 8888;
this.api.on('didFinishLaunching', () => {
if (this.config.enablePush && this.config.enableControl) {
this.log.info('Enable push and control server...');
}
else if (this.config.enablePush) {
this.log.info('Enable push server...');
}
else {
this.log.info('Enable control server...');
}
this.listener = require('http').createServer((req, res) => this.httpListener(req, res));
this.listener.listen(this.port);
this.log.info('Listening on port ' + this.port);
});
}
log.info("Init done!");
},
pollLoop: function(s7PlatformAccessories) {
s7PlatformAccessories.forEach((accessory) => {
accessory.poll();
});
},
forwardHTTP: function(logprefix, url) {
require('http').get(url, (resp) => {
if (resp.statusCode !== 200) {
this.log.error(logprefix + " Forward failed with HTTP status: " + resp.statusCode);
return;
}
}).on('error', function(e) {
this.log.error(logprefix + " Forward failed: " + e.message);
}.bind(this));
},
httpListener: function(req, res) {
var data = '';
var url = '';
if (req.method == 'POST') {
req.on('data', (chunk) => {
data += chunk;
});
req.on('end', () => {
this.log.info('Received POST and body data:');
this.log.info(data.toString());
});
}
else if (req.method == 'PUT' || req.method == 'GET') {
req.on('end', () => {
url = require('url').parse(req.url, true); // will parse parameters into query string
if (this.config.enablePush && 'push' in url.query && 'db' in url.query && 'offset' in url.query && 'value' in url.query) {
this.log.debug("[HTTP Push] (" + req.socket.remoteAddress + ") Received update for accessory:" + url.query.db + " offset:" + url.query.offset +" value:" + url.query.value);
var db = parseInt(url.query.db);
var offset = parseFloat(url.query.offset);
var value = url.query.value;
var offsetHandled = false;
var dbHandled = false;
this.s7PlatformAccessories.forEach((accessory) => {
if (accessory.config.db == db) {
dbHandled = true;
offsetHandled = accessory.updatePush(offset, value) || offsetHandled;
}
});
if (typeof(this.config.mirror) != 'undefined' && this.config.mirror) {
this.forwardHTTP("[HTTP Push]", this.config.mirror + req.url);
}
if(!dbHandled) {
if (typeof(this.config.forward) != 'undefined' && this.config.forward) {
this.forwardHTTP("[HTTP Push]", this.config.forward + req.url);
}
else{
this.log.warn("[HTTP Push] (" + req.socket.remoteAddress + ") " + "No accessory configured for db:" + url.query.db + " offset:" + url.query.offset +" value:" + url.query.value);
}
}
else if (!offsetHandled) {
this.log.warn("[HTTP Push] (" + req.socket.remoteAddress + ") " + "Offset not configured for accessory db:" + url.query.db + " offset:" + url.query.offset +" value:" + url.query.value);
}
}
else if (this.config.enableControl && 'control' in url.query && 'db' in url.query && 'offset' in url.query && 'value' in url.query) {
this.log.debug("[HTTP Control] (" + req.socket.remoteAddress + ") Received control request for accessory:" + url.query.db + " offset:" + url.query.offset +" value:" + url.query.value);
var db = parseInt(url.query.db);
var offset = parseFloat(url.query.offset);
var value = url.query.value;
var offsetHandled = false;
var dbHandled = false;
this.s7PlatformAccessories.forEach((accessory) => {
if (accessory.config.db == db) {
dbHandled = true;
offsetHandled = accessory.updateControl(offset, value) || offsetHandled;
}
});
if(!dbHandled) {
if (typeof(this.config.forward) != 'undefined' && this.config.forward) {
this.forwardHTTP("[HTTP Control]", this.config.forward + req.url);
}
else {
this.log.warn("[HTTP Control] (" + req.socket.remoteAddress + ") " + "No accessory configured for db:" + url.query.db + " offset:" + url.query.offset +" value:" + url.query.value);
}
}
else if (!offsetHandled) {
this.log.warn("[HTTP Control] (" + req.socket.remoteAddress + ") " + "Offset not configured for accessory db:" + url.query.db + " offset:" + url.query.offset +" value:" + url.query.value);
}
}
else
{
if (!this.config.enablePush && 'push' in url.query) {
this.log.warn("[HTTP Push] (" + req.socket.remoteAddress + ") enablePush is not set in platform config!");
}
else if (!this.config.enableControl && 'control' in url.query) {
this.log.warn("[HTTP Control] (" + req.socket.remoteAddress + ") enableControl is not set in platform config!");
}
else if (!('push' in url.query) && !('control' in url.query) ) {
this.log.warn("[HTTP Push/Control] (" + req.socket.remoteAddress + ") unknown operation: " + req.url);
}
else if (!('db' in url.query)) {
this.log.warn("[HTTP Push/Control] (" + req.socket.remoteAddress + ") parameter db is missing in url: " + req.url);
}
else if (!('offset' in url.query)) {
this.log.warn("[HTTP Push/Control] (" + req.socket.remoteAddress + ") parameter offset is missing in url: " + req.url);
}
else if (!('value' in url.query)) {
this.log.warn("[HTTP Push/Control] (" + req.socket.remoteAddress + ") parameter value is missing in url: " + req.url);
}
}
});
}
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end();
},
mirrorGet: function(logprefix, parameter) {
if ( 'mirror' in this.config && this.config.mirror) {
var url = this.config.mirror + "/?push" + parameter;
require('http').put(url, (resp) => {
if (resp.statusCode !== 200) {
this.log.error(logprefix, "Mirror failed (" + url + "): HTTP status " + resp.statusCode);
return;
}
}).on('error', function(e) {
this.log.error(logprefix, "Mirror failed (" + url + "): "+ e.message);
}.bind(this));
}
return;
},
//PLC connection check function
S7ClientConnect: function() {
let typeName = ["invalid", "PG-Communication", "OP-Communication"]
var log = this.log;
var S7Client = this.S7Client;
var ip = this.config.ip;
var rack = this.config.rack;
var slot = this.config.slot;
var type = S7Client.CONNTYPE_PG;
var rv = false;
if ( 'communicationOP' in this.config && this.config.communicationOP) {
type = S7Client.CONNTYPE_OP;
}
if (S7Client.Connected()) {
rv = true;
}
else {
log.info("Connecting to %s (%s:%s) %s", ip, rack, slot, typeName[type]);
if (!this.isConnectOngoing) {
this.isConnectOngoing = true;
var ok = S7Client.SetConnectionType(type);
if(ok) {
ok = S7Client.ConnectTo(ip, rack, slot);
this.isConnectOngoing = false;
if(ok) {
log.info("Connected to %s (%s:%s) %s", ip, rack, slot, typeName[type]);
rv = true;
}
else {
log.error("Connection to %s (%s:%s) failed", ip, rack, slot);
}
}
else {
this.isConnectOngoing = false;
log.error("Set connection type to %s (%s:%s) %s failed", ip, rack, slot, typeName[type]);
}
}
}
return rv;
}
};
function GenericPLCAccessory(platform, config, accessoryNumber) {
this.platform = platform;
this.log = platform.log;
this.name = config.name;
var uuid = UUIDGen.generate(config.name + config.accessory);
this.config = config;
this.accessory = new PlatformAccessory(this.name, uuid);
this.modFunctionGet = this.plain;
this.modFunctionSet = this.plain;
if ('enablePolling' in platform.config && platform.config.enablePolling && config.enablePolling) {
this.pollActive = true;
this.pollInterval = config.pollInterval || platform.config.defaultPollInterval;
if (platform.config.distributePolling) {
this.pollCounter = (accessoryNumber % this.pollInterval) + 1;
}
else
{
this.pollCounter = this.pollInterval;
}
this.log.debug("Polling enabled interval " + this.pollInterval + "s. First polling is done in " + this.pollCounter + "s");
}
// INIT handling ///////////////////////////////////////////////
// Lightbulb, Outlet, Switch
////////////////////////////////////////////////////////////////
if (config.accessory == 'PLC_LightBulb' || config.accessory == 'PLC_Outlet' || config.accessory == 'PLC_Switch') {
this.service = new Service.Lightbulb(this.name);
if (config.accessory == 'PLC_LightBulb')
{
this.service = new Service.Lightbulb(this.name);
}
else if (config.accessory == 'PLC_Outlet')
{
this.service = new Service.Outlet(this.name);
}
else
{
this.service = new Service.Switch(this.name);
}
this.accessory.addService(this.service);
this.initOn(true);
if (config.accessory == 'PLC_LightBulb') {
if ('get_Brightness' in config) {
this.service.getCharacteristic(Characteristic.Brightness)
.on('get', function(callback) {this.getByte(callback,
config.db,
config.get_Brightness,
'get Brightness'
);}.bind(this))
.on('set', function(value, callback) {this.setByte(value, callback,
config.db,
config.set_Brightness,
'set Brightness'
);}.bind(this))
.setProps({
minValue: ('minBrightnessValue' in config) ? config.minBrightnessValue : 0,
maxValue: ('maxBrightnessValue' in config) ? config.maxBrightnessValue : 100,
minStep: ('minBrightnessStep' in config) ? config.minBrightnessStep : 1
});
}
}
}
// INIT handling ///////////////////////////////////////////////
// TemperatureSensor
////////////////////////////////////////////////////////////////
else if (config.accessory == 'PLC_TemperatureSensor') {
this.service = new Service.TemperatureSensor(this.name);
this.accessory.addService(this.service);
this.initCurrentTemperature(true);
this.initStatusTampered();
this.initStatusLowBattery();
}
// INIT handling ///////////////////////////////////////////////
// HumiditySensor
////////////////////////////////////////////////////////////////
else if (config.accessory == 'PLC_HumiditySensor') {
this.service = new Service.HumiditySensor(this.name);
this.accessory.addService(this.service);
this.initCurrentRelativeHumidity(true);
this.initStatusTampered();
this.initStatusLowBattery();
}
// INIT handling ///////////////////////////////////////////////
// Thermostat
////////////////////////////////////////////////////////////////
else if (config.accessory == 'PLC_Thermostat'){
this.service = new Service.Thermostat(this.name);
this.accessory.addService(this.service);
informFunction = function(notUsed){
// update target state and current state value.
this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState).getValue(function(err, value) {
if (!err) {
this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState).updateValue(value);
}
}.bind(this));
this.service.getCharacteristic(Characteristic.CurrentHeatingCoolingState).getValue(function(err, value) {
if (!err) {
this.service.getCharacteristic(Characteristic.CurrentHeatingCoolingState).updateValue(value);
}
}.bind(this));
}.bind(this);
if ('mapSetTargetHeatingCoolingState' in config && config.mapSetTargetHeatingCoolingState) {
this.modFunctionSet = function(value){return this.mapFunction(value, config.mapSetTargetHeatingCoolingState);}.bind(this);
}
if ('mapGetTargetHeatingCoolingState' in config && config.mapGetTargetHeatingCoolingState) {
this.modFunctionGet = function(value){return this.mapFunction(value, config.mapGetTargetHeatingCoolingState);}.bind(this);
}
this.modFunctionGetCurrent = this.plain;
if ('mapGetCurrentHeatingCoolingState' in config && config.mapGetCurrentHeatingCoolingState) {
this.modFunctionGetCurrent = function(value){return this.mapFunction(value, config.mapGetCurrentHeatingCoolingState);}.bind(this);
}
if ('get_CurrentHeatingCoolingState' in config) {
this.service.getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.on('get', function(callback) {this.getByte(callback,
config.db,
config.get_CurrentHeatingCoolingState,
'get CurrentHeatingCoolingState',
this.modFunctionGetCurrent
);}.bind(this));
}
else
{
this.service.getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.on('get', function(callback) {this.getDummy(callback,
1, // currently return fixed value inactive=0, idle=1, heating=2, cooling=3
'get CurrentHeatingCoolingState'
);}.bind(this));
}
if ('get_TargetHeatingCoolingState' in config) {
this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('get', function(callback) {this.getByte(callback,
config.db,
config.get_TargetHeatingCoolingState,
'get TargetHeatingCoolingState',
this.modFunctionGet
);}.bind(this))
.on('set', function(value, callback) {this.setByte(value, callback,
config.db,
config.set_TargetHeatingCoolingState,
'set TargetHeatingCoolingState',
informFunction,
this.modFunctionSet
);}.bind(this));
}
else {
this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('get', function(callback) {this.getDummy(callback,
3, // currently return fixed value off=0, heat=1, cool=2, automatic=3
'get TargetHeatingCoolingState'
);}.bind(this))
.on('set', function(value, callback) {this.setDummy(value, callback,
'set TargetHeatingCoolingState',
// ignore set and return current fixed values
informFunction
);}.bind(this));
}
this.service.getCharacteristic(Characteristic.TemperatureDisplayUnits)
.on('get', function(callback) {this.getDummy(callback,
0, // currently return fixed value celsius=0, fahrenheit=1
'get TemperatureDisplayUnits'
);}.bind(this))
.on('set', function(value, callback) {this.setDummy(value, callback,
'set TemperatureDisplayUnits'
);}.bind(this));
this.initCurrentTemperature(true);
if ('get_TargetTemperature' in config && 'set_TargetTemperature' in config) {
this.service.getCharacteristic(Characteristic.TargetTemperature)
.on('get', function(callback) {this.getReal(callback,
config.db,
config.get_TargetTemperature,
'get TargetTemperature'
);}.bind(this))
.on('set', function(value, callback) {this.setReal(value, callback,
config.db,
config.set_TargetTemperature,
'set TargetTemperature'
);}.bind(this))
.setProps({
minValue: ('minTargetTemperatureValue' in config) ? config.minTargetTemperatureValue : 10,
maxValue: ('maxTargetTemperatureValue' in config) ? config.maxTargetTemperatureValue : 38,
minStep: ('minTargetTemperatureStep' in config) ? config.minTargetTemperatureStep : 0.1
});
} else {
this.log.error("Mandatory config get_TargetTemperature or set_TargetTemperature missing")
.on('get', function(callback) {this.getDummy(callback,
20,
'get TargetTemperature'
);}.bind(this))
.on('set', function(value, callback) {this.setDummy(value, callback,
'set TargetTemperature',
// ignore set and return current fixed values
informFunction
);}.bind(this));
}
this.initCurrentRelativeHumidity(false);
if ('get_TargetRelativeHumidity' in config) {
this.service.getCharacteristic(Characteristic.TargetRelativeHumidity)
.on('get', function(callback) {this.getReal(callback,
config.db,
config.get_TargetRelativeHumidity,
'get TargetRelativeHumidity'
);}.bind(this))
.on('set', function(value, callback) {this.setReal(value, callback,
config.db,
config.set_TargetRelativeHumidity,
'set TargetRelativeHumidity'
);}.bind(this))
.setProps({
minValue: ('minTargetHumidityValue' in config) ? config.minTargetHumidityValue : 0,
maxValue: ('maxTargetHumidityValue' in config) ? config.maxTargetHumidityValue : 100,
minStep: ('minTargetHumidityStep' in config) ? config.minTargetHumidityStep : 1
})
;
}
//This will generate a warning but will work anyway.
this.initStatusTampered();
//This will generate a warning but will work anyway.
this.initStatusLowBattery();
}
// INIT handling ///////////////////////////////////////////////
// Humidifier Dehumidifier
////////////////////////////////////////////////////////////////
else if (config.accessory == 'PLC_HumidifierDehumidifier'){
this.service = new Service.HumidifierDehumidifier(this.name);
this.accessory.addService(this.service);
informFunction = function(notUsed){
// update target state and current state value.
this.service.getCharacteristic(Characteristic.TargetHumidifierDehumidifierState).getValue(function(err, value) {
if (!err) {
this.service.getCharacteristic(Characteristic.TargetHumidifierDehumidifierState).updateValue(value);
}
}.bind(this));
this.service.getCharacteristic(Characteristic.CurrentHumidifierDehumidifierState).getValue(function(err, value) {
if (!err) {
this.service.getCharacteristic(Characteristic.CurrentHumidifierDehumidifierState).updateValue(value);
}
}.bind(this));
}.bind(this);
if ('mapSetTargetHumidifierDehumidifierState' in config && config.mapSetTargetHumidifierDehumidifierState) {
this.modFunctionSet = function(value){return this.mapFunction(value, config.mapSetTargetHumidifierDehumidifierState);}.bind(this);
}
if ('mapGetTargetHumidifierDehumidifierState' in config && config.mapGetTargetHumidifierDehumidifierState) {
this.modFunctionGet = function(value){return this.mapFunction(value, config.mapGetTargetHumidifierDehumidifierState);}.bind(this);
}
this.modFunctionGetCurrent = this.plain;
if ('mapGetCurrentHumidifierDehumidifierState' in config && config.mapGetCurrentHumidifierDehumidifierState) {
this.modFunctionGetCurrent = function(value){return this.mapFunction(value, config.mapGetCurrentHumidifierDehumidifierState);}.bind(this);
}
if ('get_CurrentHumidifierDehumidifierState' in config) {
this.service.getCharacteristic(Characteristic.CurrentHumidifierDehumidifierState)
.on('get', function(callback) {this.getByte(callback,
config.db,
config.get_CurrentHumidifierDehumidifierState,
'get CurrentHumidifierDehumidifierState',
this.modFunctionGetCurrent
);}.bind(this));
}
else
{
this.service.getCharacteristic(Characteristic.CurrentHumidifierDehumidifierState)
.on('get', function(callback) {this.getDummy(callback,
1, // currently return fixed value inactive=0, idle=1, humidifying=2, dehumidifying=3
'get CurrentHumidifierDehumidifierState'
);}.bind(this));
}
if ('get_TargetHumidifierDehumidifierState' in config) {
this.service.getCharacteristic(Characteristic.TargetHumidifierDehumidifierState)
.on('get', function(callback) {this.getByte(callback,
config.db,
config.get_TargetHumidifierDehumidifierState,
'get TargetHumidifierDehumidifierState',
this.modFunctionGet
);}.bind(this))
.on('set', function(value, callback) {this.setByte(value, callback,
config.db,
config.set_TargetHumidifierDehumidifierState,
'set TargetHumidifierDehumidifierState',
informFunction,
this.modFunctionSet
);}.bind(this));
}
else {
this.service.getCharacteristic(Characteristic.TargetHumidifierDehumidifierState)
.on('get', function(callback) {this.getDummy(callback,
config.default_TargetHumidifierDehumidifierState || 0, // currently return fixed value auto=0, humidifier=1, dehumidifier=2
'get TargetHeatingCoolingState'
);}.bind(this))
.on('set', function(value, callback) {this.setDummy(value, callback,
'set TargetHeatingCoolingState',
// ignore set and return current fixed values
informFunction
);}.bind(this));
}
if ('get_RelativeHumidityDehumidifierThreshold' in config) {
this.service.getCharacteristic(Characteristic.RelativeHumidityDehumidifierThreshold)
.on('get', function(callback) {this.getReal(callback,
config.db,
config.get_RelativeHumidityDehumidifierThreshold,
'get RelativeHumidityDehumidifierThreshold'
);}.bind(this))
.on('set', function(value, callback) {this.setReal(value, callback,
config.db,
config.set_RelativeHumidityDehumidifierThreshold,
'set RelativeHumidityDehumidifierThreshold'
);}.bind(this));
}
if ('get_RelativeHumidityHumidifierThreshold' in config) {
this.service.getCharacteristic(Characteristic.RelativeHumidityHumidifierThreshold)
.on('get', function(callback) {this.getReal(callback,
config.db,
config.get_RelativeHumidityHumidifierThreshold,
'get RelativeHumidityHumidifierThreshold'
);}.bind(this))
.on('set', function(value, callback) {this.setReal(value, callback,
config.db,
config.set_RelativeHumidityHumidifierThreshold,
'set RelativeHumidityHumidifierThreshold'
);}.bind(this));
}
if ('get_RotationSpeed' in config) {
this.service.getCharacteristic(Characteristic.RotationSpeed)
.on('get', function(callback) {this.getReal(callback,
config.db,
config.get_RotationSpeed,
'get RotationSpeed'
);}.bind(this))
.on('set', function(value, callback) {this.setReal(value, callback,
config.db,
config.set_RotationSpeed,
'set RotationSpeed'
);}.bind(this));
}else if ('get_RotationSpeedByte' in config) {
this.service.getCharacteristic(Characteristic.RotationSpeed)
.on('get', function(callback) {this.getByte(callback,
config.db,
config.get_RotationSpeedByte,
'get RotationSpeed'
);}.bind(this))
.on('set', function(value, callback) {this.setByte(value, callback,
config.db,
config.set_RotationSpeedByte,
'set RotationSpeed'
);}.bind(this));
}
this.initCurrentRelativeHumidity(true);
this.initActive(true);
if ('get_SwingMode' in config) {
this.service.getCharacteristic(Characteristic.SwingMode)
.on('get', function(callback) {this.getByte(callback,
config.db,
config.get_SwingMode,
'get SwingMode'
);}.bind(this))
.on('set', function(value, callback) {this.setByte(value, callback,
config.db,
config.set_SwingMode,
'set SwingMode'
);}.bind(this));
}
if ('get_WaterLevel' in config) {
this.service.getCharacteristic(Characteristic.WaterLevel)
.on('get', function(callback) {this.getReal(callback,
config.db,
config.get_WaterLevel,
'get WaterLevel'
);}.bind(this));
}
//This will generate a warning but will work anyway.
this.initStatusTampered();
//This will generate a warning but will work anyway.
this.initStatusLowBattery();
}
// INIT handling ///////////////////////////////////////////////
// Window, WindowCovering and Door
////////////////////////////////////////////////////////////////
else if (config.accessory == 'PLC_Window' || config.accessory == 'PLC_WindowCovering' || config.accessory == 'PLC_Door'){
if (config.accessory == 'PLC_Window')
{
this.service = new Service.Window(this.name);
}
else if (config.accessory == 'PLC_WindowCovering')
{
this.service = new Service.WindowCovering(this.name);
}
else
{
this.service = new Service.Door(this.name);
}
this.accessory.addService(this.service);
this.lastTargetPos = 0;
this.modFunctionGetCurrent = this.plain;
this.modFunctionGetTarget = this.plain;
this.modFunctionSetTarget = this.plain;
// default do nothing after set of target position
var informFunction = function(value){}.bind(this);
if ('forceCurrentPosition' in config && config.forceCurrentPosition) {
informFunction = function(value){this.service.getCharacteristic(Characteristic.CurrentPosition).updateValue(value);}.bind(this);
}
if ('enablePolling' in platform.config && platform.config.enablePolling) {
if ('adaptivePolling' in config && config.adaptivePolling) {
// high frequency polling during home app tirggerd movement
this.adaptivePollActive = false;
this.adaptivePollingInterval = config.adaptivePollingInterval || 1;
this.pollCounter = this.adaptivePollingInterval;
this.log.debug("Adaptive polling enabled interval " + this.adaptivePollingInterval + "s");
// When execution set save target position and enable polling with high frequency
informFunction = function(value){ this.lastTargetPos = value; this.pollCounter = this.adaptivePollingInterval; this.adaptivePollActive = true; }.bind(this);
}
}
if ('invertPosition' in config && config.invertPosition) {
this.modFunctionGetCurrent = this.invert_0_100;
this.modFunctionGetTarget = this.invert_0_100;
this.modFunctionSetTarget = this.invert_0_100;
}
if ('mapGetCurrentPosition' in config && config.mapGetCurrentPosition) {
this.modFunctionGetCurrent = function(value){return this.mapFunction(value, config.mapGetCurrentPosition);}.bind(this);
}
if ('mapGetTargetPosition' in config && config.mapGetTargetPosition) {
this.modFunctionGetTarget = function(value){return this.mapFunction(value, config.mapGetTargetPosition);}.bind(this);
}
if ('mapSetTargetPosition' in config && config.mapSetTargetPosition) {
this.modFunctionSetTarget = function(value){return this.mapFunction(value, config.mapSetTargetPosition);}.bind(this);
}
// create handlers for required characteristics
this.service.getCharacteristic(Characteristic.CurrentPosition)
.on('get', function(callback) {this.getByte(callback,
config.db,
config.get_CurrentPosition,
'get CurrentPosition',
this.modFunctionGetCurrent
);}.bind(this));
if ('get_TargetPosition' in config) {
// Windows or WindowCover can be electrically moved
this.service.getCharacteristic(Characteristic.TargetPosition)
.on('get', function(callback) {this.getByte(callback,
config.db,
config.get_TargetPosition,
'get TargetPosition',
this.modFunctionGetTarget
);}.bind(this))
.on('set', function(value, callback) {this.setByte(value, callback,
config.db,
config.set_TargetPosition,
'set TargetPosition',
informFunction,
this.modFunctionSetTarget
);}.bind(this));
}
else
{
// Not possible give a target position always use current position as target position.
this.service.getCharacteristic(Characteristic.TargetPosition)
.on('get', function(callback) {this.getByte(callback,
config.db,
config.get_CurrentPosition, // always use current position as target position
'get CurrentPosition',
this.modFunctionGetCurrent
);}.bind(this))
.on('set', function(value, callback) {this.setDummy(value, callback,
'set TargetPosition',
function(value){
// ignore new target value instead get current value and use it target position
this.service.getCharacteristic(Characteristic.CurrentPosition).getValue(function(err, value) {
if (!err) {
this.service.getCharacteristic(Characteristic.TargetPosition).updateValue(value);
}
}.bind(this));
}.bind(this)
);}.bind(this));
}
if ('get_PositionState' in config) {
this.service.getCharacteristic(Characteristic.PositionState)
.on('get', function(callback) {this.getByte(callback,
config.db,
config.get_PositionState,
'get PositionState'
);}.bind(this));
}
else {
this.service.getCharacteristic(Characteristic.PositionState)
.on('get', function(callback) {this.getDummy(callback,
2,
'get PositionState'
);}.bind(this));
}
if ('set_HoldPosition' in config) {
this.service.getCharacteristic(Characteristic.HoldPosition)
.on('set', function(value, callback) { this.setBit(value, callback,
config.db,
Math.floor(config.set_HoldPosition), Math.floor((config.set_HoldPosition*10)%10),
'set HoldPosition'
);}.bind(this));
}
else {
this.service.getCharacteristic(Characteristic.HoldPosition)
.on('set', function(callback) {this.handleDummy(callback,
'set HoldPosition'
);}.bind(this));
}
}
// INIT handling ///////////////////////////////////////////////
// OccupancySensor
////////////////////////////////////////////////////////////////
else if (config.accessory == 'PLC_OccupancySensor'){
this.service = new Service.OccupancySensor(this.name);
this.accessory.addService(this.service);
if ('invertOccupancy' in config && config.invertOccupancy) {
this.modFunctionGet = this.invert_bit;
}
this.service.getCharacteristic(Characteristic.OccupancyDetected)
.on('get', function(callback) {this.getBit(callback,
config.db,
Math.floor(config.get_OccupancyDetected), Math.floor((config.get_OccupancyDetected*10)%10),
"get OccupancyDetected",
this.modFunctionGet
);}.bind(this));
this.initStatusTampered();
this.initStatusLowBattery();
}
// INIT handling ///////////////////////////////////////////////
// MotionSensor
////////////////////////////////////////////////////////////////
else if (config.accessory == 'PLC_MotionSensor'){
this.service = new Service.MotionSensor(this.name);
this.accessory.addService(this.service);
if ('invertMotionDetected' in config && config.invertMotionDetected) {
this.modFunctionGet = this.invert_bit;
}
this.service.getCharacteristic(Characteristic.MotionDetected)
.on('get', function(callback) {this.getBit(callback,
config.db,
Math.floor(config.get_MotionDetected), Math.floor((config.get_MotionDetected*10)%10),
"get MotionDetected",
this.modFunctionGet
);}.bind(this));
this.initStatusTampered();
this.initStatusLowBattery();
}
// INIT handling ///////////////////////////////////////////////
// ContactSensor
////////////////////////////////////////////////////////////////
else if (config.accessory == 'PLC_ContactSensor'){
this.service = new Service.ContactSensor(this.name);
this.accessory.addService(this.service);
if ('invertContactSensorState' in config && config.invertContactSensorState) {
this.modFunctionGet = this.invert_bit;
}
this.service.getCharacteristic(Characteristic.ContactSensorState)
.on('get', function(callback) {this.getBit(callback,
config.db,
Math.floor(config.get_ContactSensorState), Math.floor((config.get_ContactSensorState*10)%10),
"get get_ContactSensorState",
this.modFunctionGet
);}.bind(this));
this.initStatusTampered();
this.initStatusLowBattery();
}
// INIT handling ///////////////////////////////////////////////
// LeakSensor
////////////////////////////////////////////////////////////////
else if (config.accessory == 'PLC_LeakSensor'){
this.service = new Service.LeakSensor(this.name);
this.accessory.addService(this.service);
if ('invertLeakDetected' in config && config.invertLeakDetected) {
this.modFunctionGet = this.invert_bit;
}
this.service.getCharacteristic(Characteristic.LeakDetected)
.on('get', function(callback) {this.getBit(callback,
config.db,
Math.floor(config.get_LeakDetected), Math.floor((config.get_LeakDetected*10)%10),
"get LeakDetected",
this.modFunctionGet
);}.bind(this));
this.initStatusTampered();
this.initStatusLowBattery();
}
// INIT handling ///////////////////////////////////////////////
// Faucet
////////////////////////////////////////////////////////////////
else if (config.accessory == 'PLC_Faucet'){
this.service = new Service.Faucet(this.name);
this.accessory.addService(this.service);
this.initActive(true);
}
// INIT handling ///////////////////////////////////////////////
// Valve
////////////////////////////////////////////////////////////////
else if (config.accessory == 'PLC_Valve'){
this.service = new Service.Valve(this.name);
this.accessory.addService(this.service);
this.initActive(true);
this.service.getCharacteristic(Characteristic.InUse)
.on('get', function(callback) {this.getBit(callback,
config.db,
Math.floor(config.get_Active), Math.floor((config.get_Active*10)%10),
'get InUse'
);}.bind(this));
if ('ValveType' in config) {
this.service.getCharacteristic(Characteristic.ValveType)
.on('get', function(callback) {this.getDummy(callback,
config.ValveType,
'get ValveType'
);}.bind(this));
}
if ('get_RemainingDuration' in config) {
this.service.getCharacteristic(Characteristic.RemainingDuration)
.on('get', function(callback) {this.getDInt(callback,
config.db,
config.get_RemainingDuration,
"get RemainingDuration",
this.s7time2int
);}.bind(this));
this.service.getCharacteristic(Characteristic.SetDuration)
.on('get', function(callback) {this.getDInt(callback,
config.db,
config.get_SetDuration,
"get SetDuration",
this.s7time2int
);}.bind(this))
.on('set', function(value, callback) {this.setDInt(value, callback,
config.db,
config.set_SetDuration,
"set SetDuration",
function(value){this.service.getCharacteristic(Characteristic.RemainingDuration).updateValue(value);}.bind(this),
this.int27time
);}.bind(this));
}
}
// INIT handling ///////////////////////////////////////////////
// SecuritySystem
////////////////////////////////////////////////////////////////
else if (config.accessory == 'PLC_SecuritySystem'){
this.service = new Service.SecuritySystem(this.name);
this.accessory.addService(this.service);
this.modFunctionGetCurrent = this.plain;
informFunction = function(notUsed){
// get the current target system state and update the value.
this.service.getCharacteristic(Characteristic.SecuritySystemTargetState).getValue(function(err, value) {
if (!err) {
this.service.getCharacteristic(Characteristic.SecuritySystemTargetState).updateValue(value);
}
}.bind(this));
// get the current system state and update the value.
this.service.getCharacteristic(Characteristic.SecuritySystemCurrentState).getValue(function(err, value) {
if (!err) {
this.service.getCharacteristic(Characteristic.SecuritySystemCurrentState).updateValue(value);
}
}.bind(this));