forked from jensmoes/Niffler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (136 loc) · 5.66 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
/* global global, zway */
/*** Niffler ZAutomation module ****************************************
Version: 1.0.0
(c) Jens Troest, 2015
-------------------------------------------------------------------------------
Author: Jens Troest
Description:
This module executes NIF bindings for devices incapable of sending unsolicited reports
******************************************************************************/
// ----------------------------------------------------------------------------
// --- Class definition, inheritance and setup
// ----------------------------------------------------------------------------
function Niffler (id, controller) {
// Call superconstructor first (AutomationModule)
Niffler.super_.call(this, id, controller);
}
inherits(Niffler, AutomationModule);
//Static declations
_module = Niffler;
Niffler.binderMethods;
// ----------------------------------------------------------------------------
// --- Module instance initialized
// ----------------------------------------------------------------------------
Niffler.prototype.init = function (config) {
Niffler.super_.prototype.init.call(this, config);
var self = this;
this.binderMethods = [];//Hold methods used to bind
console.log("Niffler: init");
//The boot sequence of ZWay is not well defined.
//This method is used to detect device creation on boot and niffle any device on the list
this.deviceCreated = function (vDev) {
self.niffleDevice(vDev);
};
this.deviceDeleted = function (vDev) {
self.unNiffle([vDev.id]);
};
//Register for events
this.controller.devices.on('created', this.deviceCreated);
this.controller.devices.on('removed', this.deviceDeleted);
//Niffle all listed devices on each start, this will handle restarts after boot
this.config.sourceDevices.forEach(function(devId) {
self.niffleDevice(this.controller.devices.get(devId));
});
};
Niffler.prototype.stop = function () {
console.log("Niffler: stop() called");
//Unnifle any niffled devices
if(this.config.sourceDevices.length) {
this.unNiffle(this.config.sourceDevices);
this.binderMethods = [];
}
//Unregister for device creation
this.controller.devices.off('created',this.deviceCreated);
this.controller.devices.off('removed',this.deviceDeleted);
Niffler.super_.prototype.stop.call(this);
};
// ----------------------------------------------------------------------------
// --- Module methods
// ----------------------------------------------------------------------------
//Do the actual niffle on the physical device
Niffler.prototype.niffle = function(virtualDevice) {
var index = this.getDeviceIndex(virtualDevice.id);
if (global.ZWave && !isNaN(index) && zway && zway.devices[index]) {
var binderMethod;
var deviceType = virtualDevice.get('deviceType');
if(deviceType === 'doorlock' && zway.devices[index].Alarm){
binderMethod = function(type) {
console.log("Niffler","doorlock alarm event");
zway.devices[index].DoorLock.Get(); //This call will poll and update the zway UI. Useful since most alarms are lock/unlock events
};
zway.devices[index].Alarm.data.V1event && zway.devices[index].Alarm.data.V1event.bind(binderMethod);
zway.devices[index].Alarm.data[6] && zway.devices[index].Alarm.data[6].bind(binderMethod);
}else{
binderMethod = function(type) {
zway.devices[index].Basic.Get();
};
zway.devices[index].data.nodeInfoFrame.bind(binderMethod);
}
this.binderMethods.push( [index,binderMethod] );//Add method to array for later unbind
}
};
Niffler.prototype.unNiffle = function(UNList) {
var self = this;
if(UNList.length)
{
console.log("Niffler: unNiffling existing devices");
UNList.forEach(function(vDevId) {
var index = self.getDeviceIndex(vDevId);
var unBinder = null;
for(n=0; n<self.binderMethods.length; n++) {
if(self.binderMethods[n][0] === index) {
unBinder = self.binderMethods[n][1];
break;
}
}
console.log("Niffler: unNiffling ", vDevId);
if (global.ZWave && !isNaN(index) && unBinder !== null && zway && zway.devices[index]) {
var vdev = this.controller.devices.get(vDevId);
if(vdev && vdev.get('deviceType') === 'doorlock' && zway.devices[index].Alarm) {
console.log("Niffler: unNiffling doorlock");
zway.devices[index].Alarm.data.V1event && zway.devices[index].Alarm.data.V1event.unbind(unBinder);
zway.devices[index].Alarm.data[6] && zway.devices[index].Alarm.data[6].unbind(unBinder);
} else {
zway.devices[index].data.nodeInfoFrame.unbind(unBinder);
}
}
});
}
};
//Retrieve the index of the physical device. null if not found
Niffler.prototype.getDeviceIndex = function(vdevid) {
var str = vdevid;
console.log("Niffler: getdeviceindex: ", str);
var res = str.split("_");
if(res.length != 3 && str[0] != "ZWayVDev")
return null;
return res[2].split("-")[0];
};
//Niffle a device if it is in the source list.
//vdevid is the virtual device and index is the physical device location
Niffler.prototype.niffleDevice = function(vdev) {
if(!vdev) return;
var sdev;
//Should this device be niffled? Look for it in the source list
this.config.sourceDevices.forEach(function(adev) {
if(adev === vdev.id) {
sdev = adev;
return;
}
});
if(sdev) {//We have a match
//Niffle this device
console.log("Niffler: Niffling device ",vdev.id);
this.niffle(vdev);
}
};