forked from ioBroker/ioBroker.hm-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hm-rpc.js
1498 lines (1353 loc) · 56.2 KB
/
hm-rpc.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* jshint -W097 */
/* jshint strict: false */
/*jslint node: true */
'use strict';
const utils = require(__dirname + '/lib/utils'); // Get common adapter utils
const images = require(__dirname + '/lib/images');
let connected = false;
let displays = {};
const FORBIDDEN_CHARS = /[\]\[*,;'"`<>\\\s?]/g;
// msgBuffer = [{line: line2, icon: icon2}, {line: line3, icon: icon3}, {line: '', icon: ''}];
// Icons:
// 0x80 AUS
// 0x81 EIN
// 0x82 OFFEN
// 0x83 geschlossen
// 0x84 fehler
// 0x85 alles ok
// 0x86 information
// 0x87 neue nachricht
// 0x88 servicemeldung
// Tonfolgen
// 0xC0 AUS
// 0xC1 LANG LANG
// 0xC2 LANG KURZ
// 0xC3 LANG KURZ KURZ
// 0xC4 KURZ
// 0xC5 KURZ KURZ
// 0xC6 LANG
// 0xC7
// 0xC9
// 0xCA
// Signale
// 0xF0 AUS
// 0xF1 Rotes Blitzen
// 0xF2 Grünes Blitzen
// 0xF3 Orangenes Blitzen
function number2hex(num) {
if (typeof num === 'number') {
num = num.toString(16).toUpperCase();
if (num.length < 2) num = '0' + num;
num = '0x' + num;
}
return num;
}
function combineEPaperCommand(lines, signal, ton, repeats, offset) {
signal = number2hex(signal || '0xF0');
ton = number2hex(ton || '0xC0');
const substitutions = {
'A': '0x41',
'B': '0x42',
'C': '0x43',
'D': '0x44',
'E': '0x45',
'F': '0x46',
'G': '0x47',
'H': '0x48',
'I': '0x49',
'J': '0x4A',
'K': '0x4B',
'L': '0x4C',
'M': '0x4D',
'N': '0x4E',
'O': '0x4F',
'P': '0x50',
'Q': '0x51',
'R': '0x52',
'S': '0x53',
'T': '0x54',
'U': '0x55',
'V': '0x56',
'W': '0x57',
'X': '0x58',
'Y': '0x59',
'Z': '0x5A',
'a': '0x61',
'b': '0x62',
'c': '0x63',
'd': '0x64',
'e': '0x65',
'f': '0x66',
'g': '0x67',
'h': '0x68',
'i': '0x69',
'j': '0x6A',
'k': '0x6B',
'l': '0x6C',
'm': '0x6D',
'n': '0x6E',
'o': '0x6F',
'p': '0x70',
'q': '0x71',
'r': '0x72',
's': '0x73',
't': '0x74',
'u': '0x75',
'v': '0x76',
'w': '0x77',
'x': '0x78',
'y': '0x79',
'z': '0x7A',
'0': '0x30',
'1': '0x31',
'2': '0x32',
'3': '0x33',
'4': '0x34',
'5': '0x35',
'6': '0x36',
'7': '0x37',
'8': '0x38',
'9': '0x39',
' ': '0x20',
'!': '0x21',
'"': '0x22',
'%': '0x25',
'&': '0x26',
'=': '0x27',
'(': '0x28',
')': '0x29',
'*': '0x2A',
'+': '0x2B',
',': '0x2C',
'-': '0x2D',
'.': '0x2E',
'/': '0x2F',
'Ä': '0x5B',
'Ö': '0x23',
'Ü': '0x24',
'ä': '0x7B',
'ö': '0x7C',
'ü': '0x7D',
'ß': '0x5F',
':': '0x3A',
';': '0x3B',
'@': '0x40',
'>': '0x3E'
};
offset = 10;
repeats = 1;
let command = '0x02,0x0A';
for (let m = 0; m < lines.length; m++) {
let line = lines[m].line;
let icon = lines[m].icon;
if (line || icon) {
command = command + ',0x12';
let i;
if ((line.substring(0, 2) === '0x') && (line.length === 4)) {
command = command + ',' + line;
i = 12;
}
else {
i = 0;
}
while ((i < line.length) && (i < 12)) {
command += ',' + substitutions[line[i]] || '0x2A';
i++;
}
if (icon) {
command += ',0x13,' + number2hex(icon);
}
}
command = command + ',0x0A';
}
command = command + ',0x14,' + ton + ',0x1C,';
if (repeats < 1) {
command = command + '0xDF,0x1D,';
}
else {
if (repeats < 11) {
command = command + '0xD' + (repeats - 1) + ',0x1D,';
}
else {
if (repeats === 11) {
command = command + '0xDA,0x1D,';
}
else {
if (repeats === 12) {
command = command + '0xDB,0x1D,';
}
else {
if (repeats === 13) {
command = command + '0xDC,0x1D,';
}
else {
if (repeats === 14) {
command = command + '0xDD,0x1D,';
}
else {
command = command + '0xDE,0x1D,';
}
}
}
}
}
}
if (offset <= 10) {
command = command + '0xE0,0x16,';
}
else {
if (offset <= 100) {
command = command + '0xE' + (offset - 1 / 10) + ',0x16,';
}
else {
if (offset <= 110) {
command = command + '0xEA,0x16,';
}
else {
if (offset <= 120) {
command = command + '0xEB,0x16,';
}
else {
if (offset <= 130) {
command = command + '0xEC,0x16,';
}
else {
if (offset <= 140) {
command = command + '0xED,0x16,';
}
else {
if (offset <= 150) {
command = command + '0xEE,0x16,';
}
else {
command = command + '0xEF,0x16,';
}
}
}
}
}
}
}
command = command + signal + ',0x03';
return command;
}
function controlEPaper(id, data) {
let tmp = id.split('.');
tmp[3] = '3';
tmp[4] = 'SUBMIT';
const val = combineEPaperCommand(data.lines, data.signal || '0xF0', data.tone || '0xC0');
try {
if (rpcClient && connected) {
rpcClient.methodCall('setValue', [tmp[2] + ':' + tmp[3], tmp[4], val], err => {
if (err) {
adapter.log.error(adapter.config.type + 'rpc -> setValue ' + JSON.stringify([tmp[3], tmp[4], val]));
adapter.log.error(err);
}
});
} else {
adapter.log.warn('Cannot setValue "' + id + '", because not connected.');
}
} catch (err) {
adapter.log.error('Cannot call setValue: :' + err);
}
}
function readSignals(id) {
displays[id] = null;
let data = {
lines: [{}, {}, {}],
signal: '0xF0',
tone: '0xC0'
};
let count = 0;
count++;
adapter.getForeignState(id + '.0.EPAPER_LINE2', (err, state) => {
data.lines[0].line = state ? state.val || '' : '';
if (!--count) controlEPaper(id, data);
});
count++;
adapter.getForeignState(id + '.0.EPAPER_ICON2', (err, state) => {
data.lines[0].icon = state ? state.val || '' : '';
if (!--count) controlEPaper(id, data);
});
count++;
adapter.getForeignState(id + '.0.EPAPER_LINE3', (err, state) => {
data.lines[1].line = state ? state.val || '' : '';
if (!--count) controlEPaper(id, data);
});
count++;
adapter.getForeignState(id + '.0.EPAPER_ICON3', (err, state) => {
data.lines[1].icon = state ? state.val || '' : '';
if (!--count) controlEPaper(id, data);
});
count++;
adapter.getForeignState(id + '.0.EPAPER_LINE4', (err, state) => {
data.lines[2].line = state ? state.val || '' : '';
if (!--count) controlEPaper(id, data);
});
count++;
adapter.getForeignState(id + '.0.EPAPER_ICON4', (err, state) => {
data.lines[2].icon = state ? state.val || '' : '';
if (!--count) controlEPaper(id, data);
});
count++;
adapter.getForeignState(id + '.0.EPAPER_SIGNAL', (err, state) => {
data.signal = state ? state.val || '0xF0' : '0xF0';
if (!--count) controlEPaper(id, data);
});
count++;
adapter.getForeignState(id + '.0.EPAPER_TONE', (err, state) => {
data.tone = state ? state.val || '0xC0' : '0xC0';
if (!--count) controlEPaper(id, data);
});
}
function readSettings(id) {
displays[id] = null;
let data = {
lines: [{}, {}, {}],
signal: '0xF0',
tone: '0xC0'
};
let count = 0;
count++;
adapter.getForeignState(id + '.0.EPAPER_LINE2', (err, state) => {
data.lines[0].line = state ? state.val || '' : '';
if (!--count) controlEPaper(id, data);
});
count++;
adapter.getForeignState(id + '.0.EPAPER_ICON2', (err, state) => {
data.lines[0].icon = state ? state.val || '' : '';
if (!--count) controlEPaper(id, data);
});
count++;
adapter.getForeignState(id + '.0.EPAPER_LINE3', (err, state) => {
data.lines[1].line = state ? state.val || '' : '';
if (!--count) controlEPaper(id, data);
});
count++;
adapter.getForeignState(id + '.0.EPAPER_ICON3', (err, state) => {
data.lines[1].icon = state ? state.val || '' : '';
if (!--count) controlEPaper(id, data);
});
count++;
adapter.getForeignState(id + '.0.EPAPER_LINE4', (err, state) => {
data.lines[2].line = state ? state.val || '' : '';
if (!--count) controlEPaper(id, data);
});
count++;
adapter.getForeignState(id + '.0.EPAPER_ICON4', (err, state) => {
data.lines[2].icon = state ? state.val || '': '';
if (!--count) controlEPaper(id, data);
});
}
// the adapter object
const adapter = utils.Adapter({
name: 'hm-rpc',
ready: function () {
adapter.subscribeStates('*');
main();
},
stateChange: function (id, state) {
if (state && state.ack !== true) {
const tmp = id.split('.');
let val;
if (id === adapter.namespace + '.updated') return;
adapter.log.debug(adapter.config.type + 'rpc -> setValue ' + tmp[3] + ' ' + tmp[4] + ': ' + state.val);
if (!dpTypes[id]) {
adapter.log.error(adapter.config.type + 'rpc -> setValue: no dpType for ' + id + '!');
return;
}
if (dpTypes[id].UNIT === '%' && dpTypes[id].MIN !== undefined) {
state.val = (state.val / 100) * (dpTypes[id].MAX - dpTypes[id].MIN) + dpTypes[id].MIN;
state.val = Math.round(state.val * 1000) / 1000;
} else
if (dpTypes[id].UNIT === '100%') {
state.val = state.val / 100;
}
const type = dpTypes[id].TYPE;
if (type === 'EPAPER_LINE' || type === 'EPAPER_ICON') {
const _id = tmp[0] + '.' + tmp[1] + '.' + tmp[2];
if (displays[_id] && displays[_id].timer) {
clearTimeout(displays[_id].timer);
if (displays[_id].withTone) {
displays[_id] = {timer: setTimeout(readSignals, 300, _id), withTone: true};
return;
}
}
displays[_id] = {timer: setTimeout(readSettings, 300, _id), withTone: false};
return;
} else if (type === 'EPAPER_SIGNAL' || type === 'EPAPER_TONE') {
const _id = tmp[0] + '.' + tmp[1] + '.' + tmp[2];
if (displays[_id] && displays[_id].timer) {
clearTimeout(displays[_id].timer);
}
displays[_id] = {timer: setTimeout(readSignals, 300, _id), withTone: true};
return;
} else {
switch (type) {
case 'BOOL':
val = (state.val === 'false' || state.val === '0') ? false : !!state.val;
break;
case 'FLOAT':
val = {explicitDouble: state.val};
break;
default:
val = state.val;
}
}
adapter.log.debug('setValue ' + JSON.stringify([tmp[2] + ':' + tmp[3], tmp[4], val]) + ' ' + type);
try {
if (rpcClient && connected) {
rpcClient.methodCall('setValue', [tmp[2] + ':' + tmp[3], tmp[4], val], (err, data) => {
if (err) {
adapter.log.error(adapter.config.type + 'rpc -> setValue ' + JSON.stringify([tmp[3], tmp[4], state.val]) + ' ' + type);
adapter.log.error(err);
}
});
} else {
adapter.log.warn('Cannot setValue "' + id + '", because not connected.');
}
} catch (err) {
adapter.log.error('Cannot call setValue: :' + err);
}
}
},
// Add messagebox Function for ioBroker.occ
message: function (obj) {
if (obj.command === 'stopInstance') {
if (rpcServer && rpcServer.server) {
try {
rpcServer.server.close(() => {
console.log('server closed.');
rpcServer.server.unref();
});
} catch (e) {
}
}
if (rpcClient && rpcClient.socket) {
try {
rpcClient.socket.destroy();
} catch (e) {
}
}
// force close
setTimeout(() => process.exit(), 3000);
} else
if (obj.message.params === undefined || obj.message.params === null) {
try {
if (rpcClient && connected) {
rpcClient.methodCall(obj.command, [obj.message.ID, obj.message.paramType], (err, data) => {
if (obj.callback) adapter.sendTo(obj.from, obj.command, {result: data, error: err}, obj.callback);
});
} else {
adapter.log.warn('Cannot send "' + obj.command + '" "' + obj.message.ID + '": because not connected');
if (obj.callback) adapter.sendTo(obj.from, obj.command, {error: 'not connected'}, obj.callback);
}
} catch (err) {
adapter.log.error('Cannot call ' + obj.command + ': ' + err);
adapter.sendTo(obj.from, obj.command, {error: err}, obj.callback);
}
} else {
try {
if (rpcClient && connected) {
rpcClient.methodCall(obj.command, [obj.message.ID, obj.message.paramType, obj.message.params], (err, data) => {
if (obj.callback) adapter.sendTo(obj.from, obj.command, {
result: data,
error: err
}, obj.callback);
});
} else {
adapter.log.warn('Cannot send "' + obj.command + '" "' + obj.message.ID + '": because not connected');
if (obj.callback) adapter.sendTo(obj.from, obj.command, {error: 'not connected'}, obj.callback);
}
} catch (err) {
adapter.log.error('Cannot call ' + obj.command + ': ' + err);
adapter.sendTo(obj.from, obj.command, {error: err}, obj.callback);
}
}
},
unload: function (callback) {
try {
if (eventInterval) {
clearInterval(eventInterval);
eventInterval = null;
}
if (connInterval) {
clearInterval(connInterval);
connInterval = null;
}
if (connTimeout) {
clearTimeout(connTimeout);
connTimeout = null;
}
if (adapter.config && rpcClient) {
adapter.log.info(adapter.config.type + 'rpc -> ' + adapter.config.homematicAddress + ':' + adapter.config.homematicPort + ' init ' + JSON.stringify([daemonURL, '']));
try {
rpcClient.methodCall('init', [daemonURL, ''], (err, data) => {
if (connected) {
adapter.log.info('Disconnected');
connected = false;
adapter.setState('info.connection', false, true);
}
if (callback) callback();
callback = null;
});
} catch (err) {
if (connected) {
adapter.log.info('Disconnected');
connected = false;
adapter.setState('info.connection', false, true);
}
adapter.log.error('Cannot call init: [' + daemonURL + ', ""]' + err);
if (callback) callback();
callback = null;
}
} else {
if (callback) callback();
callback = null;
}
} catch (e) {
if (adapter && adapter.log) {
adapter.log.error('Unload error: ' + e);
} else {
console.log(e);
}
if (callback) callback();
callback = null;
}
}
});
let rpc;
let rpcClient;
let rpcServer;
let metaValues = {};
let metaRoles = {};
let dpTypes = {};
let lastEvent = 0;
let eventInterval;
let connInterval;
let connTimeout;
let daemonURL = '';
let daemonProto = '';
function main() {
adapter.config.reconnectInterval = parseInt(adapter.config.reconnectInterval, 10) || 30;
if (adapter.config.reconnectInterval < 10) {
adapter.log.error('Reconnect interval is less than 10 seconds. Set reconnect interval to 10 seconds.');
adapter.config.reconnectInterval = 10;
}
adapter.config.checkInitInterval = parseInt(adapter.config.checkInitInterval, 10);
if (adapter.config.checkInitInterval < 10) {
adapter.log.error('Check init interval is less than 10 seconds. Set init interval to 10 seconds.');
adapter.config.checkInitInterval = 10;
}
adapter.setState('info.connection', false, true);
if (adapter.config.type === 'bin') {
rpc = require('binrpc');
daemonProto = 'xmlrpc_bin://';
} else {
rpc = require('homematic-xmlrpc');
adapter.config.type = 'xml';
daemonProto = 'http://';
}
// Load VALUE paramsetDescriptions (needed to create state objects)
adapter.objects.getObjectView('hm-rpc', 'paramsetDescription', {startkey: 'hm-rpc.meta.VALUES', endkey: 'hm-rpc.meta.VALUES.\u9999'}, function handleValueParamSetDescriptions(err, doc) {
if (err) adapter.log.error('getObjectView hm-rpc: ' + err);
if (doc && doc.rows) {
for (let i = 0; i < doc.rows.length; i++) {
const channel = doc.rows[i].id.slice(19);
metaValues[channel] = doc.rows[i].value.native;
}
}
// Load common.role assignments
adapter.objects.getObject('hm-rpc.meta.roles', (err, res) => {
if (err) adapter.log.error('hm-rpc.meta.roles: ' + err);
if (res) metaRoles = res.native;
// Start Adapter
if (adapter.config) initRpcServer();
});
});
adapter.objects.getObjectView('system', 'state', {startkey: adapter.namespace, endkey: adapter.namespace + '\u9999'}, function handleStateViews(err, res) {
if (!err && res.rows) {
for (let i = 0; i < res.rows.length; i++) {
if (res.rows[i].id === adapter.namespace + '.updated') continue;
if (!res.rows[i].value.native) {
adapter.log.warn('State ' + res.rows[i].id + ' does not have native.');
dpTypes[res.rows[i].id] = {UNIT: '', TYPE: ''};
} else {
dpTypes[res.rows[i].id] = {
UNIT: res.rows[i].value.native.UNIT,
TYPE: res.rows[i].value.native.TYPE,
MIN: res.rows[i].value.native.MIN,
MAX: res.rows[i].value.native.MAX
};
if (typeof dpTypes[res.rows[i].id].MIN === 'number') {
dpTypes[res.rows[i].id].MIN = parseFloat(dpTypes[res.rows[i].id].MIN);
dpTypes[res.rows[i].id].MAX = parseFloat(dpTypes[res.rows[i].id].MAX);
if (dpTypes[res.rows[i].id].UNIT === '100%') {
dpTypes[res.rows[i].id].UNIT = '%';
}
if (dpTypes[res.rows[i].id].MAX === 99) {
dpTypes[res.rows[i].id].MAX = 100;
}
}
}
}
}
});
}
function sendInit() {
try {
if (rpcClient && (rpcClient.connected === undefined || rpcClient.connected)) {
adapter.log.debug(adapter.config.type + 'rpc -> ' + adapter.config.homematicAddress + ':' + adapter.config.homematicPort + ' init ' + JSON.stringify([daemonURL, adapter.namespace]));
rpcClient.methodCall('init', [daemonURL, adapter.namespace], function handleInit(err, data) {
if (!err) {
if (adapter.config.daemon === 'CUxD') {
getCuxDevices(function handleCuxDevices(err2) {
if (!err2) {
updateConnection();
} else {
adapter.log.error('getCuxDevices error: ' + err2);
}
});
} else {
updateConnection();
}
} else {
adapter.log.error('init error: ' + err);
}
});
}
} catch (err) {
adapter.log.error('Init not possible, going to stop: ', err);
adapter.stop();
}
}
function sendPing() {
if (rpcClient) {
adapter.log.debug('Send PING...');
try {
rpcClient.methodCall('ping', [adapter.namespace], (err, data) => {
if (!err) {
adapter.log.debug('PING ok');
} else {
adapter.log.error('Ping error: ' + err);
if (connected) {
adapter.log.info('Disconnected');
connected = false;
adapter.setState('info.connection', false, true);
connect();
}
}
});
} catch (err) {
adapter.log.error('Cannot call ping [' + adapter.namespace + ']: ' + err);
}
} else {
adapter.warn('Called PING, but client does not exist');
if (connected) {
adapter.log.info('Disconnected');
connected = false;
adapter.setState('info.connection', false, true);
connect();
}
}
}
function initRpcServer() {
adapter.config.homematicPort = parseInt(adapter.config.homematicPort, 10);
adapter.config.port = parseInt(adapter.config.port, 10);
//adapterPort was introduced in v1.0.1. If not set yet then try 2000
const adapterPort = parseInt(adapter.config.port || adapter.config.homematicPort, 10) || 2000;
const callbackAddress = adapter.config.callbackAddress || adapter.config.adapterAddress;
adapter.getPort(adapterPort, port => {
daemonURL = daemonProto + callbackAddress + ':' + port;
rpcServer = rpc.createServer({host: adapter.config.adapterAddress, port: port});
adapter.log.info(adapter.config.type + 'rpc server is trying to listen on ' + adapter.config.adapterAddress + ':' + port);
adapter.log.info(adapter.config.type + 'rpc client is trying to connect to ' + adapter.config.homematicAddress + ':' + adapter.config.homematicPort + ' with ' + JSON.stringify([daemonURL, adapter.namespace]));
connect(true);
rpcServer.on('NotFound', (method, params) => adapter.log.warn(adapter.config.type + 'rpc <- undefined method ' + method + ' ' + JSON.stringify(params).slice(0, 80)));
rpcServer.on('system.multicall', (method, params, callback) => {
updateConnection();
let response = [];
for (let i = 0; i < params[0].length; i++) {
if (methods[params[0][i].methodName]) {
adapter.log.debug(adapter.config.type + ' multicall <' + params[0][i].methodName + '>: ' + params[0][i].params);
response.push(methods[params[0][i].methodName](null, params[0][i].params));
} else {
response.push('');
}
}
callback(null, response);
});
rpcServer.on('system.listMethods', (err, params, callback) => {
if (err) {
adapter.log.warn(' Error on system.listMethods: ' + err);
}
adapter.log.info(adapter.config.type + 'rpc <- system.listMethods ' + JSON.stringify(params));
callback(null, ['event', 'deleteDevices', 'listDevices', 'newDevices', 'system.listMethods', 'system.multicall']);
});
rpcServer.on('event', (err, params, callback) => {
if (err) {
adapter.log.warn(' Error on system.listMethods: ' + err);
}
updateConnection();
try {
callback(null, methods.event(err, params));
} catch (err) {
adapter.log.error('Cannot response on event:' + err);
}
});
rpcServer.on('newDevices', (err, params, callback) => {
if (err) {
adapter.log.warn(' Error on system.listMethods: ' + err);
}
const newDevices = params[1];
adapter.log.info(adapter.config.type + 'rpc <- newDevices ' + newDevices.length);
// for a HmIP-adapter we have to filter out the devices that
// are already present if forceReinit is not set
if (adapter.config.forceReInit === false && adapter.config.daemon === 'HMIP') {
adapter.objects.getObjectView('hm-rpc', 'listDevices', {
startkey: 'hm-rpc.' + adapter.instance + '.',
endkey: 'hm-rpc.' + adapter.instance + '.\u9999'
}, (err, doc) => {
if (doc && doc.rows) {
for (let i = 0; i < doc.rows.length; i++) {
if (doc.rows[i].id === adapter.namespace + '.updated') continue;
// lets get the device description
const val = doc.rows[i].value;
if (typeof val.ADDRESS === 'undefined') continue;
// lets find the current device in the newDevices array
// and if it doesn't exist we can delete it
let index = -1;
for (let j = 0; j < newDevices.length; j++) {
if (newDevices[j].ADDRESS === val.ADDRESS && newDevices[j].VERSION === val.VERSION) {
index = j;
break;
}
}
// if index is -1 than the newDevices doesn't have the
// device with address val.ADDRESS anymore, thus we can delete it
if (index === -1) {
if (val.ADDRESS && !adapter.config.dontDelete) {
if (val.ADDRESS.indexOf(':') !== -1) {
const address = val.ADDRESS.replace(':', '.').replace(FORBIDDEN_CHARS, '_');
const parts = address.split('.');
adapter.deleteChannel(parts[parts.length - 2], parts[parts.length - 1]);
adapter.log.info('obsolete channel ' + address + ' ' + JSON.stringify(address) + ' deleted');
} else {
adapter.deleteDevice(val.ADDRESS);
adapter.log.info('obsolete device ' + val.ADDRESS + ' deleted');
}
}
} else {
// we can remove the item at index because it is already registered
// to ioBroker
newDevices.splice(index, 1);
}
}
}
adapter.log.info('new HmIP devices/channels after filter: ' + newDevices.length);
createDevices(newDevices, callback);
});
} else {
createDevices(newDevices, callback);
}
});
rpcServer.on('listDevices', (err, params, callback) => {
if (err) {
adapter.log.warn(' Error on system.listMethods: ' + err);
}
adapter.log.info(adapter.config.type + 'rpc <- listDevices ' + JSON.stringify(params));
adapter.objects.getObjectView('hm-rpc', 'listDevices', {startkey: 'hm-rpc.' + adapter.instance + '.', endkey: 'hm-rpc.' + adapter.instance + '.\u9999'}, (err, doc) => {
let response = [];
// we only fill the response if this isn't a force reinit and
// if the adapter instance is not bothering with HmIP (which seems to work slightly different in terms of XMLRPC)
if (!adapter.config.forceReInit && adapter.config.daemon !== 'HMIP' && doc && doc.rows) {
for (let i = 0; i < doc.rows.length; i++) {
if (doc.rows[i].id === adapter.namespace + '.updated') continue;
const val = doc.rows[i].value;
/*if (val.PARENT_TYPE) {
channelParams[val.ADDRESS] = val.PARENT_TYPE + '.' + val.TYPE + '.' + val.VERSION;
}*/
if (val.ADDRESS) response.push({ADDRESS: val.ADDRESS, VERSION: val.VERSION});
}
}
adapter.log.info(adapter.config.type + 'rpc -> ' + response.length + ' devices');
//log.info(JSON.stringify(response));
try {
for (let r = response.length - 1; r >= 0; r--) {
if (!response[r].ADDRESS) {
adapter.log.warn(adapter.config.type + 'rpc -> found empty entry at position ' + r + ' !');
response.splice(r, 1);
}
}
callback(null, response);
} catch (err) {
adapter.log.error('Cannot response on listDevices:' + err);
require('fs').writeFileSync(__dirname + '/problem.json', JSON.stringify(response));
}
});
});
rpcServer.on('deleteDevices', (err, params, callback) => {
if (err) {
adapter.log.warn(' Error on system.listMethods: ' + err);
}
adapter.log.info(adapter.config.type + 'rpc <- deleteDevices ' + params[1].length);
for (let i = 0; i < params[1].length; i++) {
if (params[1][i].indexOf(':') !== -1) {
params[1][i] = params[1][i].replace(':', '.').replace(FORBIDDEN_CHARS, '_');
adapter.log.info('channel ' + params[1][i] + ' ' + JSON.stringify(params[1][i]) + ' deleted');
const parts = params[1][i].split('.');
adapter.deleteChannel(parts[parts.length - 2], parts[parts.length - 1]);
} else {
adapter.log.info('device ' + params[1][i] + ' deleted');
adapter.deleteDevice(params[1][i]);
}
}
try {
callback(null, '');
} catch (err) {
adapter.log.error('Cannot response on deleteDevices:' + err);
}
});
});
}
const methods = {
event: function (err, params) {
adapter.log.debug(adapter.config.type + 'rpc <- event ' + JSON.stringify(params));
let val;
// CUxD ignores all prefixes!!
if (params[0] === 'CUxD' || params[0].indexOf(adapter.name) === -1) {
params[0] = adapter.namespace;
}
const channel = params[1].replace(':', '.').replace(FORBIDDEN_CHARS, '_');
const name = params[0] + '.' + channel + '.' + params[2];
if (dpTypes[name]) {
if (dpTypes[name].MIN !== undefined && dpTypes[name].UNIT === '%') {
val = ((parseFloat(params[3]) - dpTypes[name].MIN) / (dpTypes[name].MAX - dpTypes[name].MIN)) * 100;
val = Math.round(val * 100) / 100;
} else if (dpTypes[name].UNIT === '100%' || (dpTypes[name].UNIT === '%' && dpTypes[name].MAX === 1)) {
val = params[3] * 100;
} else {
val = params[3];
}
} else {
val = params[3];
}
adapter.log.debug(name + ' ==> UNIT: "' + (dpTypes[name] ? dpTypes[name].UNIT : 'none') + '" (min: ' + (dpTypes[name] ? dpTypes[name].MIN : 'none') + ', max: ' + (dpTypes[name] ? dpTypes[name].MAX : 'none') + ') From "' + params[3] + '" => "' + val + '"');
adapter.setState(channel + '.' + params[2], {val: val, ack: true});
return '';
}
};
let queueValueParamsets = [];
function addParamsetObjects(channel, paramset, callback) {
let channelChildren = [];
let count = 0;
for (const key in paramset) {
if (!paramset.hasOwnProperty(key)) continue;
channelChildren.push(channel._id + '.' + key);
let commonType = {
ACTION: 'boolean',
BOOL: 'boolean',
FLOAT: 'number',
ENUM: 'number',
INTEGER: 'number',
STRING: 'string'
};
const obj = {
type: 'state',
common: {
def: paramset[key].DEFAULT,
type: commonType[paramset[key].TYPE] || paramset[key].TYPE || '',
read: !!(paramset[key].OPERATIONS & 1),
write: !!(paramset[key].OPERATIONS & 2)
},
native: paramset[key]
};
if (obj.common.type === 'number') {
let i;
obj.common.min = paramset[key].MIN;
obj.common.max = paramset[key].MAX;
if (paramset[key].TYPE === 'ENUM') {
obj.common.states = {};
for (i = 0; i < paramset[key].VALUE_LIST.length; i++) {
obj.common.states[i] = paramset[key].VALUE_LIST[i];
}
}
if (paramset[key].SPECIAL) {
if (!obj.common.states) obj.common.states = {};
for (i = 0; i < paramset[key].SPECIAL.length; i++) {
obj.common.states[paramset[key].SPECIAL[i].VALUE] = paramset[key].SPECIAL[i].ID;
}
}
}
if (paramset[key].STATES) {
obj.common.states = paramset[key].STATES;
}
if (paramset[key].UNIT === '100%') {
obj.common.unit = '%';
obj.common.max = 100 * paramset[key].MAX;
} else if (paramset[key].UNIT !== '') {
obj.common.unit = paramset[key].UNIT;
if (obj.common.unit === '�C' || obj.common.unit === '°C') {
obj.common.unit = '°C';
} else if (obj.common.unit === '�F' || obj.common.unit === '°F') {
obj.common.unit = '°F';
}
}
if (metaRoles.dpCONTROL && metaRoles.dpCONTROL[obj.native.CONTROL]) {
obj.common.role = metaRoles.dpCONTROL[obj.native.CONTROL];
} else if (metaRoles.chTYPE_dpNAME && metaRoles.chTYPE_dpNAME[channel.native.TYPE + '.' + key]) {
obj.common.role = metaRoles.chTYPE_dpNAME[channel.native.TYPE + '.' + key];
} else if (metaRoles.dpNAME && metaRoles.dpNAME[key]) {
obj.common.role = metaRoles.dpNAME[key];
}
if (obj.common.role === 'state' && obj.common.write) {
obj.common.role = 'switch';
}
if (obj.common.role === 'level.color.hue') {
obj.common.max = 200;
}
if (paramset[key].OPERATIONS & 8) {
obj.common.role = 'indicator.service';
}