forked from schmupu/ioBroker.contactid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontactid.js
450 lines (285 loc) · 10.9 KB
/
contactid.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
'use strict';
var utils = require(__dirname + '/lib/utils'); // Get common adapter utils
var dp = require(__dirname + '/lib/datapoints');
var net = require('net');
var adapter = new utils.Adapter('contactid');
var server = null; // Server instance
// *****************************************************************************************************
// is called when adapter shuts down - callback has to be called under any circumstances!
// *****************************************************************************************************
adapter.on('unload', function(callback) {
try {
adapter.log.info('Closing Contact ID Server');
if (server) {
server.close();
}
callback();
} catch (e) {
callback();
}
});
// *****************************************************************************************************
// is called if a subscribed object changes
// *****************************************************************************************************
adapter.on('objectChange', function(id, obj) {
// Warning, obj can be null if it was deleted
if (obj) {
}
});
// *****************************************************************************************************
// is called if a subscribed state changes
// *****************************************************************************************************
adapter.on('stateChange', function(id, state) {
// Warning, state can be null if it was deleted
if (state && !state.ack) {
}
});
// *****************************************************************************************************
// is called when databases are connected and adapter received configuration.
// start here!
// *****************************************************************************************************
adapter.on('ready', function() {
adapter.log.info(adapter.namespace);
main();
});
// *****************************************************************************************************
// Main function
// *****************************************************************************************************
function main() {
// delete not used / missing object in configuration
deleteObects();
// add object from configuration.
createObjects();
// start socket server
serverStart();
// in this contactid all states changes inside the adapters namespace are subscribed
// adapter.subscribeStates('*');
}
// *****************************************************************************************************
// convert subcriber to ID for using as channel name. Special characters and spaces are deleted.
// *****************************************************************************************************
function getSubscriberID(subscriber) {
var id = subscriber.replace(/[.\s]+/g, '_');
return id;
}
// *****************************************************************************************************
// delete channel and states which are missing in configuration
// *****************************************************************************************************
function deleteChannel(obj) {
// adapter.log.info('deleteChannel: ' + JSON.stringify(obj));
// search recrusive for channel name. If found and missing in
// configuration, delete channel and all states
Object.keys(obj).forEach(key => {
if (obj[key] && typeof obj[key] === 'object') {
deleteChannel(obj[key]); // recurse.
} else {
if (obj[key] == 'channel') {
var found = false;
var channelname = obj.common.name;
// Channel Name ist ein subscriber
for (var i = 0; i < adapter.config.keys.length; i++) {
var key = adapter.config.keys[i];
var id = getSubscriberID(key.subscriber);
if (id == channelname) {
found = true;
}
}
if (!found) {
adapter.deleteChannel(channelname);
}
}
}
});
}
// *****************************************************************************************************
// list of all objects (devices, channel, states) for this instance. call function deleteChannel
// for deleting old (not used) channels in configuration
// *****************************************************************************************************
function deleteObects() {
adapter.getAdapterObjects(function(obj) {
deleteChannel(obj);
});
}
// *****************************************************************************************************
// create for every ID a channel and create a few states
// *****************************************************************************************************
function createObjectCID(id, key) {
let obj = dp.dpCID || {};
adapter.setObjectNotExists(id, {
type: 'channel',
common: {
name: key.subscriber
},
native: {}
});
for (let prop in obj) {
let sid = id + '.' + prop;
let parameter = JSON.parse(JSON.stringify(obj[prop]));
parameter.name = key.subscriber + ' ' + parameter.name;
adapter.setObjectNotExists(sid, {
type: 'state',
common: parameter,
native: {}
});
}
}
// *****************************************************************************************************
// read configuration, and create for all subscribers a channel and states
// *****************************************************************************************************
function createObjects() {
for (var i = 0; i < adapter.config.keys.length; i++) {
var key = adapter.config.keys[i];
var id = getSubscriberID(key.subscriber);
createObjectCID(id, key);
}
}
// *****************************************************************************************************
// read configuration by subscriber and return the alarmsytem
// *****************************************************************************************************
function getAlarmSystem(subscriber) {
for (var i = 0; i < adapter.config.keys.length; i++) {
var key = adapter.config.keys[i];
if (key.subscriber == subscriber) {
return key.alarmsystem;
}
}
return null;
}
// *****************************************************************************************************
// Acknowledge for CID
// *****************************************************************************************************
function ackCID(cid) {
var ack = null;
switch (getAlarmSystem(cid.subscriber)) {
case "lupusec_xt1":
ack = new Buffer(1);
ack[0] = 6; //Acknowledge Lupusex 0x6
break;
case "lupusec_xt1p":
case "lupusec_xt2":
case "lupusec_xt2p":
case "lupusec_xt3":
ack = cid.data; // komplette Nachricht wieder zurückegeben
break;
default:
ack = null;
}
return ack;
}
// *****************************************************************************************************
// Set state for contact id message
// *****************************************************************************************************
function setStatesCID(cid) {
var obj = dp.dpCID || {};
var val = null;
if (cid) {
for (var i = 0; i < adapter.config.keys.length; i++) {
var key = adapter.config.keys[i];
if (key.subscriber == cid.subscriber) {
var id = getSubscriberID(cid.subscriber);
for (let prop in obj) {
var sid = id + '.' + prop;
switch (prop) {
case 'subscriber':
val = cid.subscriber;
break;
case 'event':
val = cid.event;
break;
case 'eventtext':
val = cid.eventtext;
break;
case 'group':
val = cid.group;
break;
case 'qualifier':
val = cid.qualifier;
break;
case 'sensor':
val = cid.sensor;
break;
case 'message':
val = cid.data;
break;
default:
val = null;
}
adapter.setState(sid, {
val: val,
ack: true
});
}
}
}
}
}
// *****************************************************************************************************
// start socket server for listining for contact IDs
// *****************************************************************************************************
function serverStart() {
server = net.createServer(onClientConnected);
server.listen(adapter.config.port, adapter.config.bind, function() {
var text = 'Contact ID Server listening on IP-Adress: ' + server.address().address + ':' + server.address().port;
adapter.log.info(text);
});
}
// *****************************************************************************************************
// alarm system connected and sending contact ID message
// *****************************************************************************************************
function onClientConnected(sock) {
var remoteAddress = sock.remoteAddress + ':' + sock.remotePort;
var strclose = "close"
var len = strclose.length;
var ack = null;
// adapter.log.info('New client connected: ' + remoteAddress);
sock.on('data', function(data) {
var strdata = data.toString().trim();
adapter.log.info(remoteAddress + ' sending following message: ' + strdata);
// [alarmanlage 18140101001B4B6]
// [alarmanlage 18160200000C5B7]
var cid = parseCID(strdata);
if (cid) {
// adapter.log.info("Received message: " + JSON.stringify(cid));
setStatesCID(cid);
ack = ackCID(cid);
sock.end(ack);
} else {
sock.end();
}
});
sock.on('close', function() {
adapter.log.info('connection from ' + remoteAddress + ' closed');
});
sock.on('error', function(err) {
adapter.log.error('Connection ' + remoteAddress + ' error: ' + err.message);
});
}
// *****************************************************************************************************
// parse contactid and put into object
// *****************************************************************************************************
function parseCID(data) {
var reg = /^\[(.+) 18(.)(.{3})(.{2})(.{3})(.)(.*)\]/gm;
var match = reg.exec(data);
var cid = null;
if (match) {
// <ACCT><MT><QXYZ><GG><CCC><S>
cid = {
data: data,
subscriber: match[1].trim(),
qualifier: match[2],
event: match[3],
eventtext: getEventText(match[3]),
group: match[4],
sensor: match[5],
checksum: match[6]
};
}
return cid;
}
// *****************************************************************************************************
// Text for Events
// *****************************************************************************************************
function getEventText(event) {
var events = dp.events || [];
return events[event];
}