diff --git a/fhem/CHANGED b/fhem/CHANGED index 23a5fe21c..006fbcfc9 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,7 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - change: 93_DbRep: attr executeBeforeProc, executeAfterProc can execute + FHEM commands as well as PERL code - bugfix: 82_LGTV_WebOS: fix little typos and change version - change: 93_DBLog: changed default splitting, Forum: #133537 - feature: AutomowerConnectFamily: set cutting height for user defined zones diff --git a/fhem/FHEM/93_DbRep.pm b/fhem/FHEM/93_DbRep.pm index d25e72a61..ae31c31ec 100644 --- a/fhem/FHEM/93_DbRep.pm +++ b/fhem/FHEM/93_DbRep.pm @@ -59,6 +59,7 @@ no if $] >= 5.017011, warnings => 'experimental::smartmatch'; # Version History intern my %DbRep_vNotesIntern = ( + "8.52.7" => "16.05.2023 DbRep_afterproc, DbRep_beforeproc can execute FHEM commands as well as PERL code ", "8.52.6" => "11.04.2023 change diffValue for aggr month ", "8.52.5" => "10.04.2023 change diffValue, Forum: https://forum.fhem.de/index.php?msg=1271853 ", "8.52.4" => "10.04.2023 fix perl warning ", @@ -465,8 +466,8 @@ sub DbRep_Initialize { "dumpMemlimit ". "dumpSpeed ". "dumpFilesKeep:0,1,2,3,4,5,6,7,8,9,10 ". - "executeBeforeProc ". - "executeAfterProc ". + "executeBeforeProc:textField-long ". + "executeAfterProc:textField-long ". "expimpfile ". "fastStart:0,1 ". "fetchRoute:ascent,descent ". @@ -1638,6 +1639,16 @@ sub DbRep_Attr { } } } + + if ($aName =~ /executeAfterProc|executeBeforeProc/xs) { + if($cmd eq "set") { + if ($aVal =~ m/^\s*(\{.*\}|{.*|.*})\s*$/xs) { + $aVal = $1; + eval $aVal; + return $@ if ($@); + } + } + } if ($aName eq "role") { if($cmd eq "set") { @@ -4837,11 +4848,6 @@ sub DbRep_diffvalDone { my %rh = split("§", $rowlist); - #Log3 ($name, 4, "DbRep $name - print result of diffValue calculation after decoding ..."); - #for my $key (sort(keys(%rh))) { - # Log3 ($name, 4, "DbRep $name - runtimestring Key: $key, value: ".$rh{$key}); - #} - readingsBeginUpdate($hash); for my $key (sort(keys(%rh))) { @@ -12402,21 +12408,21 @@ return ($txt); ################################################################################### sub DbRep_beforeproc { my $hash = shift; - my $txt = shift // q{process}; + my $cmd = shift // q{process}; my $name = $hash->{NAME}; + my $fn = AttrVal($name, 'executeBeforeProc', ''); - my $ebd = AttrVal($name, 'executeBeforeProc', ''); - - if($ebd) { - Log3 ($name, 3, "DbRep $name - execute command before $txt: '$ebd' "); - my $err = AnalyzeCommandChain(undef, $ebd); + if($fn) { + Log3 ($name, 3, "DbRep $name - execute command before $cmd: '$fn' "); + + my $err = _DbRep_procCode ($hash, $fn); if ($err) { - Log3 ($name, 2, "DbRep $name - command message before $txt: \"$err\" "); - my $erread = "Warning - message from command before $txt appeared"; + Log3 ($name, 2, "DbRep $name - command message before $cmd: \"$err\" "); + my $erread = "Warning - message from command before $cmd appeared"; - ReadingsSingleUpdateValue ($hash, "before".$txt."_message", $err, 1); + ReadingsSingleUpdateValue ($hash, "before_".$cmd."_message", $err, 1); ReadingsSingleUpdateValue ($hash, "state", $erread, 1); } } @@ -12432,21 +12438,21 @@ sub DbRep_afterproc { my $cmd = shift // q{process}; my $bfile = shift // q{}; - my $erread; + my ($err,$erread); my $name = $hash->{NAME}; $cmd = (split " ", $cmd)[0]; my $sval = ReadingsVal ($name, 'state', ''); - my $ead = AttrVal ($name, 'executeAfterProc', ''); - - if($ead) { - Log3 ($name, 3, "DbRep $name - execute command after $cmd: '$ead' "); - - my $err = AnalyzeCommandChain(undef, $ead); + my $fn = AttrVal ($name, 'executeAfterProc', ''); + if($fn) { + Log3 ($name, 3, "DbRep $name - execute command after $cmd: '$fn' "); + + $err = _DbRep_procCode ($hash, $fn); + if ($err) { - Log3 ($name, 2, qq{DbRep $name - command message after $cmd: "$err"}); - + Log3 ($name, 2, qq{DbRep $name - command message after $cmd: >$err<}); + $erread = $sval eq 'error' ? $sval : qq(WARNING - $cmd finished, but message after command appeared); ReadingsSingleUpdateValue ($hash, 'after_'.$cmd.'_message', $err, 1); @@ -12470,6 +12476,34 @@ sub DbRep_afterproc { return ''; } +################################################################################### +# Befehl oder Code prozessieren +################################################################################### +sub _DbRep_procCode { + my $hash = shift; + my $fn = shift; + + my $err = q{}; + my $name = $hash->{NAME}; + + $fn =~ s/\s*#.*//g; # Kommentare entfernen + $fn = join ' ', split /\s+/sx, $fn; # Funktion serialisieren + + if ($fn =~ m/^\s*(\{.*\})\s*$/xs) { # unnamed Funktion direkt mit {...} + $fn = $1; + + eval $fn; + if ($@) { + $err = $@; + } + } + else { + $err = AnalyzeCommandChain (undef, $fn); + } + +return $err; +} + ############################################################################################## # timestamp_begin, timestamp_end bei Einsatz datetime-Picker entsprechend # den Anforderungen formatieren @@ -16508,54 +16542,59 @@ sub dbval { -
  • executeAfterProc - you can specify a FHEM command or perl function which should be executed - after command execution.
    - Perl functions have to be enclosed in {} .

    +
  • executeAfterProc

    + + You can specify a FHEM command or Perl code that should be executed after the command is processed.
    + Perl code is to be enclosed in {...}. The variables $hash (hash of the DbRep device) and $name + (name of the DbRep device) are available.

    - +
  • -
  • executeBeforeProc - you can specify a FHEM command or perl function which should be executed - before command execution.
    - Perl functions have to be enclosed in {} .

    +
  • executeBeforeProc

    + + A FHEM command or Perl code can be specified which is to be executed before the command is processed.
    + Perl code is to be enclosed in {...}. The variables $hash (hash of the DbRep device) and $name + (name of the DbRep device) are available.

    - +
  • +
  • expimpfile </path/file> [MAXLINES=<lines>] @@ -19550,54 +19589,60 @@ sub dbval {
  • -
  • executeAfterProc - Es kann ein FHEM-Kommando oder eine Perl-Funktion angegeben werden welche nach der - Befehlsabarbeitung ausgeführt werden soll.
    - Funktionen sind in {} einzuschließen.

    +
  • executeAfterProc

    + + Es kann ein FHEM-Kommando oder Perl Code angegeben werden der nach der Befehlsabarbeitung ausgeführt + werden soll.
    + Perl Code ist in {...} einzuschließen. Es stehen die Variablen $hash (Hash des DbRep Devices) und $name + (Name des DbRep-Devices) zur Verfügung.

    - +
  • -
  • executeBeforeProc - Es kann ein FHEM-Kommando oder eine Perl-Funktion angegeben werden welche vor der - Befehlsabarbeitung ausgeführt werden soll.
    - Funktionen sind in {} einzuschließen.

    +
  • executeBeforeProc

    + + Es kann ein FHEM-Kommando oder Perl Code angegeben werden der vor der Befehlsabarbeitung ausgeführt + werden soll.
    + Perl Code ist in {...} einzuschließen. Es stehen die Variablen $hash (Hash des DbRep Devices) und $name + (Name des DbRep-Devices) zur Verfügung.

    - +
  • expimpfile </Pfad/Filename> [MAXLINES=<lines>]