-
Notifications
You must be signed in to change notification settings - Fork 1
/
node-spc-domoticz-binding.js
executable file
·358 lines (319 loc) · 11.5 KB
/
node-spc-domoticz-binding.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
#!/usr/bin/env node
/*
* Binding between SPC Web Gateway and Domoticz
*/
/* Accept self signed certificate */
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var config = require('./config.json');
// SPC Websocket Client
var ws_client = require('websocket').client;
var spc_ws_client = new ws_client();
// SPC Http Client
var digest = require('./lib/http-digest-client');
var spc_http_client = digest.createClient(config.spc_get_user, config.spc_get_password, true);
// Domoticz Http Client
var hc2_http_client = require('http');
// Update Domoticz with current SPC Areas and Zones status
getSpcStatus('area', handleSpcAreaData);
getSpcStatus('zone', handleSpcZoneData);
// Listen on events from SPC
spc_ws_client.connect('wss://' + config.spc_gw_host + ':' + config.spc_gw_port + '/ws/spc?username=' + config.spc_ws_user + '&password=' + config.spc_ws_password);
spc_ws_client.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
});
spc_ws_client.on('connect', function(connection) {
console.log('SPC WebSocket client connected');
connection.on('error', function(error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function() {
console.log('echo-protocol Connection Closed');
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
manageSiaEvent(message.utf8Data);
}
});
});
/**********************************************************************
* setDomoticzUserVariable
**********************************************************************/
function setDomoticzVariable(globalVariableHC2, value){
/*
http://192.168.1.16:8080/json.htm?type=command¶m=udevice&idx=$idx&nvalue=0&svalue=79
http://192.168.1.16:8080/json.htm?type=command¶m=saveuservariable&vname=test2&vtype=2&vvalue=testing
Where type is 0 to 4:
0 = Integer, e.g. -1, 1, 0, 2, 10
1 = Float, e.g. -1.1, 1.2, 3.1
2 = String
3 = Date in format DD/MM/YYYY
4 = Time in 24 hr format HH:MM
Update an existing variable
/json.htm?type=command¶m=updateuservariable&idx=idx&vname=uservariablename&vtype=uservariabletype&vvalue=uservariablevalue
List all variables
/json.htm?type=command¶m=getuservariables
List one variable:
/json.htm?type=command¶m=getuservariable&idx=IDX
Delete a variable
/json.htm?type=command¶m=deleteuservariable&idx=IDX
*/
var options = {
hostname: config.domo_host,
port: config.domo_port,
path: '/json.htm?type=command¶m=updateuservariable&vname=' + globalVariableHC2 + '&vtype=2&vvalue=' + value,
/*auth: config.hc2_user + ':' + config.hc2_password + '@',*/
method: 'PUT'
}
var req = hc2_http_client.request(options, function(res) {
if (res.statusCode == 400) { /* Create variable if not found */
console.log('400 Error, waiting and retrying');
setTimeout(function(){
setDomoticzVariable(globalVariableHC2, value)
}, 1000);
req.abort();
return;
}
var reply = '';
res.on('data', function(chunk) {
reply += chunk;
});
res.on('end', function(){
var replyobj = null;
console.log(reply);
try {
replyobj = JSON.parse(reply);
if (replyobj.status == 'ERR') { /* Create variable if not found */
createDomoticzVariable(globalVariableHC2, value);
}
} catch (e) {
console.log('Failed to parse reply, expected JSON');
}
});
}).on('error', function(e) {
console.log('Error: ' + e.message);
});
req.end();
}
/**********************************************************************
* createDomoticzVariable
**********************************************************************/
function createDomoticzVariable(globalVariableHC2, value){
var options = {
hostname: config.domo_host,
port: config.domo_port,
path: '/json.htm?type=command¶m=saveuservariable&vname=' + globalVariableHC2 + '&vtype=2&vvalue=' + value,
/*auth: config.hc2_user + ':' + config.hc2_password + '@',*/
method: 'POST'
}
var req = hc2_http_client.request(options, function(res) {
if (res.statusCode == 400) { /* Create variable if not found */
console.log('400 Error, aborting, and retrying?');
setTimeout(function(){
createDomoticzVariable(globalVariableHC2, value)
}, 1000);
req.abort();
return;
}
var reply = '';
res.on('data', function(chunk) {
reply += chunk;
});
res.on('end', function(){
console.log(reply);
});
}).on('error', function(e) {
console.log('Error: ' + e.message);
});
req.end();
}
/**********************************************************************
* handleSpcAreaData
**********************************************************************/
function handleSpcAreaData(data) {
console.log(data)
data.area.forEach(function(area) {
var area_mode = "unknown";
switch (parseInt(area.mode)) {
case 0:
area_mode = "unset";
break;
case 1:
area_mode = "partset_a";
break;
case 2:
area_mode = "partset_b";
break;
case 3:
area_mode = "set";
break;
}
console.log(area_mode)
if (!config.naming) {
var modeVariableHC2 = 'G_SPC_AREA_MODE_' + area.id;
} else if (config.naming) {
// Special characters need to be replaced, ideally have full list/array of chars here
var area_name = area.name.replace(/ /g,"_").replace("/","_");
var modeVariableHC2 = 'G_SPC_AREA_' + area_name;
}
setDomoticzVariable(modeVariableHC2, area_mode);
});
}
/**********************************************************************
* handleSpcZoneData
**********************************************************************/
function handleSpcZoneData(data) {
data.zone.forEach(function(zone) {
if (zone.input != undefined) {
var zone_input = "unknown";
switch (parseInt(zone.input)) {
case 0:
zone_input = "closed";
break;
case 1:
zone_input = "open";
break;
case 2:
zone_input = "short";
break;
case 3:
zone_input = "disconnected";
break;
case 4:
zone_input = "pir_masked";
break;
case 5:
zone_input = "dc_substitution";
break;
case 6:
zone_input = "sensor_missing";
break;
case 7:
zone_input = "offline";
break;
}
if (!config.naming) {
var inputVariableHC2 = 'G_SPC_ZONE_INPUT_' + zone.id;
} else if (config.naming) {
// Special characters need to be replaced, ideally have full list/array of chars here
var zone_name = zone.zone_name.replace(/ /g,"_").replace("/","_");
var inputVariableHC2 = 'G_SPC_ZONE_INPUT_' + zone_name;
}
setDomoticzVariable(inputVariableHC2, zone_input);
}
if (zone.status != undefined) {
var zone_status = "unknown";
switch (parseInt(zone.status)) {
case 0:
zone_status = "ok";
break;
case 1:
zone_status = "inhibit";
break;
case 2:
zone_status = "isolate";
break;
case 3:
zone_status = "soak";
break;
case 4:
zone_status = "tamper";
break;
case 5:
zone_status = "alarm";
break;
case 6:
zone_status = "ok";
break;
case 7:
zone_status = "trouble";
break;
}
if (!config.naming) {
var statusVariableHC2 = 'G_SPC_ZONE_STATUS_' + zone.id;
} else if (config.naming) {
// Special characters need to be replaced, ideally have full list/array of chars here
var zone_name = zone.zone_name.replace(/ /g,"_").replace("/","_");
var statusVariableHC2 = 'G_SPC_ZONE_STATUS_' + zone_name;
}
setDomoticzVariable(statusVariableHC2, zone_status);
}
});
}
/**********************************************************************
* getSpcStatus
**********************************************************************/
function getSpcStatus(uri, callback) {
var options = {
host: config.spc_gw_host,
port: config.spc_gw_port,
path: '/spc/' + uri,
method: 'GET'
}
var reply = "";
var req = spc_http_client.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk){
reply += chunk;
});
res.on('end', function(){
var data = JSON.parse(reply);
if (data.status === 'success'){
callback && callback(data.data);
console.log(data);
}
else {
console.log("Unable to get data from SPC: " + uri);
}
});
});
}
/**********************************************************************
* manageSiaEvent
**********************************************************************/
function manageSiaEvent(message){
data = JSON.parse(message);
if (data.status === 'success'){
var sia = data.data.sia;
sia_code = sia.sia_code;
sia_address = sia.sia_address;
sia_zone_name = sia.description;
// Update status dependent on type of SIA event
switch (sia_code){
case 'BA': /* Burglar Alarm */
getSpcStatus('area', handleSpcAreaData);
getSpcStatus('zone', handleSpcZoneData);
break;
case 'BR': /* Burglar Alarm Restore */
getSpcStatus('area', handleSpcAreaData);
getSpcStatus('zone', handleSpcZoneData);
break;
case 'BB': /* Inhibited or Isolated */
getSpcStatus('zone', handleSpcZoneData);
break;
case 'BU': /* Deinhibited or Deisolated */
getSpcStatus('zone', handleSpcZoneData);
break;
case 'NL': /* Area Activated (Part Set) */
case 'CG': /* Area Activated (Full Set) */
getSpcStatus('area', handleSpcAreaData);
break;
case 'OG': /* Area De-Activated (Un-Set) */
getSpcStatus('area', handleSpcAreaData);
break;
case 'ZC': /* Zone Closed */
case 'ZO': /* Zone Opened */
var value = (sia_code == 'ZC') ? 0:1;
var data = {
zone: [
{
id: sia_address,
input: value,
zone_name: sia_zone_name
}
]
}
handleSpcZoneData(data);
break;
}
}
}