-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
646 lines (583 loc) · 36.1 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
const packageJson = require('./package.json');
var elink = require('./envisalink.js');
const partitionDevice = require('./accessories/partitionAccessory');
const zoneDevices = require('./accessories/zoneAccessory');
const customDevices = require('./accessories/customAccessory');
var Service, Characteristic, UUIDGen;
var alarm;
// Envisakit HomeBridge Plugin
const PLUGIN_NAME = 'homebridge-envisalink-ademco';
const PLATFORM_NAME = 'Envisalink-Ademco';
class EnvisalinkPlatform {
constructor(log, config, api) {
this.log = log;
this.api = api;
this.config = config;
this.accessories = [];
// Must define configuration file and IP address for Envisakit server
if (!config || !config.host) {
this.log.error("No configuration or host address defined for plug-in. Please configure the Envisakit Ademco plug-in.");
// terminate plug-in initialization
return;
} else {
// Read configuration file and set default if needed.
this.deviceType = config.deviceType ? config.deviceType : "10P";
this.deviceDescription = "Honeywell VISTA-" + this.deviceType;
this.partitions = config.partitions ? config.partitions : [{
name: 'House'
}];
this.masterPin = this.config.pin ? this.config.pin : "1234";
this.bypass = config.bypass ? config.bypass : [];
this.speedKeys = config.speedKeys ? config.speedKeys : [];
this.zones = config.zones ? config.zones : [];
this.platformPartitionAccessories = [];
this.platformZoneAccessories = [];
this.platformZoneAccessoryMap = {};
this.platformPartitionAccessoryMap = {};
this.activeAccessoryMap = {};
this.chime = config.chimeToggle ? config.chimeToggle: false;
this.batteryRunTime = config.batteryRunTime ? config.batteryRunTime: 0;
this.commandTimeOut = Math.min(30,Math.max(1,config.commandTimeOut));
// Should partition be changed when executing command?
// Option only valid if this is a multiple partitions system
this.changePartition = config.changePartition ? config.changePartition: false;
// Allow alarm to be enable to be set with fire trouble
this.ignoreFireTrouble = config.ignoreFireTrouble ? config.ignoreFireTrouble: false;
// Allow alarm to be enable with system trouble
this.ignoreSystemTrouble = config.ignoreSystemTrouble ? config.ignoreSystemTrouble: false;
// are we in maintenance mode?
this.isMaintenanceMode = config.maintenanceMode ? config.maintenanceMode: false;
// suppress envisalink failure?
this.isEnvisalinkFailureSuppress = config.envisalinkFailureSuppress ? config.envisalinkFailureSuppress: false;
// When this event is fired it means Homebridge has restored all cached accessories from disk.
// Dynamic Platform plugins should only register new accessories after this event was fired,
// in order to ensure they weren't added to homebridge already. This event can also be used
// to start discovery of new accessories.
api.on('didFinishLaunching', () => {
// Create connection object
alarm = new elink(log, config);
// Build device list
this.log("Configuring", this.deviceDescription, "for Homekit...");
this.refreshPartitionsAccessories();
this.refreshZoneAccessories();
this.refreshCustomAccessories();
this.removeOrphanAccessory();
// Provide status on configurations completed
this.log(`Partition configured: ${this.partitions.length}`);
if (this.zones.length > 0) this.log(`Zone accessories configured: ${this.zones.length}`);
if (this.bypass.length > 0){
if (this.bypass[0].enabledbyPass) this.log("Bypass accessories configured.");
}
if (this.speedKeys.length > 0) this.log("Speed keys accessories configured.");
if (this.chime) this.log("Chime toggle accessory configured.")
// Begin connection process and bind alarm events to local function.
// Should plug-in run in a disconnect mode. Allow maintenance without resulting in a lot of log errors
if (this.isMaintenanceMode == false){
// Start connection to Envisalink module
alarm.startSession();
// Bind event to local functions
alarm.on('keypadupdate', this.systemUpdate.bind(this));
alarm.on('zoneevent', this.zoneUpdate.bind(this));
alarm.on('updatepartition', this.partitionUpdate.bind(this));
alarm.on('cidupdate', this.cidUpdate.bind(this));
// Should module errors be suppress from homekit notification?
if (this.isEnvisalinkFailureSuppress == false) alarm.on('envisalinkupdate', this.envisalinkUpdate.bind(this));
else this.log.warn("No alarm Tamper will be generated for Envisalink communication failure. Please refer to your Homebridge logs for communication failures.");
// Generate bypassed zone event to update the bypass accessories
setTimeout(function () {alarm.getBypassedZones(this.masterPin)}.bind(this),config.heartbeatInterval*1000);
}
else
this.log.warn("This plug-in is running in maintenance mode. All updates and operations are disabled!");
});
}
}
// ****************************************************
// * Homekit Accessories from envisalink module *
// ****************************************************
// Create associates in Homekit based on configuration file
refreshPartitionsAccessories() {
// Process partition data
for (var i = 0; i < this.partitions.length; i++) {
var partition = this.partitions[i];
var partitionNumber = Number(partition.partitionNumber ? partition.partitionNumber : (i+1));
partition.pin = partition.partitionPin ? partition.partitionPin: this.masterPin;
if(isNaN(partition.pin)) {
this.log.error("Ademco Pin must be a number. Please update configuration for the Envisakit Ademco plug-in.");
// terminate plug-in initialization
return;
}
if(partition.pin.length != 4) {
this.log.warn("Ademco PIN are normally length of 4 digits. The provided PIN length may result in unusual behavior.");
}
partition.model = this.deviceDescription + " Keypad";
partition.deviceType = this.deviceType;
// set command timeout
partition.commandTimeOut = this.commandTimeOut;
partition.batteryRunTime = this.batteryRunTime * 60 * 60;
partition.changePartition = this.changePartition;
partition.serialNumber = "envisalink.partition." + partitionNumber;
partition.partitionNumber = partitionNumber;
// Allow alarm to be enable to be set with fire trouble
partition.ignoreFireTrouble = this.ignoreFireTrouble;
// Allow alarm to be enable with system trouble
partition.ignoreSystemTrouble = this.ignoreSystemTrouble;
var partitionAccessory = new partitionDevice(this.log, partition, Service, Characteristic, UUIDGen, alarm);
// check the accessory was not restored from cache
var foundAccessory = this.accessories.find(accessory => accessory.UUID === partitionAccessory.uuid)
if (!foundAccessory) {
// create a new accessory
let newAccessory = new this.api.platformAccessory(partitionAccessory.name, partitionAccessory.uuid);
// add services and Characteristic
partitionAccessory.setAccessory(newAccessory);
// register the accessory
this.addAccessory(partitionAccessory);
}
else { // accessory already exist just set characteristic
partitionAccessory.setAccessory(foundAccessory);
}
// Add to active accessory list, which is later used to remove unused cache entries
this.activeAccessoryMap[partitionAccessory.uuid] = true;
var partitionIndex = this.platformPartitionAccessories.push(partitionAccessory) - 1;
this.platformPartitionAccessoryMap['p.' + partitionNumber] = partitionIndex;
this.log.debug("refreshAccessories: Partition number - ", partitionNumber , " configured.");
}
}
// Create associates zone in Homekit based on configuration file
refreshZoneAccessories() {
//process zone data
var maxZone = this.zones.length;
for (var i = 0; i < this.zones.length; i++) {
var zone = this.zones[i];
if ((zone.sensorType == "motion" || zone.sensorType == "glass" || zone.sensorType == "window" || zone.sensorType == "door" || zone.sensorType == "leak" || zone.sensorType == "smoke" || zone.sensorType == "co") && (zone.name != undefined)){
var zoneNum = Number(zone.zoneNumber ? zone.zoneNumber : (i+1));
if (zoneNum > maxZone) {
maxZone = zoneNum;
}
zone.model = this.deviceDescription + " " + zone.sensorType.charAt(0).toUpperCase() + zone.sensorType.slice(1) + " sensor";
zone.serialNumber = "envisalink." + zone.sensorType + "."+ zone.partition + "." + zoneNum;
if (this.bypass.length > 0)
zone.masterBypass = this.bypass[0].enabledbyPass;
else
zone.masterBypass = false;
zone.pin = this.masterPin;
zone.commandTimeOut = this.commandTimeOut;
var zoneAccessory = new zoneDevices(this.log, zone, Service, Characteristic, UUIDGen, alarm);
// check the accessory was not restored from cache
var foundAccessory = this.accessories.find(accessory => accessory.UUID === zoneAccessory.uuid)
if (!foundAccessory) {
// create a new accessory
let newAccessory = new this.api.platformAccessory(zoneAccessory.name, zoneAccessory.uuid);
// add services and Characteristic
zoneAccessory.setAccessory(newAccessory);
// register the accessory
this.addAccessory(zoneAccessory);
}
else { // accessory already exist just set characteristic
zoneAccessory.setAccessory(foundAccessory);
}
// Add to active accessory list, which is later used to remove unused cache entries
this.activeAccessoryMap[zoneAccessory.uuid] = true;
var accessoryIndex = this.platformZoneAccessories.push(zoneAccessory) - 1;
this.platformZoneAccessoryMap['z.' + zoneNum] = accessoryIndex;
this.log.debug("refreshAccessories: Zone number - ", zoneNum , " configured.");
} else
this.log.error("Misconfigured zone definition " + zone.name + ". Entry - " + i + " ignoring.");
}
}
// Create associates custom in Homekit based on configuration file
refreshCustomAccessories() {
// Process toggle chime switch functionality
if (this.chime ) {
var chimeswitch = {};
chimeswitch.pin = this.masterPin;
chimeswitch.model = this.deviceDescription + " Keypad";
chimeswitch.name = "Chime";
chimeswitch.customType = "chimemode";
chimeswitch.serialNumber = "envisalink.chime.all";
chimeswitch.commandTimeOut = this.commandTimeOut;
// Create Chime Toggle button
var customAccessory = new customDevices(this.log, chimeswitch, Service, Characteristic, UUIDGen, alarm);
// check the accessory was not restored from cache
var foundAccessory = this.accessories.find(accessory => accessory.UUID === customAccessory.uuid)
if (!foundAccessory) {
// create a new accessory
let newAccessory = new this.api.platformAccessory(customAccessory.name, customAccessory.uuid);
// add services and Characteristic
customAccessory.setAccessory(newAccessory);
// register the accessory
this.addAccessory(customAccessory);
}
else {// accessory already exist just set characteristic
customAccessory.setAccessory(foundAccessory);
}
// Add to active accessory list, which is later used to remove unused cache entries
this.activeAccessoryMap[customAccessory.uuid] = true;
var accessoryIndex = this.platformPartitionAccessories.push(customAccessory) - 1;
this.platformPartitionAccessoryMap['c.chimemode'] = accessoryIndex;
}
// Process bypass features (only one bypass button is created)
if (this.bypass.length > 0) {
if (this.bypass[0].enabledbyPass) {
var bypassswitch = this.bypass[0];
bypassswitch.pin = this.masterPin;
bypassswitch.model = this.deviceDescription+ " Keypad";
bypassswitch.name = "Zone Bypass";
bypassswitch.customType = "bypass";
bypassswitch.serialNumber = "envisalink.bypass.all";
bypassswitch.commandTimeOut = this.commandTimeOut;
bypassswitch.zoneDevices = this.platformZoneAccessories;
// Create bypass switch
var customAccessory = new customDevices(this.log, bypassswitch, Service, Characteristic, UUIDGen, alarm);
// check the accessory was not restored from cache
var foundAccessory = this.accessories.find(accessory => accessory.UUID === customAccessory.uuid)
if (!foundAccessory) {
// create a new accessory
let newAccessory = new this.api.platformAccessory(customAccessory.name, customAccessory.uuid);
// add services and Characteristic
customAccessory.setAccessory(newAccessory);
// register the accessory
this.addAccessory(customAccessory);
}
else { // accessory already exist just set characteristic
customAccessory.setAccessory(foundAccessory);
}
// Add to active accessory list, which is later used to remove unused cache entries
this.activeAccessoryMap[customAccessory.uuid] = true;
var accessoryIndex = this.platformPartitionAccessories.push(customAccessory) - 1;
this.platformPartitionAccessoryMap['c.bypass'] = accessoryIndex;
}
}
// Creating macro/speed keys
if (this.speedKeys.length > 0) {
var speedkey = [];
speedkey.pin = this.masterPin;
speedkey.model = this.deviceDescription + " Keypad";
speedkey.customType = "speedkeys";
speedkey.serialNumber = "envisalink.speedKey.all";
speedkey.name = "Speed Key";
speedkey.keyList = this.speedKeys;
var customAccessory = new customDevices(this.log, speedkey, Service, Characteristic, UUIDGen, alarm);
// check the accessory was not restored from cache
var foundAccessory = this.accessories.find(accessory => accessory.UUID === customAccessory.uuid)
if (!foundAccessory) {
// create a new accessory
let newAccessory = new this.api.platformAccessory(customAccessory.name, customAccessory.uuid);
// add services and Characteristic
customAccessory.setAccessory(newAccessory);
// register the accessory
this.addAccessory(customAccessory);
}
else { // accessory already exist just set characteristic
customAccessory.setAccessory(foundAccessory);
}
// Add to active accessory list, which is later used to remove unused cache entries
this.activeAccessoryMap[customAccessory.uuid] = true;
var accessoryIndex = this.platformPartitionAccessories.push(customAccessory) - 1;
this.platformPartitionAccessoryMap['c.speedkey'] = accessoryIndex;
}
}
// ****************************************************
// * Event Processor from envisalink module *
// ****************************************************
// The envisalink event represent issue related to low level layers which effect all partitions.
envisalinkUpdate(data) {
this.log.debug('envisalinkUpdate: Status changed - ', data);
// since issue related to EVL module it affect all partition. For each partition set condition
for (var i = 1; i < this.partitions.length+1; i++) {
var partitionIndex = this.platformPartitionAccessoryMap['p.' + Number(i)];
if (partitionIndex !== undefined ) {
var partition = this.platformPartitionAccessories[partitionIndex];
if (partition) {
var partitionService = partition.accessory.getService(Service.SecuritySystem);
switch(data.source)
{
case "session_connect_status":
{
if(data.qualifier == 1) partitionService.updateCharacteristic(Characteristic.StatusFault,Characteristic.StatusTampered.TAMPERED);
if(data.qualifier == 3) partitionService.updateCharacteristic(Characteristic.StatusFault,Characteristic.StatusTampered.NOT_TAMPERED);
}
break;
}
}
}
}
}
// Capture general keypad updates including chime and bypass events
systemUpdate(data) {
this.log.debug('systemUpdate: Status changed - ', data);
var partitionIndex = this.platformPartitionAccessoryMap['p.' + Number(data.partition)];
var accessorybypassIndex = this.platformPartitionAccessoryMap['c.bypass'];
var accessoryChimeIndex = this.platformPartitionAccessoryMap['c.chimemode'];
if (partitionIndex !== undefined ) {
var partition = this.platformPartitionAccessories[partitionIndex];
// partition update information
if (partition) {
if ((partition.processingPartitionCmd == false) && (partition.accessoryType == "partition")) {
if ((partition.envisakitCurrentStatus != data.mode)) {
partition.envisakitCurrentStatus = data.mode;
this.log.debug("systemUpdate: partition change - " + partition.name + ' to ' + partition.envisakitCurrentStatus);
const partitionService = partition.accessory.getService(Service.SecuritySystem);
if (partitionService) {
if (partition.homekitLastTargetState != partition.ENVISA_TO_HOMEKIT_TARGET[data.mode])
{
partitionService.updateCharacteristic(Characteristic.SecuritySystemCurrentState,partition.ENVISA_TO_HOMEKIT_CURRENT[data.mode]);
if(data.mode != 'ALARM') {
partitionService.updateCharacteristic(Characteristic.SecuritySystemTargetState,partition.ENVISA_TO_HOMEKIT_TARGET[data.mode]);
partition.homekitLastTargetState = partition.ENVISA_TO_HOMEKIT_TARGET[data.mode];
}
}
// if system is not ready or has a fault set general fault
if (partition.envisakitCurrentStatus.includes('NOT_READY') || partition.envisakitCurrentStatus.includes('ALARM_MEMORY') || partition.envisakitCurrentStatus.includes('READY_FIRE_TROUBLE') || partition.envisakitCurrentStatus.includes('READY_SYSTEM_TROUBLE')) partitionService.updateCharacteristic(Characteristic.StatusFault,Characteristic.StatusFault.GENERAL_FAULT);
else partitionService.updateCharacteristic(Characteristic.StatusFault,Characteristic.StatusFault.NO_FAULT);
}
}
}
}
} else {
this.log("System status reported: Partition is not monitored, dismissing status update.");
}
// if chime enable update status;
if (accessoryChimeIndex !== undefined) {
var accessoryChime = this.platformPartitionAccessories[accessoryChimeIndex];
if (accessoryChime) {
if ((accessoryChime.envisakitCurrentStatus != data.keypadledstatus.chime) && (accessoryChime.isProcessingChimeOnOff == false)) {
accessoryChime.envisakitCurrentStatus = data.keypadledstatus.chime;
this.log.debug("systemUpdate: Accessory chime change - " + accessoryChime.name + ' to ' + accessoryChime.envisakitCurrentStatus);
if (accessoryChime.customType == "chimemode") {
var accessoryService = accessoryChime.accessory.getService(Service.Switch);
accessoryService.updateCharacteristic(Characteristic.On,data.keypadledstatus.chime);
}
}
// Is system an event? Has condition been met that processing flag can be cleared?
if((accessoryChime.envisakitCurrentStatus == data.keypadledstatus.chime) && (accessoryChime.isProcessingChimeOnOff == true)){
accessoryChime.isProcessingChimeOnOff = false;
}
}
}
// if bypass switch enable update status
if (accessorybypassIndex !== undefined) {
var accessoryBypass = this.platformPartitionAccessories[accessorybypassIndex];
if (accessoryBypass) {
if (accessoryBypass.envisakitCurrentStatus != data.mode){
accessoryBypass.envisakitCurrentStatus = data.mode;
this.log.debug("systemUpdate: Accessory bypass change - " + accessoryBypass.name + ' to ' + accessoryBypass.envisakitCurrentStatus);
if (accessoryBypass.customType == "bypass") {
var accessoryService = accessoryBypass.accessory.getService(Service.Switch);
accessoryService.updateCharacteristic(Characteristic.On,accessoryBypass.ENVISA_BYPASS_TO_HOMEKIT[data.mode]);
}
}
}
}
// Check if panel is on battery power (loss power)
if(!data.keypadledstatus.ac_present){
if(partition.ChargingState == Characteristic.ChargingState.CHARGING) {
partition.ChargingState = Characteristic.ChargingState.NOT_CHARGING;
partition.downTime = new Date();
}
} else partition.ChargingState = Characteristic.ChargingState.CHARGING;
}
// Capture partition updates usually associated with arm, disarm events
partitionUpdate(data) {
this.log.debug('partitionUpdate: status change - ', data);
var partitionIndex = this.platformPartitionAccessoryMap['p.' + Number(data.partition)];
if (partitionIndex !== undefined ) {
var partition = this.platformPartitionAccessories[partitionIndex];
if (partition) {
const partitionService = partition.accessory.getService(Service.SecuritySystem);
if (partitionService) {
if (partition.homekitLastTargetState != partition.ENVISA_TO_HOMEKIT_TARGET[data.mode] && (partition.processingPartitionCmd == false))
{
partition.envisakitCurrentStatus = data.mode;
this.log.debug("partitionUpdate: Partition data - " + partition.name + ' to ' + partition.envisakitCurrentStatus);
partitionService.updateCharacteristic(Characteristic.SecuritySystemCurrentState,partition.ENVISA_TO_HOMEKIT_CURRENT[data.mode]);
if(data.mode != 'ALARM') {
partitionService.updateCharacteristic(Characteristic.SecuritySystemTargetState,partition.ENVISA_TO_HOMEKIT_TARGET[data.mode]);
partition.homekitLastTargetState = partition.ENVISA_TO_HOMEKIT_TARGET[data.mode];
}
}
// if system is not ready set general fault
if (partition.envisakitCurrentStatus.includes('NOT_READY') || partition.envisakitCurrentStatus.includes('ALARM_MEMORY')) partitionService.updateCharacteristic(Characteristic.StatusFault,Characteristic.StatusFault.GENERAL_FAULT);
else partitionService.updateCharacteristic(Characteristic.StatusFault,Characteristic.StatusFault.NO_FAULT);
// If event occured, assume it related to user orginated activity and clear state
if (partition.processingPartitionCmd) {
// clear timer
partition.processingPartitionCmd = false;
clearTimeout(partition.armingTimeOut);
partition.armingTimeOutHandle = undefined;
}
}
}
}
else {
this.log.debug("Partition status change: Partition not monitored dismissing partition update. ");
}
}
// Capture zone updates usually associated sensor going from open to close and vice-versa
zoneUpdate(data) {
this.log.debug('zoneUpdate: Status change - ', data);
var accessoryIndex = this.platformZoneAccessoryMap['z.' + Number(data.zone)];
if (accessoryIndex !== undefined) {
var zoneaccessory = this.platformZoneAccessories[accessoryIndex];
if (zoneaccessory) {
zoneaccessory.envisakitCurrentStatus = data.mode;
this.log.debug("zoneUpdate: Accessory change - " + zoneaccessory.name + ' to ' + zoneaccessory.envisakitCurrentStatus);
var accessoryService = zoneaccessory.service;
switch(zoneaccessory.sensorType) {
case "motion":
case "glass":
if (accessoryService) accessoryService.getCharacteristic(Characteristic.MotionDetected).setValue(zoneaccessory.ENVISA_TO_HOMEKIT_MOTION[data.mode]);
break;
case "door":
case "window":
if (accessoryService) accessoryService.getCharacteristic(Characteristic.ContactSensorState).setValue(zoneaccessory.ENVISA_TO_HOMEKIT_CONTACT[data.mode]);
break;
case "leak":
if (accessoryService) accessoryService.getCharacteristic(Characteristic.LeakDetected).setValue(zoneaccessory.ENVISA_TO_HOMEKIT_LEAK[data.mode]);
break;
case "smoke":
if (accessoryService) accessoryService.getCharacteristic(Characteristic.SmokeDetected).setValue(zoneaccessory.ENVISA_TO_HOMEKIT_SMOKE[data.mode]);
break;
case "co":
if (accessoryService) accessoryService.getCharacteristic(Characteristic.CarbonMonoxideDetected).setValue(zoneaccessory.ENVISA_TO_HOMEKIT_CO[data.mode]);
break;
}
}
}
}
// Capture low level updates that are not generate from keypad events, but sent to monitoring station.
cidUpdate(data)
{
this.log.debug('cidUpdate: Status change - ', data);
/// Zone event
if ((data.type == 'zone') && (Number(data.zone) > 0)) {
var accessoryIndex = this.platformZoneAccessoryMap['z.' + Number(data.zone)];
if (accessoryIndex !== undefined) {
var accessory = this.platformZoneAccessories[accessoryIndex];
var accessoryService = accessory.service;
this.log.debug(`cidUpdate: Accessory change - Zone: ${data.zone} Name: ${accessory.name} Code: ${data.code} Qualifier: ${data.qualifier}.`);
switch (Number(data.code)) {
case 150: //Alarm, 24-Hour Auxiliar
if(data.qualifier == 1) accessoryService.updateCharacteristic(Characteristic.StatusFault, Characteristic.StatusFault.GENERAL_FAULT);
if(data.qualifier == 3) accessoryService.updateCharacteristic(Characteristic.StatusFault, Characteristic.StatusFault.NO_FAULT);
break;
case 384: // RF LOW BATTERY
if(data.qualifier == 1) accessoryService.updateCharacteristic(Characteristic.StatusLowBattery, Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW);
if(data.qualifier == 3) accessoryService.updateCharacteristic(Characteristic.StatusLowBattery, Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL);
break;
case 383: // SENSOR TAMPER
if(data.qualifier == 1) accessoryService.updateCharacteristic(Characteristic.StatusTampered, Characteristic.StatusTampered.TAMPERED);
if(data.qualifier == 3) accessoryService.updateCharacteristic(Characteristic.StatusTampered, Characteristic.StatusTampered.NOT_TAMPERED);
break;
// qualifier can be 1 = 'Event or Opening', 3 = 'Restore or Closing'
case 570: // Bypass event
if(data.qualifier == 1){
this.log(`${accessory.name} has been bypass.`);
accessory.bypassStatus = true;
}
if(data.qualifier == 3){
this.log(`${accessory.name} has been un-bypass.`);
accessory.bypassStatus = false;
}
alarm.isProcessingBypassqueue = alarm.isProcessingBypassqueue - 1;
if ((alarm.isProcessingBypassqueue <= 0 ) && (alarm.isProcessingBypass)) {
alarm.isProcessingBypass = false;
alarm.isProcessingBypassqueue = 0;
this.log(`All queued bypass/un-bypass command(s) completed.`)
}
break;
}
}
}
// Event is related to a partition
if (Number(data.partition) > 0) {
var partitionIndex = this.platformPartitionAccessoryMap['p.' + Number(data.partition)];
if (partitionIndex !== undefined ) {
var partition = this.platformPartitionAccessories[partitionIndex];
this.log.debug(`cidUpdate: Partition change - Partition: ${partitionIndex} Name: ${partition.name} Code: ${data.code} Qualifier: ${data.qualifier}.`);
switch (Number(data.code)) {
case 301: // Trouble-AC Power
if((data.qualifier == 1) && (partition.ChargingState == Characteristic.ChargingState.CHARGING)) {
partition.ChargingState = Characteristic.ChargingState.NOT_CHARGING;
partition.downTime = new Date();
}
if(data.qualifier == 3) partition.ChargingState = Characteristic.ChargingState.CHARGING;
break;
case 302: // Trouble-Low Battery (AC is lost, battery is getting low)
if(data.qualifier == 1)
if (partition.batteryLevel > 20) partition.batteryLevel = 20;
if(data.qualifier == 3) partition.batteryLevel = 100;
var partitionServiceBattery = partition.getService(Service.Battery);
if (partitionServiceBattery) partitionServiceBattery.updateCharacteristic(Characteristic.BatteryLevel,partition.batteryLevel);
break;
case 309: // Trouble-Battery Test Failure (Battery failed at test interval)
case 311: // Trouble-Battery Missing
if(data.qualifier == 1)
partition.batteryLevel = 0;
if(data.qualifier == 3) partition.batteryLevel = 100;
var partitionService = partition.accessory.getService(Service.SecuritySystem);
if (partitionService) partitionService.updateCharacteristic(Characteristic.BatteryLevel,partition.batteryLevel);
break;
case 144: // Alarm-Sensor Tamper-#
case 145: // Alarm-Exp. Module Tamper-#
case 137: // Burg-Tamper-#
case 316: // Trouble System Tamper
var partitionService = partition.accessory.getService(Service.SecuritySystem);
if (partitionService) {
if(data.qualifier == 1) partitionService.updateCharacteristic(Characteristic.StatusTampered, Characteristic.StatusTampered.TAMPERED);
if(data.qualifier == 3) partitionService.updateCharacteristic(Characteristic.StatusTampered, Characteristic.StatusTampered.NOT_TAMPERED);
}
break;
}
}
}
}
// Find accessory with no association with this plug-in and remove
removeOrphanAccessory() {
var cachedAccessory = this.accessories;
var foundAccessory;
for (var i = 0; i < cachedAccessory.length; i++)
{
let accessory = cachedAccessory[i];
foundAccessory = this.activeAccessoryMap[accessory.UUID];
if (foundAccessory == undefined) {
this.removeAccessory(accessory,true);
}
}
}
// Add accessory to homekit dashboard
addAccessory(device) {
this.log.debug('Adding accessory',device.accessory.displayName);
try {
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [device.accessory]);
this.accessories.push(device.accessory);
} catch (err) {
this.log.error(`Envisalink load Error: An error occurred while adding accessory: ${err}`);
}
}
// Remove accessory to homekit dashboard
removeAccessory(accessory, updateIndex) {
this.log.debug('Envisalink Removing accessory:',accessory.displayName );
if (accessory) {
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
if (updateIndex) {
if (this.accessories.indexOf(accessory) > -1) {
this.accessories.splice(this.accessories.indexOf(accessory), 1);
}}
}
// This function is invoked when homebridge restores cached accessories from disk at startup.
// It should be used to setup event handlers for characteristics and update respective values.
configureAccessory(accessory) {
this.log.debug('Loading accessory from cache:', accessory.displayName);
// add the restored accessory to the accessories cache so we can track if it has already been registered
this.accessories.push(accessory);
}
}
const homebridge = homebridge => {
Accessory = homebridge.hap.Accessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, EnvisalinkPlatform);
};
module.exports = homebridge;