forked from rejoe2/FHEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hm.js
2708 lines (2631 loc) · 101 KB
/
hm.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
FW_version["hm.js"] = "$Id: hm.js 2007 2020-07-21 12:00:00Z frank $";
var hm_debug = true;
var csrf;
var tplt = {
name: '',
type: '', // '','short','long'
info: '',
dev: new Map(), //dev: link (name:peer), use, pars[]
reg: new Map(), //reg: name, value, parId, master
par: new Map() //par: id, name, value, masterReg, clients[]
};
$(document).ready(function() {
// get csrf token
var body = document.querySelector('body');
if(body != null) {csrf = body.getAttribute('fwcsrf');}
// get the device name
var seldiv = document.querySelector('div.makeSelect');
if(seldiv != null) {
//var isChannelDevice = false;
var device = seldiv.getAttribute('dev');
// use jsonlist2 to get all device data
var cmd = 'jsonlist2 ' + device;
if(hm_debug) {log('HMtools: ' + cmd);}
var url = HM_makeCommand(cmd);
$.getJSON(url,function(data) {
var object = data.Results[0];
// we add the actions for CUL_HM only
if(object != null && object.Internals.TYPE == 'CUL_HM' &&
object.Attributes.model != 'ACTIONDETECTOR' &&
object.Attributes.model != 'VIRTUAL') {
var isParentDev = true;
if(object.Internals.DEF.length == 8) {
isParentDev = false;
var devspec = object.Internals.device +','+ object.Internals.NAME;
body.setAttribute('longpollfilter',devspec);
FW_closeConn();
setTimeout(FW_longpoll, 3000); // ff/longpoll=websocket only with delay
}
HM_createRegisterTable(object,isParentDev);
}
});
}
});
function HMTools_UpdateLine(d) {
if(document.getElementById('hm_reg_link_dev') == null) {return;}
var device = document.getElementById('hm_reg_link_dev').getAttribute('device');
//if(d[0].match('^' +device+ '-hm_configCheck$')) {HM_setIconFromConfigCheck(d[1]);}
if(d[0].match('^' +device+ '-cfgState$')) {HM_setIconFromConfigCheck(d[1]);}
if(d[0].match(/-commState$/)) {
var iconCommState = document.getElementById('hm_icon_commState');
if(iconCommState == null) {return;}
HM_setIconFromCommState(d[1]);
var textCommState = document.getElementById('hm_text_commState');
textCommState.innerHTML = d[1];
}
//sabotageAttackId_ErrIoId_1FBEA7 cnt:6
//sabotageAttack_ErrIoAttack_cnt 8
if(d[0].match(/-sabotageAttack_ErrIoAttack_cnt$/)) {
HM_setIconFromAttack(d[1]);
}
if(d[0].match(/-sabotageError$/)) {
HM_setIconFromSabotage(d[1]);
}
if(d[0].match(/-Activity$/)) {HM_setIconFromActionDetector(d[1]);}
}
function HM_setIconFromCommState(commState) {
// color commState
// ---------------------------------------------------------
// white Info_Cleared, Info_Unknown (missing reading)
// yellow CMDs_processing..., CMDs_FWupdate
// orange CMDs_pending
// red CMDs_done_Errors:1
// green CMDs_done, CMDs_done_FWupdate
var color = '';
if(commState.match(/^(Info_Cleared|Info_Unknown)$/)) {color = '';}
else if(commState.match(/^(CMDs_done|CMDs_done_FWupdate)$/)) {color = 'lime';}
else if(commState.match(/^(CMDs_processing...|CMDs_FWupdate)$/)) {color = 'yellow';}
else if(commState.match(/^CMDs_pending$/)) {color = 'orange';}
else if(commState.match(/^CMDs_done_Errors:/)) {color = 'red';}
var iconCommState = document.getElementById('hm_icon_commState');
iconCommState.title = 'commState: ' + commState;
if(iconCommState.innerHTML == '') {
var cmd = "{FW_makeImage('rc_dot@" +color+ "')}";
var url = HM_makeCommand(cmd);
if(hm_debug) {log('HMtools: ' + cmd);}
$.get(url, function(data){
if(data) {
iconCommState.innerHTML = data;
$("svg.rc_dot").css('height','12px');
$("svg.rc_dot").css('width','12px');
}
});
}
else {$("svg.rc_dot path").css('fill',color);}
}
function HM_setIconFromConfigCheck(cfgState) {
/*
color cfgState
--------------------------------------------------------
green "ok"
white "Info_Unknown" (no reading)
yellow "updating"
orange "Info_Expecting" (waiting for check result)
red list of errors
*/
var color;
if(cfgState.match(/^ok$/)) {color = 'lime';}
else if(cfgState.match(/^Info_Unknown$/)) {color = '';}
else if(cfgState.match(/^updating$/)) {color = 'yellow';}
else if(cfgState.match(/^Info_Expecting$/)) {color = 'orange';}
else {color = 'red';}
var iconConfigCheck = document.getElementById('hm_icon_configCheck');
if(color == 'red') {
/*
Device name:Thermostat.OZ
mId :0039 Model=HM-CC-TC
mode :config,wakeup,burstCond - activity:alive
protState : CMDs_done pending: none
configuration check: TmplChk
TmplChk: template mismatch
=>0:0-> failed
*/
var device = document.getElementById('hm_toolsTable').getAttribute('device');
var cmd = 'get ' +device+ ' deviceInfo short';
var url = HM_makeCommand(cmd);
if(hm_debug) {log('HMtools: ' + cmd);}
$.get(url, function(data){
if(data) {
iconConfigCheck.title = data;
if(cfgState.match(/TmplChk/)) {
var lines = data.split('\n');
for(var l = 0; l < lines.length; ++l) {
var line = lines[l];
if(line.match(/failed$/)) {
//=>0:0-> failed
var mIdx = line.trim().match(/=>([^:]+)/);
var peer = (mIdx[1] == 0)? 'dev': mIdx[1];
var link = document.getElementById('hm_reg_link_' + peer);
if(link.style.color != 'red') {link.style.color = 'red';};
}
}
$("[id^='hm_reg_link_']").each(function() {
if(this.style.color.match(/^(yellow|orange)$/)) {this.style.color = 'lime';}
});
}
}
});
}
else {
iconConfigCheck.title = 'cfgState: ' + cfgState;
}
if(iconConfigCheck.innerHTML == '') {
var cmd = "{FW_makeImage('edit_settings@" +color+ "')}";
var url = HM_makeCommand(cmd);
if(hm_debug) {log('HMtools: ' + cmd);}
$.get(url, function(data){
if(data) {
iconConfigCheck.innerHTML = data;
$("svg.edit_settings").css('height','20px');
$("svg.edit_settings").css('width','20px');
}
});
}
else {$("svg.edit_settings g").attr('fill',color);}
if(!cfgState.match(/TmplChk/)) {
$("[id^='hm_reg_link_']").each(function() {
if(cfgState == 'Info_Unknown') {
if(this.style.color.match(/^(lime|orange|red)$/)) {this.style.color = 'yellow';}
}
else if(cfgState == 'Info_Expecting') {
if(this.style.color.match(/^(lime|yellow|red)$/)) {this.style.color = 'orange';}
}
else {
if(this.style.color.match(/^(yellow|orange|red)$/)) {this.style.color = 'lime';}
}
});
}
}
function HM_setIconFromActionDetector(actionDetectorState) {
// color actionDetectorState
// ------------------------------------------
// white unused (no attr actCycle)
// yellow switchedOff (actCycle = 000:00)
// orange unknown
// red dead
// green alive
var color = '';
if(actionDetectorState == 'unused') {color = '';}
else if(actionDetectorState == 'switchedOff') {color = 'yellow';}
else if(actionDetectorState == 'unknown') {color = 'orange';}
else if(actionDetectorState == 'dead') {color = 'red';}
else if(actionDetectorState == 'alive') {color = 'lime';}
var iconActionDetector = document.getElementById('hm_icon_actionDetector');
iconActionDetector.title = 'Activity: '+actionDetectorState;
if(iconActionDetector.innerHTML == '') {
var cmd = "{FW_makeImage('message_attention@" +color+ "')}";
var url = HM_makeCommand(cmd);
if(hm_debug) {log('HMtools: ' + cmd);}
$.get(url, function(data){
if(data) {
iconActionDetector.innerHTML = data;
$("svg.message_attention").css('height','20px');
$("svg.message_attention").css('width','20px');
}
});
}
else {$("svg.message_attention path").css('fill',color);}
}
function HM_setIconFromRssi(rssiList) {
/*
hminfo: I_rssiMinLevel 59<:11 60>:10 80>:4 99>:1
color rssi special
-----------------------------------------------------
white missing_rssi
green -80 < rssi
yellow -90 < rssi <= -80
orange -99 <= rssi <= -90
red rssi < -99 missing_IODev
*/
var color = '';
if(rssiList.match(/missing_IODev/)) {color = 'red';}
else {
//rssi_at_cul868 cnt:484 min:-39.5 max:-38 avg:-39.09 lst:-39.5
var mRssi = rssiList.match(/min:([^\s]*)/);
var rssi = (mRssi != null && mRssi[1] != undefined)? mRssi[1]: '';
if(rssi != '') {
if(-80 < rssi) {color = 'lime';}
else if(-90 < rssi && rssi <= -80) {color = 'yellow';}
else if(-99 <= rssi && rssi <= -90) {color = 'orange';}
else if(rssi < -99) {color = 'red';}
}
else {color = '';}
}
var iconRssi = document.getElementById('hm_icon_rssi');
iconRssi.title = rssiList;
if(iconRssi.innerHTML == '') {
var cmd = "{FW_makeImage('it_wifi@" +color+ "')}";
var url = HM_makeCommand(cmd);
if(hm_debug) {log('HMtools: ' + cmd);}
$.get(url, function(data){
if(data) {
iconRssi.innerHTML = data;
$("svg.it_wifi").css('height','20px');
$("svg.it_wifi").css('width','20px');
}
});
}
else {$("svg.it_wifi g").attr('fill',color);}
}
function HM_setIconFromSabotage(sabotage) {
// color sabotage
// -------------------
// green off
// red on
var color = '';
if(sabotage == 'off') {color = 'lime';}
else if(sabotage == 'on') {color = 'red';}
var iconSabotage = document.getElementById('hm_icon_sabotage');
iconSabotage.title = 'sabotageError: ' + sabotage;
if(iconSabotage.innerHTML == '') {
var cmd = "{FW_makeImage('secur_locked@" +color+ "')}";
var url = HM_makeCommand(cmd);
if(hm_debug) {log('HMtools: ' + cmd);}
$.get(url, function(data){
if(data) {
iconSabotage.innerHTML = data;
$("svg.secur_locked").css('height','20px');
$("svg.secur_locked").css('width','20px');
}
});
}
else {
$("svg.secur_locked g").css('fill',color);
}
}
function HM_setIconFromAttack(attack) {
// color attack
// -------------------
// red attack
var color = 'red';
//if(attack.match(/^[0-9]+$/)) {color = 'red';}
var iconAttack = document.getElementById('hm_icon_attack');
iconAttack.title = 'sabotageAttack_ErrIoAttack_cnt: ' + attack;
if(iconAttack.innerHTML == '') {
var cmd = "{FW_makeImage('ring@" +color+ "')}";
var url = HM_makeCommand(cmd);
if(hm_debug) {log('HMtools: ' + cmd);}
$.get(url, function(data){
if(data) {
iconAttack.innerHTML = data;
$("svg.ring").css('height','20px');
$("svg.ring").css('width','20px');
}
});
}
else {
//$("svg.ring path").css('fill',color);
//$("svg.ring polygon").css('fill',color);
//$("svg.ring rect").css('fill',color);
var ring1 = document.getElementById('polygon5');
var ring2 = document.getElementById('path3');
var ring3 = document.getElementById('rect7');
ring1.animate([{fill: 'white'}, {fill: 'red'}], {duration: 500, iterations: 1, easing: 'ease'});
ring2.animate([{fill: 'white'}, {fill: 'red'}], {duration: 500, iterations: 1, easing: 'ease'});
ring3.animate([{fill: 'white'}, {fill: 'red'}], {duration: 500, iterations: 1, easing: 'ease'});
//$("svg.ring").animate([{fill: 'white'}, {fill: 'red'}], {duration: 3000, iterations: 3});
}
}
function HM_setIconFromBattery(battery) {
// color level battery
// --------------------------
// green 75 ok
// orange 25 low
// red 0 critical
var color = '';
var level = 0;
if(battery == 'ok') {color = 'lime';level = 75;}
else if(battery == 'low') {color = 'orange';level = 25;}
else if(battery == 'critical') {color = 'red';level = 0;}
var iconBattery = document.getElementById('hm_icon_battery');
iconBattery.title = 'battery: '+battery;
var cmd = "{FW_makeImage('measure_battery_" +level+ "@" +color+ "')}";
var url = HM_makeCommand(cmd);
if(hm_debug) {log('HMtools: ' + cmd);}
$.get(url, function(data){
if(data) {
iconBattery.innerHTML = data;
$("svg.measure_battery_" + level).css('height','20px');
$("svg.measure_battery_" + level).css('width','20px');
}
});
}
function HM_setClearMsgEvents(device) {
var cmd = 'set '+device+' clear msgEvents';
if(hm_debug) {log('HMtools: ' + cmd);}
var url = HM_makeCommand(cmd);
$.get(url,function(data) {
if(data) {FW_okDialog(data);}
});
}
function HM_setClearRssi(device) {
var cmd = 'set '+device+' clear rssi';
if(hm_debug) {log('HMtools: ' + cmd);}
var url = HM_makeCommand(cmd);
$.get(url,function(data) {
if(data) {FW_okDialog(data);}
else {
var cmd = 'jsonlist2 ' + device;
if(hm_debug) {log('HMtools: ' + cmd);}
var url = HM_makeCommand(cmd);
$.get(url,function(data) {
if(data) {
var object = data.Results[0];
internalsString = JSON.stringify(object.Internals);
var curIoDev = (object.Internals.IODev != null)?
object.Internals.IODev:
'missing_IODev';
var curIoRssi = 'rssi_at_'+curIoDev+' => ' + ((internalsString.match('rssi_at_'+curIoDev))?
object.Internals['rssi_at_'+curIoDev]:
'missing_rssi');
HM_setIconFromRssi(curIoRssi);
}
else {
}
});
}
});
}
function HM_setClearAttack(device) {
var cmd = 'set '+device+' clear attack';
if(hm_debug) {log('HMtools: ' + cmd);}
var url = HM_makeCommand(cmd);
$.get(url,function(data) {
if(data) {FW_okDialog(data);}
else {
var cmd = 'jsonlist2 ' + device;
if(hm_debug) {log('HMtools: ' + cmd);}
var url = HM_makeCommand(cmd);
$.get(url,function(data) {
if(data) {
var object = data.Results[0];
//readingsString = JSON.stringify(object.Readings);
if(object.Readings.sabotageAttack_ErrIoAttack_cnt != null) {
HM_setIconFromAttack(object.Readings.sabotageAttack_ErrIoAttack_cnt.Value);
}
else {
var iconAttack = document.getElementById('hm_icon_attack');
iconAttack.innerHTML = '';
}
}
else {
}
});
}
});
}
function HM_setBatteryChange(device) {
var cmd = 'attr '+device+' comment';
if(hm_debug) {log('HMtools: ' + cmd);}
var url = HM_makeCommand(cmd);
$.get(url,function(data) {
if(data) {FW_okDialog(data);}
});
}
/*
function HM_getHminfoConfigCheck(device) {
HM_setIconFromConfigCheck('Info_Expecting');
//var parentDev = document.getElementById('hm_toolsTable').getAttribute('parentDev');
var hminfo = document.getElementById('hm_toolsTable').getAttribute('hminfo');
//cmd = 'get ' +hminfo+ ' configCheck -f ^' +device+ '$';
//{my $t;; $t = fhem("get hminfo configCheck");; fhem("trigger DimPBU01 hm_configCheck: $t");;}
//var cmd = '{my $t=fhem("get '+hminfo+' configCheck -f ^'+device+'\\$");;fhem("trigger '+device+' hm_configCheck: $t");;}';
//var cmd = '{fhem("setreading ' +device+ ' cfgState Info_Expecting");; fhem("get ' +hminfo+ ' configCheck",1)}';
//var cmd = "setreading " +device+ " cfgState Info_Expecting; get " +hminfo+ " configCheck";
var cmd = "get " +hminfo+ " configCheck";
if(hm_debug) {log('HMtools: ' + cmd);}
var url = HM_makeCommand(cmd);
$.get(url,function(data) {
//FW_cmd(FW_root+"?cmd="+cmd+"&XHR=1", function(data) {
//if(data) {FW_okDialog(data);}
});
}
*/
// create an extra table with buttons for device and peer register sets
function HM_createRegisterTable(object,isParentDev) {
// we will insert the table before the internals
var intdiv = document.querySelector('div.makeTable.wide.internals');
var div = document.createElement('div');
intdiv.parentElement.insertBefore(div,intdiv);
div.id = 'hm_toolsTable';
div.setAttribute('device',object.Internals.NAME);
div.setAttribute('parentDev',((isParentDev)? object.Internals.NAME: object.Internals.device));
//tab.setAttribute('class','makeTable wide');
div.setAttribute('class','makeTable wide internals');
HM_checkHminfo(object.Internals.NAME); //check if hminfo is running and set attribute "hminfo"
var header = document.createElement('span');
div.appendChild(header);
//header.setAttribute('class','col_header pinHeader');
header.setAttribute('class','mkTitle');
header.innerHTML = 'Tools';
var table = document.createElement('table');
div.appendChild(table);
//table.setAttribute('class','block wide internals wrapcolumns');
table.setAttribute('class','block wide internals');
var tbody = document.createElement('tbody');
table.appendChild(tbody);
var tr = document.createElement('tr');
tbody.appendChild(tr);
tr.setAttribute('class','odd');
//commState icon and reading with longpoll
var td = document.createElement('td');
tr.appendChild(td);
td.style.width = '250px';
//icon&text commState
var iconCommState = document.createElement('span'); // element <a> or <span> for links?
td.appendChild(iconCommState);
iconCommState.id = 'hm_icon_commState';
iconCommState.style.cursor = 'pointer';
iconCommState.setAttribute('onclick',"HM_setClearMsgEvents('"
+((isParentDev)? object.Internals.NAME: object.Internals.device)+ "')");
var dummy = document.createElement('span');
td.appendChild(dummy);
dummy.innerHTML = ' ';
var textCommState = document.createElement('span');
td.appendChild(textCommState);
textCommState.id = 'hm_text_commState';
//icon actionDetector
var dummy = document.createElement('span');
td.appendChild(dummy);
dummy.innerHTML = ' ';
var iconActionDetector = document.createElement('span');
td.appendChild(iconActionDetector);
iconActionDetector.id = 'hm_icon_actionDetector';
//icon rssi
var dummy = document.createElement('span');
td.appendChild(dummy);
dummy.innerHTML = ' ';
var iconRssi = document.createElement('span');
td.appendChild(iconRssi);
iconRssi.id = 'hm_icon_rssi';
iconRssi.style.cursor = 'pointer';
iconRssi.setAttribute('onclick',"HM_setClearRssi('"
+((isParentDev)? object.Internals.NAME: object.Internals.device)+ "')");
//icon sabotage
var dummy = document.createElement('span');
td.appendChild(dummy);
dummy.innerHTML = ' ';
var iconSabotage = document.createElement('span');
td.appendChild(iconSabotage);
iconSabotage.id = 'hm_icon_sabotage';
//iconSabotage.style.cursor = 'pointer';
//iconSabotage.setAttribute('onclick',"HM_setAlarmChange('" +((isParentDev)? object.Internals.NAME: object.Internals.device)+ "')");
//icon attack
var dummy = document.createElement('span');
td.appendChild(dummy);
dummy.innerHTML = ' ';
var iconAttack = document.createElement('span');
td.appendChild(iconAttack);
iconAttack.id = 'hm_icon_attack';
iconAttack.style.cursor = 'pointer';
iconAttack.setAttribute('onclick',"HM_setClearAttack('" +((isParentDev)? object.Internals.NAME: object.Internals.device)+ "')");
//icon battery
var dummy = document.createElement('span');
td.appendChild(dummy);
dummy.innerHTML = ' ';
var iconBattery = document.createElement('span');
td.appendChild(iconBattery);
iconBattery.id = 'hm_icon_battery';
//iconBattery.style.cursor = 'pointer';
//iconBattery.setAttribute('onclick',"HM_setBatteryChange('" +((isParentDev)? object.Internals.NAME: object.Internals.device)+ "')");
//icon configCheck
var dummy = document.createElement('span');
td.appendChild(dummy);
dummy.innerHTML = ' ';
var iconConfigCheck = document.createElement('span'); //link to start configCheck
td.appendChild(iconConfigCheck);
iconConfigCheck.id = 'hm_icon_configCheck';
//iconConfigCheck.style.cursor = 'pointer';
//iconConfigCheck.setAttribute('onclick',"HM_getHminfoConfigCheck('" +object.Internals.NAME+ "')");
//init status icons
var commStateDev = object.Internals.NAME;
var actionDetectorState;
var commState;
var internalsString;
var readingsString;
var curIoDev;
var curIoRssi;
if(object.Internals.DEF.length == 8) { //current device is channelDevice
commStateDev = object.Internals.device;
var cmd = 'jsonlist2 ' + commStateDev;
if(hm_debug) {log('HMtools: ' + cmd);}
var url = HM_makeCommand(cmd);
$.getJSON(url,function(data) {
//commState
commState = (data.Results[0].Readings.commState != null)?
data.Results[0].Readings.commState.Value: 'Info_Unknown';
HM_setIconFromCommState(commState);
textCommState.innerHTML = commState;
//actionDetector
if(data.Results[0].Attributes.actCycle != null) {
actionDetectorState = (data.Results[0].Attributes.actStatus != null)?
data.Results[0].Attributes.actStatus:
'unknown';
}
else {actionDetectorState = 'unused';}
HM_setIconFromActionDetector(actionDetectorState);
//rssi
internalsString = JSON.stringify(data.Results[0].Internals);
curIoDev = (data.Results[0].Internals.IODev != null)?
data.Results[0].Internals.IODev:
'missing_IODev';
curIoRssi = 'rssi_at_'+curIoDev+' => ' + ((internalsString.match('rssi_at_'+curIoDev))?
data.Results[0].Internals['rssi_at_'+curIoDev]:
'missing_rssi');
HM_setIconFromRssi(curIoRssi);
//sabotage
if(data.Results[0].Readings.sabotageError != null) {
HM_setIconFromSabotage(data.Results[0].Readings.sabotageError.Value);
}
//attack
//readingssString = JSON.stringify(data.Results[0].Readings);
if(data.Results[0].Readings.sabotageAttack_ErrIoAttack_cnt != null) {
HM_setIconFromAttack(data.Results[0].Readings.sabotageAttack_ErrIoAttack_cnt.Value);
}
//battery
if(data.Results[0].Readings.battery != null) {
HM_setIconFromBattery(data.Results[0].Readings.battery.Value);
}
});
}
else { //current device is parentDevice
//commState
commState = (object.Readings.commState != null)?
object.Readings.commState.Value: 'Info_Unknown';
HM_setIconFromCommState(commState);
textCommState.innerHTML = commState;
//actionDetector
if(object.Attributes.actCycle != null) {
actionDetectorState = (object.Attributes.actStatus != null)? object.Attributes.actStatus: 'unknown';
}
else {actionDetectorState = 'unused';}
HM_setIconFromActionDetector(actionDetectorState);
//rssi
internalsString = JSON.stringify(object.Internals);
curIoDev = (object.Internals.IODev != null)?
object.Internals.IODev:
'missing_IODev';
curIoRssi = 'rssi_at_'+curIoDev+' => ' + ((internalsString.match('rssi_at_'+curIoDev))?
object.Internals['rssi_at_'+curIoDev]:
'missing_rssi');
HM_setIconFromRssi(curIoRssi);
//sabotage
if(object.Readings.sabotageError != null) {
HM_setIconFromSabotage(object.Readings.sabotageError.Value);
}
//attack
//readingssString = JSON.stringify(object.Readings);
if(object.Readings.sabotageAttack_ErrIoAttack_cnt != null) {
HM_setIconFromAttack(object.Readings.sabotageAttack_ErrIoAttack_cnt.Value);
}
//battery
if(object.Readings.battery != null) {
HM_setIconFromBattery(object.Readings.battery.Value);
}
}
//textCommState.setAttribute('informid',commStateDev + '-commState'); //update in HMTools_UpdateLine
//link device registerset
var td = document.createElement('td');
tr.appendChild(td);
var list = document.createElement('a');
td.appendChild(list);
list.id = 'hm_reg_link_dev';
list.innerHTML = 'Device';
list.setAttribute('def',object.Internals.DEF);
list.setAttribute('device',object.Internals.NAME);
list.setAttribute('model',object.Attributes.model);
list.setAttribute('onclick',"HM_changeRegister('" + object.Internals.NAME + "','')");
list.style.margin = '2px 20px 2px 20px';
list.style.cursor = 'pointer';
list.style.color = (object.Readings.tmpl_0 != null)? 'yellow': '';
//if device has peers - create a button for every peer
if(object.Internals.peerList != null) {
var peers = object.Internals.peerList.split(',');
var readings = JSON.stringify(object.Readings);
for(var i = 0; i < peers.length; ++i) {
var p = peers[i];
var peerIsExtern = (p != '' && p.match(/^self\d\d$/) == null);
if(p.length > 0) {
var mSpecial = readings.match('R-' +p+ '_chn-01-');
var suffix = (mSpecial != null)? '_chn-01': '';
list = document.createElement('a');
td.appendChild(list);
list.id = 'hm_reg_link_' + p;
list.innerHTML = p;
if(peerIsExtern) {list.setAttribute('peersuffix',suffix);}
list.setAttribute('onclick',"HM_changeRegister('" + object.Internals.NAME + "','" +p+ "')");
list.style.margin = '2px 20px 2px 20px';
list.style.cursor = 'pointer';
var mReadings = readings.match('tmpl_' +p+suffix+ ':');
list.style.color = (mReadings != null)? 'yellow': '';
}
}
}
var cfgState = (object.Readings.cfgState != null)?
object.Readings.cfgState.Value: 'Info_Unknown';
HM_setIconFromConfigCheck(cfgState);
}
//open a popup window to change the register values
function HM_changeRegister (device,peer) {
var content = HM_openPopup(device,peer); //create popup window
HM_initTemplateTable(device,peer); //create template elements
//first get the register list
var cmd = 'get ' +device+ ' regList';
if(hm_debug) {log('HMtools: ' + cmd);}
var url = HM_makeCommand(cmd);
//http://fhem:8083/fhem?cmd=get%20HM_123456_Sw_01%20regList&XHR=1
$.get(url,function(data){
//parse register definitions into a map
var regmap = HM_parseRegisterList(data);
//get the current register values
var cmd = 'get ' +device+ ' reg all';
if(hm_debug) {log('HMtools: ' + cmd);}
var url = HM_makeCommand(cmd);
$.get(url,function(data){
//create a table with all registers
var div = document.createElement('div');
content.appendChild(div);
var table = document.createElement('table');
div.appendChild(table);
table.id = 'hm_reg_table';
table.hidden = true;
table.style.margin = '10px 0px 0px 0px';
var colgroup = document.createElement('colgroup');
table.appendChild(colgroup);
var col1 = document.createElement('col');
colgroup.appendChild(col1);
col1.id = 'hm_reg_table_col1';
col1.setAttribute('span','1');
var col2 = document.createElement('col');
colgroup.appendChild(col2);
col2.id = 'hm_reg_table_col2';
col2.setAttribute('span','3');
var thead = document.createElement('thead');
table.appendChild(thead);
var row = document.createElement('tr');
thead.appendChild(row);
var headerList = ['use','register','value','description'];
for(var h = 0; h < 4; ++h) {
var th = document.createElement('th');
row.appendChild(th);
th.setAttribute('scope','col');
th.hidden = (h == 0);
th.innerHTML = headerList[h];
}
var tbody = document.createElement('tbody');
table.appendChild(tbody);
var missedReg = "Register in 'get "+device+" reg all' are missing in 'get "+device+" regList'!";
var missedVal = 'Some register values are not verified!';
var lines = data.split('\n');
var regCtr = 0, shCtr = 0, lgCtr = 0;
for(var i = 2; i < lines.length; ++i) { //first line #3
var line = lines[i];
//var match = line.match(/(\d):([\w.-]*)\s+([\w-]+)\s+:([\w.:-]+)/);
var match = line.match(/(\d):([\w.-]*)\s+([\w-]+)\s+:([^\s]+)/);
if(match != null) {
var regname = match[3];
var regdesc = regmap.get(regname);
if(regdesc != null) { //sometimes regs not found in regList, bug in cul_hm
var regvalue = match[4];
var peerchn01 = peer + '_chn-01';
if( (peer == match[2] || peerchn01 == match[2])
&& (regname != 'pairCentral' && regname != 'sign')) {
if(regvalue.match(/^set_/) != null) {missedVal += '<br>- ' +regname+ ': ' + regvalue;}
++regCtr;
if(regname.match(/^sh/)) {++shCtr;}
else if(regname.match(/^lg/)) {++lgCtr;}
//new row
var row = document.createElement('tr');
tbody.appendChild(row);
row.id = 'hm_reg_row_' + regname;
row.name = regname;
//column 1
var c1 = document.createElement('td');
row.appendChild(c1);
var tplParValue = 'off';
var select = document.createElement('select');
c1.appendChild(select);
select.title = 'use register in current template';
select.id = 'hm_tplt_reg_' + regname;
select.name = regname;
select.setAttribute('orgvalue',tplParValue);
select.style.width = 'auto';
select.style.margin = '0px 0px 0px 0px';
select.style.backgroundColor = 'white';
select.setAttribute('onchange',"HM_parseTemplateFromInputs('" +device+ "','" +peer+ "','hm_tplt_reg_" +regname+ "')");
select.setAttribute('onmousedown',"HM_updatePopupTpltRegOptions('hm_tplt_reg_" +regname+ "')");
for(var o = 0; o < 11; ++o) { //off,on,p0,p1, ...
var sel = (o == 0)? 'off': (o == 1)? 'on': 'p' + (o - 2);
var opt = document.createElement('option');
select.appendChild(opt);
opt.innerHTML = sel;
opt.id = 'hm_tplt_regOpt' +sel+ '_' + regname;
opt.value = sel;
opt.style.backgroundColor = (o == 0)? 'white': (o == 1)? 'lightgreen': 'white';
opt.selected = (sel == tplParValue);
}
//column 2
var c2 = document.createElement('td');
row.appendChild(c2);
c2.id = 'hm_reg_name_' + regname;
c2.name = regname;
c2.innerHTML = regname;
//column 3
var c3 = document.createElement('td');
row.appendChild(c3);
if(regdesc.literals == null) {
var input = document.createElement('input');
c3.appendChild(input);
input.id = 'hm_reg_val_' + regname;
input.name = regname;
input.title = 'range:' +regdesc.range+ ' => current:' + regvalue;
input.placeholder = '(' +regvalue+ ')';
input.value = regvalue;
input.setAttribute('orgvalue',regvalue);
input.setAttribute('onchange',"HM_updatePopupRegister('hm_reg_val_" +regname+ "')");
input.style.width = '140px';
input.style.margin = '0px 0px 0px 0px';
}
else {
var select = document.createElement('select');
c3.appendChild(select);
select.id = 'hm_reg_val_' + regname;
select.name = regname;
select.setAttribute('orgvalue',regvalue);
select.setAttribute('onchange',"HM_updatePopupRegister('hm_reg_val_" +regname+ "')");
select.style.width = '150px';
select.style.margin = '0px 0px 0px 0px';
for(var l = 0; l < regdesc.literals.length; ++l) {
var lit = regdesc.literals[l];
var opt = document.createElement('option');
select.appendChild(opt);
opt.innerHTML = lit;
opt.value = lit;
opt.selected = (lit == regvalue);
opt.style.backgroundColor = (lit == regvalue)? 'silver': 'white';
}
}
//column 4
var c4 = document.createElement('td');
row.appendChild(c4);
c4.id = 'hm_reg_desc_' + regname;
c4.innerHTML = regdesc.desc;
}
}
else {missedReg += '<br>- ' +regname;}
}
}
//detection if registerset is good for generic template type
if((shCtr + lgCtr) == regCtr && shCtr == lgCtr) {
$('#hm_tplt_select').attr('generic','true');
$("[id^='hm_reg_name_']").each(function() {
var nameGen = this.name.replace(/^(?:sh|lg)/,'');
this.setAttribute('namegen',nameGen);
var val = (this.name.match(/^sh/))? 'short': 'long';
this.setAttribute('class', val);
});
$("[id^='hm_reg_row_']").each(function() {
var val = (this.name.match(/^sh/))? 'short': 'long';
this.setAttribute('class', val);
});
}
var last = document.createElement('div');
content.appendChild(last);
// output for template define
var output = document.createElement('textarea');
last.appendChild(output);
output.id = 'hm_tplt_define';
output.setAttribute('orgvalue','');
output.value = '';
output.title = 'the resulting define command for sharing with other users';
output.hidden = true;
output.readOnly = true;
output.rows = 3;
output.style.minWidth = 'calc(100% - 20px)';
output.style.margin = '30px 0px 10px 0px';
output.style.resize = 'none';
output.style.backgroundColor = 'white';
output.style.color = 'black';
if(missedVal != 'Some register values are not verified!') {
missedVal += "<br><br>Please read the values first with 'set " +device+ " getConfig'";
FW_okDialog(missedVal);
HM_cancelPopup();
}
else {
var hminfo = document.getElementById('hm_toolsTable').getAttribute('hminfo');
if(hminfo != '') {HM_updateTemplateList(device,peer,'init');}
else {HM_updatePopupMode(device,peer);}
if(missedReg != "Register in 'get "+device+" reg all' are missing in 'get "+device+" regList'!") {
FW_okDialog(missedReg);
}
}
});
});
}
function HM_initTemplateTable(device,peer) {
var content = document.getElementById(device +peer+ 'hm_popup_content');
var hminfo = document.getElementById('hm_toolsTable').getAttribute('hminfo');
var first = document.createElement('div');
content.appendChild(first);
first.id = 'hm_tplt_select_first';
first.hidden = true;
var table = document.createElement('table');
first.appendChild(table);
table.style.margin = '0px 0px 30px 0px';
//table.style.tableLayout = 'auto';
table.style.width = '100%';
var row1 = document.createElement('tr');
table.appendChild(row1);
row1.style.fontSize = '16px';
row1.style.fontWeight = 'bold';
var left = document.createElement('td');
row1.appendChild(left);
var left1 = document.createElement("span");
left.appendChild(left1);
left1.innerHTML = "register configuration";
//left1.style.color = "yellow";
var left2 = document.createElement("span");
left.appendChild(left2);
left2.innerHTML = " ( " + device + ":" + ((peer != '')? peer: "general") + " )";
var right = document.createElement("td");
row1.appendChild(right);
right.align = "right";
var err = document.createElement("a");
right.appendChild(err);
err.title = "hminfo is not running";
err.href = "https://wiki.fhem.de/wiki/HomeMatic_HMInfo";
var errText = document.createElement("span");
err.appendChild(errText);
errText.hidden = (hminfo != "");
errText.innerHTML = " ! ";
errText.style.color = "red";
var help = document.createElement("a");
right.appendChild(help);
help.title = "open wiki templates";
help.href = "https://wiki.fhem.de/wiki/HomeMatic_Templates";
help.innerHTML = " ? ";
var row2 = document.createElement("tr");
table.appendChild(row2);
var left = document.createElement("td");
row2.appendChild(left);
// use a drop down box to select the templates
var select = document.createElement('select');
left.appendChild(select);
select.id = 'hm_tplt_select';
select.setAttribute('device',device);
select.setAttribute('peer',peer);
select.setAttribute('generic','false');
select.setAttribute('onchange',"HM_updatePopupMode('" +device+ "','" +peer+ "')");
select.style.minWidth = '180px';
select.style.margin = '20px 0px 0px 0px';
var opt = document.createElement('option');
select.appendChild(opt);
opt.innerHTML = 'expert mode';
opt.value = 'expert';
opt.selected = true;
opt.setAttribute('cat', 'white');
opt.style.backgroundColor = 'white';
// input for new template name
var input = document.createElement('input');
left.appendChild(input);
input.id = 'hm_tplt_name';
input.setAttribute('orgvalue','');
input.value = '';
input.title = 'please give me a good template name!';
input.hidden = true;
input.placeholder = 'new_template_name';
input.setAttribute('onchange',"HM_parseTemplateFromInputs('" +device +"','" +peer+ "','hm_tplt_name')");
input.style.minWidth = '180px';
input.style.margin = '20px 0px 0px 10px';
var right = document.createElement('td');
row2.appendChild(right);
right.align = 'right';
//drop down box for generic template type
var select = document.createElement('select');
right.appendChild(select);
select.id = 'hm_tplt_generic';
select.setAttribute('orgvalue','');
select.hidden = true;
select.setAttribute('onchange',"HM_parseTemplateFromInputs('" +device+ "','" +peer+ "','hm_tplt_generic')");
select.style.width = 'auto';
select.style.margin = '20px 0px 0px 0px';
var options = ['short AND long','generic from short','generic from long'];
var values = ['','short','long'];
for(var o = 0; o < 3; ++o) {
var opt = document.createElement('option');
select.appendChild(opt);
opt.innerHTML = options[o];
opt.value = values[o];
opt.selected = (o == 0);
}
//select box for template details
var select = document.createElement('select');
right.appendChild(select);
select.id = 'hm_tplt_details';
select.setAttribute('orgvalue','basic');
select.hidden = true;
select.setAttribute('onchange','HM_updateTemplateDetails()');
select.style.width = 'auto';
select.style.margin = '20px 0px 0px 0px';
var options = ['basic details','used register','register set','global usage','define','all details'];
var values = ['basic','reg','regset','usg','def','all'];
for(var o = 0; o < 6; ++o) {
var opt = document.createElement('option');
select.appendChild(opt);
opt.innerHTML = options[o];
opt.value = values[o];
opt.selected = (o == 0);
}
/*
var row3 = document.createElement("tr");
table.appendChild(row3);
var left = document.createElement("td");