-
Notifications
You must be signed in to change notification settings - Fork 1
/
91_ntf.pm
1497 lines (1371 loc) · 60.9 KB
/
91_ntf.pm
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
##############################################
# $Id: 91_ntf.pm 24129 2021-04-02 16:56:29Z rudolfkoenig $
package main;
use strict;
use warnings;
use vars qw($FW_ME); # webname (default is fhem)
#####################################
## disable will stop all notification. Restart cost more performance
## active/inactive just stops reading capture and command execution
##
##
#####################################
sub ntf_Initialize($) {
my ($hash) = @_;
$hash->{DefFn} = "ntf_Define";
$hash->{AttrFn} = "ntf_Attr";
# programmer tutorial
# with the assignment of a NotifyFn each entity will be triggered with each and every
# configuration action (define, rename, delete, attr)
# readings setting
# for all entities in the system
# For perfornamce reasons it is strictly adviced to restrict the trigger.
# by using "notifyRegexpChanged" methode the kernal will be enabled to restrict the
# notifications to relevant "trigger-sourcing entites"
# see define function and notification for an example
$hash->{NotifyFn} = "ntf_Notify";
$hash->{SetFn} = "ntf_Set";
$hash->{GetFn} = "ntf_Get";
$hash->{StateFn} = "ntf_State";
ntf_defCmdAttrModule($hash);
}
sub ntf_defCmdAttrModule($) {
my $hash = shift; # hash to module
# programmer tutorial
# this methode defines the set-commands, get-commands and attributes used.
# any of those can be identical for each entity of the module-TYPE or individual
# per entity.
# Nomenclature:
# () identifies a list of valid options separated by '|'
# example (opt1|opt2|opt3|...)
# [] identifies an optional parameter.
# optional parameter must be the last in the definition. I.e must not be
# followed by required parameter
# legal example: "(opt1|opt2) [-hello-] [-timeout-]
# illegal example: "(opt1|opt2) [-hello-] -timeout-
# {} identifies the default value of an optional parameter.
# logically default values shall only be assigned to parameter if
# all of the pre-params are required or assigned a default as well
# legal example: "(opt1|opt2) [(opt3|{opt4})] [-timeout-]
# illegal example: "(opt1|opt2) [-timeout-] [(opt3|{opt4})]
# opt4 cannot be assigned if timeout is not given
# -- identifies an individual value-set. Programmer can dynamically
# stuff the value-set with legal entries and take advntage of system parsing
# or leave this value-set unspecified
# '' offers the option or comment the command for short user advice or reminder
# there is no operational function assigned
# ... states that the number of parameter is not restricted to the params in the
# definition
# min..max;step
# defined a value range for a parameter. Quite obvious the entered value must
# be in the range min<=X<=max.
# step is optional and defaults to '1' if not given. The parser will round the
# user inputs according to step
# 0..100;1 # will allow the entry of a value between 0 and 100
# (0..100;1) # offers a drop-down list in the web-frontend
# [(0..100;1|{10})] # is optional. if not given '10' will be used
# [(0..100;1|{10}|on|off)] # is optional. besides 0..100 also the
# # liternal on/off are valid options
# keywords: 'multiple','multiple-strict' or 'textField-long'
#
# Definition and assignment:
# Programmer should define a hash per 'set','get' and 'attr' with an entry per
# command and the definition as string
# If the commands are identical for all the entites of the module it is
# recommended to define the hashes once at module init. It is good practice to
# assign the reference to the command-definition to each entity at definition.
# This will reduce memor as well as processing performance. Example:
# $hash->{helper}{cmds}{sets} = $modules{$type}{set};
# $hash->{helper}{cmds}{gets} = $modules{$type}{get};
# $hash->{helper}{cmds}{attr} = $modules{$type}{attr};
# Possibly the commands vary from entiy to entity. By then it is up to the
# programmer to define a hash per entiy.
# Pre-processing: the function will automatically pre-process the commands once
# for perfornamce reasons. An additional entry '.cmdPrep' is added. Programmer
# can force re-evaluation of the command-list by deleting this entry. Example:
# delete $modules{$type}{set}{'.cmdPrep'}
#
# Dynamic value list
# Dynamic list of values actually are quite common requirement and it is quite
# powerful supported if the programmer takes advantate of the offer.
# Any value in '--' can be assigned a dynamic valueList.
# Example:
# $hmod->{set}{param} = "-pValueList-";
# pValueList is now the name of a value. Programmer can assign a list of comma
# separated value-list per module in $hash->{helper}{cmds}{dynValLst}
# $hash->{helper}{cmds}{dynValLst}{pValueList} = "param1,param2,param3"
# The functions will parse the entered command for valid options and - if specified -
# offer a drop-down list for command entry
#
# Functions and operations
# at command execution (set/get)
# The command parser will check the user input for validity. It will in addition
# generate the system response. Very important: The reply to undefined commands
# is generated automatically and performat. The "set entity ?" is the most common
# command required for user interface and web-frontend. programmer should take
# advantage of the offer to reply here.
# Programme can rely on that the user-input complies to the command definition
# Example of parser usage
# sub ntf_Set($@) {
# my ($hash, @a) = @_;
# my $err = ntf_cmdParser($hash,$hash->{helper}{cmds}{sets},@a);
# return $err if($err);
# at attribute assignment
# actually no difference to set/get. Atributes only have one value - nevertheless
# the supply for the web-frontend is similar important as well as the drop-down
# lists and the verification of parameters for valid.
# get cmdList
# The get-command 'cmdList' should be implemented for all entites.
#
{ # set
$hash->{helper}{cmds}{sets}{inactive} = "";
$hash->{helper}{cmds}{sets}{active} = "";
$hash->{helper}{cmds}{sets}{clear} = "(trigger|readings|{readingLog})";
$hash->{helper}{cmds}{sets}{simTrigger} = "-trigger- ...";
$hash->{helper}{cmds}{sets}{trgAdd} = "(-name-) [(:)] [(-event-)] ...";
$hash->{helper}{cmds}{sets}{trgDel} = "(-combTrgElement-)'part of a combined trigger attribut'";
# $hash->{helper}{cmds}{sets}{tstAddSet} = "-cmd- [-param-] ...";
# $hash->{helper}{cmds}{sets}{tstAddGet} = "-cmd- [-param-] ...";
# $hash->{helper}{cmds}{sets}{tstAddAttr} = "-cmd- [-param-] ...";
# $hash->{helper}{cmds}{sets}{tstDelSet} = "(-localSetCmd-)";
# $hash->{helper}{cmds}{sets}{tstDelGet} = "(-localGetcmd-)";
# $hash->{helper}{cmds}{sets}{tstDelAttr} = "(-localAttr-)";
# $hash->{helper}{cmds}{sets}{tst1} = "(-combTrgElement-) [(a|{b})] [-text-] [(1|2|3|4)]";
# $hash->{helper}{cmds}{sets}{tst2} = "(-combTrgElement-) [(a|{b})] [-text-] [({1}|2|3|4)]";
# $hash->{helper}{cmds}{sets}{tst3} = "(-combTrgElement-) [(a|{b})] [({def}|-text-)] [(1|2|3|4)]";
# $hash->{helper}{cmds}{sets}{tst4} = "(-combTrgElement-) [(a|b)] [-text-] [(1|2|3|4)]";
# $hash->{helper}{cmds}{sets}{tst5} = "(-combTrgElement-) (a|{b}) [-text-] [(1|2|3|4)]";
# $hash->{helper}{cmds}{sets}{tst7} = "(-combTrgElement-) [(a|{b})] [({def}|-text-)] [(1|2|{3}|{4})]";
# $hash->{helper}{cmds}{sets}{tst8} = "(-combTrgElement-) [(a|{b})] [({def}|-text-)] [(1|2|3|{4})]";
# $hash->{helper}{cmds}{sets}{tst9} = "(-combTrgElement-) [(1..112;5|{b})] [-text-] [(1|2|3|4)] 'haha'";
}
{ # get
$hash->{helper}{cmds}{gets}{list} = "[({default}|hidden|module)]";
$hash->{helper}{cmds}{gets}{cmdList} = "[({short}|long)]";
$hash->{helper}{cmds}{gets}{clear} = "(trigger|readings)";
$hash->{helper}{cmds}{gets}{shEnroled} = "";
$hash->{helper}{cmds}{gets}{trgFilter} = "";
}
{ # attr
$hash->{helper}{cmds}{attr}{disable} = '(1|0)';
$hash->{helper}{cmds}{attr}{disabledForIntervals} = '-disable-';
$hash->{helper}{cmds}{attr}{disabledAfterTrigger} = '-seconds-';
$hash->{helper}{cmds}{attr}{forwardReturnValue} = "(1|0)";
$hash->{helper}{cmds}{attr}{ignoreRegexp} = "(1|0)";
$hash->{helper}{cmds}{attr}{readLog} = "(1|0)";
$hash->{helper}{cmds}{attr}{trgDevice} = "multiple,(-trgNames-) 'regex possible light.* or (light1|light2)'";
$hash->{helper}{cmds}{attr}{trgEvent} = "textField-long,-Event- 'Reading:value'";
$hash->{helper}{cmds}{attr}{trgReading} = "-readingRegex- 'reading(s) to be evaluated e.g.'";
$hash->{helper}{cmds}{attr}{trgReadValue} = "-readingValueRegex-)] 'value of the reading'";
$hash->{helper}{cmds}{attr}{trgCombined} = "textField-long,-NameEvent- 'should have a colon <name1>:<event1>,<name2>:<event2>'";
$hash->{helper}{cmds}{attr}{trgCmd} = "textField-long,-Cmd-";
$hash->{helper}{cmds}{attr}{logTrgNames} = "(1|0)";
}
}
sub ntf_defCmdAttrEntity($) {
my $hash = shift; # hash to module
# programmer tutorial
# define sets gets and attributes unique for an entity
#
#
{ # set
;
# $hash->{helper}{cmds}{sets}{param} = "-param-" ;
# $hash->{helper}{cmds}{sets}{count4} = "'change:'[(-testList-)]";
# $hash->{helper}{cmds}{sets}{counttrgEvt} = 'textField-long,-Event-' ;
# $hash->{helper}{cmds}{sets}{cSlider2} = "'change:'slider,(0..10;4|{10}|-testList2-)" ;
}
{ # get
;
# $hash->{helper}{cmds}{gets}{Eget} = "[({normal}|full)]" ;
}
{ # attr
;
# $hash->{helper}{cmds}{attr}{Eattr} = '(hallo|holla)' if ($hash->{NAME} eq "aa");
# $hash->{helper}{cmds}{attr}{test} = 'textField-long,-NameEvent-';
# $hash->{helper}{cmds}{attr}{d1} ='(-disable1-)';
# $hash->{helper}{cmds}{attr}{d2} ='-disable2-';
# $hash->{helper}{cmds}{attr}{d3} ='(a|b|-disable3-)';
}
}
sub ntf_Define($$) {#####################################
my ($hash, $def) = @_;
my ($name, $type, $re, $command) = split("[ \t\n]+", $def, 4);
$hash->{helper}{active} = 1;
$hash->{helper}{disbled} = 0;
ntf_defCmdAttrEntity($hash);# programmer tutorial: define sets, gets, attr unique per module
ntf_cmdParser($hash,"attr",("",""));
InternalTimer(gettimeofday()+2, sub(){notifyRegexpChanged($hash,0,1); }, $hash);# no notification required for this device
if (defined $re && $re ne ""){
CommandAttr (undef,"$name trgCombined $re");
if (defined $command && $command ne ""){
CommandAttr (undef,"$name trgCmd $command");
}
}
readingsSingleUpdate($hash, "state", "active", 1);
return undef;
}
sub ntf_AttrCheck(@) {############################
# verify if attr is applicable
# programmer tutorial
# recommended usage
# my $chk = ntf_AttrCheck($name, $aSet, $aName, $aVal);
# return undef if ($chk && $chk eq "ignoreAttr");
# return $chk if ($chk);
# Usage
# sub ntf_Attr(@) {
# my @a = @_;
# my ($aSet,$name,$aName,$aVal) =@a;
# my $hash = $defs{$name};
# my $chk = ntf_AttrCheck($hash, $aSet, $aName, $aVal);
# return undef if ($chk && $chk eq "ignoreAttr");
# return $chk if ($chk);
my ($aSet,$name,$aName,$aVal) = @_;
if ( !defined $modules{$defs{$name}{TYPE}}{helper}{cmds}{attr}{'.cmdPrep'}
|| !defined $defs{$name}{helper}{cmds}{attr}{'.cmdPrep'}){
ntf_cmdParser($defs{$name},"attr",("",""));
}
return "ignoreAttr" if (!$init_done);
return undef if ($aSet ne "set"); # allow delete any time
my $chk = ntf_cmdParser($defs{$name},"attr",($name,$aName,(defined $aVal?$aVal:"")));
if ($chk && $chk =~ m/^Unknown/){#not a module attribute
my $a = " ".getAllAttr($name)." ";
if($a !~ m/ $aName[ :]+/){
$a =~ s/:.*? //g;
return "attribut $aName not valid. Use one of $a";
}
else{
return "ignoreAttr" ; # attr valid but not in module context - ok for me
}
}
my $attrOpt = defined $modules{$defs{$name}{TYPE}}{helper}{cmds}
&& defined $modules{$defs{$name}{TYPE}}{helper}{cmds}{attr}
&& defined $modules{$defs{$name}{TYPE}}{helper}{cmds}{attr}{'.cmdPrep'}
&& defined $modules{$defs{$name}{TYPE}}{helper}{cmds}{attr}{'.cmdPrep'}{cmd}
&& defined $modules{$defs{$name}{TYPE}}{helper}{cmds}{attr}{'.cmdPrep'}{cmd}{$aName}
? $modules{$defs{$name}{TYPE}}{helper}{cmds}{attr}{'.cmdPrep'}{cmd}{$aName}{paraOpts}
: defined $defs{$name}{helper}{cmds}{attr}
&& defined $defs{$name}{helper}{cmds}{attr}{'.cmdPrep'}
&& defined $defs{$name}{helper}{cmds}{attr}{'.cmdPrep'}{cmd}
&& defined $defs{$name}{helper}{cmds}{attr}{'.cmdPrep'}{cmd}{$aName}
? $defs{$name}{helper}{cmds}{attr}{'.cmdPrep'}{cmd}{$aName}{paraOpts}
:"";
return undef if (!$attrOpt # any value allowed
|| $attrOpt =~ m/^(multiple|textField-)/ # any value allowed
|| $attrOpt !~ m/^\(\)$/ # no list defined
|| grep/^$aVal$/,split(",",$attrOpt) # identified
);
return "value $aVal not allowed. Choose one of:$attrOpt";
}
sub ntf_cmdParser($$@){#hash to entity, hash to commands, input array
my $hash = shift;
my $type = shift;
my $ref = \@_;
my $cmd = @$ref[1];
my $ret;
return "ntf_cmdParser called while hash not defined - contact admin" if (!defined $hash
|| !defined $hash->{TYPE}
|| !defined $modules{$hash->{TYPE}});
# programmer tutorial
# this sub can be used generic for ANY module.
# it has potential and should actually be included in the kernal SW
# usage
# 1) set
# sub ntf_Set($@) {
# my ($hash, @a) = @_;
# my $chk = ntf_cmdParser($hash,"sets",@a);
# return undef if ($chk && $chk eq "done");
# return $chk if($chk);
# 2) get
# sub ntf_Get($@) {
# my ($hash, @a) = @_;
# my $chk = ntf_cmdParser($hash,"gets",@a);
# return undef if ($chk && $chk eq "done");
# return $chk if($chk);
# 3) attr
# sub ntf_Attr(@) {
# see ntf_AttrCheck($hash,$aSet, $aName,$aVal);
# 4) update {ntf_cmdParser($defs{a},"attr",(undef,"updt"))}
# my $chk = ntf_cmdParser($hash,<gets|sets|attr>,(undef,"updt",<{module}|device>));
################# do we have to update the pre-processing section?
my @defaults = ();
my @sources;
foreach my $h ($hash,$modules{$hash->{TYPE}}){ #first modules - then optional overwrite by entits
push @sources,( !defined $h->{helper}
||!defined $h->{helper}{cmds}
||!defined $h->{helper}{cmds}{$type}
) ? 0
: $h->{helper}{cmds}{$type}
;
}
if($cmd eq "updt"){
delete $sources[0]->{'.cmdPrep'} if($sources[0]); # renew device update always
if (!defined @$ref[2] || @$ref[2] ne "device"){
delete $sources[1]->{'.cmdPrep'} if($sources[1]); # renew device update optional
}
# return undef;
}
my $updated = 0;
foreach my $pass (1,0){ #first modules - then optional overwrite by entits
my $h = $sources[$pass];
next if(!$h);
if (!defined $h->{'.cmdPrep'}){#parse settings and prepare shortcuts
$updated = 1;
if($pass == 1){# the module def changed - invalidate the entity as well - for all of the module entites
foreach (devspec2array("TYPE=$hash->{TYPE}")){
delete $defs{$_}{helper}{cmds}{$type}{'.cmdPrep'}
if ( defined $defs{$_}{helper}
&& defined $defs{$_}{helper}{cmds}
&& defined $defs{$_}{helper}{cmds}{$type}
);
}
}
$h->{'.cmdPrep'}{init} = 1;
foreach my $defCmd (grep !/\./,keys %$h){ # prepare and parse the inputs
$h->{'.cmdPrep'}{cmd}{$defCmd}{paraOpts} = $h->{$defCmd};
my $hCmdIdx = $h->{'.cmdPrep'}{cmd}{$defCmd};
$hCmdIdx->{paraOpts} =~ s/\'.*?\'//g;
$hCmdIdx->{paraOpts} =~ s/^(textField-long,|multiple,|multiple-strict,|slider,)//g;
$hCmdIdx->{paraOpts} =~ s/[\s]+/ /g;
$hCmdIdx->{max} = $h->{$defCmd} =~ m/\.\.\./ ? 99 : scalar(split(" ",$hCmdIdx->{paraOpts}));
$hCmdIdx->{paraOpts} =~ s/\.\.\.//g;
my ($reqOpts) = map{my $foo = $_;$foo =~ s/(\[.*?\])//g;$foo}($hCmdIdx->{paraOpts}); # remove optionals
$hCmdIdx->{min} = scalar(split(" ",$reqOpts));
my $pCnt = 0;
my $defUpTo = 0;
foreach my $param (split(" ",$hCmdIdx->{paraOpts})){
my ($opt,$def) = (0,0);
if($pCnt > $hCmdIdx->{min}){
if($param !~ m/^\[.*\]$/){ #any after min must be optional
Log3 $hash->{NAME},1,"non optional parameter in $defCmd at position $pCnt while optional param before";
}
}
$pCnt++;
}
$hCmdIdx->{paraOpts} =~ s/[\[\]]//g;
$hCmdIdx->{paraOpts} =~ s/[\s]+/ /g;
}
}
}
if($updated){ # prepare answer for "set <entity> ?" - generate "unknown"
my %cPrepH;
foreach my $pass (1,0){ #first modules - then optional overwrite by entits
my $h = $sources[$pass];
next if(!$h);
# prepare reply for "set/get <entity> ?"
foreach my $cmdS (grep !/\.cmdPrep/,keys %{$h}){
my $val = $h->{$cmdS};
$val =~ s/\'.*?\'//g; # remove comments
my $def = $val =~ m/\{(.*)\}/ ? $1 : "";
$val =~ s/[\{\}]//g; # remove default marking - not relevant for web-frontend
$val =~ s/\s*$//g; # remove default marking - not relevant for web-frontend
if ($val =~ m/^textField-long/){ # textfield is unique
$val = ":textField-long";
}
elsif($val =~ m/^slider,/) { # implement slider
if($val =~ m/slider,[\d.]+,[\d.]+,?[\d.]*$/){
$val = ":$val";
}
elsif($val =~ m/slider,\(([\d.]+)\.\.([\d.]+)[;|\)]([\d.])?[|\)]?/){
$val = ":slider,$1,".(defined $3 ? "$3": "1").",$2";
}
else{
$val = "";
}
}
elsif($val =~ m/^(noArg|)$/) { # no argument required
$val = ":noArg";
}
elsif($val =~ m/ / # multi-parameter - no quick select
||$val !~ m/^(\[|multiple,|multiple-strict,)?\(.*\)\]?$/){
$val = "";
}
else { # no space - this is a single param command (or less)
my ($dispOpt,$list) = $val =~ m/(.*)\((.*)\)/;
$dispOpt =~ s/[\[,]//g; # preserve display option, e.g. multiple
my %items;
foreach my $item (split('\|',$list)){
if ($item =~ m/(.*)\.\.(.*)/ ){ #"(0..100;1)"
my ($min,$max,$step) = ($1,$2,1);
if ($max =~ m/(.*);(.*)/){
($max,$step) = ($1,$2);
}
my $f = 0;
($f) = map{(my $foo = $_) =~ s/.*\.//;length($foo)}($step) if ($step =~ m/\./);
my $m = ($max - $min)/$step;
$items{$_} = 1 foreach(map{sprintf("%.${f}f",$min + $_ * $step)}(0..$m));
}
else{
$items{$item} = 1;
}
}
$val = ":".join(",",$def # place default first
,grep!/$def/,(sort { $a <=> $b } grep /^[\d\.]+$/,keys %items)
,(sort grep!/^[\d\.]+$/,keys %items));
$val =~ s/:,/:/;
$val =~ s/:/:$dispOpt,/ if($dispOpt);
}
$cPrepH{$cmdS} = $val;
}
if ($type eq "attr"){# attr does not require "unknown arg" but the definition of an attrList
my $attrHash;
if ($pass == 0){# entity level
if(defined $hash->{helper}{cmds}{attr}{'.cmdPrep'}{cmd}){
$hash->{'.AttrList'} = join(" ",map{$_.$cPrepH{$_}} keys %cPrepH) ;
$attrHash = \$hash->{'.AttrList'};
}
else{
delete $hash->{'.AttrList'};
}
}
else{ # module level
$modules{$hash->{TYPE}}{AttrList} = join(" ",map{$_.$cPrepH{$_}} keys %cPrepH);
$attrHash = \$modules{$hash->{TYPE}}{AttrList};
}
if ($attrHash && $$attrHash =~ m/\-[^\s]*\-/){# add dynamic param options
if( $pass == 0
&& defined $hash->{helper}
&& defined $hash->{helper}{cmds}
&& defined $hash->{helper}{cmds}{dynValLst}){
foreach(keys %{$hash->{helper}{cmds}{dynValLst}}){ # add dynamic values
my $f = $$attrHash =~ s/\-$_\-/$hash->{helper}{cmds}{dynValLst}{$_}/g;
}
}
if( defined $modules{$hash->{TYPE}}->{helper}
&& defined $modules{$hash->{TYPE}}->{helper}{cmds}
&& defined $modules{$hash->{TYPE}}->{helper}{cmds}{dynValLst}){
foreach(keys %{$modules{$hash->{TYPE}}->{helper}{cmds}{dynValLst}}){ # add dynamic values
my $f = $$attrHash =~ s/\-$_\-/$modules{$hash->{TYPE}}->{helper}{cmds}{dynValLst}{$_}/g;
}
}
$$attrHash =~ s/\-[^\s]*\-//g;
$$attrHash =~ s/( )//g;
$$attrHash =~ s/:,/:/g;
}
}
else{
$h->{'.cmdPrep'}{unknown} = join(" ",map{$_.$cPrepH{$_}} sort keys %cPrepH)." ";
}
}
}
################# is the command/attr defined
my $found = 0;
foreach my $pass (1,0){ # check first entity then module - prio
my $h = $sources[$pass];
next if(!$h);
if(defined($h->{$cmd})) {# is command defined?
$found = 1;
# command is defined check number of parameter
my $paramCnt = scalar(@$ref-2);
my $hCmdPrep = $h->{'.cmdPrep'}{cmd}{$cmd};
if($paramCnt < $hCmdPrep->{min} || $paramCnt > $hCmdPrep->{max}){# command parameter count ok?
$ret = "$cmd requires $hCmdPrep->{min}: $h->{cmd}{$cmd}"
."\n number of parameter given:$paramCnt"
."\n $cmd:$h->{$cmd}"
;
}
else{ # number of parameter is ok - check content
my @parIn = @$ref[2..$#$ref];
my $pCnt = 0;
my $paraFail = "";
foreach my $param (split(" ",$hCmdPrep->{paraOpts})){ # check each parameter
if($paramCnt -1 < $pCnt){ # no param input given - use default if available
if ($param =~ m/\(.*\{(.*)\}.*\)/){
push @defaults,$1;
}
else{
last; # no input params anymore, defaults anymore - we are finished
}
}
else{ # still input params - check content
$param =~ s/.*\((.*)\).*/$1/;# remove brackets
$param =~ s/[\{\}]//g; # remove brackets for default
my $found2 = 0;
next if (!$param);
foreach my $option(split('\|',$param)){
if ($parIn[$pCnt] eq $option){ # valid
$found2 = 1;
last;
}
elsif($option =~ m/^-(.*)-$/){
my $optSpecial = $1;
my $par = $parIn[$pCnt];
if ( defined $hash->{helper}
&& defined $hash->{helper}{cmds}
&& defined $hash->{helper}{cmds}{options}
&& defined $hash->{helper}{cmds}{options}{$optSpecial}){#options defined - so have to match
if ($hash->{helper}{cmds}{options}{$optSpecial} =~ m/[^,]$par[,]/){
$found2 = 1;
last;
}
}
else{#wildcard: anything matches
$found2 = 1;
last;
}
}
elsif($option =~ m/^(\d+)..(\d+);?(\d*)/){
if($parIn[$pCnt]>=$1 && $parIn[$pCnt]<=$2) {
$found2 = 1;
last;
}
else{
}
}
}
if(!$found2){
$ret = "param $pCnt:'$parIn[$pCnt]' does not match options '$param'";
last;
}
}
$pCnt++;
}
}
}
}
if (!$found){
$ret = $sources[0] && $sources[0]->{'.cmdPrep'} && $sources[0]->{'.cmdPrep'}{unknown} ? $sources[0]->{'.cmdPrep'}{unknown}
:$sources[1] && $sources[1]->{'.cmdPrep'} && $sources[1]->{'.cmdPrep'}{unknown} ? $sources[1]->{'.cmdPrep'}{unknown}
:"";
if ( $ret && $ret =~ m/\-.*\-/){
if( defined $hash->{helper}
&& defined $hash->{helper}{cmds}
&& defined $hash->{helper}{cmds}{dynValLst}){
foreach(keys %{$hash->{helper}{cmds}{dynValLst}}){ # add dynamic values
$ret =~ s/\-$_\-/$hash->{helper}{cmds}{dynValLst}{$_}/g;
}
}
if ( $ret =~ m/\-.*\-/){#remove list if any general value is still available
# cmd:para1,para2,-placeholder- => cmd
$ret =~ s/([^\s]*?):[^\s]*?-[^\s]*?-[^\s]*? / $1 /g;
# cmd:multiple,para1,para2,-placeholder- => cmd:multiple,para1,para2
$ret =~ s/multiple([^\s]*),-[^\s]*-/multiplexx$1/g; # remove -val- from multiple but keep the statement
}
}
$ret = "Unknown argument $cmd, choose one of $ret";
}
return ($ret,@defaults) if ($ret);
if($type eq "gets"){# system relevant commands
if($cmd eq "list") { ###############################################
#"[({default}|hidden|module)]"
my $globAttr = AttrVal("global","showInternalValues","undef");
$attr{global}{showInternalValues} = @$ref[2] eq "default" ? 0 : 1;
$ret = @$ref[2] eq "module" ? "MODULE: $hash->{TYPE}\n".PrintHash($modules{$hash->{TYPE}}, 2)
: CommandList(undef,$hash->{NAME});
if ($globAttr eq "undef"){
delete $attr{global}{showInternalValues};
}
else{
$attr{global}{showInternalValues} = $globAttr;
}
}
elsif($cmd eq "cmdList") { ###############################################
foreach my $type ("sets","gets","attr"){
$ret .= "commands for $type\n ";
my @cmd = ();
push @cmd,map{$_ =~ s/:(textField-long,|multiple,|multiple-strict,|slider,)/:/g;$_}
map {"$_\t:$hash->{helper}{cmds}{$type}{$_}"}
grep!/^\./,
keys %{$hash->{helper}{cmds}{$type}}
if( defined $hash->{helper}
&& defined $hash->{helper}{cmds}
&& defined $hash->{helper}{cmds}{$type})
;
push @cmd,map{$_ =~ s/:(textField-long,|multiple,|multiple-strict,|slider,)/:/g;$_}
map {"$_\t:$modules{$hash->{TYPE}}->{helper}{cmds}{$type}{$_}"}
grep!/^\./,
keys %{$modules{$hash->{TYPE}}->{helper}{cmds}{$type}}
if( defined $modules{$hash->{TYPE}}{helper}
&& defined $modules{$hash->{TYPE}}->{helper}{cmds}
&& defined $modules{$hash->{TYPE}}->{helper}{cmds}{$type});
$ret .= join("\n ",sort @cmd);
$ret .= "\n\n";
}
}
}
return ($ret,@defaults);
}
sub ntf_Attr(@) {###################################
my @a = @_;
my $chk = ntf_AttrCheck(@a);
return undef if ($chk && $chk eq "ignoreAttr");
return $chk if ($chk);
my ($aSet,$name,$aName,$aVal) = @a;
my $hash = $defs{$name};
if ($aName eq "readLog" ) {
if ($aSet eq "set"){
if($aVal) {
$logInform{$name} = sub($$){
my ($me, $msg) = @_;
return if(defined($hash->{CHANGED}));
$hash->{CHANGED}[0] = $msg;
ntf_Notify($hash, $hash);
delete($hash->{CHANGED});
}
}
else {
delete $logInform{$name};
}
return;
}
}
elsif ($aName eq "trgDevice" ) {
ntf_prepTgrNames($hash
,undef
,($aSet eq "set" ? $aVal : "")
,undef
);
}
elsif ($aName eq "trgEvent" ) {
my $read = AttrVal($name,"trgReading",undef);
my $readVal = $read ? "$read: ". AttrVal($name,"trgReadValue",".*")
: "";
ntf_prepTgrNames($hash
,undef
,undef
,join("|",grep/./,($readVal
,$aSet eq "set" ? $aVal
: ".*"))
);
}
elsif ($aName eq "trgReading" ) {
ntf_prepTgrNames($hash
,undef
,undef
,join("|",grep/./,(($aSet eq "set" && $aVal
? $aVal.": ". AttrVal($name,"trgReadValue",".*")
: undef)
,AttrVal($name,"trgEvent","")))
);
}
elsif ($aName eq "trgReadValue") {
ntf_prepTgrNames($hash
,undef
,undef
,join("|",grep/./,(($aSet eq "set" && $aVal
? AttrVal($name,"trgReading",".*").": ". $aVal
: undef)
,AttrVal($name,"trgEvent","")))
);
}
elsif ($aName eq "trgCombined" ) {
ntf_prepTgrNames($hash
,($aSet eq "set" ? $aVal : "")
,undef
,undef
);
if($aSet eq "set"){
$hash->{helper}{cmds}{dynValLst}{combTrgElement} = $aVal;
}
else{
delete $hash->{helper}{cmds}{dynValLst}{combTrgElement};
}
}
elsif ($aName eq "trgCmd" ) {
if($aSet eq "set"){
my %specials= (
"%NAME" => $name,
"%TYPE" => $name,
"%EVENT" => "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0",
"%SELF" => $name,
);
my $err = perlSyntaxCheck($aVal, %specials);
return $err if($err);
$hash->{".COMMAND"} = $aVal;
}
}
elsif ($aName eq "logTrgNames" ) {
if(!$aVal){# delete or set to 0
delete $hash->{READINGS}{$_} foreach(grep/^log_/,keys %{$hash->{READINGS}});
}
}
elsif ($aName eq "ignoreRegexp") {
if($aSet eq "set"){
return "Missing argument for ignoreRegexp";
eval { "HALLO" =~ m/$aVal/ };
return $@;
}
}
elsif ($aName eq "disable" ) {
$hash->{helper}{disbled} = $aSet eq "set" ? $aVal : 0;
my $state = $hash->{helper}{disbled} ? "disabled"
:$hash->{helper}{active} ? "active"
: "inactive";
readingsSingleUpdate($hash, "state", $state, 1);
ntf_prepTgrNames( $hash
,undef
,undef
,undef
);
}
Log3 $name, 5, "set attr $aName:$aVal";
return undef;
}
sub ntf_Set($@) {###################################
my ($hash, @a) = @_;
my $me = $hash->{NAME};
my ($chk,@defaults) = ntf_cmdParser($hash,"sets",@a);
push @a,@defaults;
return undef if ($chk && $chk eq "done");
return $chk if($chk);
my $cmd = $a[1];
if ($cmd eq "clear") {
if ($a[2] eq "trigger" ){
delete $hash->{READINGS}{$_} foreach(grep /^(trg|log_)/,keys %{$hash->{READINGS}});
}
elsif($a[2] eq "readingLog"){
delete $hash->{READINGS}{$_} foreach(grep/^log_/,keys %{$hash->{READINGS}});
}
elsif($a[2] eq "readings" ){
delete $hash->{READINGS}{$_} foreach(keys %{$hash->{READINGS}});
}
}
elsif($cmd eq "inactive") {
if (!AttrVal($me,"disabled",0)){# disabled is higher that inactive
readingsSingleUpdate($hash, "state", "inactive", 1);
}
$hash->{helper}{active} = 0;
}
elsif($cmd eq "active") {
if(!AttrVal($me, "disable", 0)){
readingsSingleUpdate($hash, "state", "active", 1) ;
}
$hash->{helper}{active} = 1;
}
elsif($cmd eq "simTrigger") {
my @b = @a;
shift @b;
shift @b;
my ($trgDevice,$trgEvt) = split(" ",join(" ",@b),2);
return "$trgDevice undefined" if(!defined $defs{$trgDevice});
ntf_Notify($hash,$defs{$trgDevice},($trgEvt));
}
elsif($cmd eq "trgAdd") {
my (undef,undef,@val)=@a;
my ($trgDevice,$trgVal) = map{my $foo=$_;$foo=~s/ //g;$foo}
split(":",join(" ",@val),2);
$trgDevice = ".*" if (!$trgDevice);
$trgVal = ".*" if (!$trgVal);
my %h = map { $_ => 1 } (split(",",AttrVal($me,"trgCombined","")),"$trgDevice:$trgVal");
my $ret = CommandAttr(undef, "$me trgCombined ".join(",",keys %h));
return $ret if($ret);
}
elsif($cmd eq "trgDel") {
my @trigger=();
foreach my $trg (split(",",AttrVal($me,"trgCombined",""))){
push @trigger,$trg if($trg ne $a[2]);
}
CommandAttr(undef, "$me trgCombined ".join(",",@trigger));
}
# elsif($cmd =~ m/tst\d/) {
# return "$cmd : ".join(",",@a);
# }
# elsif($cmd eq "tstAddSet") {
# $hash->{helper}{cmds}{sets}{$a[2]} = (defined $a[3] ? join(" ",@a[3..$#a]):"") ;
# delete $hash->{helper}{cmds}{sets}{'.cmdPrep'}; # force recalculation
# $hash->{helper}{cmds}{dynValLst}{localSetCmd} = join(",",keys %{ $hash->{helper}{cmds}{sets}});
# }
# elsif($cmd eq "tstAddGet") {
# $hash->{helper}{cmds}{gets}{$a[2]} = (defined $a[3] ? join(" ",@a[3..$#a]):"") ;
# delete $hash->{helper}{cmds}{gets}{'.cmdPrep'}; # force recalculation
# $hash->{helper}{cmds}{dynValLst}{localGetCmd} = join(",",keys %{ $hash->{helper}{cmds}{gets}});
# }
# elsif($cmd eq "tstAddAttr") {
# $hash->{helper}{cmds}{attr}{$a[2]} = (defined $a[3] ? join(" ",@a[3..$#a]):"") ;
# delete $hash->{helper}{cmds}{attr}{'.cmdPrep'}; # force recalculation
# $hash->{helper}{cmds}{dynValLst}{localGetCmd} = join(",",keys %{ $hash->{helper}{cmds}{attr}});
# }
# elsif($cmd eq "tstDelSet") {
# delete $hash->{helper}{cmds}{sets}{$a[2]};
# delete $hash->{helper}{cmds}{sets}{'.cmdPrep'}; # force recalculation
# $hash->{helper}{cmds}{dynValLst}{localSetCmd} = join(",",keys %{ $hash->{helper}{cmds}{sets}});
# }
# elsif($cmd eq "tstDelGet") {
# delete $hash->{helper}{cmds}{gets}{$a[2]};
# delete $hash->{helper}{cmds}{gets}{'.cmdPrep'}; # force recalculation
# $hash->{helper}{cmds}{dynValLst}{localGetCmd} = join(",",keys %{ $hash->{helper}{cmds}{gets}});
# }
# elsif($cmd eq "tstDelAttr") {
# delete $hash->{helper}{cmds}{attr}{$a[2]};
# delete $hash->{helper}{cmds}{attr}{'.cmdPrep'}; # force recalculation
# $hash->{helper}{cmds}{dynValLst}{localGetCmd} = join(",",keys %{ $hash->{helper}{cmds}{attr}});
# }
else{
return "command $cmd:".join(" ",@a[2..$#a])." defined but programmed - contact your admin";
}
return undef;
}
sub ntf_Get($@) {###################################
my ($hash, @a) = @_;
my $me = $hash->{NAME};
my ($chk,@defaults) = ntf_cmdParser($hash,"gets",@a);
push @a,@defaults;
return undef if ($chk && $chk eq "done");
return $chk if($chk);
my $cmd = $a[1];
my $ret;
if( $cmd eq "shEnroled") { ###############################################
$ret = "enroled for :\n".join("\n",sort map{$_=~s/^(.*)\:.*/$1/;;$_} grep/[:,]$me\,/,map{$_.":".join(',',@{$ntfyHash{$_}}).','}keys %ntfyHash);
}
elsif($cmd eq "trgFilter") { ###############################################
if (defined $hash->{helper}{trg}){
foreach my $x(keys %{$hash->{helper}{trg}}){
$ret .= "\n$x:";
$hash->{helper}{trg}{$x} =~ m/^\^\((.*)\)\$$/;
foreach(split('\|',$1)){
my($re,$va)=split(": ",$_);
$ret.="\n read: $re \t=> val: $va";
}
}
}
else{
$ret = "no trigger defined";
}
}
return $ret;
}
sub ntf_Notify($$;@) {###################################
my ($hash, $dev,@sim) = @_;
my $name = $hash->{NAME} // return;
return "" if(IsDisabled($name));
my $now = gettimeofday();
my $dat = AttrVal($name, "disabledAfterTrigger", 0);
return "" if( $hash->{TRIGGERTIME}
&& $init_done
&& $now < $hash->{TRIGGERTIME} + $dat
);
return if (!defined $dev->{NAME});
my $trgDevice = $dev->{NAME};
my $iRe = AttrVal($name, "ignoreRegexp", undef);
my $events;
if (scalar @sim > 0){
$events = \@sim;
}
else{
$events = deviceEvents($dev, 1);
}
return if(!$events); # Some previous ntf deleted the array.
my $max = int(@{$events});
my $t = $dev->{TYPE};
my $ret = "";
my %cfgChn = (name => 0,attr => 0);
for (my $i = 0; $i < $max; $i++) {
next if(!$init_done && $events->[$i] !~ m/(INITIALIZED|REREADCFG)/);
if ($trgDevice eq "global"){# need to verify filter upon config is changed
if(defined $events->[$i]){
if($events->[$i] =~ m/(INITIALIZED|REREADCFG)/){
foreach my $a (%{$attr{$name}}){# evaluate all attributes by re-assign
next if(!defined $a || $a !~ m/../ || !$attr{$name}{$a});
CommandAttr(undef, "$name $a $attr{$name}{$a}");
}
if ( !defined $modules{$hash->{TYPE}}{helper}{cmds}{attr}{'.cmdPrep'}
|| !defined $hash->{helper}{cmds}{attr}{'.cmdPrep'}){
ntf_cmdParser($hash,"attr",("","undef"));
}
}
if($events->[$i] =~ m/(INITIALIZED|REREADCFG|DELETED|DEFINED|RENAMED)/){
$cfgChn{name} = 1;
$modules{$hash->{TYPE}}{helper}{cmds}{dynValLst}{trgNames} = join(",",sort keys %defs);
ntf_cmdParser($hash,"attr",("","undef"));
if ($events->[$i] =~ m/^RENAMED ([^\s]+) ([^\s]+)/){# refresh the selection of trigger entites
my ($old,$new) = ($1,$2);
my $trgDevice = AttrVal($name,"trgDevice","");
my $trgNameNew = $trgDevice;
$trgNameNew =~ s/([\(\|])$old([\)\|])/$1$new$2/g;
$trgNameNew =~ s/^$old$/$new/;
if($trgNameNew ne $trgDevice){
CommandAttr(undef, "$name trgDevice $trgNameNew");
Log3 $name, 2, "updated attr trgDevice from $trgDevice to $trgNameNew due to rename";
}
}
}
elsif ($events->[$i] =~ m/^(ATTR|DELETEATTR)/){#ATTR
if ($events->[$i] =~ m/ignore.*1/){
$cfgChn{name} = 1;
}
else{
$cfgChn{attr} = 1;
}
}
}
}
{
last if(!$hash->{helper}{active}); #no processing when inactive
my $s = !defined $events->[$i] ? "" :$events->[$i];
next if( !$hash->{helper}{trg}{$trgDevice}
|| $s !~ m/$hash->{helper}{trg}{$trgDevice}/); # filter does not match
my ($r,$v) = split(": ",$s,2);
if ($trgDevice eq "global"){($r,$v) = split(" " ,$s,2);}
else {($r,$v) = split(": ",$s,2);}
$v = "--" if(!defined $v);
{#command execute
Log3 $name, 5, "Triggering $name from $trgDevice : $s";
my %specials= (
"%NAME" => $trgDevice,
"%TYPE" => $t,
"%EVENT"=> $s,
"%SELF" => $name
);
my $cmd = AttrVal($name,"trgCmd","");
my $exec = EvalSpecials($cmd, %specials);
{# set readings
readingsBeginUpdate($hash);
readingsBulkUpdate($hash,"lastTrgDevice" , $trgDevice);
readingsBulkUpdate($hash,"lastTrgEvent" , $s );
readingsBulkUpdate($hash,"lastTrgReading" , $r );
readingsBulkUpdate($hash,"lastTrgReadVal" , $v );
readingsBulkUpdate($hash,"state" , "last:".FmtDateTime($now) );
readingsBulkUpdate($hash,"log_$trgDevice" , $s ) if(AttrVal($name,"logTrgNames",0));
readingsEndUpdate($hash,1);
}
next if($cmd !~ m/.../);
Log3 $name, 4, "$name exec $exec";
my $r = AnalyzeCommandChain(undef, $exec);
if($r){
Log3 $name, 3, "$name return value: $r";
$ret .= " $r";
}
last if($dat);
}
}
}
if ( ($cfgChn{attr} && $hash->{helper}{trgByAttr})
||$cfgChn{name} ){# search my new friends
ntf_prepTgrNames($hash,undef,undef,undef);
ntf_cmdParser($hash,"attr",("","updt","module")) if ($cfgChn{name});
}
return (AttrVal($name, "forwardReturnValue", 0) ? $ret : undef);
}
sub ntf_prepTgrNames($$$$){################################
my ($hash,$trgComb,$trgDevice,$trgEvnt) = @_;
my $nameLstOld = defined $hash->{helper}{trg}
? join("|",sort grep /./,(keys %{$hash->{helper}{trg}},"global"))
: ""
;
my %names;
$hash->{helper}{trgByAttr} = 0;
if (!$hash->{helper}{disbled}){
$trgComb = AttrVal($hash->{NAME},"trgCombined","") if(!defined $trgComb );
$trgDevice = AttrVal($hash->{NAME},"trgDevice" ,"") if(!defined $trgDevice);
foreach (split(",",$trgComb)){