From edfc3ce967f64bb7c1bcaa90b9ec78a196b8b100 Mon Sep 17 00:00:00 2001 From: zap <> Date: Thu, 24 Mar 2022 10:38:58 +0000 Subject: [PATCH] AndroidDB: Added creation of readings for shell commands git-svn-id: https://svn.fhem.de/fhem/trunk@25879 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/89_AndroidDB.pm | 40 ++++++++++++++++++++++++++++++----- fhem/FHEM/89_AndroidDBHost.pm | 2 +- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 5996163b4..f05a9a4d5 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # 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. + - feature: 89_AndroidDB: Added creation of readings for shell commands - feature: 98_archetype: new option for FILTER in actual_.* attributes - change: 98_archetype: now packaged, extend commandref - feature: 89_AndroidDB: Added FHEM reading attributes diff --git a/fhem/FHEM/89_AndroidDB.pm b/fhem/FHEM/89_AndroidDB.pm index 517b176ce..dda09c056 100644 --- a/fhem/FHEM/89_AndroidDB.pm +++ b/fhem/FHEM/89_AndroidDB.pm @@ -4,7 +4,7 @@ # # 89_AndroidDB # -# Version 0.7 +# Version 0.8 # # FHEM Integration for Android Devices # @@ -37,7 +37,7 @@ sub AndroidDB_Initialize ($) $hash->{ShutdownFn} = "AndroidDB::Shutdown"; $hash->{parseParams} = 1; - $hash->{AttrList} = 'connect:0,1 macros:textField-long preset presetFile '.$readingFnAttributes; + $hash->{AttrList} = 'connect:0,1 createReadings macros:textField-long preset presetFile '.$readingFnAttributes; $data{RC_layout}{MagentaTVStick} = "AndroidDB::RCLayoutMagentaTVStick"; $data{RC_layout}{MagentaOne} = "AndroidDB::RCLayoutMagentaOne"; @@ -59,6 +59,7 @@ BEGIN { readingsBulkUpdateIfChanged readingsBeginUpdate readingsEndUpdate + makeReadingName setDevAttrList CommandDefine CommandSet @@ -276,7 +277,10 @@ sub Set ($@) elsif ($lcopt eq 'shell') { return "Usage: set $name $opt ShellCommand" if (scalar(@$a) == 0); my ($rc, $result, $error) = AndroidDBHost::Run ($hash, $opt, '.*', @$a); - return $result.$error, + return $error if ($rc == 0); + my $createReadings = AttrVal ($name, 'createReadings', ''); + return $result if ($createReadings eq '' || $createReadings !~ /$createReadings/); + UpdateReadings ($hash, $result); } elsif ($lcopt eq 'remotecontrol') { my $macroName = shift @$a // return "Usage: set $name $opt MacroName"; @@ -390,8 +394,10 @@ sub Attr ($@) if ($cmd eq 'set') { if ($attrName eq 'macros') { + delete $hash->{adb}{preset}{_custom_} if (exists($hash->{adb}{preset}{_custom_})); + delete $hash->{adb}{macro}{_custom_} if (exists($hash->{adb}{macro}{_custom_})); foreach my $macroDef (split /;/, $attrVal) { - my ($macroName, $macroPar) = split (':', $macroDef); + my ($macroName, $macroPar) = split (':', $macroDef, 2); if (!defined($macroDef)) { Log3 $name, 2, "Missing defintion for macro $macroName"; return "Missing definition for macro $macroName"; @@ -415,6 +421,7 @@ sub Attr ($@) } elsif ($cmd eq 'del') { delete $hash->{adb}{preset}{_custom_} if (exists($hash->{adb}{preset}{_custom_})); + delete $hash->{adb}{macro}{_custom_} if (exists($hash->{adb}{macro}{_custom_})); } return undef; @@ -459,7 +466,7 @@ sub LoadPresets ($$) foreach my $l (@lines) { next if ($l =~ /^#/); # Comments are allowed - my ($macroName, $macroPar) = split (':', $l); + my ($macroName, $macroPar) = split (':', $l, 2); if (!defined($macroPar)) { next if (!defined($macroName) || $macroName eq ''); if ($macroName !~ /^[a-zA-Z0-9-_]+$/) { @@ -519,6 +526,24 @@ sub ExportPresets ($$) return 0; } +sub UpdateReadings ($$) +{ + my ($hash, $data) = @_; + + readingsBeginUpdate ($hash); + + foreach my $line (split /[\n\r]+/, $data) { + $line =~ s/^\s+//; # Remove leading whitespace characters + next if ($line eq ''); # Ignore empty lines + my @a = split('=', $line); + next if (scalar(@a) != 2); + my $r = makeReadingName ($a[0]); + readingsBulkUpdate ($hash, $r, $a[1]); + } + + readingsEndUpdate ($hash, 1); +} + sub ShowMessage ($$$) { my ($hash, $level, $msg) = @_; @@ -695,6 +720,11 @@ sub RCLayoutMagentaTVExt () {