-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path98_monitoring.pm
1661 lines (1527 loc) · 60.2 KB
/
98_monitoring.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: 98_monitoring.pm 26029 2022-05-10 repair DeleteFn Beta-User $
#
# copyright ###################################################################
#
# 98_monitoring.pm
#
# Originally initiated by igami
#
# This file is part of FHEM.
#
# FHEM is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 2 of the License, or (at your option) any later
# version.
#
# FHEM is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# FHEM. If not, see <http://www.gnu.org/licenses/>.
# packages ####################################################################
package FHEM::Automation::monitoring; ##no critic qw(Package)
use strict;
use warnings;
use Carp qw(carp);
use Scalar::Util qw(looks_like_number);
use Time::HiRes qw(gettimeofday);
use GPUtils qw(GP_Import);
sub ::monitoring_Initialize { goto &Initialize }
BEGIN {
GP_Import( qw(
readingsSingleUpdate
readingsBeginUpdate
readingsBulkUpdate
readingsEndUpdate
Log3 fhem
defs attr
DAYSECONDS HOURSECONDS MINUTESECONDS
init_done
InternalTimer
RemoveInternalTimer
readingFnAttributes
IsDisabled
AttrVal
InternalVal
ReadingsVal
ReadingsNum
devspec2array
AnalyzeCommandChain
AnalyzeCommand
EvalSpecials
AnalyzePerlCommand
perlSyntaxCheck
FmtDateTime time_str2num
notifyRegexpChanged
deviceEvents
) )
};
# initialize ##################################################################
sub Initialize {
my $hash = shift // return;
$hash->{DefFn} = \&Define;
$hash->{UndefFn} = \&Undefine;
#$hash->{RenameFn} = \&Rename;
$hash->{SetFn} = \&Set;
$hash->{GetFn} = \&Get;
$hash->{AttrFn} = \&Attr;
$hash->{NotifyFn} = \&Notify;
$hash->{AttrList} =
"addStateEvent:1,0 ".
"blacklist:textField-long ".
"disable:1,0 ".
"disabledForIntervals ".
"errorFuncAdd:textField-long ".
"errorFuncAdded:textField-long ".
"errorFuncRemove:textField-long ".
"errorWait ".
"errorReturn:textField-long ".
"getDefault:all,error,warning ".
"setActiveFunc:textField-long ".
"setInactiveFunc:textField-long ".
"warningFuncAdd:textField-long ".
"warningFuncAdded:textField-long ".
"warningFuncRemove:textField-long ".
"warningWait ".
"warningReturn:textField-long ".
"whitelist:textField-long ".
$readingFnAttributes;
return;
}
# regular Fn ##################################################################
sub Define {
my $hash = shift // return;
my $def = shift // return;
my ($SELF, $TYPE, @re) = split m{\s+}xms, $def, 5;
return("Usage: define <name> $TYPE <add-event> [<remove-event>]")
if( !@re || @re > 2);
monitoring_NOTIFYDEV($hash) if !$init_done;
setActive($hash) if $init_done;
return;
}
sub Undefine {
my $hash = shift // return;
setInactive($hash);
monitoring_RemoveInternalTimer($hash);
return;
}
sub Set {
my ($hash, @arr) = @_;
my $TYPE = $hash->{TYPE};
return '"set <monitoring>" needs at least one argument' if @arr < 2;
my $SELF = shift @arr;
my $argument = shift @arr;
my $value = @arr ? join ' ', @arr : '';
my %monitoring_sets = (
active => 'active:noArg',
clear => 'clear:all,error,warning',
errorAdd => 'errorAdd:textField',
errorRemove => 'errorRemove:'.
join( q{,}, ReadingsVal($SELF, 'error', '')),
inactive => 'inactive:noArg',
warningAdd => 'warningAdd:textField',
warningRemove => 'warningRemove:'.
join q{,}, ReadingsVal($SELF, 'warning', '')
);
return "Unknown argument $argument, choose one of ".
join " ", sort values %monitoring_sets
if !exists $monitoring_sets{$argument};
if ( $argument eq 'active' ) {
return setActive($hash);
}
if ( $argument eq 'inactive' ) {
setInactive($hash);
readingsSingleUpdate($hash, 'state', $argument, 0);
Log3($SELF, 3, "$SELF ($TYPE) set $SELF inactive");
return monitoring_RemoveInternalTimer($hash);
}
if ( $argument eq 'clear' ) {
readingsBeginUpdate($hash);
if ( $value =~ m{\A(warning|all)\z}xms ) {
readingsBulkUpdate($hash, 'warning', '', 0);
readingsBulkUpdate($hash, 'warningCount', 0, 0);
for my $r (keys %{$hash->{READINGS}}){
if($r =~ m{(warning)Add_(.+)}xms){
RemoveInternalTimer("$SELF|$1|add|$2");
delete $hash->{READINGS}{$r};
}
}
}
if ( $value =~ m{\A(error|all)\z}xms ) {
readingsBulkUpdate($hash, 'error', '', 0);
readingsBulkUpdate($hash, 'errorCount', 0, 0);
for my $r ( keys %{$hash->{READINGS}} ) {
if ( $r =~ m{(error)Add_(.+)}xms ) {
RemoveInternalTimer("$SELF|$1|add|$2");
delete $hash->{READINGS}{$r};
}
}
}
readingsBulkUpdate($hash, 'state', "$argument $value", 0)
if !IsDisabled($SELF);
readingsEndUpdate($hash, 0);
Log3($SELF, 2, "$TYPE ($SELF) set $SELF $argument $value");
}
elsif($argument =~ m{\A(error|warning)(Add|Remove)\z}xms){
monitoring_modify("$SELF|$1|".lc($2)."|$value");
}
return;
}
sub Get {
my ($hash, @arr) = @_;
my $TYPE = $hash->{TYPE};
my $SELF = shift @arr;
return if IsDisabled($SELF);
return("\"get $TYPE\" needs at least one argument") if !@arr;
my $argument = shift @arr;
my $value = @arr ? join ' ', @arr : '';
my $default = AttrVal($SELF, 'getDefault', 'all');
my %monitoring_gets = (
all => 'all:noArg',
default => 'default:noArg',
error => 'error:noArg',
warning => 'warning:noArg'
);
my @ret;
return
"Unknown argument $argument, choose one of ".
join ' ', sort values %monitoring_gets if !exists $monitoring_gets{$argument};
if ( $argument eq 'all' || $argument eq 'default' && $default eq 'all' ) {
push @ret, monitoring_return($hash, 'error');
push @ret, monitoring_return($hash, 'warning');
}
elsif ( $argument eq 'default' ) {
push @ret, monitoring_return($hash, $default);
}
elsif($argument eq 'error' || $argument eq 'warning') {
push @ret, monitoring_return($hash, $argument);
}
@ret = grep { defined } @ret; #prevent uninitialized warnings
return join ("\n\n", @ret)."\n" if @ret;
return;
}
sub Attr {
my ($cmd, $SELF, $attribute, $value) = @_;
my $hash = $defs{$SELF} // return;
if($attribute =~ "blacklist" && $value){
my @blacklist;
push @blacklist, devspec2array($_) for (split m{[\s]+}x, $value);
my %blacklist = map{ $_ => 1 } @blacklist;
for my $name (sort keys %blacklist){
monitoring_modify("$SELF|warning|remove|$name");
monitoring_modify("$SELF|error|remove|$name");
}
}
elsif($attribute eq 'whitelist'){
monitoring_NOTIFYDEV($hash);
return if !$value;
my @whitelist;
push @whitelist, devspec2array($_) for (split m{[\s]+}x, $value);
for my $list ( qw(warning error) ){
for my $name ( split m{,}x, ReadingsVal($SELF, $list, '') ) {
monitoring_modify("$SELF|$list|remove|$name")
if !grep {m{$name}x} @whitelist;
}
}
}
elsif($attribute eq 'disable'){
if($cmd eq 'set' and $value == 1){
return setActive($hash);
}
setInactive($hash);
readingsSingleUpdate($hash, 'state', 'disabled', 0);
Log3($SELF, 3, "$hash->{TYPE} ($SELF) attr $SELF disabled");
}
return;
}
sub Notify {
my $hash = shift // return;
my $dev_hash = shift // return;
my $SELF = $hash->{NAME};
my $name = $dev_hash->{NAME};
my $TYPE = $hash->{TYPE};
return if(
!$init_done ||
IsDisabled($SELF) ||
IsDisabled($name) ||
$SELF eq $name # do not process own events
);
my $events = deviceEvents($dev_hash, AttrVal($SELF, 'addStateEvent', 0));
return if !$events;
if($name eq 'global' && 'INITIALIZED|REREADCFG' =~ m{\Q@{$events}\E}x){
setActive($hash);
return;
}
my ($addRegex, $removeRegex) = split m{[\s]+}x, InternalVal($SELF, 'DEF', '');
=pod this seems to be useless?
return unless(
$addRegex =~ m/^$name:/ ||
$removeRegex && $removeRegex =~ m/^$name:/ ||
$events
);
=cut
my @blacklist;
push @blacklist, devspec2array($_)
for (split m{[\s]+}x, AttrVal($SELF, 'blacklist', ''));
return if @blacklist && grep { m{$name}x } @blacklist;
my @whitelist;
push @whitelist, devspec2array($_)
for (split m{[\s]+}x, AttrVal($SELF, 'whitelist', ''));
return if @whitelist && !grep { m{$name}x } @whitelist;
for my $event (@{$events}){
next if !$event;
my $addMatch = "$name:$event" =~ m{\A$addRegex\z}xms;
my $removeMatch = $removeRegex ? "$name:$event" =~ m{\A$removeRegex\z}xms : 0;
#Log3($hash, 3, "notify called with add $addMatch and remove $removeMatch");
#next unless(defined($event) && ($addMatch || $removeMatch));
next if !$addMatch && !$removeMatch;
#Log3($hash, 3, "notify unless 1 replacement passed w. $addMatch and remove $removeMatch");
Log3($SELF, 4 , "$TYPE ($SELF) triggered by \"$name $event\"");
for my $list ( qw (error warning) ){
my $listFuncAdd = AttrVal($SELF, $list.'FuncAdd', 'preset');
my $listFuncRemove = AttrVal($SELF, $list.'FuncRemove', 'preset');
#my $listWait = eval(AttrVal($SELF, $list.'Wait', 0));
my $cmd = AttrVal($SELF, $list.'Wait', 0);
my %specials = (
'$name' => $name, #Name des Event auslösenden Gerätes
'$SELF' => $SELF, #Eigenname des monitoring
);
$cmd = EvalSpecials($cmd, %specials);
# CMD ausführen
my $listWait = AnalyzePerlCommand( $hash, $cmd );
$listWait = 0 if !looks_like_number($listWait);
if ( $listFuncAdd eq 'preset' && $listFuncRemove eq 'preset' ) {
Log3(
$SELF, 5, "$TYPE ($SELF) ".
$list."FuncAdd and $list"."FuncRemove are preset"
);
if ( !$removeRegex ) {
if ( $listWait == 0 ) {
Log3(
$SELF, 2, "$TYPE ($SELF) ".
"set \"$list"."Wait\" while \"$list".
"FuncAdd\" and \"$list"."FuncRemove\" are same"
) if $list eq 'error';
next;
}
Log3($SELF, 5, "$TYPE ($SELF) only addRegex is defined");
monitoring_modify("$SELF|$list|remove|$name");
monitoring_modify("$SELF|$list|add|$name|$listWait");
next;
}
else{
#next unless($list eq 'error' || AttrVal($SELF, 'errorWait', undef));
next if $list ne 'error' && !AttrVal($SELF, 'errorWait', undef);
Log3(
$SELF, 5, "$TYPE ($SELF) ".
"addRegex ($addRegex) and removeRegex ($removeRegex) are defined"
);
monitoring_modify("$SELF|$list|remove|$name") if $removeMatch;
monitoring_modify("$SELF|$list|add|$name|$listWait") if $addMatch;
next;
}
}
$listFuncAdd = 1 if $listFuncAdd eq 'preset' && $addMatch;
if(!$removeRegex){
Log3($SELF, 5, "$TYPE ($SELF) only addRegex is defined");
if ( $listFuncRemove eq 'preset' ) {
if ( $listWait == 0 ) {
Log3(
$SELF, 2, "$TYPE ($SELF) ".
"set \"$list"."Wait\" while \"$list".
"FuncAdd\" and \"$list"."FuncRemove\" are same"
) if $list eq 'error';
next;
}
$listFuncRemove = $listFuncAdd;
}
}
else{
Log3(
$SELF, 5, "$TYPE ($SELF) ".
"addRegex ($addRegex) and removeRegex ($removeRegex) are defined"
);
$listFuncRemove = 1 if $listFuncRemove eq 'preset' && $removeMatch;
}
#$listFuncAdd = eval($listFuncAdd) if $listFuncAdd =~ /^\{.*\}$/s;
if ( $listFuncAdd =~ m{\A\{.*\}\z}xs ) {
$listFuncAdd = EvalSpecials($listFuncAdd, %specials);
# CMD ausführen
$listFuncAdd = AnalyzePerlCommand( $hash, $listFuncAdd)
};
if($listFuncRemove =~ m{\A\{.*\}\z}xs ) {
$listFuncRemove = EvalSpecials($listFuncRemove, %specials);
$listFuncRemove = AnalyzePerlCommand($hash, $listFuncRemove)
}
monitoring_modify("$SELF|$list|remove|$name")
if $listFuncRemove && $listFuncRemove eq '1';
monitoring_modify("$SELF|$list|add|$name|$listWait")
if $listFuncAdd && $listFuncAdd eq '1';
next;
}
}
return;
}
# module Fn ###################################################################
sub monitoring_modify {
my ($SELF, $list, $operation, $value, $wait) = split m{[|]}x, shift;
my $hash = $defs{$SELF} // return;
return if IsDisabled($SELF);
my $at;
#$at = eval($wait + gettimeofday()) if $wait && $wait ne 'quiet';
if ( $wait && $wait ne 'quiet' ) {
$at = looks_like_number($wait) ? $wait : AnalyzeCommandChain($hash, $wait);
$at +=gettimeofday();
}
my $TYPE = $hash->{TYPE};
my (@change, %readings);
%readings = map{ $_ => 1 } split m{,}xms, ReadingsVal($SELF, $list, '');
my $arg = "$SELF|$list|$operation|$value";
my $reading = $list."Add_".$value;
Log3(
$SELF, 5 , "$TYPE ($SELF)".
"\n entering monitoring_modify".
"\n reading: $list".
"\n operation: $operation".
"\n value: $value".
"\n at: ".($at ? FmtDateTime($at) : "now")
);
if ( $operation eq 'add' ) {
return if
$readings{$value} ||
ReadingsVal($SELF, 'error', '') =~ m{(?:^|,)$value(?:,|$)}x
;
if ( $at ){
return if $hash->{READINGS}{$reading};
readingsSingleUpdate($hash, $reading, FmtDateTime($at), 0);
InternalTimer($at, \&monitoring_modify, $arg);
return;
}
monitoring_modify("$SELF|warning|remove|$value|quiet")
if $list eq 'error';
$readings{$value} = 1;
delete $hash->{READINGS}{$reading};
}
elsif ( $operation eq 'remove' ) {
push(@change, 1) if delete $readings{$value};
delete $hash->{READINGS}{$reading};
}
RemoveInternalTimer("$SELF|$list|add|$value");
#return unless(@change || $operation eq 'add');
return if !@change && $operation ne 'add';
my $allCount =
int(keys %readings) +
ReadingsNum($SELF, ($list eq 'warning' ? 'error' : 'warning').'Count', 0)
;
if ($operation eq 'add') {
my $name = $value;
my $listFuncAdded = AttrVal($SELF, $list.'FuncAdded', '');
my %specials = (
'$name' => $name, #Name des Event auslösenden Gerätes
'$SELF' => $SELF, #Eigenname des monitoring
);
$listFuncAdded = EvalSpecials($listFuncAdded, %specials);
$listFuncAdded = AnalyzeCommandChain($hash, $listFuncAdded);
#$listFuncAdded = $listFuncAdded =~ /^\{.*\}$/s ? eval($listFuncAdded) : fhem($listFuncAdded);
}
readingsBeginUpdate($hash);
readingsBulkUpdate($hash, 'state', "$list $operation: $value");
readingsBulkUpdate($hash, $list, join ",", sort keys %readings);
readingsBulkUpdate($hash, $list.'Count', int(keys %readings));
readingsBulkUpdate($hash, 'allCount', $allCount)
if !$wait || $wait ne 'quiet';
readingsEndUpdate($hash, 1);
return;
}
sub monitoring_NOTIFYDEV {
my $hash = shift // return;
my $SELF = $hash->{NAME} // return;
my $NOTIFYDEV = $init_done ? AttrVal($SELF, 'whitelist', undef) : 'global'; #|| join(",", (InternalVal($SELF, "DEF", undef) =~ m/(?:^|\s)([^:\s]+):/g))
if ($NOTIFYDEV) {
$NOTIFYDEV =~ s{[\s]+}{,}gx;
return notifyRegexpChanged($hash, "$NOTIFYDEV,global");
}
$NOTIFYDEV = join q{|}, split m{[\s]+}x, InternalVal($SELF, 'DEF', undef);
return notifyRegexpChanged($hash, "$NOTIFYDEV|global");
}
sub monitoring_RemoveInternalTimer {
my $hash = shift // return;
my $SELF = $hash->{NAME} // return;
for my $reading (sort keys %{$hash->{READINGS}}){
RemoveInternalTimer("$SELF|$1|add|$2")
if $reading =~ m{(error|warning)Add_(.+)}xms;
}
return;
}
sub monitoring_return {
my $hash = shift // return;
my $list = shift // return;
my $SELF = $hash->{NAME} // return;
my @errors = split m{,}xms, ReadingsVal($SELF, 'error', '');
my @warnings = split m{,}xms, ReadingsVal($SELF, 'warning', '');
my $value = ReadingsVal($SELF, $list, undef);
my $ret = AttrVal($SELF, $list.'Return', undef);
$ret = '"$list: $value"' if !$ret && $value;
return if !$ret;
#return ;
if ( !eval{ $ret = eval($ret) ; 1 } ) { ##no critic qw(StringyEval)
return Log3($hash->{NAME}, 1, "Evaluation error: $@");
}
return $ret;
}
sub setActive {
my $hash = shift // return;
my $SELF = $hash->{NAME} // return;
my $TYPE = $hash->{TYPE};
monitoring_NOTIFYDEV($hash);
readingsSingleUpdate($hash, 'state', 'active', 0);
Log3($SELF, 3, "$TYPE ($SELF) set $SELF active");
for my $reading (reverse sort keys %{$hash->{READINGS}}){
if ( $reading =~ m{(error|warning)Add_(.+)}xms ) {
my $wait = time_str2num(ReadingsVal($SELF, $reading, ''));
next if !looks_like_number($wait);
$wait -= gettimeofday();
if($wait > 0){
Log3($SELF, 4 , "$TYPE ($SELF) restore Timer \"$SELF|$1|add|$2\"");
monitoring_modify("$SELF|$1|add|$2|$wait");
}
else{
monitoring_modify("$SELF|$1|add|$2");
}
}
}
AnalyzeCommandChain(undef, AttrVal($SELF, 'setActiveFunc', 'preset'));
return;
}
sub setInactive {
my $hash = shift // return;
my $SELF = $hash->{NAME} // return;
my $TYPE = $hash->{TYPE};
notifyRegexpChanged($hash,'',1);
AnalyzeCommandChain(undef, AttrVal($SELF, 'setInactiveFunc', 'preset'));
return;
}
1;
__END__
# commandref ##################################################################
=pod
=item helper
=encoding utf8
=item summary monitors devices towards events and stores them in two lists
=item summary_DE überwacht Geräte auf Events und speichert diese in zwei Listen
=begin html
<a id="monitoring"></a>
<h3>monitoring</h3>
<div>
<ul>
Each <i>monitoring</i> has a <i>warning</i>- and an <i>error</i> list, which are stored
as readings. <br>
When a defined add-event occurs, the device is set to the warning
list after a predefined time.<br>
After a further predefined time, the device is deleted from the
warning list and set to the error list.<br>
If a defined remove-event occurs, the device is deleted from both
lists and still running timers are canceled.<br>
This makes it easy to create group messages and send them
formatted by two attributes.<br>
<br>
The following applications are possible and are described
<a href="#monitoring-examples"><u>below</u></a>:<br>
<ul>
<li>opened windows</li>
<li>battery warnings</li>
<li>activity monitor</li>
<li>
regular maintenance (for example changing the table water
filter or cleaning rooms)
</li>
<li>
operating hours dependent maintenance (for example clean the
Beamer filter)
</li>
</ul>
<br>
The monitor does not send a message by itself, a <a href="#notify"><u>notify</u></a>
or <a href="#DOIF"><u>DOIF</u></a> is
necessary, which responds to the event "<monitoring-name> error
add: <name>" and then sends the return value of "get
<monitoring-name> default".
<br>
<a id="monitoring-define"></a>
<h4>Define</h4>
<ul>
<code>
define <name> monitoring <add-event> [<remove-event>]
</code>
<br>
The syntax for <add-event> and <remove-event> is the
same as the pattern for <a href="#notify">notify</a>
(device-name or device-name:event).<br>
If only an <add-event> is defined, the device is deleted from
both lists as it occurs and the timers for warning and error are
started.<br>
</ul>
<a id="monitoring-set"></a>
<h4>Set</h4>
<ul>
<a id="monitoring-set-active"></a><li>
<code>active</code><br>
Two things will happen:<br>
1. Restores pending timers, or sets the devices immediately to the
corresponding list if the time is in the past.<br>
2. Executes the commands specified under the "setActiveFunc" attribute.
</li>
<a id="monitoring-set-clear"></a><li>
<code>clear (warning|error|all)</code><br>
Removes all devices from the specified list and aborts timers for this
list. With "all", all devices are removed from both lists and all
running timers are aborted.
</li>
<a id="monitoring-set-errorAdd"></a><li>
<code>errorAdd <name></code><br>
Add <name> to the error list.
</li>
<a id="monitoring-set-errorRemove"></a><li>
<code>errorRemove <name></code><br>
Removes <name> from the error list.
</li>
<a id="monitoring-set-inactive"></a><li>
<code>inactive</code><br>
Two things will happen:<br>
1. Executes the commands specified under the "setInactiveFunc" attribute.<br>
2. Inactivates the current device. Note the slight difference to the
disable attribute: using set inactive the state is automatically saved
to the statefile on shutdown, there is no explicit save necesary.
</li>
<a id="monitoring-set-warningAdd"></a><li>
<code>warningAdd <name></code><br>
Add <name> to the warning list.
</li>
<a id="monitoring-set-warningRemove"></a><li>
<code>warningRemove <name></code><br>
Removes <name> from the warning list.
</li>
</ul>
<a id="monitoring-get"></a>
<h4>Get</h4>
<ul>
<a id="monitoring-get-all"></a><li>
<code>all</code><br>
Returns the error and warning list, separated by a blank line.<br>
The formatting can be set with the attributes "errorReturn" and
"warningReturn".
</li>
<a id="monitoring-get-default"></a><li>
<code>default</code><br>
The "default" value can be set in the attribute "getDefault" and is
intended to leave the configuration for the return value in the
monitoring device. If nothing is specified "all" is used.
</li>
<a id="monitoring-get-error"></a><li>
<code>error</code><br>
Returns the error list.<br>
The formatting can be set with the attribute "errorReturn".
</li>
<a id="monitoring-get-warning"></a><li>
<code>warning</code><br>
Returns the warning list.<br>
The formatting can be set with the attribute "warningReturn".
</li>
</ul>
<a id="monitoring-readings"></a>
<h4>Readings</h4><br>
<ul>
<li>
<code>allCount</code><br>
Displays the amount of devices on the warning and error list..
</li>
<li>
<code>error</code><br>
Comma-separated list of devices.
</li>
<li>
<code>errorAdd_<name></code><br>
Displays the time when the device will be set to the error list.
</li>
<li>
<code>errorCount</code><br>
Displays the amount of devices on the error list.
</li>
<li>
<code>state</code><br>
Displays the status (active, inactive, or disabled). In "active" it
displays which device added to which list or was removed from which
list.
</li>
<li>
<code>warning</code><br>
Comma-separated list of devices.
</li>
<li>
<code>warningAdd_<name></code><br>
Displays the time when the device will be set to the warning list.
</li>
<li>
<code>warningCount</code><br>
Displays the amount of devices on the warning list.
</li>
</ul>
<a id="monitoring-attr"></a>
<h4>Attributes</h4>
<ul>
<li>
<a href="#addStateEvent">
<u><code>addStateEvent</code></u>
</a>
</li>
<a id="monitoring-attr-blacklist"></a><li>
<code>blacklist <devspec list></code><br>
Space-separated list of devspecs which will be ignored.<br>
If the attribute is set all devices which are specified by the devspecs
are removed from both lists.
</li>
<a id="monitoring-attr-disable"></a><li>
<code>disable (1|0)</code><br>
1: Executes the commands specified under the "setInactiveFunc" attribute
and disables the monitoring.<br>
0: see "set active"
</li>
<li>
<a href="#disabledForIntervals">
<u><code>disabledForIntervals HH:MM-HH:MM HH:MM-HH-MM ...</code></u>
</a>
</li>
<a id="monitoring-attr-errorFuncAdd"></a><li>
<code>errorFuncAdd {<perl code>}</code><br>
The following variables are available in this function:
<br>
<ul>
<li>
<code>$name</code><br>
Name of the event triggering device
</li>
<li>
<code>$event</code><br>
Includes the complete event, e.g.
<code>measured-temp: 21.7 (Celsius)</code>
</li>
<li>
<code>$addMatch</code><br>
Has the value 1 if the add-event is true
</li>
<li>
<code>$removeMatch</code><br>
Has the value 1 if the remove-event is true
</li>
<li>
<code>$SELF</code><br>
Name of the monitoring
</li>
</ul>
If the function returns a 1, the device is set to the error list after
the wait time.<br>
If the attribute is not set, it will be checked for
<code>$addMatch</code>.
</li>
<a id="monitoring-attr-errorFuncAdded"></a><li>
<code>errorFuncAdded {<perl code>}</code><br>
The following variables are available in this function:
<br>
<ul>
<li>
<code>$name</code><br>
Name of the event triggering device
</li>
<li>
<code>$SELF</code><br>
Name of the monitoring
</li>
</ul>
This function will be executed when a device is added to the error list.
</li>
<a id="monitoring-attr-errorFuncRemove"></a><li>
<code>errorFuncRemove {<perl code>}</code><br>
This function provides the same variables as for "errorFuncAdd".<br>
If the function returns a 1, the device is removed from the error list
and still running timers are canceled.<br>
If the attribute is not set, it will be checked for
<code>$removeMatch</code> if there is a
<code><remove-event></code> in the DEF, otherwise it will be
checked for <code>errorFuncAdd</code>.
</li>
<a id="monitoring-attr-errorWait"></a><li>
<code>errorWait <perl code></code><br>
Code returning waiting time (in seconds) until the device is set to the error list.
</li>
<a id="monitoring-attr-errorReturn"></a><li>
<code>errorReturn {<perl code>}</code><br>
The following variables are available in this attribute:
<ul>
<li>
<code>@errors</code><br>
Array with all devices on the error list.
</li>
<li>
<code>@warnings</code><br>
Array with all devices on the warning list.
</li>
<li>
<code>$SELF</code><br>
Name of the monitoring
</li>
</ul>
With this attribute the output created with "get <name> error"
can be formatted.<br>
Note: As evaluation is done in package context, using functions from main may require redirection in lexical main context (e.g. use double colons "::myFunction($SELF)").
</li>
<a id="monitoring-attr-getDefault"></a><li>
<code>getDefault (all|error|warning)</code><br>
This attribute can be used to specify which list(s) are / are returned
by "get <name> default". If the attribute is not set, "all" will
be used.
</li>
<a id="monitoring-attr-setActiveFunc"></a><li>
<code>setActiveFunc <statement></code><br>
The statement is one of the FHEM command types and is executed when you
define the monitoring or "set active".<br>
For a battery message <code>"trigger battery=low battery: low"</code>
can be useful.
</li>
<a id="monitoring-attr-setInactiveFunc"></a><li>
<code>setInactiveFunc <statement></code><br>
The statement is one of the FHEM command types and is executed when you
define the monitoring or "set inactive".
</li>
<a id="monitoring-attr-warningFuncAdd"></a><li>
<code>warningFuncAdd {<perl code>}</code><br>
Like errorFuncAdd, just for the warning list.
</li>
<a id="monitoring-attr-warningFuncAdded"></a><li>
<code>warningFuncAdded {<perl code>}</code><br>
Like errorFuncAdded, just for the warning list.
</li>
<a id="monitoring-attr-warningFuncRemove"></a><li>
<code>warningFuncRemove {<perl code>}</code><br>
Like errorFuncRemove, just for the warning list.
</li>
<a id="monitoring-attr-warningWait"></a><li>
<code>warningWait <perl code></code><br>
Like errorWait, just for the warning list.
</li>
<a id="monitoring-attr-warningReturn"></a><li>
<code>warningReturn {<perl code>}</code><br>
Like errorReturn, just for the warning list.
</li>
<a id="monitoring-attr-whitelist"></a><li>
<code>whitelist <devspec list></code><br>
Space-separated list of devspecs which are allowed.<br>
If the attribute is set all devices which are not specified by the
devspecs are removed from both lists.
</li>
<li>
<a href="#readingFnAttributes">
<u><code>readingFnAttributes</code></u>
</a>
</li>
</ul>
<a id="monitoring-examples"></a>
<h4>Examples</h4>
<ul>
<a href="https://wiki.fhem.de/wiki/Import_von_Code_Snippets">
<u>The following sample codes can be imported via "Raw definition".</u>
</a>
<br><br>
<li>
<b>
Global, flexible opened windows/doors message
<a href="https://forum.fhem.de/index.php/topic,36504">
<u>(similar to those described in the forum)</u>
</a>
</b>
<br>
<pre>defmod Fenster_monitoring monitoring .*:(open|tilted) .*:closed
attr Fenster_monitoring errorReturn {return if !@errors;;\
$_ = AttrVal($_, 'alias', $_) for @errors;;\
return("Das Fenster \"$errors[0]\" ist schon länger geöffnet.") if(int(@errors) == 1);;\
@errors = sort {lc($a) cmp lc($b)} @errors;;\
return(join("\n - ", "Die folgenden ".@errors." Fenster sind schon länger geöffnet:", @errors))\
}
attr Fenster_monitoring errorWait {AttrVal($name, "winOpenTimer", 60*10)}
attr Fenster_monitoring warningReturn {return if !@warnings;;\
$_ = AttrVal($_, 'alias', $_) for @warnings;;\
return("Das Fenster \"$warnings[0]\" ist seit kurzem geöffnet.") if(int(@warnings) == 1);;\
@warnings = sort {lc($a) cmp lc($b)} @warnings;;\
return(join("\n - ", "Die folgenden ".@warnings." Fenster sind seit kurzem geöffnet:", @warnings))\
}</pre>
As soon as a device triggers an "open" or "tilded" event, the device is
set to the warning list and a timer is started after which the device
is moved from the warning to the error list. The waiting time can be
set for each device via userattr "winOpenTimer". The default value is
10 minutes.<br>
As soon as a device triggers a "closed" event, the device is deleted
from both lists and still running timers are stopped.
</li>
<br>
<li>
<b>Battery monitoring</b><br>
<pre>defmod Batterie_monitoring monitoring .*:battery:.low .*:battery:.ok
attr Batterie_monitoring errorReturn {return if !@errors;;\
$_ = AttrVal($_, 'alias', $_) for @errors;;\
return("Bei dem Gerät \"$errors[0]\" muss die Batterie gewechselt werden.") if(int(@errors) == 1);;\
@errors = sort {lc($a) cmp lc($b)} @errors;;\