forked from NodeAlarmProxy/NodeAlarmProxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodealarmproxy.js
344 lines (320 loc) · 10.7 KB
/
nodealarmproxy.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
var net = require('net');
var elink = require('./envisalink.js');
var events = require('events');
var eventEmitter = new events.EventEmitter();
//var config = require('./config.js');
var connections = [];
var alarmdata = {
zone: {},
partition: {},
user: {}
};
var actual, server, config;
var consoleWrapper = new Object();
var commandCallback = undefined;
exports.initConfig = function (initconfig) {
consoleWrapper.log = function () {
// default is to log unless specifically disabled
if (initconfig.logging !== false) {
console.log.apply(this, arguments);
}
}
config = initconfig;
if (!config.actualport) {
config.actualport = 4025;
}
if (!config.proxyenable) {
config.proxyenable = false;
}
actual = net.connect({ port: config.actualport, host: config.actualhost }, function () {
consoleWrapper.log('actual connected');
});
if (config.proxyenable) {
if (!config.serverport) {
config.serverport = 4025;
}
if (!config.serverhost) {
config.serverhost = '0.0.0.0';
}
if (!config.serverpassword) {
config.serverpassword = config.actualpassword;
}
var server = net.createServer(function (c) { //'connection' listener
consoleWrapper.log('server connected');
connections.push(c);
c.on('error', function (e) {
consoleWrapper.log('error', e);
connections = [];
});
c.on('end', function () {
var index = connections.indexOf(c);
if (~index) connections.splice(index, 1);
consoleWrapper.log('server disconnected:', connections);
});
c.on('data', function (data) {
consoleWrapper.log('data', data.toString());
var dataslice = data.toString().replace(/[\n\r]/g, ',').split(',');
for (var i = 0; i < dataslice.length; i++) {
var rec = elink.applicationcommands[dataslice[i].substring(0, 3)];
if (rec) {
if (rec.bytes === '' || rec.bytes === 0) {
consoleWrapper.log(rec.pre, rec.post);
} else {
consoleWrapper.log(rec.pre, dataslice[i].substring(3, dataslice[i].length - 2), rec.post);
}
if (rec.action === 'checkpassword') {
checkpassword(c, dataslice[i]);
}
consoleWrapper.log('rec.action', rec.action);
if (rec.action === 'forward') {
sendforward(dataslice[i].substring(0, dataslice[i].length - 2));
}
sendcommand(c, rec.send);
}
}
});
c.write('505300');
c.pipe(c);
});
server.listen(config.serverport, config.serverhost, function () { //'listening' listener
consoleWrapper.log('server bound');
});
function checkpassword(c, data) {
if (data.substring(3, data.length - 2) == config.serverpassword) {
consoleWrapper.log('Correct Password! :)');
sendcommand(c, '5051');
} else {
consoleWrapper.log('Incorrect Password :(');
sendcommand(c, '5050');
c.end();
}
}
function sendforward(data) {
consoleWrapper.log('sendforward:', data);
sendcommand(actual, data);
}
function broadcastresponse(response) {
if (connections.length > 0) {
for (var i = 0; i < connections.length; i++) {
consoleWrapper.log('response', response);
sendcommand(connections[i], response);
}
}
}
}
function loginresponse(data) {
var loginStatus = data.substring(3, 4);
if (loginStatus === '0') {
consoleWrapper.log('Incorrect Password :(');
}
if (loginStatus === '1') {
consoleWrapper.log('successfully logged in! getting current data...');
sendcommand(actual, '001');
}
if (loginStatus === '2') {
consoleWrapper.log('Request for Password Timed Out :(');
}
if (loginStatus === '3') {
consoleWrapper.log('login requested... sending response...');
sendcommand(actual, '005' + config.password);
}
}
function updatezone(tpi, data) {
var zone = parseInt(data.substring(3, 6));
var initialUpdate = alarmdata.zone[zone] === undefined;
if (zone <= config.zone) {
alarmdata.zone[zone] = { 'send': tpi.send, 'name': tpi.name, 'code': data };
if (config.atomicEvents && !(initialUpdate && config.suppressInitialUpdate)) {
//eventEmitter.emit('zoneupdate', [zone, alarmdata.zone[zone]]);
var zoneId = parseInt(data.substring(3, 6));
var evtData = {
evtType: "zoneUpdate",
zone: zoneId,
code: data.substring(0, 3),
status: tpi.send
};
if( config.zoneInfo && config.zoneInfo[zoneId] && config.zoneInfo[zoneId].label)
evtData.zoneLabel = config.zoneInfo[zoneId].label;
else
evtData.zoneLabel = "Zone-" + zoneId;
eventEmitter.emit('zoneupdate', evtData);
} else {
eventEmitter.emit('data', alarmdata);
}
}
}
function updatepartition(tpi, data) {
var partition = parseInt(data.substring(3, 4));
var initialUpdate = alarmdata.partition[partition] === undefined;
if (partition <= config.partition) {
alarmdata.partition[partition] = { 'send': tpi.send, 'name': tpi.name, 'code': data };
if (config.atomicEvents && !(initialUpdate && config.suppressInitialUpdate)) {
//eventEmitter.emit('partitionupdate', [partition, alarmdata.partition[partition]]);
var cmd = data.substring(0,3);
var partId = parseInt(data.substring(3,4));
var evtData = {
evtType: "partitionUpdate",
partition: partId,
code: data.substring(0, 3),
status: tpi.send
};
if (cmd == "652") { // Partition Armed
var armModeInt = parseInt(data.substring(4,5));
evtData.armMode = (['away','stay','zero-entry-away','zero-entry-stay'])[armModeInt];
} else if( cmd == "700" || cmd == "750") { // 700=User closing, 750=User opening
if( cmd == "700")
evtData.armType = "userClosing";
else if( cmd == "750")
evtData.armType = "userOpening";
evtData.userId = parseInt(data.substring(4,8));
if( config.userInfo && config.userInfo[evtData.userId] && config.userInfo[evtData.userId].label)
evtData.userLabel = config.userInfo[evtData.userId].label;
// Also call updatepartitionuser() to update alarmdata and emit the partitionuserupdate event as well for those who uses it (hope this does not break anything)
updatepartitionuser(tpi,data);
} else if( cmd == "701") { // Special closing
evtData.armType = "specialClosing";
} else if( cmd == "751") { // Special opening
evtData.armType = "specialOpening";
}
//eventEmitter.emit('partitionupdate', { partition: parseInt(data.substring(3, 4)), code: data.substring(0, 3), mode: data.substring(4, 5), evtName: tpi.send });
eventEmitter.emit('partitionupdate', evtData);
} else {
eventEmitter.emit('data', alarmdata);
}
}
}
function updatepartitionuser(tpi, data) {
var partition = parseInt(data.substring(3, 4));
var user = parseInt(data.substring(4, 8));
var initialUpdate = alarmdata.user[user] === undefined;
if (partition <= config.partition) {
alarmdata.user[user] = { 'send': tpi.send, 'name': tpi.name, 'code': data };
if (config.atomicEvents && !initialUpdate) {
eventEmitter.emit('partitionuserupdate', [user, alarmdata.user[user]]);
} else {
eventEmitter.emit('data', alarmdata);
}
}
}
function updatesystem(tpi, data) {
var code = data.substring(0,3);
var systemData = {
"evtType": "systemUpdate",
"code": code,
"status": tpi.send
};
if( code == 840 || code == 841) {
var partId = parseInt(data.substring(3,4));
systemData.partition = partId;
if( partId > config.partition)
return; // don't emit this event since it's about a partition we're not interested in
} else if( code == 849) { // Verbose Trouble Status
var trouble_bitfield = parseInt( "0x" + data.substring(3,5));
var trouble = {};
trouble.service_is_required = false;
trouble.ac_power_lost = false;
trouble.telephone_line_fault = false;
trouble.failure_to_communicate = false;
trouble.sensor_or_zone_fault = false;
trouble.sensor_or_zone_tamper = false;
trouble.sensor_or_zone_low_battery = false;
trouble.loss_of_time = false;
if( trouble_bitfield & (1<<0))
trouble.service_is_required = true;
if( trouble_bitfield & (1<<1))
trouble.ac_power_lost = true;
if( trouble_bitfield & (1<<2))
trouble.telephone_line_fault = true;
if( trouble_bitfield & (1<<3))
trouble.failure_to_communicate = true;
if( trouble_bitfield & (1<<4))
trouble.sensor_or_zone_fault = true;
if( trouble_bitfield & (1<<5))
trouble.sensor_or_zone_tamper = true;
if( trouble_bitfield & (1<<6))
trouble.sensor_or_zone_low_battery = true;
if( trouble_bitfield & (1<<7))
trouble.loss_of_time = true;
systemData.troubleStatus = trouble;
}
if (config.atomicEvents) {
eventEmitter.emit('systemupdate', systemData);
} else {
eventEmitter.emit('data', systemData);
}
}
actual.on('data', function (data) {
var dataslice = data.toString().replace(/[\n\r]/g, ',').split(',');
for (var i = 0; i < dataslice.length; i++) {
var datapacket = dataslice[i];
if (datapacket !== '') {
var tpi = elink.tpicommands[datapacket.substring(0, 3)];
if (tpi) {
if (tpi.bytes === '' || tpi.bytes === 0) {
consoleWrapper.log(tpi.pre, tpi.post);
} else {
consoleWrapper.log(tpi.pre, datapacket.substring(3, datapacket.length - 2), tpi.post);
if (tpi.action === 'updatezone') {
updatezone(tpi, datapacket);
}
else if (tpi.action === 'updatepartition') {
updatepartition(tpi, datapacket);
}
else if (tpi.action === 'updatepartitionuser') {
updatepartitionuser(tpi, datapacket);
}
else if (tpi.action === 'updatesystem') {
//updatepartitionuser(tpi, datapacket); // surely this could not have been right?
updatesystem(tpi, datapacket);
}
else if (tpi.action === 'loginresponse') {
loginresponse(datapacket);
}
else if (tpi.action === 'command-completed') {
if (commandCallback) {
commandCallback();
}
}
else if (tpi.action === 'command-error') {
if (commandCallback) {
commandCallback(datapacket.substring(3, datapacket.length - 2));
}
}
}
if (config.proxyenable) {
broadcastresponse(datapacket.substring(0, datapacket.length - 2));
}
}
}
}
//actual.end();
});
actual.on('end', function () {
consoleWrapper.log('actual disconnected');
});
return eventEmitter;
};
function sendcommand(addressee, command) {
var checksum = 0;
for (var i = 0; i < command.length; i++) {
checksum += command.charCodeAt(i);
}
checksum = checksum.toString(16).slice(-2).toUpperCase();
consoleWrapper.log('sendcommand', command + checksum)
addressee.write(command + checksum + '\r\n');
}
exports.manualCommand = function (command, callback) {
if (actual) {
if (callback) {
commandCallback = callback;
} else {
commandCallback = undefined;
}
sendcommand(actual, command);
} else {
//not initialized
}
};
exports.getCurrent = function () {
eventEmitter.emit('data', alarmdata);
};