-
Notifications
You must be signed in to change notification settings - Fork 1
/
HMdeviceTools.js
2156 lines (2103 loc) · 83.6 KB
/
HMdeviceTools.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["HMdeviceTools.js"] = "$Id: HMdeviceTools.js 1001 2021-11-22 18:00:00Z frank $";
var HMdeviceTools_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(HMdeviceTools_debug) {log('HMdeviceTools: ' + cmd);}
var url = HMdeviceTools_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') {
var isParentDev = true;
if(object.Internals.DEF.length == 8) {
isParentDev = false;
//var devspec = object.Internals.device +','+ object.Internals.NAME;
//body.setAttribute('longpollfilter',devspec);
}
HMdeviceTools_createRegisterTable(object,isParentDev);
}
});
}
});
// create an extra table with buttons for device and peer register sets
function HMdeviceTools_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 = 'HMdeviceTools_toolsTable';
div.setAttribute('installation','init'); // init, ready
div.setAttribute('loaded_icons','init'); // 0_icons: "init"; 9_icons: "9"
div.setAttribute('errordevices_data','init'); // init, start, first, ready
div.setAttribute('device',object.Internals.NAME);
div.setAttribute('parentDev',((isParentDev)? object.Internals.NAME: object.Internals.device));
div.setAttribute('iolist',((isParentDev)? object.Internals.IODev: ''));
div.setAttribute('model',object.Attributes.model);
//tab.setAttribute('class','makeTable wide');
div.setAttribute('class','makeTable wide internals');
HMdeviceTools_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 = 'HMdeviceTools';
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');
//dummy icons
var td = document.createElement('td');
tr.appendChild(td);
td.id = 'HMdeviceTools_toolsTable_svg';
td.hidden = true;
//td.style.whiteSpace = 'nowrap';
//td.style.width = '200px';
//icons
var td = document.createElement('td');
tr.appendChild(td);
td.id = 'HMdeviceTools_toolsTable_icons';
td.style.whiteSpace = 'nowrap';
td.style.width = '200px';
//link device registerset
var td = document.createElement('td');
tr.appendChild(td);
var list = document.createElement('a');
td.appendChild(list);
list.id = 'HMdeviceTools_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',"HMdeviceTools_changeRegister('" + object.Internals.NAME + "','')");
list.style.margin = '2px 20px 2px 20px';
list.style.cursor = 'pointer';
list.style.color = (object.Readings.tmpl_0 != null)? 'yellow': 'white';
//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 = 'HMdeviceTools_reg_link_' + p;
list.innerHTML = p;
if(peerIsExtern) {list.setAttribute('peersuffix',suffix);}
list.setAttribute('onclick',"HMdeviceTools_changeRegister('" + object.Internals.NAME + "','" +p+ "')");
list.style.margin = '2px 20px 2px 20px';
list.style.cursor = 'pointer';
//var mReadings = readings.match('tmpl_' +p+suffix+ ':');
var mReadings = readings.match('tmpl_' +p+ ':');
list.style.color = (mReadings != null)? 'yellow': 'white';
}
}
}
div.setAttribute('installation','ready');
}
//open a popup window to change the register values
function HMdeviceTools_changeRegister (device,peer) {
var content = HMdeviceTools_openPopup(device,peer); //create popup window
HMdeviceTools_initTemplateTable(device,peer); //create template elements
//first get the register list
var cmd = 'get ' +device+ ' regList';
if(HMdeviceTools_debug) {log('HMdeviceTools: ' + cmd);}
var url = HMdeviceTools_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 = HMdeviceTools_parseRegisterList(data);
//get the current register values
var cmd = 'get ' +device+ ' reg all';
if(HMdeviceTools_debug) {log('HMdeviceTools: ' + cmd);}
var url = HMdeviceTools_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',"HMdeviceTools_parseTemplateFromInputs('" +device+ "','" +peer+ "','hm_tplt_reg_" +regname+ "')");
select.setAttribute('onmousedown',"HMdeviceTools_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',"HMdeviceTools_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',"HMdeviceTools_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);
HMdeviceTools_cancelPopup();
}
else {
var hminfo = document.getElementById('HMdeviceTools_toolsTable').getAttribute('hminfo');
if(hminfo != '') {HMdeviceTools_updateTemplateList(device,peer,'init');}
else {HMdeviceTools_updatePopupMode(device,peer);}
if(missedReg != "Register in 'get "+device+" reg all' are missing in 'get "+device+" regList'!") {
FW_okDialog(missedReg);
}
}
});
});
}
function HMdeviceTools_initTemplateTable(device,peer) {
var content = document.getElementById(device +peer+ 'hm_popup_content');
var hminfo = document.getElementById('HMdeviceTools_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',"HMdeviceTools_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',"HMdeviceTools_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',"HMdeviceTools_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','HMdeviceTools_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");
row3.appendChild(left);
*/
var div = document.createElement("div");
content.appendChild(div);
// input for template info text
var input = document.createElement("textarea");
div.appendChild(input);
input.id = "hm_tplt_info";
input.setAttribute("orgvalue","");
input.value = "";
input.title = "please give me a good template info text!";
input.hidden = true;
input.placeholder = "new_template_info_text";
input.rows = 3;
input.style.minWidth = "calc(100% - 20px)";
input.style.margin = "10px 0px 0px 0px";
input.style.resize = "none";
input.setAttribute('onchange',"HMdeviceTools_parseTemplateFromInputs('" +device+ "','" +peer+ "','hm_tplt_info')");
var div = document.createElement("div");
content.appendChild(div);
// template parameter table
var table = document.createElement("table");
div.appendChild(table);
table.setAttribute("id","hm_par_table");
table.setAttribute("hidden",true);
table.style.margin = '10px 0px 0px 0px';
var thead = document.createElement("thead");
table.appendChild(thead);
var row = document.createElement("tr");
thead.appendChild(row);
row.id = "hm_tplt_parRow_header";
row.hidden = true;
var headerList = ["", "parameter", "value", "range", "description"];
for(var h = 0; h < 5; ++h) { // 5 columns
var thCol = document.createElement("th");
row.appendChild(thCol);
thCol.hidden = (h == 2 || h == 3);
thCol.setAttribute("scope","col");
thCol.innerHTML = headerList[h];
}
var tbody = document.createElement('tbody');
table.appendChild(tbody);
for(var r = 0; r < 9; ++r) { // init table for all 9 possible parameter
var parId = 'p' + r;
var row = document.createElement('tr');
tbody.appendChild(row);
row.id = 'hm_tplt_parRow_' + r;
row.hidden = true;
//row.style.backgroundColor = '#333';
//row header
var thRow = document.createElement('th');
row.appendChild(thRow);
thRow.setAttribute('scope','row');
thRow.innerHTML = parId;
//column 1: template parameter name (input/output)
var c1 = document.createElement('td');
row.appendChild(c1);
c1.align = 'left';
var inp = document.createElement('span');
c1.appendChild(inp);
inp.hidden = false;
var input = document.createElement('input');
inp.appendChild(input);
input.id = 'hm_tplt_' +parId+ '_nameIn';
input.name = parId;
input.title = 'please give me a good parameter name!';
input.style.width = '250px';
input.style.margin = '0px 0px 0px 0px';
input.setAttribute('onchange',"HMdeviceTools_parseTemplateFromInputs('" +device+ "','" +peer+ "','hm_tplt_" +parId+ "_nameIn')");
var out = document.createElement('span');
c1.appendChild(out);
out.id = 'hm_tplt_' +parId+ '_nameOut';
out.innerHTML = 'parameter_name' + r;
out.hidden = true;
//column 2: template parameter value
var c2 = document.createElement('td');
row.appendChild(c2);
c2.id = 'hm_tplt_' +parId+ '_valCell';
//column 3: template parameter value range
var c3 = document.createElement('td');
row.appendChild(c3);
c3.innerHTML = 'new_parameter_range' + r;
c3.hidden = true;
//column 4: template parameter description
var c4 = document.createElement('td');
row.appendChild(c4);
c4.id = 'hm_tplt_p' +r+ '_desc';
c4.innerHTML = 'new_parameter_description' + r;
}
//init table for possible devices
var div = document.createElement('div');
content.appendChild(div);
var table = document.createElement('table');
div.appendChild(table);
table.id = 'hm_dev_table';
table.hidden = true;
table.style.margin = '10px 0px 0px 0px';
//table.style.tableLayout = 'auto';
//table.style.width = 'auto';
var thead = document.createElement('thead');
table.appendChild(thead);
var row = document.createElement('tr');
thead.appendChild(row);
row.id = 'hm_dev_row_header';
var headerList = ['use','with device:peer','p0','p1','p2','p3','p4','p5','p6','p7','p8'];
for(var h = 0; h < 11; ++h) { // 11 columns
var thCol = document.createElement('th');
row.appendChild(thCol);
thCol.id = 'hm_dev_h' + h;
thCol.hidden = (h > 1)? true: false;;
thCol.innerHTML = headerList[h];
}
var tbody = document.createElement('tbody');
table.appendChild(tbody);
tbody.id = 'hm_dev_tbody';
//get possible links (device:peer)
//list TYPE=CUL_HM:FILTER=model=HM-CC-TC:FILTER=DEF=......
var def = $('#HMdeviceTools_reg_link_dev').attr('def');
var chnIdx = def.replace(/^....../, '');
var model = $('#HMdeviceTools_reg_link_dev').attr('model');
var peerList = (peer == '')? '': ' i:peerList';
var cmd = 'list TYPE=CUL_HM:FILTER=model=' +model+ ':FILTER=DEF=......' +chnIdx+ peerList;
if(HMdeviceTools_debug) {log('HMdeviceTools: ' + cmd);}
var url = HMdeviceTools_makeCommand(cmd);
$.get(url, function(data){
tplt.dev.clear();
/*
HM_25E38E self01,self02,
HM_3913D3 Tuer.SZ,self01,self02,
*/
lines = data.split('\n');
for(var l = 0; l < lines.length; ++l) { //init table for possible device:peer combinations
var mLine = lines[l].match(/^([^\s]+)(?:\s+(.*),)?$/);
if(mLine != null) {
var devName = (mLine[1] == 'Internals:')? device: mLine[1];
var peers = [peer];
if(mLine[2] != undefined) {peers = mLine[2].split(',');}
for(var k = 0; k < peers.length; ++k) {
var idx = (peers[k] == '')? 0: peers[k];
var devObj = {link: '', use: false, pars: []}; //dev:link(name:peer),use,pars[]
devObj.link = devName + ':' + idx;
if(!tplt.dev.has(devObj.link)) { //if no row exist in device table, append new row
tplt.dev.set(devObj.link, devObj);
var row = HMdeviceTools_appendRowForDeviceTable(devName,idx);
//if(devName == device && peers[k] == peer) {row.style.backgroundColor = '#333';}
var peerIsExtern = (peers[k] != '' && peers[k].match(/^self\d\d$/) == null);
if(devName == device && peers[k] == peer && peerIsExtern) {
var inpCheck = document.getElementById('hm_dev_use_' +devName+ ':' + idx);
var suffix = document.getElementById('HMdeviceTools_reg_link_' + peer).getAttribute('peersuffix');
inpCheck.setAttribute('peersuffix',suffix);
}
}
}
if(mLine[1] == 'Internals:') {break;} //list response for only one device without peer
}
}
});
}
function HMdeviceTools_appendRowForDeviceTable(devName, idx) {
var tbody = document.getElementById('hm_dev_tbody');
var row = document.createElement('tr');
tbody.appendChild(row);
row.id = 'hm_dev_row_' +devName+ ':' + idx;
//column 1: checkbox, selected if device is assigned to template
var c1 = document.createElement('td');
row.appendChild(c1);
c1.align = 'left';
var check = document.createElement('input');
c1.appendChild(check);
check.id = 'hm_dev_use_' +devName+ ':' + idx;
check.type = 'checkbox';
check.checked = false;
//check.value = 'on';
check.setAttribute('orgvalue','off');
check.style.margin = '5px 0px 0px 0px';
check.setAttribute('onchange',"HMdeviceTools_updateUsedDevicesTable('hm_dev_use_" +devName+ ":" +idx+ "')");
//column 2: device name
var c2 = document.createElement('td');
row.appendChild(c2);
var a = document.createElement('a');
c2.appendChild(a);
a.href = '/fhem?detail=' + devName;
a.style.cursor = 'pointer';
var div = document.createElement('div');
a.appendChild(div);
div.id = 'hm_dev_name_' +devName+ ':' + idx;
div.name = 'links';
var strIdx = (idx == 0)? 'general': idx;
div.innerHTML = devName + ':' + strIdx;
//for all 9 possible pars
for(var p = 0; p < 9; ++p) { // init table for possible pars
var td = document.createElement('td');
row.appendChild(td);
td.id = 'hm_dev_p' +p+ '_' +devName+ ':' + idx;
td.hidden = true;
td.setAttribute('orgvalue','');
}
return row;
}
function HMdeviceTools_updateTemplateDetails() {
var value = $('#hm_tplt_details').val();
//elements hide allways
$('#hm_tplt_name').hide();
$('#hm_tplt_generic').hide();
//elements show allways
$('#hm_tplt_details').show();
$('#hm_tplt_info').val(tplt.info);
$('#hm_tplt_info').prop('readOnly',true);
$('#hm_tplt_info').show();
//$('#hm_tplt_parRow_header').hide();
//$("[id^='hm_tplt_parRow_']").hide();
$('#hm_par_table th:nth-child(3),#hm_par_table td:nth-child(3)').show(); //par value input
$('#hm_par_table').show();
//elements show sometimes
$('#hm_reg_table').hide();
$('#hm_reg_table th:nth-child(1),#hm_reg_table td:nth-child(1)').show(); //reg select input
$('#hm_tplt_define').hide();
if(value == 'reg' || value == 'regset' || value == 'all') {
var val = (value == 'all')? 'reg': value;
var type = (tplt.type == '')? '': '.' + tplt.type;
$("[id^='hm_reg_row_']").hide();
if(val == 'reg') {$("[id^='hm_reg_row_'].template").show();}
if(val == 'regset') {$("[id^='hm_reg_row_']" + type).show();}
$('#hm_reg_table').show();
//console.log('details:' +value+ ' type:' +tplt.type);
}
if(value == 'usg' || value == 'all') {
($('#hm_popup_btn_use').attr('active') == 'on')? $('#hm_popup_btn_use').show(): $('#hm_popup_btn_use').hide();
$('#hm_popup_btn_useAll').show();
$('#hm_popup_btn_useNone').show();
$('#hm_popup_btn_set').hide();
$('#hm_popup_btn_unassign').hide();
$('#hm_dev_table').show();
}
else {
$('#hm_popup_btn_use').hide();
$('#hm_popup_btn_useAll').hide();
$('#hm_popup_btn_useNone').hide();
($('#hm_popup_btn_set').attr('active') == 'on')? $('#hm_popup_btn_set').show(): $('#hm_popup_btn_set').hide();
($('#hm_popup_btn_unassign').attr('active') == 'on')? $('#hm_popup_btn_unassign').show(): $('#hm_popup_btn_unassign').hide();
$('#hm_dev_table').hide();
}
if(value == 'def' || value == 'all') {
$('#hm_tplt_define').prop('readOnly',true);
$('#hm_tplt_define').show();
}
}
function HMdeviceTools_updateUsedDevicesTable(id) {
var mId = id.match(/^hm_dev_([^_]+)_(.+)$/);
if(mId != null) {
var link = mId[2];
var curDevice = document.getElementById('hm_tplt_select').getAttribute('device');
var curPeer = document.getElementById('hm_tplt_select').getAttribute('peer');
var curLink = curDevice + ':' + ((curPeer == '')? 0: curPeer);
if(mId[1] != 'use' && curLink == link) {
var input = document.getElementById(id);
if(input.getAttribute('trigger') == '') {
var nbr = mId[1].match(/\d$/);
$('#hm_tplt_p' +nbr+ '_value').attr('trigger','sync');
$('#hm_tplt_p' +nbr+ '_value').val(input.value);
$('#hm_tplt_p' +nbr+ '_value').trigger('change');
}
else {input.setAttribute('trigger','');}
}
var inpCheck = document.getElementById('hm_dev_use_' + link);
var checkIsChanged = ( inpCheck.getAttribute('orgvalue') == 'on' && inpCheck.checked == false
|| inpCheck.getAttribute('orgvalue') == 'off' && inpCheck.checked == true );
var inpParIsChanged = false; //true if any is changed
var inpParsAreDefault = true; //true if all are default
for(var i = 0; i < tplt.par.size; ++i) {
var inp = document.getElementById('hm_dev_v' +i+ '_' + link);
if(inp.getAttribute('orgvalue') != inp.value) {inpParIsChanged = true;}
if(inp.getAttribute('orgvalue') != '') {inpParsAreDefault = false;}
}
var outDev = document.getElementById('hm_dev_name_' + link);
if(checkIsChanged || inpParIsChanged && inpCheck.checked == true) {
outDev.setAttribute('class','changed');
}
else {outDev.removeAttribute('class','changed');}
//get current reg values for pars if you want to use the template (check)
var linkParts = link.split(':');
var device = linkParts[0];
var peer = (linkParts[1] == 0)? '': linkParts[1];
var peerIsExtern = (peer != '' && peer.match(/^self\d\d$/) == null);
if(tplt.par.size > 0 && inpCheck.getAttribute('orgvalue') == 'off' && inpCheck.checked == true && inpParsAreDefault) {
var peerChn01 = peer + '_chn-01';
var cmd = 'get ' +device+ ' reg all';
if(HMdeviceTools_debug) {log('HMdeviceTools: ' + cmd);}
var url = HMdeviceTools_makeCommand(cmd);
$.get(url,function(data) {
var msg = '';
var lines = data.split('\n');
for(var p = 0; p < tplt.par.size; ++p) {
var inp = document.getElementById('hm_dev_v' +p+ '_' + link);
for(var i = 2; i < lines.length; ++i) { //first line #3
var line = lines[i];
var match = line.match(/(\d):([\w.-]*)\s+([\w-]+)\s+:([^\s]+)/);
if(match != null) {
var regPeer = match[2];
var regName = match[3];
var regValue = match[4];
if((regPeer == peer || regPeer == peerChn01) && regName == inp.name) {
if(peerIsExtern && inpCheck.hasAttribute('peersuffix') == false) {
var suffix = (regPeer == peerChn01)? '_chn-01': '';
inpCheck.setAttribute('peersuffix',suffix);
}
if(inp.nodeName == 'SELECT') {
var orgvalue = inp.getAttribute('orgvalue');
inp.querySelector("option[value='" +orgvalue+ "']").remove();
}
inp.value = regValue;
inp.setAttribute('orgvalue',regValue);
if(inp.nodeName == 'SELECT') {
inp.querySelector("option[value='" +regValue+ "']").style.backgroundColor = 'silver';
}
else {
inp.title = inp.title.replace(/current:.*$/,'current:' + regValue);
inp.placeholder = '(' +regValue+ ')';
}
break;
}
}
}
if(inp.value == '') {msg += '<br>- ' +inp.name;}
}
if(msg != '') {
var hminfo = document.getElementById('HMdeviceTools_toolsTable').getAttribute('hminfo');
FW_okDialog('Some register values are not found for ' +device+ '!' +msg+
"<br><br>Verify errors with 'get " +hminfo+ " configCheck'");
}
});
}
else if(peerIsExtern && checkIsChanged && inpCheck.hasAttribute('peersuffix') == false) {
var cmd = 'list ' +peer+ ' i:DEF i:chanNo';
if(HMdeviceTools_debug) {log('HMdeviceTools: ' + cmd);}
var url = HMdeviceTools_makeCommand(cmd);
$.get(url,function(data) {
//SwitchPBU06 DEF 3913D3
// chanNo 01
var lines = data.split('\n');
var peerIsDevice = (lines[0].match(/[0-9A-F]+$/).length == 6);
var suffix = '';
if(peerIsDevice && lines.length > 1 && lines[1].match(/[0-9]+$/) == '01') {
suffix = '_chn-01';
}
inpCheck.setAttribute('peersuffix',suffix);
});
}
}
}
function HMdeviceTools_initUsedDevicesTable() {
$("[id^='hm_dev_use_']").each(function() {
this.checked = false;
this.setAttribute('orgvalue','off');
var devId = this.id.replace(/_use_/,'_name_');
document.getElementById(devId).removeAttribute('class','changed');
});
$("[id^='hm_dev_p']").empty();
}
function HMdeviceTools_initRegisterTable() {
$("[id^='hm_reg_row_']").each(function() {this.classList.remove('template');});
$("[id^='hm_tplt_reg_']").each(function() {
this.value = 'off';
this.disabled = false;
this.style.backgroundColor = 'white';
});
$("[id^='hm_reg_val_']").each(function() {
this.value = this.getAttribute('orgvalue');
this.disabled = false;
});
$("[id^='hm_reg_name_']").each(function() {this.classList.remove('changed');});
}
// update popup from mode-select
function HMdeviceTools_updatePopupMode(device,peer) {
tplt.name = '';
tplt.type = '';
tplt.info = '';
tplt.par.clear(); //par: id, name, value, masterReg, clients[]
tplt.reg.clear(); //reg: name, value, parId, master
var select = document.getElementById('hm_tplt_select');
var value = select.value;
select.style.backgroundColor = $("#hm_tplt_select option[value='" +value+ "']").attr('cat');
$('#hm_tplt_select_first').show();
HMdeviceTools_initRegisterTable();
HMdeviceTools_changeRegNamesFromTemplateType();
if (value == 'expert') { //######################################################################
$('#hm_tplt_name').hide();
$('#hm_tplt_generic').hide();
$('#hm_tplt_details').hide();
$('#hm_tplt_info').hide();
$('#hm_par_table').hide();
$('#hm_dev_table').hide();
$('#hm_tplt_define').hide();
$('#hm_reg_table th:nth-child(1),#hm_reg_table td:nth-child(1)').hide();
$('#hm_reg_table').show();
$("[id^='hm_popup_btn_use']").hide();
$('#hm_popup_btn_allOn').hide();
$('#hm_popup_btn_allOff').hide();
$('#hm_popup_btn_check').hide();
$('#hm_popup_btn_execute').hide();
$('#hm_popup_btn_set').hide();
$('#hm_popup_btn_unassign').hide();
$('#hm_popup_btn_delete').hide();
$('#hm_popup_btn_show').hide();
$('#hm_popup_btn_define').hide();
HMdeviceTools_showApplyBtn();
} else if (value == 'new') { //##################################################################
$('#hm_tplt_details').hide();
$('#hm_dev_table').hide();
$('#hm_tplt_name').val(tplt.name);
$('#hm_tplt_name').show();
$('#hm_tplt_generic').val('');
(select.getAttribute('generic') == 'true')? $('#hm_tplt_generic').show(): $('#hm_tplt_generic').hide();
$('#hm_tplt_info').val('');
$('#hm_tplt_info').prop('readOnly',false);
$('#hm_tplt_info').show();
$('#hm_par_table th:nth-child(3),#hm_par_table td:nth-child(3)').hide(); //par value input
$('#hm_tplt_parRow_header').hide();
$("[id^='hm_tplt_parRow_']").hide();
$('#hm_par_table').show();
$('#hm_reg_table th:nth-child(1),#hm_reg_table td:nth-child(1)').show(); //reg select input
$('#hm_reg_table').show();
$('#hm_tplt_define').val('');
$('#hm_tplt_define').show();
$("[id^='hm_popup_btn_use']").hide();
$('#hm_popup_btn_allOn').show();
$('#hm_popup_btn_allOff').show();
//$('#hm_popup_btn_check').hide();
//$('#hm_popup_btn_execute').hide();
$('#hm_popup_btn_set').hide();
$('#hm_popup_btn_unassign').hide();
$('#hm_popup_btn_delete').hide();
$('#hm_popup_btn_show').show();
$('#hm_popup_btn_define').show();
$('#hm_popup_btn_apply').hide();
} else { //template name ########################################################################
$('#hm_par_table').hide();
$('#hm_dev_table').hide();
$('#hm_reg_table').hide();
$('#hm_tplt_define').hide();
HMdeviceTools_initUsedDevicesTable();
HMdeviceTools_parseTemplateFromTemplateList(device,peer,value);
$('#hm_popup_btn_allOn').hide();
$('#hm_popup_btn_allOff').hide();
//$('#hm_popup_btn_execute').hide();
//$('#hm_popup_btn_check').show();
$('#hm_popup_btn_show').hide();
$('#hm_popup_btn_define').hide();
$('#hm_popup_btn_apply').hide();
var mode = $('#hm_tplt_details').val();
var isUseMode = (mode == 'usg' || mode == 'all')? true: false;
if(select.style.backgroundColor == 'lightgreen') {
$('#hm_popup_btn_set').attr('active','off');
if(!isUseMode) {$('#hm_popup_btn_set').hide();}
$('#hm_popup_btn_unassign').attr('active','on');
if(!isUseMode) {$('#hm_popup_btn_unassign').show();}
$('#hm_popup_btn_delete').hide();
}
else if(select.style.backgroundColor == 'yellow') {
$('#hm_popup_btn_set').attr('active','on');
if(!isUseMode) {$('#hm_popup_btn_set').show();}
$('#hm_popup_btn_unassign').attr('active','off');
if(!isUseMode) {$('#hm_popup_btn_unassign').hide();}
$('#hm_popup_btn_delete').hide();
}
else if(select.style.backgroundColor == 'white') {
$('#hm_popup_btn_set').attr('active','on');
if(!isUseMode) {$('#hm_popup_btn_set').show();}
$('#hm_popup_btn_unassign').attr('active','off');
if(!isUseMode) {$('#hm_popup_btn_unassign').hide();}
if(tplt.type != '') {
var otherName = (tplt.type == 'short')? tplt.name + '_long': tplt.name + '_short';
if($("#hm_tplt_select option[value='" +otherName+ "']").attr('cat') == 'white') {
$('#hm_popup_btn_delete').show();
}
else {$('#hm_popup_btn_delete').hide();}
}
else {$('#hm_popup_btn_delete').show();}
}
}
}
// parse new template from inputs
function HMdeviceTools_parseTemplateFromInputs(device,peer,id) {
if(id != null) {
/*input_element.id =>
tplt.name: hm_tplt_name
tplt.type: hm_tplt_name ('',_short,_long)
tplt.info: hm_tplt_info
tplt.par.id: hm_tplt_reg_<regname>
tplt.par.name: hm_tplt_p<0...8>_nameIn
hm_tplt_p<0...8>_nameOut
tplt.par.value: hm_tplt_p<0...8>_value
*/
var match = id.match(/^hm_tplt_([^_]+)(?:_(.*)|)$/);
if(match != null) {
var input = document.getElementById(id);
if(match[1] == 'reg') { //inputs: select register and parameter #############################
var regObj = {}; //reg: name, value, parId, master
var newMasterReg = {}; //reg: name, value, parId, master
var newPar = {}; //par: id, name, value, masterReg, clients[]
var oldPar = {}; //par: id, name, value, masterReg, clients[]
var color = 'red';
var regname = match[2];
var newReg = !tplt.reg.has(regname);
if(newReg) {
regObj.name = regname;
regObj.value = '';
regObj.parId = '';
regObj.master = false;
color = 'lightgreen';
}
else {regObj = tplt.reg.get(regname);}
var oldMaster = regObj.master;
var oldParId = regObj.parId;
var newParId = (input.value.match(/^p\d$/))? input.value: '';
regObj.parId = newParId;
//remove or change old par
if(oldParId) {
regObj.master = false;
oldPar = tplt.par.get(oldParId);
if(oldMaster && oldPar.clients.length == 0) { //remove old single par
tplt.par.delete(oldParId);
}
else { //change old multi par
if(oldMaster && oldPar.clients.length > 0) { //change master, change par description?
oldPar.masterReg = oldPar.clients.shift();
newMasterReg = tplt.reg.get(oldPar.masterReg);
newMasterReg.master = true;
tplt.reg.set(newMasterReg.name,newMasterReg);
}
else { //remove client