diff --git a/fhem/CHANGED b/fhem/CHANGED
index f5921a6fd..b29b70ec8 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: 93_DbRep: v4.16.1, json as option of sqlResultFormat
- feature: 31_PLAYBULB: add support for ssh remote control
- change: 42_Nextion: fault tolerance in reader / fixes
- feature: 50_TelegramBot: favorites rework - inline / allow : in inline
diff --git a/fhem/FHEM/93_DbRep.pm b/fhem/FHEM/93_DbRep.pm
index 4a9a81b76..b4866913d 100644
--- a/fhem/FHEM/93_DbRep.pm
+++ b/fhem/FHEM/93_DbRep.pm
@@ -41,6 +41,8 @@
###########################################################################################################################
# Versions History:
#
+# 4.16.1 22.05.2017 encode json without JSON module, requires at least fhem.pl 14348 2017-05-22 20:25:06Z
+# 4.16.0 22.05.2017 format json as option of sqlResultFormat, state will never be deleted in "delread"
# 4.15.1 20.05.2017 correction of commandref
# 4.15.0 17.05.2017 SUM(VALUE),AVG(VALUE) recreated for PostgreSQL, Code reviewed and optimized
# 4.14.2 16.05.2017 SQL-Statements optimized for Wildcard "%" usage if used, Wildcard "_" isn't supported
@@ -211,7 +213,7 @@ use Time::Local;
sub DbRep_Main($$;$);
-my $DbRepVersion = "4.15.1";
+my $DbRepVersion = "4.16.1";
my %dbrep_col = ("DEVICE" => 64,
"TYPE" => 64,
@@ -250,7 +252,7 @@ sub DbRep_Initialize($) {
"showVariables ".
"showStatus ".
"showTableInfo ".
- "sqlResultFormat:separated,mline,sline,table ".
+ "sqlResultFormat:separated,mline,sline,table,json ".
"timestamp_begin ".
"timestamp_end ".
"timeDiffToNow ".
@@ -273,7 +275,7 @@ sub DbRep_Define($@) {
my $name = $hash->{NAME};
return "Error: Perl module ".$DbRepMMDBI." is missing. Install it on Debian with: sudo apt-get install libdbi-perl" if($DbRepMMDBI);
-
+
my @a = split("[ \t][ \t]*", $def);
if(int(@a) < 2) {
@@ -3580,6 +3582,21 @@ sub sqlCmd_ParseDone($) {
my $fi = sprintf($formatstr, $i);
ReadingsBulkUpdateValue ($hash, "SqlResultRow_".$fi, $row);
}
+ } elsif ($srf eq "json") {
+ my %result = ();
+ my @rows = split( /§/, $rowstring );
+ my $bigint = @rows;
+ my $numd = ceil(log10($bigint));
+ my $formatstr = sprintf('%%%d.%dd', $numd, $numd);
+ my $i = 0;
+ foreach my $row ( @rows ) {
+ $i++;
+ $row =~ s/\|°escaped°\|/§/g;
+ my $fi = sprintf($formatstr, $i);
+ $result{$fi} = $row;
+ }
+ my $json = toJSON(\%result); # at least fhem.pl 14348 2017-05-22 20:25:06Z
+ ReadingsBulkUpdateValue ($hash, "SqlResult", $json);
}
ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
@@ -3946,15 +3963,15 @@ return;
sub delread($) {
# Readings löschen die nicht in der Ausnahmeliste (Attr readingPreventFromDel) stehen
my ($hash) = @_;
- my $name = $hash->{NAME};
+ my $name = $hash->{NAME};
+ my @allrds = keys%{$defs{$name}{READINGS}};
my @rdpfdel = split(",", $hash->{HELPER}{RDPFDEL}) if($hash->{HELPER}{RDPFDEL});
if (@rdpfdel) {
- my @allrds = keys%{$defs{$name}{READINGS}};
foreach my $key(@allrds) {
- # Log3 ($name, 3, "DbRep $name - Reading Schlüssel: $key");
+ # Log3 ($name, 1, "DbRep $name - Reading Schlüssel: $key");
my $dodel = 1;
foreach my $rdpfdel(@rdpfdel) {
- if($key =~ /$rdpfdel/) {
+ if($key =~ /$rdpfdel/ || $key eq "state") {
$dodel = 0;
}
}
@@ -3963,7 +3980,10 @@ sub delread($) {
}
}
} else {
- delete $defs{$name}{READINGS};
+ foreach my $key(@allrds) {
+ # Log3 ($name, 1, "DbRep $name - Reading Schlüssel: $key");
+ delete($defs{$name}{READINGS}{$key}) if($key ne "state");
+ }
}
return undef;
}
@@ -4323,7 +4343,7 @@ return;
Only the content of table "history" will be included (except command "sqlCmd").
Overview which other Perl-modules DbRep is using:
-
+
POSIX
Time::HiRes
Time::Local
@@ -4608,11 +4628,10 @@ return;
Example:
get <name> tableinfo
attr <name> showTableInfo current,history
- # Only informations related to tables "current" and "history" will be created
+ # Only informations related to tables "current" and "history" are going to be created
-
- a
+
@@ -4712,20 +4731,49 @@ return;
-
+ sub resfromjson { + my ($name,$reading,$value) = @_; + my $hash = $defs{$name}; + + if ($reading eq "SqlResult") { + # only reading SqlResult contains JSON encoded data + my $data = decode_json($value); + + foreach my $k (keys(%$data)) { + + # use your own processing from here for every hash-element + # e.g. output of every element that contains "Cam" + my $ke = $data->{$k}; + if($ke =~ m/Cam/i) { + my ($res1,$res2) = split("\\|", $ke); + Log3($name, 1, "$name - extract element $k by userExitFn: ".$res1." ".$res2); + } + } + } + return; + } ++
+ sub resfromjson { + my ($name,$reading,$value) = @_; + my $hash = $defs{$name}; + + if ($reading eq "SqlResult") { + # nur Reading SqlResult enthält JSON-kodierte Daten + my $data = decode_json($value); + + foreach my $k (keys(%$data)) { + + # ab hier eigene Verarbeitung für jedes Hash-Element + # z.B. Ausgabe jedes Element welches "Cam" enthält + my $ke = $data->{$k}; + if($ke =~ m/Cam/i) { + my ($res1,$res2) = split("\\|", $ke); + Log3($name, 1, "$name - extract element $k by userExitFn: ".$res1." ".$res2); + } + } + } + return; + } ++