-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
1646 lines (1492 loc) · 50.7 KB
/
main.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
/**
* comfoair adapter
*/
/* jshint -W097 */ // jshint strict:false
/*jslint node: true */
'use strict';
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
const {
SerialPort
} = require('serialport');
const {
InterByteTimeoutParser
} = require('@serialport/parser-inter-byte-timeout');
var schedule = require('node-schedule');
let adapter;
var deviceIpAdress;
var port;
var net = require('net');
var DelimiterStream = require('delimiter-stream');
var StringDecoder = require('string_decoder').StringDecoder;
var hexout = [];
var buffarr = [];
var buff;
var statcmdimaster = [
[0x07, 0xF0, 0x00, 0xD1, 0x00, 0x7E, 0x07, 0x0F], //Temperaturen
[0x07, 0xF0, 0x00, 0xCD, 0x00, 0x7A, 0x07, 0x0F], //Ventilatorenstati
[0x07, 0xF0, 0x00, 0xDD, 0x00, 0x8A, 0x07, 0x0F], //Betriebsstunden
[0x07, 0xF0, 0x00, 0x0D, 0x00, 0xBA, 0x07, 0x0F], //Status Bypass
[0x07, 0xF0, 0x00, 0xC9, 0x00, 0x76, 0x07, 0x0F], //Verzögerungen (Filterwechsel)
[0x07, 0xF0, 0x00, 0xD9, 0x00, 0x86, 0x07, 0x0F], // Störungen/Filterwechsel
[0x07, 0xF0, 0x00, 0x97, 0x00, 0x44, 0x07, 0x0F] //Werte Enthalpietauscher
];
var statcmdisafe = [
[0x07, 0xF0, 0x00, 0xDD, 0x00, 0x8A, 0x07, 0x0F], //Betriebsstunden
[0x07, 0xF0, 0x00, 0x0D, 0x00, 0xBA, 0x07, 0x0F], //Status Bypass
[0x07, 0xF0, 0x00, 0xC9, 0x00, 0x76, 0x07, 0x0F], //Verzögerungen (Filterwechsel)
[0x07, 0xF0, 0x00, 0xD9, 0x00, 0x86, 0x07, 0x0F] //Störungen/Filterwechsel
];
var setfanstate = [
[0x07, 0xF0, 0x00, 0x99, 0x01, 0x01, 0x48, 0x07, 0x0F], //Stufe abwesend
[0x07, 0xF0, 0x00, 0x99, 0x01, 0x02, 0x49, 0x07, 0x0F], //Stufe niedrig
[0x07, 0xF0, 0x00, 0x99, 0x01, 0x03, 0x4A, 0x07, 0x0F], //Stufe mittel
[0x07, 0xF0, 0x00, 0x99, 0x01, 0x04, 0x4B, 0x07, 0x0F]
];
var setcomfotemp = [0x07, 0xF0, 0x00, 0xD3, 0x01, 0x14, 0x48, 0x07, 0x0F]; //Komforttemperatur setzen
var setreset = [0x07, 0xF0, 0x00, 0xDB, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x0F];
var setvent = [0x07, 0xF0, 0x00, 0xCF, 0x09, 0x0F, 0x28, 0x46, 0x0F, 0x28, 0x46, 0x5A, 0x5A, 0x00, 0x00, 0x07, 0x0F]; //Ventilatorstufen setzen
var setventlevel = ['ABLabw', 'ABL1', 'ABL2', 'ZULabw', 'ZUL1', 'ZUL2', 'ABL3', 'ZUL3'];
var setrs232 = [0x07, 0xF0, 0x00, 0x9B, 0x01, 0x02, 0x4b, 0x07, 0x0F];
var set232PCLogmode = [0x07, 0xF0, 0x00, 0x9B, 0x01, 0x04, 0x4d, 0x07, 0x0F];
var set232PCMaster = [0x07, 0xF0, 0x00, 0x9B, 0x01, 0x03, 0x4c, 0x07, 0x0F];
var setrs232cceaseonly = [0x07, 0xF0, 0x00, 0x9B, 0x01, 0x00, 0x49, 0x07, 0x0F];
var selbsttest = [0x07, 0xF0, 0x00, 0xDB, 0x04, 0x00, 0x00, 0x01, 0x00, 0x8d, 0x07, 0x0F];
var verzoegerungensoll = [0x07, 0xF0, 0x00, 0xCB, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x99, 0x07, 0x0F];
var verzoegerungen = [];
var calli = 0;
var callj = 0
var callval;
var callvals;
var safemode = false;
var cceasemode = false;
var pcmaster;
var testmaster;
var testswi;
var pclogmode = false;
var pcmastermode = false;
var enthalpie = false;
var testj = 0;
var listenonly = false;
var rs232;
var connectionip = true;
var serialdevice = "/dev/ttyUSB0";
var listenonlyserial = false;
var boostlevelold;
var boostrun;
let polling;
function startAdapter(options) {
options = options || {};
Object.assign(options, {
name: 'comfoair'
});
adapter = new utils.Adapter(options);
// when adapter shuts down
adapter.on('unload', function(callback) {
try {
clearInterval(polling);
clearTimeout(polling);
clearTimeout(pcmaster);
clearBoostTimeout();
client.destroy();
adapter.log.info('[END] Stopping comfoair adapter...');
adapter.setState('info.connection', false, true);
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
adapter.log.info('objectChange ' + id + ' ' + JSON.stringify(obj));
});
// is called if a subscribed state changes
adapter.on('stateChange', function(id, state) {
// Warning, state can be null if it was deleted
try {
adapter.log.debug('stateChange ' + id + ' ' + JSON.stringify(state));
//adapter.log.debug("Adapter=" + adapter.toString());
if (!id || state.ack) return; // Ignore acknowledged state changes or error states
id = id.substring(adapter.namespace.length + 1); // remove instance name and id
state = state.val;
adapter.log.debug("id=" + id);
if ((cceasemode == true && id != "control.rs232mode") || listenonly == true) {
adapter.log.warn("CC Ease only - Modus oder Abhör-Modus: Führe keine Befehle aus");
return;
}
if (safemode == true) {
adapter.log.debug("Setze RS232 auf PC Master");
pcmaster = setTimeout(function repe() {
callcomfoair(set232PCMaster);
if (pcmastermode == false) {
setTimeout(repe, 1000);
}
}, 500);
testswi = setInterval(testswitchcommand, 1000, id, state); //Ueberprüfen, ob PC-Master-mode aktiv ist.
} else {
controlcomfoair(id, state);
}
// you can use the ack flag to detect if it is status (true) or command (false)
if (state && !state.ack) {
adapter.log.debug('ack is not set!');
}
} catch (e) {
adapter.log.debug("Fehler Befehlsauswertung: " + e);
}
});
// Some message was sent to adapter instance over message box. Used by email, pushover, text2speech, ...
adapter.on('message', function(obj) {
if (typeof obj === 'object' && obj.message) {
if (obj.command === 'send') {
// e.g. send email or pushover or whatever
adapter.log.debug('send command');
// Send response in callback if required
if (obj.callback) adapter.sendTo(obj.from, obj.command, 'Message received', obj.callback);
}
}
});
// is called when databases are connected and adapter received configuration.
adapter.on('ready', function() {
if (adapter.config.host || adapter.config.serialdevice) {
adapter.log.info('[START] Starting comfoair adapter');
adapter.setState('info.connection', true, true);
main();
} else adapter.log.warn('[START] No IP or device - address set');
});
return adapter;
} // endStartAdapter
function main() {
// Vars
deviceIpAdress = adapter.config.host;
port = adapter.config.port;
serialdevice = adapter.config.serialdevice
var modus = adapter.config.mode;
adapter.log.debug("Modus: " + modus);
if (modus == "adapteronly") {
pcmastermode = true;
listenonly = false;
safemode = false;
pclogmode = false;
} else if (modus == "listen") {
pcmastermode = false;
listenonly = true;
safemode = false;
pclogmode = false;
} else if (modus == "listencontrol") {
pcmastermode = false;
listenonly = false;
safemode = true;
pclogmode = false;
} else if (modus == "logmode") {
pcmastermode = false;
listenonly = false;
safemode = false;
pclogmode = true;
}
adapter.log.debug("Adapteronly: " + pcmastermode + ", Listenonly: " + listenonly + ", Safemode: " + safemode + ", PC - Logmode: " + pclogmode);
connectionip = adapter.config.connectionip;
if (connectionip == "true") {
adapter.log.info("IP-Verbindung auf: " + deviceIpAdress);
} else {
adapter.log.info("Serielle Verbindung auf: " + serialdevice)
}
if (pcmastermode == true || safemode == true || pclogmode == true) {
setpollingobjects();
setcontrolobjects();
}
const pollingTime = adapter.config.pollInterval || 300000;
adapter.log.info('[INFO] Configured polling interval: ' + pollingTime);
if (listenonly == true || safemode == true) {
callcomfoair(setrs232cceaseonly);
listentocomfoair();
}
if (pcmastermode == true) {
pcmaster = setTimeout(function repe() {
callcomfoair(set232PCMaster);
if (pcmastermode == false) {
setTimeout(repe, 1000);
}
}, 500);
testmaster = setInterval(test, 1000); //Ueberprüfen, ob PC-Master-mode aktiv ist.
}
if (pclogmode == true) {
pcmaster = setTimeout(function repe() {
callcomfoair(set232PCLogmode);
if (pclogmode == false) {
setTimeout(repe, 1000);
}
}, 500);
}
if (!polling) {
if (pcmastermode == true || pclogmode == true) {
polling = setTimeout(function repeat() { // poll states every [30] seconds
callval = setInterval(callvaluespcmaster, 2000); //DATAREQUEST;
setTimeout(repeat, pollingTime);
}, pollingTime);
} // endIf
if (safemode == true) {
polling = setTimeout(function repeat() { // poll states every [30] seconds
pcmaster = setTimeout(function repe() {
callcomfoair(set232PCMaster);
if (pcmastermode == false) {
setTimeout(repe, 1000);
}
}, 500);
testmaster = setInterval(testswitchpolling, 1000); //Ueberprüfen, ob PC-Master-mode aktiv ist.
setTimeout(repeat, pollingTime);
}, pollingTime);
} //end polling
}
var wdinterval = '*/' + (Math.floor(pollingTime / 60000) + 5).toString() + ' * * * *';
var watchdog = schedule.scheduleJob(wdinterval, function() {
adapter.log.debug("Prüfe ob Adapter funktioniert (alle " + (Math.floor(pollingTime / 60000) + 5).toString() + " Minuten).");
var dnow = new Date();
var utnow = dnow.getTime();
adapter.log.debug("jetzt: " + utnow);
adapter.getState('temperature.ABL', function(err, state) {
if (state) {
adapter.log.debug("Letze Datenabfrage: " + state.ts);
var tsabl = state.ts;
var tdiff = utnow - tsabl;
adapter.log.debug("Zeit seit letzter Datenabfrage (ms): " + tdiff);
if (tdiff > (2 * pollingTime)) {
adapter.log.warn("Zeit seit letzter Datenabfrage zu gross, NEUSTART");
restartAdapter();
} else {
adapter.log.debug("Adapter läuft ordnungsgemäss.");
}
}
});
});
// all states changes inside the adapters namespace are subscribed
adapter.subscribeStates('*');
} // endMain
function test() {
if (pcmastermode == true) {
adapter.log.debug("CC-Ease ausgeschaltet, Starte Adapterbetrieb");
callval = setInterval(callvaluespcmaster, 2000); //DATAREQUEST;
clearInterval(testmaster);
} else {
adapter.log.debug("Noch nicht im Adapter-only Modus");
testj++;
if (testj > 3) {
adapter.log.warn("Fehler, kann nicht umschalten");
testj = 0;
clearInterval(testmaster);
}
}
} // END test()
function testswitchpolling() {
if (pcmastermode == true) {
adapter.log.debug("CC-Ease ausgeschaltet, Frage Werte ab");
callvals = setInterval(callvaluessafe, 2000); //DATAREQUEST;
clearInterval(testmaster);
} else {
adapter.log.debug("Noch nicht im PC-Master Modus");
callcomfoair(set232PCMaster);
testj++;
if (testj > 3) {
adapter.log.warn("Fehler, kann nicht umschalten");
testj = 0;
clearInterval(testmaster);
}
}
} // END testswitchpolling()
function testswitchcommand(id, state) {
if (pcmastermode == true) {
adapter.log.debug("CC-Ease ausgeschaltet, sende Befehl");
setTimeout(function() {
controlcomfoair(id, state);
}, 500);
clearInterval(testswi);
} else {
adapter.log.warn("Noch nicht im PC-Master Modus");
callcomfoair(set232PCMaster);
testj++;
if (testj > 3) {
adapter.log.warn("Fehler, kann nicht umschalten");
testj = 0;
clearInterval(testswi);
}
}
} // END testswitchcommand()
function callvaluespcmaster() {
if (cceasemode == true) {
return;
}
hexout = statcmdimaster[calli];
adapter.log.debug("callcomfoair mit: " + hexout);
callcomfoair(hexout);
calli++;
if (calli == statcmdimaster.length) {
calli = 0;
clearInterval(callval);
}
} //end callvaluespcmaster
function callvaluessafe() {
if (cceasemode == true) {
return;
}
hexout = statcmdisafe[callj];
adapter.log.debug("callcomfoair mit: " + hexout);
callcomfoair(hexout);
callj++;
if (callj == statcmdisafe.length) {
callj = 0;
setTimeout(function() {
adapter.log.debug("Setze RS232 auf PC Logmodus");
callcomfoair(set232PCLogmode);
setTimeout(function repete() {
callcomfoair(set232PCLogmode);
if (pcmastermode == true) {
setTimeout(repete, 1000);
}
}, 500);
}, 1000);
clearInterval(callvals);
}
} //end callvaluessafe
function controlcomfoair(id, state) {
try {
switch (id) {
case "control.filterwos":
if (verzoegerungensoll == null) {
adapter.log.debug("Verzögerungswerte fehlen, Befehle wird nicht ausgeführt.");
return;
}
for (var i = 0; i < 8; i++) {
verzoegerungensoll[i + 5] = verzoegerungen[i + 7];
}
switch (state) {
case 0:
adapter.log.debug("Setzte Filtertimer auf 10 Wochen");
verzoegerungensoll[9] = 10;
verzoegerungensoll[13] = parseInt(checksumcmd(verzoegerungensoll), 16);
break;
case 1:
adapter.log.debug("Setzte Filtertimer auf 16 Wochen");
verzoegerungensoll[9] = 16;
verzoegerungensoll[13] = parseInt(checksumcmd(verzoegerungensoll), 16);
break;
case 2:
adapter.log.debug("Setzte Filtertimer auf 26 Wochen");
verzoegerungensoll[9] = 26;
verzoegerungensoll[13] = parseInt(checksumcmd(verzoegerungensoll), 16);
break;
}
callcomfoair(verzoegerungensoll);
break;
case "control.selftest":
if (state == true) {
adapter.log.debug("Starte Selbsttest");
callcomfoair(selbsttest);
}
break;
case "control.stufe":
adapter.log.debug("Setzte Stufe: " + state);
callcomfoair(setfanstate[state]);
clearBoostTimeout();
break;
case "control.comforttemp":
adapter.log.debug("Setze Komforttemperatur auf: " + state + "°C");
setcomfotemp[5] = ((state + 20) * 2);
setcomfotemp[6] = parseInt(checksumcmd(setcomfotemp), 16);
callcomfoair(setcomfotemp);
break;
case "control.resetfilterh":
if (state == true) {
adapter.log.debug("Setze Betriebsstunden Filter zurück");
setreset[8] = 1;
setreset = [0x07, 0xF0, 0x00, 0xDB, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0x0F]
setreset[9] = parseInt(checksumcmd(setreset), 16);
callcomfoair(setreset);
setreset = [0x07, 0xF0, 0x00, 0xDB, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x0F]
}
break;
case "control.rs232mode":
switch (state) {
case 0:
var statetext = "CC Ease only";
adapter.log.debug("Umschalten auf CC Ease only");
setrs232[5] = 0;
cceasemode = true;
safemode = false;
pcmastermode = false;
break;
case 1:
var statetext = "Adapter only";
adapter.log.debug("Umschalten auf Adapter only");
setrs232[5] = 3;
cceasemode = false;
safemode = false;
if (pcmastermode == false && safemode == false) {
testswi = setInterval(testswitchcommand, 1000); //Ueberprüfen, ob PC-Master-mode aktiv ist.
}
break;
case 2:
var statetext = "Parallel Mode";
adapter.log.debug("Umschalten auf Parallelbetrieb");
setrs232[5] = 4;
cceasemode = false;
safemode = false;
pcmastermode = false;
adapter.setState('status.rs232mode', 2, true);
break;
case 3:
var statetext = "Auto Switch Mode";
safemode = true;
cceasemode = false;
pcmastermode = false;
adapter.setState('status.rs232mode', 3, true);
break;
}
adapter.log.debug("Setze RS232 - Modus auf: " + statetext);
setrs232[6] = parseInt(checksumcmd(setrs232), 16);
callcomfoair(setrs232);
setrs232 = [0x07, 0xF0, 0x00, 0x9B, 0x01, 0x00, 0x49, 0x07, 0x0F];
break;
case "clearboost":
if (state == true) {
adapter.log.debug("Starte boost");
boost();
} else {
clearBoostTimeout();
}
break;
default:
if (id.slice(0, 15) == "control.setvent") {
var setventl = setventlevel.indexOf(id.slice(16));
if (state != setvent[setventl + 5]) {
adapter.log.debug("Setze Ventilationsstufen");
adapter.log.debug("Aendere: " + id.slice(16) + "Position: " + setventl);
setvent[setventl + 5] = state;
setvent[14] = parseInt(checksumcmd(setvent), 16);
adapter.log.debug("Setvent neu: " + setvent);
adapter.log.debug(id.slice(16) + " neu " + setvent[setventl + 5] + "%");
callcomfoair(setvent);
adapter.setState('control.setvent.ABLabw', setvent[5], true);
adapter.setState('control.setvent.ABL1', setvent[6], true);
adapter.setState('control.setvent.ABL2', setvent[7], true);
adapter.setState('control.setvent.ZULabw', setvent[8], true);
adapter.setState('control.setvent.ZUL2', setvent[10], true);
adapter.setState('control.setvent.ZUL1', setvent[9], true);
adapter.setState('control.setvent.ABL3', setvent[11], true);
adapter.setState('control.setvent.ZUL3', setvent[12], true);
}
} else {
adapter.log.debug("Befehl nicht erkannt");
}
}
if (safemode == true) {
setTimeout(function() {
callvals = setInterval(callvaluessafe, 2000); //DATAREQUEST;
}, 1000);
}
} catch (e) {
adapter.log.debug("Fehler controlcomfoair: " + e);
}
} //end controlcomfoair
function callcomfoair(hexout) {
if (connectionip == "true") { //Abfrage über IP-Verbindung
var client = new net.Socket();
client.connect(port, deviceIpAdress, function() { //Connection Data ComfoAir
adapter.log.debug('Connected by IP');
var msgbuf = new Buffer(hexout);
var hexoutarr = [...msgbuf];
adapter.log.debug("out " + msgbuf.toString('hex'));
adapter.log.debug("outarr: " + hexoutarr);
client.write(msgbuf);
});
client.on('error', function(ex) {
adapter.log.warn("callcomfoair connection error: " + ex);
});
client.on('data', function(data) {
var buff = new Buffer(data, 'utf8');
adapter.log.debug('Received: ' + buff.toString('hex'));
buffarr = [...buff];
adapter.log.debug('Received arr: ' + buffarr);
try {
if (buffarr.length > 3) {
adapter.log.debug("ACK: " + buffarr[0] + ", " + buffarr[1]);
adapter.log.debug("Checksumme aus Datensatz: " + buffarr[buffarr.length - 3]);
adapter.log.debug("Checksumme berechnet: " + parseInt(checksumcmd(buff.slice(2)), 16));
if (buffarr[0] == 7 && buffarr[1] == 243 && buffarr[buffarr.length - 3] == parseInt(checksumcmd(buff.slice(2)), 16)) {
adapter.log.debug("ACK erhalten und Checksumme ok");
readComfoairData(buffarr);
} else {
adapter.log.debug("ACK zu Datenabfrage nicht erhalten oder Checksumme falsch");
}
} else {
if (buff.toString('hex') == "07f3") {
adapter.log.debug("ACK erhalten");
switch (hexout[3]) {
case 153:
adapter.setState('status.statstufe', (hexout[5] - 1), true);
break;
case 211:
adapter.setState('temperature.statcomfort', ((hexout[5] / 2) - 20), true);
adapter.getState('control.comforttemp', function(err, state) {
if (state) {
adapter.setState('control.comforttemp', ((hexout[5] / 2) - 20), true);
}
});
break;
case 219:
if (hexout[5] == 1) {
adapter.log.debug("Störungen zurückgesetzt");
}
if (hexout[6] == 1) {
adapter.log.debug("Einstellungen zurückgesetzt");
}
if (hexout[7] == 1) {
adapter.log.debug("Selbsttest gestartet");
}
if (hexout[8] == 1) {
adapter.log.debug("Betriebsstunden Filter zurückgesetzt");
adapter.setState('status.filterChange', false, true);
adapter.setState('status.filterhinst', 0, true);
adapter.getState('status.filterh', function(err, state) {
if (state) {
adapter.setState('status.filterhlastreset', state.val, true);
}
});
}
break;
case 207:
adapter.setState('status.ventlevel.ABLabw', parseInt(hexout[5]), true);
adapter.setState('status.ventlevel.ABL1', parseInt(hexout[6]), true);
adapter.setState('status.ventlevel.ABL2', parseInt(hexout[7]), true);
adapter.setState('status.ventlevel.ZULabw', parseInt(hexout[8]), true);
adapter.setState('status.ventlevel.ZUL2', parseInt(hexout[10]), true);
adapter.setState('status.ventlevel.ZUL1', parseInt(hexout[9]), true);
adapter.setState('status.ventlevel.ABL3', parseInt(hexout[11]), true);
adapter.setState('status.ventlevel.ZUL3', parseInt(hexout[12]), true);
adapter.log.debug("Ventilationsstufen gesetzt");
}
} else {
adapter.log.debug("ACK zu Kommando nicht erhlaten");
}
}
} catch (e) {
adapter.log.warn("Client-Data - Fehler" + e);
}
client.destroy();
});
client.on('close', function() {
adapter.log.debug('Connection closed');
});
} else { //Abfrage über serielle Verbindung
var msgbuf = Buffer.from(hexout);
const port = new SerialPort({
path: serialdevice,
baudRate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
autoOpen: false,
flowControl: false
});
const parser = port.pipe(new InterByteTimeoutParser({
interval: 20
}));
port.open(function(err) {
if (err) {
adapter.log.info('Error opening port: ' + err.message);
return adapter.log.info('Error opening port: ', err.message);
}
})
// The open event is always emitted
port.on('open', function() {
adapter.log.debug('Connected to serial port');
parser.on('data', function(data) {
var buff = Buffer.from(data);
adapter.log.debug('Data received (hex): ' + buff.toString('hex'));
var buffarr = [...buff];
adapter.log.debug('Received arr: ' + buffarr);
try {
if (buffarr.length > 3) {
adapter.log.debug("ACK: " + buffarr[0] + ", " + buffarr[1]);
adapter.log.debug("Checksumme aus Datensatz: " + buffarr[buffarr.length - 3]);
adapter.log.debug("Checksumme berechnet: " + parseInt(checksumcmd(buff.slice(2)), 16));
if (buffarr[0] == 7 && buffarr[1] == 243 && buffarr[buffarr.length - 3] == parseInt(checksumcmd(buff.slice(2)), 16)) {
adapter.log.debug("ACK erhalten und Checksumme ok");
readComfoairData(buffarr);
} else {
adapter.log.debug("ACK zu Datenabfrage nicht erhalten oder Checksumme falsch");
}
} else {
if (buff.toString('hex') == "07f3") {
adapter.log.debug("ACK erhalten");
switch (hexout[3]) {
case 153:
adapter.setState('status.statstufe', (hexout[5] - 1), true);
break;
case 211:
adapter.setState('temperature.statcomfort', ((hexout[5] / 2) - 20), true);
break;
case 219:
if (hexout[5] == 1) {
adapter.log.debug("Störungen zurückgesetzt");
}
if (hexout[6] == 1) {
adapter.log.debug("Einstellungen zurückgesetzt");
}
if (hexout[7] == 1) {
adapter.log.debug("Selbsttest gestartet");
}
if (hexout[8] == 1) {
adapter.log.debug("Betriebsstunden Filter zurückgesetzt");
adapter.setState('status.filterChange', false, true);
}
break;
case 207:
adapter.setState('status.ventlevel.ABLabw', parseInt(hexout[5]), true);
adapter.setState('status.ventlevel.ABL1', parseInt(hexout[6]), true);
adapter.setState('status.ventlevel.ABL2', parseInt(hexout[7]), true);
adapter.setState('status.ventlevel.ZULabw', parseInt(hexout[8]), true);
adapter.setState('status.ventlevel.ZUL2', parseInt(hexout[10]), true);
adapter.setState('status.ventlevel.ZUL1', parseInt(hexout[9]), true);
adapter.setState('status.ventlevel.ABL3', parseInt(hexout[11]), true);
adapter.setState('status.ventlevel.ZUL3', parseInt(hexout[12]), true);
adapter.log.debug("Ventilationsstufen gesetzt");
}
} else {
adapter.log.debug("ACK zu Kommando nicht erhlaten");
}
}
} catch (e) {
adapter.log.warn("Client-Data - Fehler" + e);
}
});
if (listenonlyserial == false) {
setTimeout(function() {
port.write(msgbuf, function(err, result) {
if (err) {
return adapter.log.info('Error on write: ', err.message);
}
//adapter.log.info("Result: " + result.message);
adapter.log.debug('Daten sent: ' + msgbuf.toString('hex'));
var hexoutarr = [...msgbuf];
adapter.log.debug("outarr: " + hexoutarr);
});
setTimeout(function() {
port.close();
}, 500);
}, 500);
}
});
port.on('error', function(err) {
adapter.log.info('Error2: ', err.message);
port.close();
});
port.on('close', function() {
adapter.log.debug("serial port closed");
});
}
} //end callcomfoair
function listentocomfoair() {
if (connectionip == "true") { // Zuhören über IP - Verbindung
var decoder = new StringDecoder('utf8');
var delimiterstream = new DelimiterStream({
delimiter: '7,243,'
});
delimiterstream.on('data', function(chunk) {
var chunkarr = decoder.write(chunk).split(',');
if (chunkarr[3] > 65) {
chunkarr.splice(0, 0, "7", "243");
if (pcmastermode == false) {
adapter.log.debug("an readcomfoair: " + chunkarr);
readComfoairData(chunkarr);
}
}
});
var client = new net.Socket();
var client = client.connect(port, deviceIpAdress, function() {
// write out connection details
adapter.log.debug('Connected to comfoair by IP');
});
client.on('data', function(data) {
var buff = new Buffer(data, 'utf8');
//adapter.log.debug('Received: ' + buff.toString('hex'));
buffarr = [...buff];
delimiterstream.write(buffarr.toString('hex'));
});
client.on('close', function() {
adapter.log.debug("Connection closed");
});
} else { // Zuhören über serielle Verbindung
setTimeout(function() {
listenonlyserial = true;
callcomfoair(hexout)
}, 10000);
}
} // end listentocomfoair()
function readComfoairData(buffarr) {
try {
adapter.log.debug("Verarbeite Daten");
var cmd = parseInt(buffarr[5]);
switch (cmd) {
case 210:
// listener & polling
adapter.log.debug(cmd + " : lese Temperaturwerte");
adapter.setState('temperature.statcomfort', ((buffarr[7] / 2) - 20), true);
adapter.getState('control.comforttemp', function(err, state) {
if (state) {
adapter.setState('control.comforttemp', ((buffarr[7] / 2) - 20), true);
}
});
adapter.setState('temperature.AUL', ((buffarr[8] / 2) - 20), true);
adapter.setState('temperature.ZUL', ((buffarr[9] / 2) - 20), true);
adapter.setState('temperature.ABL', ((buffarr[10] / 2) - 20), true);
adapter.setState('temperature.FOL', ((buffarr[11] / 2) - 20), true);
adapter.setState('temperature.EWT', ((buffarr[13] / 2) - 20), true);
break;
case 206:
// listener & polling
adapter.log.debug(cmd + " : lese Ventilatorstatus");
adapter.setState('status.ventlevel.ABLabw', parseInt(buffarr[7]), true);
adapter.setState('status.ventlevel.ABL1', parseInt(buffarr[8]), true);
adapter.setState('status.ventlevel.ABL2', parseInt(buffarr[9]), true);
adapter.setState('status.ventlevel.ZULabw', parseInt(buffarr[10]), true);
adapter.setState('status.ventlevel.ZUL1', parseInt(buffarr[11]), true);
adapter.setState('status.ventlevel.ZUL2', parseInt(buffarr[12]), true);
adapter.setState('status.ventABL', parseInt(buffarr[13]), true);
adapter.setState('status.ventZUL', parseInt(buffarr[14]), true);
adapter.setState('status.statstufe', parseInt((buffarr[15] - 1)), true);
adapter.setState('status.ventlevel.ABL3', parseInt(buffarr[17]), true);
adapter.setState('status.ventlevel.ZUL3', parseInt(buffarr[18]), true);
adapter.getState('control.stufe', function(err, state) {
if (state) {
adapter.setState('control.stufe', parseInt((buffarr[15] - 1)), true);
}
});
for (var i = 5; i < 11; i++) {
setvent[i] = buffarr[i + 2];
}
setvent[11] = buffarr[17];
setvent[12] = buffarr[18];
break;
case 222:
//polling
adapter.log.debug(cmd + " : lese Betriebsstunden");
adapter.setState('status.hLVLabw', parseInt((buffarr[7]) * 256 + buffarr[8] * 256 + buffarr[9]), true);
adapter.setState('status.hLVL1', parseInt(buffarr[10] * 256 + buffarr[11] * 256 + buffarr[12]), true);
// adapter.setState('status.hLVL1', parseInt((buffarr[10].toString(16).padStart(2, '0') + buffarr[11].toString(16).padStart(2, '0') + buffarr[12].toString(16).padStart(2, '0')), 16), true);
adapter.setState('status.hLVL2', parseInt(buffarr[13] * 256 + buffarr[14] * 256 + buffarr[15]), true);
//adapter.setState('status.hLVL2', parseInt((buffarr[13].toString(16).padStart(2, '0') + buffarr[14].toString(16).padStart(2, '0') + buffarr[15].toString(16).padStart(2, '0')), 16), true);
adapter.setState('status.hBypO', parseInt(buffarr[20] * 256 + buffarr[21]), true);
//adapter.setState('status.hBypO', parseInt((buffarr[20].toString(16).padStart(2, '0') + buffarr[21].toString(16).padStart(2, '0')), 16), true);
adapter.setState('status.hFrostS', parseInt(buffarr[16] * 256 + buffarr[17]), true);
//adapter.setState('status.hFrostS', parseInt((buffarr[16].toString(16).padStart(2, '0') + buffarr[17].toString(16).padStart(2, '0')), 16), true);
adapter.setState('status.hVorh', parseInt(buffarr[18] * 256 + buffarr[19]), true);
// adapter.setState('status.hVorh', parseInt((buffarr[18].toString(16).padStart(2, '0') + buffarr[19].toString(16).padStart(2, '0')), 16), true);
adapter.setState('status.hLVL3', parseInt(buffarr[24] * 256 + buffarr[25] * 256 + buffarr[26]), true);
//adapter.setState('status.hLVL3', parseInt((buffarr[24].toString(16).padStart(2, '0') + buffarr[25].toString(16).padStart(2, '0') + buffarr[26].toString(16).padStart(2, '0')), 16), true);
//adapter.setState('status.filterh', parseInt((buffarr[22].toString(16).padStart(2, '0') + buffarr[23].toString(16).padStart(2, '0')), 16), true);
adapter.setState('status.filterh', parseInt(buffarr[22]) * 256 + parseInt(buffarr[23]), true);
adapter.getState('status.filterhlastreset', function(err, state) {
if (state) {
if (parseInt(buffarr[22]) * 256 + parseInt(buffarr[23]) == 0) {
adapter.setState('status.filterhinst', 0, true);
} else {
adapter.setState('status.filterhinst', parseInt(buffarr[22]) * 256 + parseInt(buffarr[23]) - state.val, true);
}
}
});
//7,243,7,240,0,222,20,0,22,194,0,49,240,0,19,49,0,76,0,0,33,142,59,159,0,10,60,247,7,15
break;
case 14:
//polling
adapter.log.debug(cmd + " : lese Status Bypass");
adapter.setState('status.bypass', parseInt(buffarr[7]), true);
break;
case 224:
// listener
adapter.log.debug(cmd + " : lese Status Bypass 224");
adapter.setState('status.bypass', parseInt(buffarr[10]), true);
break;
case 202:
//polling
verzoegerungen = buffarr;
adapter.setState("status.filterw", parseInt(buffarr[11]), true);
break;
case 218:
//polling
adapter.log.debug(cmd + ": lese Störungsmeldungen");
adapter.setState("status.filterChange", !!+buffarr[15], true);
adapter.setState("status.errors.aktuellA", 'A' + errorcode(buffarr[7]), true);
adapter.setState("status.errors.aktuellE", 'E' + errorcode(buffarr[8]), true);
adapter.setState("status.errors.letzterA", 'A' + errorcode(buffarr[9]), true);
adapter.setState("status.errors.letzterE", 'E' + errorcode(buffarr[10]), true);
adapter.setState("status.errors.vorletzterA", 'A' + errorcode(buffarr[11]), true);
adapter.setState("status.errors.vorletzterE", 'E' + errorcode(buffarr[12]), true);
adapter.setState("status.errors.aktuellEA", 'EA' + errorcode(buffarr[16]), true);
adapter.setState("status.errors.letzterEA", 'EA' + errorcode(buffarr[17]), true);
adapter.setState("status.errors.vorletzterEA", 'EA' + errorcode(buffarr[18]), true);
break;
case 156:
// listener & polling
adapter.log.debug(cmd + ": lese rs232-Modus");
adapter.log.debug("Modus - Code = " + parseInt(buffarr[7]));
switch (parseInt(buffarr[7])) {
case 1:
var statetext = "PC only";
rs232 = 1;
if (safemode == true) {
adapter.log.debug("Switch to Master/PConly");
} else {
adapter.log.debug("CC-Ease ausgeschaltet");
}
pcmastermode = true;
adapter.setState('status.rs232mode', 1, true);
adapter.getState('control.rs232mode', function(err, state) {
if (state) {
adapter.setState('control.rs232mode', 1, true);
}
});
break;
case 2:
var statetext = "CC-Ease only";
rs232 = 0;
adapter.log.debug("CC-Ease only Modus: keine Aktualisierung / Befehle durch Adapter");
adapter.setState('status.rs232mode', 0, true);
adapter.getState('control.rs232mode', function(err, state) {
if (state) {
adapter.setState('control.rs232mode', 0, true);
}
});
break;
case 3:
var statetext = "PC Master";
rs232 = 1;
if (safemode == true) {
adapter.log.debug("Switch to Master");
} else {
adapter.log.debug("CC-Ease ausgeschaltet");
}
pcmastermode = true;
adapter.setState('status.rs232mode', 1, true);
adapter.getState('control.rs232mode', function(err, state) {
if (state) {
adapter.setState('control.rs232mode', 1, true);
}
});
break;
case 4:
pclogmode = true;
rs232 = 2;
if (safemode == true) {
adapter.log.debug("Switch to Logmode");
pcmastermode = false;
} else {
var statetext = "PC Logmode";