From 1acf456a416d07a82fee75e60372264bbdbb9e9d Mon Sep 17 00:00:00 2001 From: nasseeder1 Date: Sat, 27 May 2017 11:49:53 +0000 Subject: [PATCH] 93_DbRep: v4.16.1, json as option of sqlResultFormat git-svn-id: https://svn.fhem.de/fhem/trunk@14386 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/93_DbRep.pm | 131 +++++++++++++++++++++++++++++++++--------- 2 files changed, 105 insertions(+), 27 deletions(-) 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;
-
  • sqlResultFormat - determines the formatting of the "set ... sqlCmd" command result.

  • - - possible options are:
    -
  • timestamp_begin - begin of data selection (*)

  • @@ -4787,7 +4835,7 @@ return; my ($name,$reading,$value) = @_; my $hash = $defs{$name}; ... - # z.B. übergebene Daten loggen + # e.g. output transfered data Log3 $name, 1, "UserExitFn $name called - transfer parameter are Reading: $reading, Value: $value " ; ... return; @@ -5392,18 +5440,47 @@ return; # Es werden nur Information der Tabellen "current" und "history" angezeigt

    -
  • sqlResultFormat - legt die Formatierung des Ergebnisses von "set ... sqlResult" fest.

  • +
  • sqlResultFormat - legt die Formatierung des Ergebnisses von "set ... sqlResult" fest. Mögliche Optionen sind:

    - mögliche Optionen sind:

  • timestamp_begin - der zeitliche Beginn für die Datenselektion (*)