-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRiscoAccessories.js
More file actions
executable file
·1612 lines (1479 loc) · 68.6 KB
/
RiscoAccessories.js
File metadata and controls
executable file
·1612 lines (1479 loc) · 68.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
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
'use strict';
var pjson = require('./package.json');
var waitUntil = require('wait-until');
var pollingtoevent = require('polling-to-event');
const JSONreplacer = () => {
const visited = new WeakSet();
return (key, value) => {
if (typeof value === 'object' && value !== null) {
if (visited.has(value)) {
return;
}
visited.add(value);
}
return value;
};
};
class RiscoCPPartitions {
constructor(log, accConfig, api, accessory, TypeOfAcc = 'partition') {
this.log = log;
this.name = accConfig.context.name;
this.RiscoSession = accConfig.RiscoSession;
this.TypeOfAcc = TypeOfAcc;
this.RiscoPartId = accConfig.context.Id;
this.polling = accConfig.polling || false;
this.pollInterval = accConfig.pollInterval || 30000;
this.accessory = accessory;
this.api = api;
this.OccupancyPreventArming = accConfig.OccupancyPreventArming || true;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.mainService = this.accessory.getService(this.Service.SecuritySystem, this.accessory.displayName);
this.mainService
.getCharacteristic(this.Characteristic.SecuritySystemCurrentState)
.on('get', this.getCurrentState.bind(this));
this.mainService
.getCharacteristic(this.Characteristic.SecuritySystemTargetState)
.on('get', this.getTargetState.bind(this))
.on('set', this.setTargetState.bind(this));
if ((this.TypeOfAcc == 'partition') && (this.OccupancyPreventArming)) {
this.OccupancyService = this.accessory.getService(this.Service.OccupancySensor, this.accessory.displayName);
this.OccupancyService
.getCharacteristic(this.Characteristic.OccupancyDetected)
.on('get', this.getCurrentOccupancyState.bind(this));
this.OccupancyState;
}
if (this.TypeOfAcc == 'partition') {
this.long_event_name = `long_part_${this.RiscoPartId}_${(this.name.toLowerCase()).normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/ /g, '_')}`;
} else {
this.long_event_name = `long_group_${this.RiscoPartId}_${((this.name.toLowerCase()).normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/ /g, '_'))}`;
}
// Default Value
this.riscoCurrentState; // Do not set default. Looks like plugin get restarted after some time. Generates false alarms.
this.riscoTargetState = this.riscoCurrentState;
//avoid maxlistener warning
const MaxApiListeners = this.api.getMaxListeners();
const ActualListeners = this.api.listenerCount('shutdown');
this.log.debug('Api Event Shutdown : \nActual Listener :%s for Maximum :%s',this.api.listenerCount('shutdown'),MaxApiListeners);
if (ActualListeners >= MaxApiListeners) {
//give one more for other process
this.api.setMaxListeners(ActualListeners + 2);
this.log.debug('Max Listener Exceeded. Set To :%s', (ActualListeners + 2));
}
this.api.once('shutdown', () => {
this.log.debug('Cleaning Before Exit.\nRemove All Listeners for %s', this.name);
this.mainService
.getCharacteristic(this.Characteristic.SecuritySystemCurrentState)
.removeListener('get', this.getCurrentState);
this.mainService
.getCharacteristic(this.Characteristic.SecuritySystemTargetState)
.removeListener('get', this.getTargetState)
.removeListener('set', this.setTargetState);
if ((this.TypeOfAcc == 'partition') && (this.OccupancyPreventArming)) {
this.OccupancyService
.getCharacteristic(this.Characteristic.OccupancyDetected)
.removeListener('get', this.getCurrentOccupancyState);
}
});
//initialize Security System States
this.getRefreshState();
this.PollingLoop();
}
translateState(aState) {
var self = this;
var translatedSate = 'UNKNOWN';
switch (aState) {
case self.Characteristic.SecuritySystemTargetState.STAY_ARM:
translatedSate = 'STAY_ARM';
break;
case self.Characteristic.SecuritySystemTargetState.NIGHT_ARM:
translatedSate = 'NIGHT_ARM';
break;
case self.Characteristic.SecuritySystemTargetState.AWAY_ARM:
translatedSate = 'AWAY_ARM';
break;
case self.Characteristic.SecuritySystemTargetState.DISARM:
translatedSate = 'DISARM';
break;
case 4:
translatedSate = 'ALARM';
break;
};
return translatedSate;
}
PollingLoop() {
var self = this;
// set up polling if requested
if (self.polling) {
self.log.debug('Starting polling with an interval of %s ms', self.pollInterval);
// 0 - Characteristic.SecuritySystemTargetState.STAY_ARM: => Partial Mode
// 1 - Characteristic.SecuritySystemTargetState.AWAY_ARM: => Full Armed Mode
// 2 - Characteristic.SecuritySystemTargetState.NIGHT_ARM: => Partial Mode
// 3 - Characteristic.SecuritySystemTargetState.DISARM: => Really ?? Disarmed
// 4 - Characteristic.SecuritySystemCurrentState.ALARM: => Alarm
var emitter = new pollingtoevent( (done) => {
self.getRefreshState( (err, result) => {
done(err, result);
});
}, {
longpollEventName: self.long_event_name,
longpolling: true,
interval: 1000
});
emitter.on(self.long_event_name, (state) => {
if (state) {
self.log.info('Partition "%s" => New state detected: (%s) -> %s. Notify!', self.name, state[0], self.translateState(state[0]));
self.mainService.updateCharacteristic(self.Characteristic.SecuritySystemCurrentState, state[0]);
self.mainService.updateCharacteristic(self.Characteristic.SecuritySystemTargetState, state[1]);
self.riscoCurrentState = state[0];
self.riscoTargetState = state[1];
if ((this.TypeOfAcc == 'partition') && (this.OccupancyPreventArming)) {
self.log.info('Partition "%s" is %sOccupied. Notify!', self.name, ((state[2] == 0) ? 'not ' : ''));
self.OccupancyService.updateCharacteristic(self.Characteristic.OccupancyDetected, state[2]);
self.OccupancyState = state[2];
}
}
});
emitter.on('err', (err) => {
self.log.error('Polling failed, error was %s', err);
});
self.api.once('shutdown', () => {
self.log.debug('Remove Polling Listeners for %s', self.name);
emitter.removeAllListeners();
});
}
}
async getTargetState(callback) {
var self = this;
if (self.polling) {
callback(null, self.riscoTargetState);
} else {
self.log.debug('Getting target state...');
self.getState(callback);
}
}
async setTargetState(state, callback) {
var self = this;
self.log.debug('Setting "%s" state to (%s) -> %s', self.name, state, self.translateState(state));
try {
var PartId;
var armedState;
const ArmedValues = {
'disarmed': 1,
'partially': 2,
'armed': 3
};
if (self.TypeOfAcc == 'group') {
if (state != self.riscoTargetState) {
self.log.debug('Actual state: ' + self.riscoCurrentState);
self.log.debug('Futur state: ' + state);
if ((state == 3) && (self.riscoCurrentState != 3)) {
self.log.debug('The system is armed and you want to disarm. Because RiscoCloud can not afford it, it is necessary to disarm the parent(s) partition.');
self.log.debug('All other child groups in this partition will also be disarmed.');
self.log.debug('Parent Part of %s: %s', (self.RiscoPartId || 'null'), JSON.stringify((self.RiscoSession.DiscoveredAccessories.Groups[self.RiscoPartId]).parentPart, JSONreplacer(), 4));
armedState = ArmedValues['disarmed'];
} else {
self.log.debug('Add Cmd: "%sG%s:armed"', self.RiscoPartId);
armedState = ArmedValues['armed'];
}
const ArmResp = await self.RiscoSession.armDisarm(self.RiscoPartId, armedState, true);
if (ArmResp) {
if (!self.polling) {
self.log.info('Group "%s" => Set new state: (%s) -> %s',self.name, state, self.translateState(state));
}
self.riscoCurrentState = state;
self.riscoTargetState = state;
self.mainService.updateCharacteristic(self.Characteristic.SecuritySystemCurrentState, self.riscoCurrentState);
callback !== null && typeof callback === 'function' && callback(null);
return;
} else {
//treat case when parentPart of group is already armed
self.log.debug('Error on armDisarm!!! Maybe a sensor is active and system cannot be armed')
throw new Error('Error on armDisarm!!!');
}
} else {
self.log.debug('Identical state. No change');
callback !== null && typeof callback === 'function' && callback(null);
return;
}
} else {
//if arm, away or night are customized, then pass occupancy verification
//Disable Occupancy Test While best method are develloped
if (!(self.RiscoSession.Custom_Cmd) && (this.OccupancyPreventArming)) {
switch (state) {
case 0:
case 2:
//If partition are not ready to partial arm, then don't change anything
if (!(self.RiscoSession.DiscoveredAccessories.Partitions[self.RiscoPartId].PReady)) {
state = ((self.riscoCurrentState == 4 ) ? self.riscoTargetState : self.riscoCurrentState);
self.mainService.updateCharacteristic(self.Characteristic.SecuritySystemTargetState, self.riscoTargetState);
callback !== null && typeof callback === 'function' && callback(null);
return;
}
break;
case 1:
//If partition are not ready to full arm, then don't change anything
if (!(self.RiscoSession.DiscoveredAccessories.Partitions[self.RiscoPartId].Ready)) {
state = ((self.riscoCurrentState == 4 ) ? self.riscoTargetState : self.riscoCurrentState);
self.mainService.updateCharacteristic(self.Characteristic.SecuritySystemTargetState, self.riscoTargetState);
callback !== null && typeof callback === 'function' && callback(null);
return;
}
break;
}
}
if (state != self.riscoTargetState) {
if (self.RiscoPartId != null ){
PartId = self.RiscoPartId;
} else {
PartId = 0;
}
if ((state != 3) && (self.riscoTargetState != 3)) {
self.log.debug('The system is already armed and you want to change the arming type. It is necessary to disarm the system beforehand.');
await self.setTargetState(3, null);
}
switch (state) {
case 0:
// Stay_Arm = 0
armedState = ArmedValues[self.RiscoSession.DiscoveredAccessories.Partitions[PartId].homeCommand];
break;
case 1:
// Away_Arm = 1
armedState = ArmedValues[self.RiscoSession.DiscoveredAccessories.Partitions[PartId].armCommand];
break;
case 2:
// Night_Arm = 2
armedState = ArmedValues[self.RiscoSession.DiscoveredAccessories.Partitions[PartId].nightCommand];
break;
case 3:
// Disarm = 3
armedState = ArmedValues[self.RiscoSession.DiscoveredAccessories.Partitions[PartId].disarmCommand];
break;
};
const [error, newState] = await self.GetSetArmState(self.RiscoPartId, armedState, state);
if (error !== null) {
throw new Error(error);
} else {
self.riscoCurrentState = newState;
self.riscoTargetState = newState;
self.mainService.updateCharacteristic(self.Characteristic.SecuritySystemCurrentState, self.riscoCurrentState);
callback !== null && typeof callback === 'function' && callback(null);
return;
}
}
callback !== null && typeof callback === 'function' && callback(null);
return;
}
} catch(err) {
self.log.error('Error on RiscoCPPartitions/setTargetState:\n%s', err);
callback !== null && typeof callback === 'function' && callback(err);
return;
}
}
async getCurrentState(callback) {
var self = this;
self.log.debug('Entering on RiscoCPPartitions/getCurrentState function');
try {
if (self.polling) {
callback(null, self.riscoCurrentState);
} else {
self.log.info('Partition "%s" =>Getting current state - delayed...', self.name);
waitUntil()
.interval(500)
.times(15)
.condition(function () {
return (self.riscoCurrentState ? true : false);
})
.done(async function (result) {
await self.RiscoSession.getCPStates();
await self.getRefreshState(callback);
if (self.TypeOfAcc == 'group') {
self.log.info('Partition "%s" => Actual state is: (%s) -> %s', self.name, self.riscoCurrentState, self.translateState(self.riscoCurrentState));
} else {
self.log.info('Group "%s" => Actual state is: (%s) -> %s',self.name, self.riscoCurrentState, self.translateState(self.riscoCurrentState));
}
self.mainService.updateCharacteristic(self.Characteristic.SecuritySystemCurrentState, self.riscoCurrentState);
self.mainService.updateCharacteristic(self.Characteristic.SecuritySystemTargetState, self.riscoTargetState);
return
});
}
} catch (err) {
self.log.error('Error on RiscoCPPartitions/getCurrentState:\n%s', err);
self.mainService.updateCharacteristic(self.Characteristic.SecuritySystemCurrentState, self.riscoCurrentState);
callback(null, self.riscoCurrentState);
return;
}
}
async getCurrentOccupancyState(callback) {
var self = this;
try {
if (self.polling) {
callback(null, self.OccupancyState);
} else {
self.log.info('Partition "%s" =>Getting Occupancy current state - delayed...', self.name);
waitUntil()
.interval(500)
.times(15)
.condition( () => {
return self.OccupancyState;
})
.done(async function (result) {
await self.RiscoSession.getCPStates();
await self.getRefreshState(callback);
self.log.info('Partition "%s" => Actual Occupancy state is: (%s) -> %s', self.name, self.OccupancyState, ((self.OccupancyState == 0) ? 'Not Occupied':'Occupied'));
self.OccupancyService.updateCharacteristic(self.Characteristic.OccupancyDetected, self.OccupancyState);
return;
});
}
} catch (err) {
self.log.error('Error on RiscoCPPartitions/getCurrentOccupancyState:\n%s', err);
self.OccupancyService.updateCharacteristic(self.Characteristic.OccupancyDetected, self.OccupancyState);
callback(null, self.OccupancyState);
return;
}
}
async getState(callback) {
var self = this;
try {
await self.getRefreshState(callback);
} catch (err) {
self.log.error('Error on RiscoCPPartitions/getState:\n%s', err);
callback(null, self.riscoCurrentState);
return;
}
}
async GetSetArmState(partId, armedState, state) {
var self = this;
const [ArmResp, ArmReason] = await self.RiscoSession.armDisarm(partId, armedState);
switch (ArmResp) {
case 0:
self.log.debug('Error on armDisarm!!! Maybe a sensor is active and system cannot be armed');
return [null, self.riscoCurrentState];
break;
case 1:
if (!self.polling) {
self.log.info('Partition "%s" => Set new state: (%s) -> %s',self.name, state, self.translateState(state));
}
self.mainService.updateCharacteristic(self.Characteristic.SecuritySystemCurrentState, state);
return [null, state];
break;
case 2:
const ArmDelay = ArmReason;
self.log.debug('The partition will be armed in %s milliseconds', ArmDelay);
self.log.error('The partition will be armed in %s milliseconds', ArmDelay);
setTimeout(() => {self.RiscoSession.UpdateCPStates}, ArmDelay);
if (!self.polling) {
self.log.debug('Partition "%s" State will be refreshed in %s milliseconds', self.name, ArmDelay);
}
return [null, self.riscoCurrentState];
break;
}
}
async getRefreshState(callback) {
var self = this;
try {
const PGsStatesRegistry = {
armed: 1,
partial: 2,
disarmed: 3,
};
var Datas = [];
var ItemStates;
if (self.TypeOfAcc == 'group') {
for (var Group in self.RiscoSession.DiscoveredAccessories.Groups) {
if (Group != 'type') {
Datas.push(self.RiscoSession.DiscoveredAccessories.Groups[Group]);
}
}
ItemStates = Datas.filter(groups => groups.Id == (self.RiscoPartId | ''));
} else {
for (var Parts in self.RiscoSession.DiscoveredAccessories.Partitions) {
if (Parts != 'type') {
Datas.push(self.RiscoSession.DiscoveredAccessories.Partitions[Parts]);
}
}
ItemStates = Datas.filter(parts => parts.Id == (self.RiscoPartId | ''));
}
if (ItemStates.length != 0) {
self.OccupancyState = ((ItemStates[0].Ready)? 0 : 1 );
self.riscoCurrentState = PGsStatesRegistry[ItemStates[0].actualState];
self.riscoTargetState = self.riscoCurrentState;
if (ItemStates[0].OnAlarm == true) {
self.mainService.updateCharacteristic(self.Characteristic.SecuritySystemCurrentState, 4);
self.riscoCurrentState = 4;
}
self.mainService.updateCharacteristic(self.Characteristic.SecuritySystemCurrentState, self.riscoCurrentState);
self.mainService.updateCharacteristic(self.Characteristic.SecuritySystemTargetState, self.riscoTargetState);
if (this.TypeOfAcc == 'partition') {
if (this.OccupancyPreventArming) {
self.OccupancyService.updateCharacteristic(self.Characteristic.OccupancyDetected, self.OccupancyState);
}
typeof callback === 'function' && callback(null, [self.riscoCurrentState, self.riscoTargetState, self.OccupancyState]);
} else {
typeof callback === 'function' && callback(null, [self.riscoCurrentState, self.riscoTargetState, null]);
}
return;
} else {
throw new Error('Error on RefreshState!!!');
}
} catch (err) {
self.log.error('Error on RiscoCPPartitions/getRefreshState:\n%s', err);
if (this.TypeOfAcc == 'partition') {
typeof callback === 'function' && callback(null, [self.riscoCurrentState, self.riscoTargetState, self.OccupancyState]);
} else {
typeof callback === 'function' && callback(null, [self.riscoCurrentState, self.riscoTargetState, null]);
}
return;
}
}
}
class RiscoCPGroups extends RiscoCPPartitions {
constructor(log, accConfig, api, accessory) {
super(log, accConfig, api, accessory, 'group');
}
}
class RiscoCPOutputs {
constructor(log, accConfig, api, accessory) {
this.log = log;
this.name = accConfig.context.name;
this.RiscoSession = accConfig.RiscoSession;
this.RiscoOutputId = accConfig.context.Id;
this.TypePulse = ( () => {
if (accConfig.context.Type == 'pulse') {
return true;
} else {
return false;
}
})();
this.polling = accConfig.polling || false;
this.pollInterval = accConfig.pollInterval || 30000;
this.accessory = accessory;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.mainService = this.accessory.getService(this.Service.Switch, this.accessory.displayName);
this.mainService
.getCharacteristic(this.Characteristic.On)
.on('get', this.getCurrentState.bind(this))
.on('set', this.setTargetState.bind(this));
// Default Value
this.log.debug('Output "%s" default State: %s', this.name, accConfig.context.State);
this.RiscoOutputState = accConfig.context.State;
this.IsPulsed = false;
this.long_event_name = `long_out_${this.RiscoOutputId}_${(this.name.toLowerCase()).normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/ /g, '_')}`;
//avoid maxlistener warning
const MaxApiListeners = this.api.getMaxListeners();
const ActualListeners = this.api.listenerCount('shutdown');
this.log.debug('Api Event Shutdown : \nActual Listener :%s for Maximum :%s',this.api.listenerCount('shutdown'),MaxApiListeners);
if (ActualListeners >= MaxApiListeners) {
//give one more for other process
this.api.setMaxListeners(ActualListeners + 2);
this.log.debug('Max Listener Exceeded. Set To :%s', (ActualListeners + 2));
}
this.api.once('shutdown', () => {
this.log.debug('Cleaning Before Exit.\nRemove All Listeners for %s', this.name);
this.mainService
.getCharacteristic(this.Characteristic.On)
.removeListener('get', this.getCurrentState)
.removeListener('set', this.setTargetState);
});
this.PollingLoop();
}
PollingLoop() {
var self = this;
if (self.TypePulse !== true) {
// set up polling if requested
if (self.polling) {
var emitter = new pollingtoevent( (done) => {
self.getRefreshState( (err, result) => {
done(err, result);
});
}, {
longpollEventName: self.long_event_name,
longpolling: true,
interval: 1000
});
emitter.on(self.long_event_name, (state) => {
self.log.info('Output "%s" => New state detected: (%s). Notify!', self.name, state);
self.RiscoOutputState = state;
self.mainService.updateCharacteristic(self.Characteristic.On, self.RiscoOutputState);
});
emitter.on('err', (err) => {
self.log.error('Polling failed, error was %s', err);
});
self.api.once('shutdown', () => {
self.log.debug('Remove Polling Listeners for %s', self.name);
emitter.removeAllListeners();
});
}
}
}
async getRefreshState(callback) {
var self = this;
try {
var Datas = [];
var ItemStates;
for (var Output in self.RiscoSession.DiscoveredAccessories.Outputs) {
Datas.push(self.RiscoSession.DiscoveredAccessories.Outputs[Output]);
}
ItemStates = Datas.filter(outputs => (outputs.Id == (self.RiscoOutputId | '')));
if (ItemStates.length != 0) {
self.RiscoOutputState = ((ItemStates[0].State == 0) ? false : true);
self.mainService.updateCharacteristic(self.Characteristic.On, self.RiscoOutputState);
callback(null, self.RiscoOutputState);
return;
} else {
throw new Error('Error on Output RefreshState!!!');
}
} catch (err) {
self.log.error('Error on RiscoCPOutputs/getRefreshState:\n%s', err);
callback(null, self.RiscoOutputState);
return;
}
}
async getCurrentState(callback) {
var self = this;
try {
if (self.polling) {
self.log.debug('Output is %s', self.RiscoOutputState);
if (self.TypePulse === false) {
callback(null, self.RiscoOutputState);
return;
} else {
callback(null, false);
return;
}
} else {
if (self.TypePulse === false) {
self.log.info('Output "%s" => Getting current state - delayed...', self.name);
waitUntil()
.interval(500)
.times(15)
.condition(function () {
return (self.RiscoOutputState ? true : false);
})
.done(async function (result) {
await self.RiscoSession.getCPStates();
await self.getRefreshState(callback);
self.log.debug('Output "%s" => Actual state is: (%s)', self.name, self.RiscoOutputState);
self.mainService.updateCharacteristic(self.Characteristic.On, self.RiscoOutputState);
return;
});
} else {
callback(null, false);
}
}
} catch (err) {
self.log.error('Error on RiscoCPOutputs/getCurrentState:\n%s', err);
self.mainService.updateCharacteristic(self.Characteristic.On, self.RiscoOutputState);
callback(null, self.RiscoOutputState);
return;
}
}
async setTargetState(state, callback) {
var self = this;
self.log.debug('Set Output to: ' + state);
self.RiscoOutputState = state;
const deviceType = ((self.TypePulse) ? 2 : 1 );
const lastCommand = ((self.TypePulse) ? 1 : ((state) ? 1 : 0 ));
const deviceId = self.RiscoOutputId;
const HACResp = await self.RiscoSession.OutputCommand(deviceType, lastCommand, deviceId);
if (HACResp){
if (!self.polling) {
self.log.info('Output "%s" => Set new state: (%s)', self.name, state);
}
if (self.TypePulse === false) {
self.log.debug('Not a pulse switch, update it')
self.RiscoOutputState = state;
typeof callback === 'function' && callback(null);
} else {
if (self.IsPulsed) {
self.log.debug('Pulse switch is already pulsed');
self.IsPulsed = false;
typeof callback === 'function' && callback(null);
} else {
self.log.debug('Pulse switch is not already pulsed');
self.IsPulsed = true;
setTimeout(self.ResetPulseSwitchState, 500, self);
self.RiscoOutputState = false;
typeof callback === 'function' && callback(null);
}
}
} else {
self.log.error('Error on HACommand!!!');
typeof callback === 'function' && callback(null);
}
}
async ResetPulseSwitchState(self) {
var self = self;
self.log.debug('Reset Pulse Switch State to %s', self.RiscoOutputState);
self.mainService.updateCharacteristic(self.Characteristic.On, self.RiscoOutputState);
}
}
class RiscoCPBaseDetectors {
constructor(log, accConfig, api, accessory) {
this.log = log;
this.name = accConfig.context.name;
this.RiscoSession = accConfig.RiscoSession;
this.Type = accConfig.context.accessorytype;
this.RiscoDetectorId = accConfig.context.Id;
this.polling = accConfig.polling || false;
this.pollInterval = accConfig.pollInterval || 30000;
this.accessory = accessory;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.secondCharac = undefined;
this.SetServicesAccessory();
this.SetExcludeServicesAccessory();
this.DefineAccessoryVariable();
this.DetectorReady = false;
// Default Value
this.log.debug('%s "%s" default State: %s',this.sPrefix, this.name, accConfig.context.State);
this.RiscoDetectorState = accConfig.context.State;
this.RiscoDetectorBypassState = accConfig.context.Bypassed;
this.long_event_name = `long_det_${this.RiscoDetectorId}_${(this.name.toLowerCase()).normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/ /g, '_')}`;
//avoid maxlistener warning
const MaxApiListeners = this.api.getMaxListeners();
const ActualListeners = this.api.listenerCount('shutdown');
this.log.debug('Api Event Shutdown : \nActual Listener :%s for Maximum :%s',this.api.listenerCount('shutdown'),MaxApiListeners);
if (ActualListeners >= MaxApiListeners) {
//give one more for other process
this.api.setMaxListeners(ActualListeners + 2);
this.log.debug('Max Listener Exceeded. Set To :%s', (ActualListeners + 2));
}
this.api.once('shutdown', () => {
this.log.debug('Cleaning Before Exit.\nRemove All Listeners for %s', this.name);
this.removemainListeners();
this.removeExcludeListeners();
});
this.PollingLoop();
}
PollingLoop() {
var self = this;
// set up polling if requested
if (self.polling) {
var emitter = new pollingtoevent( (done) => {
self.getRefreshState( (err, result) => {
done(err, result);
});
}, {
longpollEventName: self.long_event_name,
longpolling: true,
interval: 1000
});
emitter.on(self.long_event_name, (state) => {
self.log.info('%s "%s" => New state detected: (%s). Notify!', self.sPrefix, self.name, self.GetAccessoryState(state[0], false));
self.log.info('%s "%s" => New Bypass State detected : (%s). Notify!', self.sPrefix, self.name, ((state[1]) ? 'ByPassed' : 'Not ByPassed'));
self.ReportAccessoryState(state);
});
emitter.on('err', (err) => {
self.log.error('Polling failed, error was %s', err);
});
self.api.once('shutdown', () => {
self.log.debug('Remove All Listeners for %s', self.name);
emitter.removeAllListeners();
});
}
}
SetExcludeServicesAccessory() {
var self = this;
self.log.debug('Adding Exclude Switch to %s', this.name);
this.ExcludeService = this.accessory.getService(this.Service.Switch, this.accessory.displayName);
this.ExcludeService
.getCharacteristic(this.Characteristic.On)
.on('get', this.getCurrentExcludeState.bind(this))
.on('set', this.setCurrentExcludeState.bind(this));
}
removeExcludeListeners() {
this.ExcludeService
.getCharacteristic(this.Characteristic.On)
.removeListener('get', this.getCurrentExcludeState)
.removeListener('set', this.setCurrentExcludeState);
}
GetAccessoryState(state, AsHomeKitValue = true) {
/*
Adapt the status of the accessory according to the response expected by Homekit according to the type of accessory
*/
var self = this;
if (AsHomeKitValue) {
return ((state) ? self.ActiveValue : self.InactiveValue);
} else {
return ((state) ? self.ActiveStr : self.InactiveStr);
}
}
ReportAccessoryState(state = null) {
var self = this;
if (state != null) {
self.RiscoDetectorState = state[0];
self.RiscoDetectorBypassState = state[1];
}
try {
self.mainService.updateCharacteristic(self.mainCharac, self.GetAccessoryState(self.RiscoDetectorState));
if (self.secondCharac !== undefined) {
self.mainService.updateCharacteristic(self.secondCharac, self.GetAccessoryState(self.RiscoDetectorState));
}
self.ExcludeService.updateCharacteristic(self.Characteristic.On, self.RiscoDetectorBypassState);
return;
} catch (err) {
self.log.error('Error on RiscoBaseDetectors/ReportAccessoryState:\n%s', err);
return;
}
}
async getRefreshState(callback) {
var self = this;
try {
var Datas = [];
var ItemStates;
for (var Detector in self.RiscoSession.DiscoveredAccessories.Detectors) {
Datas.push(self.RiscoSession.DiscoveredAccessories.Detectors[Detector]);
}
ItemStates = Datas.filter(detectors => (detectors.Id == (self.RiscoDetectorId | '')));
if (ItemStates.length != 0) {
self.RiscoDetectorState = ItemStates[0].State;
self.RiscoDetectorBypassState = ItemStates[0].Bypassed;
self.DetectorReady = true;
self.ReportAccessoryState();
callback(null, [self.RiscoDetectorState, self.RiscoDetectorBypassState]);
return;
} else {
throw new Error(`Error on ${self.sPrefix} RefreshState!!!`);
}
} catch (err) {
self.log.error('Error on RiscoCPBaseDetectors/getRefreshState:\n%s', err);
self.DetectorReady = true;
callback(null, [self.RiscoDetectorState, self.RiscoDetectorBypassState]);
return;
}
}
async getCurrentState(callback) {
var self = this;
try {
if (self.polling) {
self.log.debug('%s "%s" MotionDetected: %s', self.sPrefix, self.name, self.GetAccessoryState(self.RiscoDetectorState, false));
callback(null, self.RiscoDetectorState);
return;
} else {
self.log.info('%s "%s" => Getting current state - delayed...', self.sPrefix, self.name);
waitUntil()
.interval(500)
.times(15)
.condition( () => {
return (self.RiscoDetectorState ? true : false);
})
.done(async function (result) {
await self.RiscoSession.getCPStates();
await self.getRefreshState(callback);
self.log.debug('%s "%s" => Actual Motion state is: (%s)', self.sPrefix, self.name, self.GetAccessoryState(self.RiscoDetectorState, false));
self.DetectorReady = true;
self.ReportAccessoryState();
return
});
}
} catch (err) {
self.log.error('Error on RiscoCPBaseDetectors/getCurrentState:\n%s', err);
self.ReportAccessoryState();
callback(null, self.RiscoDetectorState);
return;
}
}
async getCurrentExcludeState(callback) {
var self = this;
try {
if (self.polling) {
self.log.debug('%s "%s" Exclude State : (%s) => %s', self.sPrefix, self.name, self.RiscoDetectorBypassState, ((self.RiscoDetectorBypassState) ? 'Bypassed': 'Not Bypassed'));
callback(null, (self.RiscoDetectorBypassState)?false : true);
return;
} else {
self.log.info('%s "%s" => Getting current state - delayed...', self.sPrefix, self.name);
waitUntil()
.interval(500)
.times(15)
.condition( () => {
return self.RiscoDetectorBypassState;
})
.done(async function (result) {
await self.RiscoSession.getCPStates();
await self.getRefreshState(callback);
self.log.debug('%s "%s" => Actual Exclude State is: %s', self.sPrefix, self.name, ((self.RiscoDetectorBypassState) ? 'Bypassed': 'Not Bypassed'));
self.DetectorReady = true;
return;
});
}
} catch (err) {
self.log.error('Error on RiscoCPBaseDetectors/getCurrentExcludeState:\n%s', err);
callback(err, (self.RiscoDetectorBypassState)?false : true);
return;
}
}
async setCurrentExcludeState(state, callback) {
var self = this;
try {
if (self.DetectorReady) {
const PartId = self.RiscoSession.DiscoveredAccessories.Detectors[self.RiscoDetectorId].Partition;
const PartStatus = self.RiscoSession.DiscoveredAccessories.Partitions[PartId].actualState
var SBpResp;
if (PartStatus != 'disarmed') {
self.log.info('Cannot Modify Exclude State of Sensor from Armed Partition');
typeof callback === 'function' && callback('Cannot Modify Exclude State of Sensor from Armed Partition', 'Cannot Modify Exclude State of Sensor from Armed Partition');
self.ExcludeService.updateCharacteristic(self.Characteristic.On, self.RiscoDetectorBypassState);
return;
}
self.log.debug('Set Exclude State of %s "%s" to: %s', self.sPrefix, self.name, ((state) ? 'Bypassed': 'Not Bypassed'));
self.log.debug('%s Actual State: %s', self.name, ((self.RiscoDetectorBypassState) ? 'Bypassed': 'Not Bypassed'));
self.log.debug('%s New State: %s', self.name, ((state) ? 'Bypassed': 'Not Bypassed'));
if (self.RiscoDetectorBypassState == state) {
SBpResp = true;
self.log.debug('%s Identical State', self.name);
} else {
SBpResp = await self.RiscoSession.SetBypass(((state) ? 2 : 3), self.RiscoDetectorId);
self.log.debug('%s Different State', self.name);
}
if (SBpResp) {
if (!self.polling) {
self.log.info('%s "%s" => Set new Bypass state: (%s)', self.sPrefix, self.name, state);
}
typeof callback === 'function' && callback(null);
} else {
self.log.error('Error on SetBypass!!!');
typeof callback === 'function' && callback('Error on SetBypass!!!');
}
return;
}
} catch (err) {
self.log.error('Error on RiscoCPBaseDetectors/setCurrentExcludeState:\n%s', err);
self.ExcludeService.updateCharacteristic(self.Characteristic.On, self.RiscoDetectorBypassState);
callback(err);
return;
}
}
}
class RiscoCPDetectors extends RiscoCPBaseDetectors {
constructor(log, accConfig, api, accessory) {
super(log, accConfig, api, accessory);
}
SetServicesAccessory() {
this.mainService = this.accessory.getService(this.Service.MotionSensor, this.accessory.displayName);
this.mainService
.getCharacteristic(this.Characteristic.MotionDetected)
.on('get', this.getCurrentState.bind(this));
this.sPrefix = 'Detector';
}
DefineAccessoryVariable() {
this.mainCharac = this.Characteristic.MotionDetected;
this.ActiveValue = true;
this.InactiveValue = false;
this.ActiveStr = 'Active';
this.InactiveStr = 'Inactive';
}
removemainListeners() {
this.mainService
.getCharacteristic(this.Characteristic.MotionDetected)
.removeListener('get', this.getCurrentState);
}
}
class RiscoCPCDoor extends RiscoCPBaseDetectors {
constructor (log, accConfig, api, accessory) {
super(log, accConfig, api, accessory);
}
SetServicesAccessory() {
this.mainService = this.accessory.getService(this.Service.Door, this.accessory.displayName);
this.mainService
.getCharacteristic(this.Characteristic.CurrentPosition)
.on('get', this.getCurrentState.bind(this));
this.sPrefix = 'Door Contact';
}
DefineAccessoryVariable() {
this.mainCharac = this.Characteristic.CurrentPosition;
this.secondCharac = this.Characteristic.TargetPosition;
this.ActiveValue = 100;
this.InactiveValue = 0;
this.ActiveStr = 'open';
this.InactiveStr = 'closed';
}
removemainListeners() {
this.mainService
.getCharacteristic(this.Characteristic.CurrentPosition)
.removeListener('get', this.getCurrentState);
}
}
class RiscoCPCWindow extends RiscoCPBaseDetectors {
constructor(log, accConfig, api, accessory) {
super(log, accConfig, api, accessory);
}
SetServicesAccessory() {
this.mainService = this.accessory.getService(this.Service.Window, this.accessory.displayName);
this.mainService
.getCharacteristic(this.Characteristic.CurrentPosition)
.on('get', this.getCurrentState.bind(this));
this.sPrefix = 'Window Contact';
}
DefineAccessoryVariable() {
this.mainCharac = this.Characteristic.CurrentPosition;
this.secondCharac = this.Characteristic.TargetPosition;
this.ActiveValue = 100;
this.InactiveValue = 0;
this.ActiveStr = 'open';
this.InactiveStr = 'closed';
}
removemainListeners() {