mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-03-10 09:16:53 +00:00
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
This commit is contained in:
parent
298acb7107
commit
1acf456a41
@ -1,5 +1,6 @@
|
|||||||
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
|
# 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.
|
# 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
|
- feature: 31_PLAYBULB: add support for ssh remote control
|
||||||
- change: 42_Nextion: fault tolerance in reader / fixes
|
- change: 42_Nextion: fault tolerance in reader / fixes
|
||||||
- feature: 50_TelegramBot: favorites rework - inline / allow : in inline
|
- feature: 50_TelegramBot: favorites rework - inline / allow : in inline
|
||||||
|
@ -41,6 +41,8 @@
|
|||||||
###########################################################################################################################
|
###########################################################################################################################
|
||||||
# Versions History:
|
# 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.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.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
|
# 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($$;$);
|
sub DbRep_Main($$;$);
|
||||||
|
|
||||||
my $DbRepVersion = "4.15.1";
|
my $DbRepVersion = "4.16.1";
|
||||||
|
|
||||||
my %dbrep_col = ("DEVICE" => 64,
|
my %dbrep_col = ("DEVICE" => 64,
|
||||||
"TYPE" => 64,
|
"TYPE" => 64,
|
||||||
@ -250,7 +252,7 @@ sub DbRep_Initialize($) {
|
|||||||
"showVariables ".
|
"showVariables ".
|
||||||
"showStatus ".
|
"showStatus ".
|
||||||
"showTableInfo ".
|
"showTableInfo ".
|
||||||
"sqlResultFormat:separated,mline,sline,table ".
|
"sqlResultFormat:separated,mline,sline,table,json ".
|
||||||
"timestamp_begin ".
|
"timestamp_begin ".
|
||||||
"timestamp_end ".
|
"timestamp_end ".
|
||||||
"timeDiffToNow ".
|
"timeDiffToNow ".
|
||||||
@ -3580,6 +3582,21 @@ sub sqlCmd_ParseDone($) {
|
|||||||
my $fi = sprintf($formatstr, $i);
|
my $fi = sprintf($formatstr, $i);
|
||||||
ReadingsBulkUpdateValue ($hash, "SqlResultRow_".$fi, $row);
|
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");
|
ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
|
||||||
@ -3947,14 +3964,14 @@ sub delread($) {
|
|||||||
# Readings löschen die nicht in der Ausnahmeliste (Attr readingPreventFromDel) stehen
|
# Readings löschen die nicht in der Ausnahmeliste (Attr readingPreventFromDel) stehen
|
||||||
my ($hash) = @_;
|
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});
|
my @rdpfdel = split(",", $hash->{HELPER}{RDPFDEL}) if($hash->{HELPER}{RDPFDEL});
|
||||||
if (@rdpfdel) {
|
if (@rdpfdel) {
|
||||||
my @allrds = keys%{$defs{$name}{READINGS}};
|
|
||||||
foreach my $key(@allrds) {
|
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;
|
my $dodel = 1;
|
||||||
foreach my $rdpfdel(@rdpfdel) {
|
foreach my $rdpfdel(@rdpfdel) {
|
||||||
if($key =~ /$rdpfdel/) {
|
if($key =~ /$rdpfdel/ || $key eq "state") {
|
||||||
$dodel = 0;
|
$dodel = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3963,7 +3980,10 @@ sub delread($) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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;
|
return undef;
|
||||||
}
|
}
|
||||||
@ -4608,11 +4628,10 @@ return;
|
|||||||
Example: <br>
|
Example: <br>
|
||||||
get <name> tableinfo <br>
|
get <name> tableinfo <br>
|
||||||
attr <name> showTableInfo current,history <br>
|
attr <name> showTableInfo current,history <br>
|
||||||
# Only informations related to tables "current" and "history" will be created
|
# Only informations related to tables "current" and "history" are going to be created
|
||||||
</li>
|
</li>
|
||||||
<br><br>
|
<br><br>
|
||||||
</ul>
|
</ul>
|
||||||
a
|
|
||||||
<br>
|
<br>
|
||||||
</ul></ul>
|
</ul></ul>
|
||||||
|
|
||||||
@ -4712,20 +4731,49 @@ return;
|
|||||||
</ul><br>
|
</ul><br>
|
||||||
|
|
||||||
|
|
||||||
<li><b>sqlResultFormat </b> - determines the formatting of the "set ... sqlCmd" command result. </li> <br>
|
<li><b>sqlResultFormat </b> - determines the formatting of the "set ... sqlCmd" command result. possible options are: <br><br>
|
||||||
|
|
||||||
possible options are: <br>
|
|
||||||
<ul>
|
<ul>
|
||||||
<b>separated </b> - every line of the result will be generated sequentially in a single
|
<b>separated </b> - every line of the result will be generated sequentially in a single
|
||||||
reading. (default) <br>
|
reading. (default) <br><br>
|
||||||
<b>mline </b> - the result will be generated as multiline in
|
<b>mline </b> - the result will be generated as multiline in
|
||||||
<a href="#DbRepReadings">Reading</a> SqlResult.
|
<a href="#DbRepReadings">Reading</a> SqlResult.
|
||||||
Field separator is "|". <br>
|
Field separator is "|". <br><br>
|
||||||
<b>sline </b> - the result will be generated as singleline in
|
<b>sline </b> - the result will be generated as singleline in
|
||||||
<a href="#DbRepReadings">Reading</a> SqlResult.
|
<a href="#DbRepReadings">Reading</a> SqlResult.
|
||||||
Field separator is "|" and the dataset is separated by "]|[". <br>
|
Field separator is "|" and the dataset is separated by "]|[". <br><br>
|
||||||
<b>table </b> - the result will be generated as an table in
|
<b>table </b> - the result will be generated as an table in
|
||||||
<a href="#DbRepReadings">Reading</a> SqlResult. <br>
|
<a href="#DbRepReadings">Reading</a> SqlResult. <br><br>
|
||||||
|
<b>json </b> - creates <a href="#DbRepReadings">Reading</a> SqlResult as a JSON
|
||||||
|
coded hash.
|
||||||
|
Every hash-element consists of the serial number of the dataset (key)
|
||||||
|
and its value. </li> <br><br>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
To process the result, you may use a userExitFn in 99_myUtils for example: <br>
|
||||||
|
<pre>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
</ul> </ul>
|
||||||
</ul><br>
|
</ul><br>
|
||||||
|
|
||||||
<li><b>timestamp_begin </b> - begin of data selection (*) </li> <br>
|
<li><b>timestamp_begin </b> - begin of data selection (*) </li> <br>
|
||||||
@ -4787,7 +4835,7 @@ return;
|
|||||||
my ($name,$reading,$value) = @_;
|
my ($name,$reading,$value) = @_;
|
||||||
my $hash = $defs{$name};
|
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 " ;
|
Log3 $name, 1, "UserExitFn $name called - transfer parameter are Reading: $reading, Value: $value " ;
|
||||||
...
|
...
|
||||||
return;
|
return;
|
||||||
@ -5392,18 +5440,47 @@ return;
|
|||||||
# Es werden nur Information der Tabellen "current" und "history" angezeigt <br>
|
# Es werden nur Information der Tabellen "current" und "history" angezeigt <br>
|
||||||
</ul><br>
|
</ul><br>
|
||||||
|
|
||||||
<li><b>sqlResultFormat </b> - legt die Formatierung des Ergebnisses von "set ... sqlResult" fest. </li> <br>
|
<li><b>sqlResultFormat </b> - legt die Formatierung des Ergebnisses von "set ... sqlResult" fest. Mögliche Optionen sind: <br><br>
|
||||||
|
|
||||||
mögliche Optionen sind: <br>
|
|
||||||
<ul>
|
<ul>
|
||||||
<b>separated </b> - die Ergebniszeilen werden als einzelne Readings fortlaufend
|
<b>separated </b> - die Ergebniszeilen werden als einzelne Readings fortlaufend
|
||||||
generiert. (default)<br>
|
generiert. (default)<br><br>
|
||||||
<b>mline </b> - das Ergebnis wird als Mehrzeiler im <a href="#DbRepReadings">Reading</a>
|
<b>mline </b> - das Ergebnis wird als Mehrzeiler im <a href="#DbRepReadings">Reading</a>
|
||||||
sqlCmd dargestellt. Feldtrenner ist "|". <br>
|
SqlResult dargestellt. Feldtrenner ist "|". <br><br>
|
||||||
<b>sline </b> - das Ergebnis wird als Singleline im <a href="#DbRepReadings">Reading</a>
|
<b>sline </b> - das Ergebnis wird als Singleline im <a href="#DbRepReadings">Reading</a>
|
||||||
sqlCmd dargestellt. Feldtrenner ist "|", Satztrenner ist"]|[". <br>
|
SqlResult dargestellt. Feldtrenner ist "|", Satztrenner ist"]|[". <br><br>
|
||||||
<b>table </b> - das Ergebnis wird als Tabelle im <a href="#DbRepReadings">Reading</a>
|
<b>table </b> - das Ergebnis wird als Tabelle im <a href="#DbRepReadings">Reading</a>
|
||||||
sqlCmd dargestellt. <br>
|
SqlResult dargestellt. <br><br>
|
||||||
|
<b>json </b> - erzeugt das <a href="#DbRepReadings">Reading</a> SqlResult als
|
||||||
|
JSON-kodierten Hash.
|
||||||
|
Jedes Hash-Element (Ergebnissatz) setzt sich aus der laufenden Nummer
|
||||||
|
des Datensatzes (Key) und dessen Wert zusammen. </li><br><br>
|
||||||
|
<ul>
|
||||||
|
Die Weiterverarbeitung des Ergebnisses kann z.B. mit der folgenden userExitFn in 99_myUtils.pm erfolgen: <br>
|
||||||
|
<pre>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
</ul>
|
||||||
</ul><br>
|
</ul><br>
|
||||||
|
|
||||||
<li><b>timestamp_begin </b> - der zeitliche Beginn für die Datenselektion (*) </li> <br>
|
<li><b>timestamp_begin </b> - der zeitliche Beginn für die Datenselektion (*) </li> <br>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user