-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode-spc-fibaro-hc2-binding.js
executable file
·284 lines (258 loc) · 8.72 KB
/
node-spc-fibaro-hc2-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
#!/usr/bin/env node
/*
* Binding between SPC Web Gateway and Fibaro Home Center 2
*/
/* 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);
// Fibaro Home Center 2 Http Client
var hc2_http_client = require('http');
// Update HC2 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);
}
});
});
/**********************************************************************
* setFibaroVariable
**********************************************************************/
function setFibaroVariable(globalVariableHC2, value){
var options = {
hostname: config.hc2_host,
port: 80,
path: '/api/globalVariables/' + globalVariableHC2,
auth: config.hc2_user + ':' + config.hc2_password,
method: 'PUT'
}
var req = hc2_http_client.request(options, function(res) {
if (res.statusCode == 404) { /* Create variable if not found */
createFibaroVariable(globalVariableHC2, value);
}
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);
});
var data = {
value: value
};
req.write(JSON.stringify(data));
req.end();
}
/**********************************************************************
* createFibaroVariable
**********************************************************************/
function createFibaroVariable(globalVariableHC2, value){
var options = {
hostname: config.hc2_host,
port: 80,
path: '/api/globalVariables/' + globalVariableHC2,
auth: config.hc2_user + ':' + config.hc2_password,
method: 'POST'
}
var req = hc2_http_client.request(options, function(res) {
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);
});
var data = {
name: globalVariableHC2,
value: value
};
req.write(JSON.stringify(data));
req.end();
}
/**********************************************************************
* handleSpcAreaData
**********************************************************************/
function handleSpcAreaData(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;
}
var modeVariableHC2 = 'G_SPC_AREA_MODE_' + area.id;
setFibaroVariable(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;
}
var inputVariableHC2 = 'G_SPC_ZONE_INPUT_' + zone.id;
setFibaroVariable(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;
}
var statusVariableHC2 = 'G_SPC_ZONE_STATUS_' + zone.id;
setFibaroVariable(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);
}
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;
// Update status dependent on type of SIA event
switch (sia_code){
case 'BA': /* Burglar Alarm */
case 'BR': /* Burglar Alarm Restore */
getSpcStatus('area', handleSpcAreaData);
getSpcStatus('zone', handleSpcZoneData);
break;
case 'BB': /* Inhibited or Isolated */
case 'BU': /* Deinhibited or Deisolated */
getSpcStatus('zone', handleSpcZoneData);
break;
case 'CL': /* Area Activated (Full Set) */
case 'NL': /* Area Activated (Part Set) */
case 'OP': /* Area Deactivated */
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
}
]
}
handleSpcZoneData(data);
break;
}
}
}