forked from foxthefox/ioBroker.fritzdect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
1982 lines (1841 loc) · 99.2 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
/*jshint -W097 */// jshint strict:false
/*jslint node: true */
"use strict";
var Fritz = require('./lib/fritzhttp.js').Fritz,
parser = require('xml2json-light');
// you have to require the utils module and call adapter function
var utils = require('@iobroker/adapter-core'); // Get common adapter utils
// you have to call the adapter function and pass a options object
// name has to be set and has to be equal to adapters folder name and main file name excluding extension
// adapter will be restarted automatically every time as the configuration changed, e.g system.adapter.template.0
var fritzTimeout;
/* errorcodes hkr
0: kein Fehler
1: Keine Adaptierung möglich. Gerät korrekt am Heizkörper montiert?
2: Ventilhub zu kurz oder Batterieleistung zu schwach. Ventilstößel per Hand mehrmals öfnen und schließen oder neue Batterien einsetzen.
3: Keine Ventilbewegung möglich. Ventilstößel frei?
4: Die Installation wird gerade vorbereitet.
5: Der Heizkörperregler ist im Installationsmodus und kann auf das Heizungsventil montiert werden.
6: Der Heizkörperregler passt sich nun an den Hub des Heizungsventils an.
*/
/* HANFUN unittypes
256 = SIMPLE_ON_OFF_SWITCHABLE
257 = SIMPLE_ON_OFF_SWITCH
262 = AC_OUTLET
263 = AC_OUTLET_SIMPLE_POWER_METERING
264 = SIMPLE_LIGHT 265 = DIMMABLE_LIGHT
266 = DIMMER_SWITCH
273 = SIMPLE_BUTTON
277 = COLOR_BULB
278 = DIMMABLE_COLOR_BULB
281 = BLIND
282 = LAMELLAR
512 = SIMPLE_DETECTOR
513 = DOOR_OPEN_CLOSE_DETECTOR
514 = WINDOW_OPEN_CLOSE_DETECTOR
515 = MOTION_DETECTOR
518 = FLOOD_DETECTOR
519 = GLAS_BREAK_DETECTOR
520 = VIBRATION_DETECTOR
640 = SIREN
*/
/* HANFUN interfaces
256 = ALERT
277 = KEEP_ALIVE
512 = ON_OFF
513 = LEVEL_CTRL
514 = COLOR_CTRL
772 = SIMPLE_BUTTON
1024 = SUOTA-Update
*/
/* modes of DECT500 supported/color_mode
0 = nothing, because OFF or not present
1 = HueSaturation-Mode
2 =
3 =
4 = Colortemperature-Mode
5 =
*/
let adapter;
function startAdapter(options) {
options = options || {};
Object.assign(options, {
name: 'fritzdect',
// is called when adapter shuts down - callback has to be called under any circumstances!
unload: function (callback) {
if (fritzTimeout) clearTimeout(fritzTimeout);
try {
adapter.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
},
// is called if a subscribed object changes
objectChange: function (id, obj) {
// Warning, obj can be null if it was deleted
adapter.log.debug('objectChange ' + id + ' ' + JSON.stringify(obj));
},
// is called if a subscribed state changes
stateChange: function (id, state) {
// Warning, state can be null if it was deleted
adapter.log.debug('stateChange ' + id + ' ' + JSON.stringify(state));
var username = adapter.config.fritz_user;
var password = adapter.config.fritz_pw;
var moreParam = adapter.config.fritz_ip;
var fritz = new Fritz(username, password||"", moreParam||"");
// 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! -> command');
var tmp = id.split('.');
var dp = tmp.pop(); //should always be "state"
var idx = tmp.pop(); //is the name after fritzdect.x.
if (idx.startsWith("Comet_")){ //must be comet
id = idx.replace(/Comet_/g,''); //Thermostat
adapter.log.info('Comet ID: '+ id + ' identified for command (' + dp + ') : ' + state.val);
if (dp === 'targettemp'){
if (state.val < 8) { //kann gelöscht werden, wenn Temperaturvorwahl nicht zur Moduswahl benutzt werden soll
adapter.setState('Comet_'+ id +'.mode', {val: 1, ack: false});
fritz.setTempTarget(id, 'off').then(function (sid) {
adapter.log.debug('Switched Mode' + id + ' to closed');
})
.catch(errorHandler);
} else if (state.val > 28) { //kann gelöscht werden, wenn Temperaturvorwahl nicht zur Moduswahl benutzt werden soll
adapter.setState('Comet_'+ id +'.mode', {val: 2, ack: false});
fritz.setTempTarget(id, 'on').then(function (sid) {
adapter.log.debug('Switched Mode' + id + ' to opened permanently');
})
.catch(errorHandler);
} else {
adapter.setState('Comet_'+ id +'.mode', {val: 0, ack: false});
fritz.setTempTarget(id, state.val).then(function (sid) {
adapter.log.debug('Set target temp ' + id + state.val +' °C');
adapter.setState('Comet_'+ id +'.lasttarget', {val: state.val, ack: true}); //iobroker Tempwahl wird zum letzten Wert gespeichert
adapter.setState('Comet_'+ id +'.targettemp', {val: state.val, ack: true}); //iobroker Tempwahl wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
} else if (dp === 'mode') {
if (state.val === 0) {
adapter.getState('Comet_' + id + '.targettemp', function (err, targettemp) { // oder hier die Verwendung von lasttarget
if(targettemp.val){
var setTemp = targettemp.val;
if (setTemp < 8) {
adapter.setState('Comet_' + id + '.targettemp', {val: 8, ack:true});
setTemp = 8;
} else if (setTemp > 28) {
adapter.setState('Comet_' + id + '.targettemp', {val: 28, ack:true});
setTemp = 28;
}
fritz.setTempTarget(id, setTemp).then(function (sid) {
adapter.log.debug('Set target temp ' + id + ' ' + setTemp +' °C');
adapter.setState('Comet_'+ id +'.targettemp', {val: setTemp, ack: true}); //iobroker Tempwahl wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
else{adapter-log.error('no data in targettemp for setting mode')}
});
} else if (state.val === 1) {
fritz.setTempTarget(id, 'off').then(function (sid) {
adapter.log.debug('Switched Mode' + id + ' to closed.');
})
.catch(errorHandler);
} else if (state.val === 2) {
fritz.setTempTarget(id, 'on').then(function (sid) {
adapter.log.debug('Switched Mode' + id + ' to opened permanently');
})
.catch(errorHandler);
}
}
}
else if (idx.startsWith("Hgroup_")){ //must be comet group
id = idx.replace(/Hgroup_/g,''); //Thermostat
adapter.log.info('HGROUP ID: '+ id + ' identified for command (' + dp + ') : ' + state.val);
if (dp === 'targettemp'){
if (state.val < 8) { //kann gelöscht werden, wenn Temperaturvorwahl nicht zur Moduswahl benutzt werden soll
adapter.setState('Hgroup_'+ id +'.mode', {val: 1, ack: false});
fritz.setTempTarget(id, 'off').then(function (sid) {
adapter.log.debug('Switched Mode' + id + ' to closed');
})
.catch(errorHandler);
} else if (state.val > 28) { //kann gelöscht werden, wenn Temperaturvorwahl nicht zur Moduswahl benutzt werden soll
adapter.setState('Hgroup_'+ id +'.mode', {val: 2, ack: false});
fritz.setTempTarget(id, 'on').then(function (sid) {
adapter.log.debug('Switched Mode' + id + ' to opened permanently');
})
.catch(errorHandler);
} else {
adapter.setState('Hgroup_'+ id +'.mode', {val: 0, ack: false});
fritz.setTempTarget(id, state.val).then(function (sid) {
adapter.log.debug('Set Hgroup target temp ' + id + state.val +' °C');
adapter.setState('Hgroup_'+ id +'.lasttarget', {val: state.val, ack: true}); //iobroker Tempwahl wird zum letzten Wert gespeichert
adapter.setState('Hgroup_'+ id +'.targettemp', {val: state.val, ack: true}); //iobroker Tempwahl wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
} else if (dp === 'mode') {
if (state.val === 0) {
adapter.getState('Hgroup_' + id + '.targettemp', function (err, targettemp) { // oder hier die Verwendung von lasttarget
var setTemp = targettemp.val;
if (setTemp < 8) {
adapter.setState('Hgroup_' + id + '.targettemp', {val: 8, ack:true});
setTemp = 8;
} else if (setTemp > 28) {
adapter.setState('Hgroup_' + id + '.targettemp', {val: 28, ack:true});
setTemp = 28;
}
fritz.setTempTarget(id, setTemp).then(function (sid) {
adapter.log.debug('Set Hgroup target temp ' + id + ' ' + setTemp +' °C');
//hier noch ack=true in state?
})
.catch(errorHandler);
});
} else if (state.val === 1) {
fritz.setTempTarget(id, 'off').then(function (sid) {
adapter.log.debug('Switched Hgroup Mode' + id + ' to closed.');
})
.catch(errorHandler);
} else if (state.val === 2) {
fritz.setTempTarget(id, 'on').then(function (sid) {
adapter.log.debug('Switched Hgroup Mode' + id + ' to opened permanently');
})
.catch(errorHandler);
}
}
}
else if (idx.startsWith("DECT200_")) { //must be DECT
id = idx.replace(/DECT200_/g,''); //Switch
adapter.log.info('SWITCH ID: '+ id + ' identified for command (' + dp + ') : ' + state.val);
if (dp == 'state') {
if (state.val === 0 || state.val === '0' || state.val === 'false' || state.val === false || state.val === 'off' || state.val === 'OFF') {
fritz.setSwitchOff(id).then(function (sid) {
adapter.log.debug('Turned switch ' + id + ' off');
adapter.setState('DECT200_'+ id +'.state', {val: false, ack: true}); //iobroker State-Bedienung wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
else if (state.val === 1 || state.val === '1' || state.val === 'true' || state.val === true || state.val === 'on' || state.val === 'ON') {
fritz.setSwitchOn(id).then(function (sid) {
adapter.log.debug('Turned switch ' + id + ' on');
adapter.setState('DECT200_'+ id +'.state', {val: true, ack: true}); //iobroker State-Bedienung wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
}
}
else if (idx.startsWith("Sgroup_")) { //must be DECT switch group
id = idx.replace(/Sgroup_/g,''); //Switch
adapter.log.info('GROUP ID: '+ id + ' identified for command (' + dp + ') : ' + state.val);
if (dp == 'state') {
if (state.val === 0 || state.val === '0' || state.val === 'false' || state.val === false || state.val === 'off' || state.val === 'OFF') {
fritz.setSwitchOff(id).then(function (sid) {
adapter.log.debug('Turned group ' + id + ' off');
adapter.setState('Sgroup_'+ id +'.state', {val: false, ack: true}); //iobroker State-Bedienung wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
else if (state.val === 1 || state.val === '1' || state.val === 'true' || state.val === true || state.val === 'on' || state.val === 'ON') {
fritz.setSwitchOn(id).then(function (sid) {
adapter.log.debug('Turned group ' + id + ' on');
adapter.setState('Sgroup_'+ id +'.state', {val: true, ack: true}); //iobroker State-Bedienung wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
}
}
else if (idx.startsWith("template_")) { //must be fritzbox template
id = idx.replace(/template_/g,''); //template
adapter.log.info('Template ID: '+ id + ' identified for command (' + dp + ') : ' + state.val);
if (dp == 'toggle') {
/**
if (state.val === 0 || state.val === '0' || state.val === 'false' || state.val === false || state.val === 'off' || state.val === 'OFF') {
fritz.applyTemplate(id).then(function (sid) {
adapter.log.debug('cmd Toggle to template ' + id + ' off');
})
.catch(errorHandler);
}
*/
if (state.val === 1 || state.val === '1' || state.val === 'true' || state.val === true || state.val === 'on' || state.val === 'ON') {
fritz.applyTemplate(id).then(function (sid) {
adapter.log.debug('cmd Toggle to template ' + id + ' on');
adapter.log.debug('response ' + sid);
adapter.setState('template.lasttemplate',{val: sid, ack: true}); //when successfull toggle, the API returns the id of the template
})
.catch(errorHandler);
}
}
}
else if (idx.startsWith("DECT500_")) { //must be DECT500
id = idx.replace(/DECT500_/g,''); //Lamp
adapter.log.info('LAMP ID: '+ id + ' identified for command (' + dp + ') : ' + state.val);
if (dp == 'state') {
if (state.val === 0 || state.val === '0' || state.val === 'false' || state.val === false || state.val === 'off' || state.val === 'OFF') {
fritz.setSimpleOff(id).then(function (sid) {
adapter.log.debug('Turned lamp ' + id + ' off');
adapter.setState('DECT500_'+ id +'.state', {val: false, ack: true}); //iobroker State-Bedienung wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
else if (state.val === 1 || state.val === '1' || state.val === 'true' || state.val === true || state.val === 'on' || state.val === 'ON') {
fritz.setSimpleOn(id).then(function (sid) {
adapter.log.debug('Turned lamp ' + id + ' on');
adapter.setState('DECT500_'+ id +'.state', {val: true, ack: true}); //iobroker State-Bedienung wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
}
if (dp == 'level') {
fritz.setLevel(id, state.val ).then(function (sid) {
adapter.log.debug('Set lamp level' + id + ' to '+ state.val);
adapter.setState('DECT500_'+ id +'.level', {val: state.val, ack: true}); //iobroker State-Bedienung wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
if (dp == 'levelpercentage') {
fritz.setLevel(id, parseInt(state.val/100*255) ).then(function (sid) {//level is in 0...255
adapter.log.debug('Set lamp level %' + id + ' to '+ state.val);
adapter.setState('DECT500_'+ id +'.levelpercentage', {val: state.val, ack: true}); //iobroker State-Bedienung wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
if (dp == 'hue') {
let typ = 'hue';
fritz.setColor(id, typ, state.val).then(function (sid) {
adapter.log.debug('Set lamp color' + id + ' to '+ state.val);
adapter.setState('DECT500_'+ id +'.hue', {val: state.val, ack: true}); //iobroker State-Bedienung wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
if (dp == 'saturation') {
let typ = 'saturation';
fritz.setColor(id, typ, state.val).then(function (sid) {
adapter.log.debug('Set lamp color saturation ' + id + ' to '+ state.val);
adapter.setState('DECT500_'+ id +'.saturation', {val: state.val, ack: true}); //iobroker State-Bedienung wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
if (dp == 'temperature') {
let typ = 'saturation';
fritz.setColorTemperature(id, state.val).then(function (sid) {
adapter.log.debug('Set lamp color temperature ' + id + ' to '+ state.val);
adapter.setState('DECT500_'+ id +'.temperature', {val: state.val, ack: true}); //iobroker State-Bedienung wird nochmal als Status geschrieben, da API-Aufruf erfolgreich
})
.catch(errorHandler);
}
}
} //from if state&ack
},
// Some message was sent to adapter instance over message box. Used by email, pushover, text2speech, ...
message: function (obj) {
var wait = false;
// handle the message
if (obj) {
switch (obj.command) {
case 'devices':
var result = [];
var username = adapter.config.fritz_user;
var password = adapter.config.fritz_pw;
var moreParam = adapter.config.fritz_ip;
var fritz = new Fritz(username, password||"", moreParam||"");
fritz.getDeviceListInfos().then(function(devicelistinfos) {
var devices = parser.xml2json(devicelistinfos);
devices = [].concat((devices.devicelist || {}).device || []).map(function(device) {
// remove spaces in AINs
device.identifier = device.identifier.replace(/\s/g, '');
return device;
});
result = devices;
}).done(function (devicelistinfos){
if (obj.callback) adapter.sendTo(obj.from, obj.command, result, obj.callback);
});
wait = true;
break;
case 'groups':
var result = [];
var username = adapter.config.fritz_user;
var password = adapter.config.fritz_pw;
var moreParam = adapter.config.fritz_ip;
var fritz = new Fritz(username, password||"", moreParam||"");
fritz.getDeviceListInfos().then(function(devicelistinfos) {
var groups = parser.xml2json(devicelistinfos);
groups = [].concat((groups.devicelist || {}).group || []).map(function(group) {
// remove spaces in AINs
group.identifier = group.identifier.replace(/\s/g, '');
return group;
});
result = groups;
}).done(function (devicelistinfos){
if (obj.callback) adapter.sendTo(obj.from, obj.command, result, obj.callback);
});
wait = true;
break;
case 'templates':
var result = [];
var username = adapter.config.fritz_user;
var password = adapter.config.fritz_pw;
var moreParam = adapter.config.fritz_ip;
var fritz = new Fritz(username, password||"", moreParam||"");
fritz.getTemplateListInfos().then(function(templatelistinfos) {
var templates = parser.xml2json(templatelistinfos);
templates = [].concat((templates.templatelist || {}).template || []).map(function(template) {
// remove spaces in AINs
// template.identifier = group.identifier.replace(/\s/g, '');
return template;
});
result = templates;
}).done(function (devicelistinfos){
if (obj.callback) adapter.sendTo(obj.from, obj.command, result, obj.callback);
});
wait = true;
break;
case 'statistic':
var result = [];
var username = adapter.config.fritz_user;
var password = adapter.config.fritz_pw;
var moreParam = adapter.config.fritz_ip;
var fritz = new Fritz(username, password||"", moreParam||"");
fritz.getBasicDeviceStats(obj.message).then(function(statisticinfos) { //obj.message should be ain of device requested
var devicestats = parser.xml2json(statisticinfos);
result = devicestats;
}).done(function (statisticinfos){
if (obj.callback) adapter.sendTo(obj.from, obj.command, result, obj.callback);
});
wait = true;
break;
case 'color':
var result = [];
var username = adapter.config.fritz_user;
var password = adapter.config.fritz_pw;
var moreParam = adapter.config.fritz_ip;
var fritz = new Fritz(username, password||"", moreParam||"");
fritz.getColorDefaults(obj.message).then(function(statisticinfos) { //obj.message should be ain of device requested
var devicestats = parser.xml2json(colorinfos);
result = devicestats;
}).done(function (colorinfos){
if (obj.callback) adapter.sendTo(obj.from, obj.command, result, obj.callback);
});
wait = true;
break;
//idea for other statistics: call of message returns everything (loop over all devices)
default:
adapter.log.warn("Received unhandled message: " + obj.command);
break;
}
}
if (!wait && obj.callback) {
adapter.sendTo(obj.from, obj.command, obj.message, obj.callback);
}
return true;
},
// is called when databases are connected and adapter received configuration.
// start here!
ready: function () {
adapter.log.info('entered ready');
adapter.getForeignObject('system.config', (err, obj) => {
if (obj && obj.native && obj.native.secret) {
//noinspection JSUnresolvedVariable
adapter.config.fritz_pw = decrypt(obj.native.secret, adapter.config.fritz_pw);
} else {
//noinspection JSUnresolvedVariable
adapter.config.fritz_pw = decrypt('Zgfr56gFe87jJOM', adapter.config.fritz_pw);
}
main();
});
}
});
adapter = new utils.Adapter(options);
return adapter;
};
function decrypt(key, value) {
let result = '';
for (let i = 0; i < value.length; ++i) {
result += String.fromCharCode(key[i % key.length].charCodeAt(0) ^ value.charCodeAt(i));
}
return result;
}
function errorHandler(error) {
adapter.log.error('fritzbox returned this '+JSON.stringify(error));
if (error == "0000000000000000"){
adapter.log.error("Did not get session id- invalid username or password?");
}
else if (!error.response){
adapter.log.error('no response part in returned message');
}
else if (error.response.statusCode){
if (error.response.statusCode == 403){
adapter.log.error("no permission for this call (403), has user all the rights and access to fritzbox?")
adapter.log.error('error calling the fritzbox '+JSON.stringify(error));
}
else if (error.response.statusCode == 404){
adapter.log.error("call to API does not exist! (404)");
adapter.log.error('error calling the fritzbox '+JSON.stringify(error));
}
else if (error.response.statusCode == 400){
adapter.log.error("bad request (400), ain correct?");
adapter.log.error('error calling the fritzbox '+JSON.stringify(error));
}
else if (error.response.statusCode == 500){
adapter.log.error("internal fritzbox error");
adapter.log.error('error calling the fritzbox '+JSON.stringify(error));
}
else if (error.response.statusCode == 503){
adapter.log.error("service unavailable");
adapter.log.error('error calling the fritzbox '+JSON.stringify(error));
}
else{
adapter.log.error("statuscode not in errorhandler");
adapter.log.error('error calling the fritzbox '+JSON.stringify(error));
}
}
else {
adapter.log.error('error calling the fritzbox '+JSON.stringify(error));
}
}
process.on('SIGINT', function () {
if (fritzTimeout) clearTimeout(fritzTimeout);
});
function main() {
var username = adapter.config.fritz_user;
var password = adapter.config.fritz_pw;
var moreParam = adapter.config.fritz_ip;
var fritz = new Fritz(username, password||"", moreParam||"");
function createBasic(typ,newId,name,role,id,fw,manuf){
adapter.log.debug('create Basic objects ');
adapter.setObjectNotExists(typ + newId, {
type: 'channel',
common: {
name: name,
role: role
},
native: {
"aid": newId
}
});
adapter.setObjectNotExists(typ + newId +'.id', {
type: 'state',
common: {
"name": "ID",
"type": "string",
"read": true,
"write": false,
"role": "text",
"desc": "ID"
},
native: {
}
});
adapter.setState(typ + newId +'.id', {val: id, ack: true});
adapter.setObjectNotExists(typ + newId +'.name', {
type: 'state',
common: {
"name": "Name",
"type": "string",
"read": true,
"write": false,
"role": "text",
"desc": "Name"
},
native: {
}
});
adapter.setObjectNotExists(typ + newId +'.present', {
type: 'state',
common: {
"name": "Device present",
"type": "boolean",
"read": true,
"write": false,
"role": "indicator.connected",
"desc": "Device present"
},
native: {
}
});
adapter.setObjectNotExists(typ + newId +'.fwversion', {
type: 'state',
common: {
"name": "FW version",
"type": "string",
"read": true,
"write": false,
"role": "text",
"desc": "firmware version"
},
native: {
}
});
adapter.setState(typ + newId +'.fwversion',{val: fw, ack: true});
adapter.setObjectNotExists(typ + newId +'.manufacturer', {
type: 'state',
common: {
"name": "Manufacturer",
"type": "string",
"read": true,
"write": false,
"role": "text",
"desc": "Manufacturer"
},
native: {
}
});
adapter.setState(typ + newId +'.manufacturer', {val: manuf, ack: true});
}
function createProductName(typ,newId,prod){
adapter.log.debug('create Prodname object');
adapter.setObjectNotExists(typ + newId +'.prodname', {
type: 'state',
common: {
"name": "Product Name",
"type": "string",
"read": true,
"write": false,
"role": "text",
"desc": "Product Name"
},
native: {
}
});
adapter.setState(typ + newId +'.prodname', {val: prod, ack: true});
}
function createTemplateResponse(){
adapter.log.debug('create template.lasttemplate for response ');
adapter.setObjectNotExists('template', {
type: 'channel',
common: {
name: 'template response',
role: 'switch'
},
native: {
}
});
adapter.setObjectNotExists('template.lasttemplate', {
type: 'state',
common: {
"name": "template set",
"type": "string",
"read": true,
"write": false,
"role": "text",
"desc": "template set"
},
native: {
}
});
}
function createTemplate(typ,newId,name,role,id){
adapter.log.debug('create Template objects ');
adapter.setObjectNotExists(typ + newId, {
type: 'channel',
common: {
name: name,
role: role
},
native: {
"aid": newId
}
});
adapter.setObjectNotExists(typ + newId +'.id', {
type: 'state',
common: {
"name": "ID",
"type": "string",
"read": true,
"write": false,
"role": "text",
"desc": "ID"
},
native: {
}
});
adapter.setState(typ + newId +'.id', {val: id, ack: true});
adapter.setObjectNotExists(typ + newId +'.name', {
type: 'state',
common: {
"name": "Name",
"type": "string",
"read": true,
"write": false,
"role": "text",
"desc": "Name"
},
native: {
}
});
adapter.setState(typ + newId +'.name', {val: name, ack: true});
adapter.setObjectNotExists(typ + newId +'.toggle', {
type: 'state',
common: {
"name": "Toggle template",
"type": "boolean",
"read": true,
"write": true,
"role": "button",
"desc": "Toggle template"
},
native: {
}
});
}
function createAlert(typ,newId){
adapter.log.debug('create Alert object');
adapter.setObjectNotExists(typ + newId +'.state', {
type: 'state',
common: {
"name": "Contact OFF/ON",
"type": "boolean",
"read": true,
"write": false,
"role": "indicator.connected",
"desc": "Contact OFF/ON"
},
native: {
}
});
}
function createButton(typ,newId){
adapter.log.debug('create Button object');
adapter.setObjectNotExists(typ + newId +'.lastclick', {
type: 'state',
common: {
"name": "Button Clicktime",
"type": "number",
"read": true,
"write": false,
"role": "date",
"desc": "Button Clicktime"
},
native: {
}
});
}
function createTemperature(typ,newId){
adapter.log.debug('create Temperature object');
adapter.setObjectNotExists(typ + newId +'.temp', {
type: 'state',
common: {
"name": "actual Temp",
"type": "number",
"unit": "°C",
"read": true,
"write": false,
"role": "value.temperature",
"desc": "actual Temp"
},
native: {
}
});
adapter.setObjectNotExists(typ + newId +'.temp_offset', {
type: 'state',
common: {
"name": "Temp Offset",
"type": "number",
"unit": "°C",
"read": true,
"write": false,
"role": "value.temperature",
"desc": "Temp Offset"
},
native: {
}
});
}
function createSwitch(typ,newId){
adapter.log.debug('create Switch objects');
adapter.setObjectNotExists(typ + newId +'.state', {
type: 'state',
common: {
"name": "Switch on/off",
"type": "boolean",
"read": true,
"write": true,
"role": "switch",
"desc": "Switch on/off"
},
native: {
}
});
adapter.setObjectNotExists(typ + newId +'.mode', {
type: 'state',
common: {
"name": "Switch mode", //auto or man
"type": "string",
"read": true,
"write": false,
"role": "text",
"desc": "Switch mode"
},
native: {
}
});
adapter.setObjectNotExists(typ + newId +'.lock', {
type: 'state',
common: {
"name": "Switch UI/API lock", //switch lock 0=unlocked, 1=locked
"type": "boolean",
"read": true,
"write": false,
"role": "indicator"
},
native: {
}
});
}
function createDeviceLock(typ,newId){
adapter.log.debug('create devicelock object');
adapter.setObjectNotExists(typ + newId +'.devicelock', {
type: 'state',
common: {
"name": "Switch Button lock", //switch lock 0=unlocked, 1=locked
"type": "boolean",
"read": true,
"write": false,
"role": "indicator"
},
native: {
}
});
}
function createEnergy(typ,newId){
adapter.log.debug('create Energy objects ');
adapter.setObjectNotExists(typ + newId +'.power', {
type: 'state',
common: {
"name": "Switch act power",
"type": "number",
"unit": "W",
"min": 0,
"max": 4000,
"read": true,
"write": false,
"role": "value.power",
"desc": "Switch act power"
},
native: {
}
});
adapter.setObjectNotExists(typ + newId +'.energy', {
type: 'state',
common: {
"name": "Switch total energy",
"type": "number",
"unit": "Wh",
"min": 0,
"read": true,
"write": false,
"role": "value.power.consumption",
"desc": "Switch total energy"
},
native: {
}
});
}
function createVoltage(typ,newId){
adapter.log.debug('create Voltage object');
adapter.setObjectNotExists(typ + newId +'.voltage', {
type: 'state',
common: {
"name": "Switch act voltage",
"type": "number",
"unit": "V",
"min": 0,
"max": 250,
"read": true,
"write": false,
"role": "value.voltage",
"desc": "Switch act voltage"
},
native: {
}
});
}
function createThermostat(typ,newId){
adapter.log.debug('create Thermostat objects');
adapter.setObjectNotExists(typ + newId +'.mode', {
type:'state',
common:{
"name": "Thermostat operation mode (0=auto, 1=closed, 2=open)",
"type": "number",
"read": true,
"write": true,
"role": "value",
"min": 0,
"max": 2,
"desc": "Thermostat operation mode (0=auto, 1=closed, 2=open)"
},
native: {
}
});
adapter.setObjectNotExists(typ + newId +'.targettemp', {
type: 'state',
common: {
"name": "Target Temp",
"type": "number",
"unit": "°C",
"read": true,
"write": true,
"role": "value.temperature",
"desc": "Target Temp"
},
native: {
}
});
adapter.setObjectNotExists(typ + newId +'.lasttarget', {
type: 'state',
common: {
"name": "last setting of target temp",
"type": "number",
"unit": "°C",
"read": true,
"write": false,
"role": "value.temperature",
"desc": "last setting of target temp"
},
native: {
}
});
adapter.setObjectNotExists(typ + newId +'.comfytemp', {
type: 'state',
common: {
"name": "Comfort Temp",
"type": "number",
"unit": "°C",
"read": true,
"write": false,
"role": "value.temperature",
"desc": "Comfort Temp"
},
native: {
}
});
adapter.setObjectNotExists(typ + newId +'.nighttemp', {
type: 'state',
common: {
"name": "Night Temp",
"type": "number",
"unit": "°C",
"read": true,
"write": false,
"role": "value.temperature",
"desc": "Night Temp"
},
native: {
}
});
adapter.setObjectNotExists(typ + newId +'.actualtemp', {
type: 'state',
common: {
"name": "Actual Temp",
"type": "number",
"unit": "°C",
"read": true,
"write": false,
"role": "value.temperature",
"desc": "Actual Temp"
},
native: {
}
});
adapter.setObjectNotExists(typ + newId +'.lock', {
type: 'state',
common: {
"name": "Thermostat UI/API lock", //thermostat lock 0=unlocked, 1=locked
"type": "boolean",
"read": true,
"write": false,
"role": "indicator"
},