mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-05-04 22:19:38 +00:00
93_DbLog.pm
added support for webcharts with json (thanks to Johannes) git-svn-id: https://svn.fhem.de/fhem/trunk@2759 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
1fdccf139a
commit
cd4a584c60
@ -469,6 +469,9 @@ DbLog_Get($@)
|
||||
if($outf eq "INT") {
|
||||
$outf = "-";
|
||||
$internal = 1;
|
||||
} elsif (uc($outf) eq "WEBCHART") {
|
||||
# redirect the get request to the chartQuery function
|
||||
return chartQuery($hash, @_);
|
||||
}
|
||||
|
||||
my @readings = ();
|
||||
@ -713,6 +716,126 @@ DbLog_Get($@)
|
||||
}
|
||||
return $retval;
|
||||
}
|
||||
|
||||
################################################################
|
||||
#
|
||||
# Charting Specific functions start here
|
||||
#
|
||||
################################################################
|
||||
|
||||
################################################################
|
||||
#
|
||||
# Error handling, returns a JSON String
|
||||
#
|
||||
################################################################
|
||||
sub jsonError($) {
|
||||
my $errormsg = $_[0];
|
||||
my $json = "{success: false, msg:'$errormsg'}\n";
|
||||
return $json;
|
||||
}
|
||||
|
||||
|
||||
################################################################
|
||||
#
|
||||
# Prepare the SQL String
|
||||
#
|
||||
################################################################
|
||||
sub prepareSql(@_) {
|
||||
|
||||
my $starttime = $_[5];
|
||||
$starttime =~ s/_/ /;
|
||||
my $endtime = $_[6];
|
||||
$endtime =~ s/_/ /;
|
||||
my $device = $_[7];
|
||||
my $userquery = $_[8];
|
||||
my $xaxis = $_[9];
|
||||
my $yaxis = $_[10];
|
||||
my $savename = $_[11];
|
||||
my ($sql, $jsonstring);
|
||||
|
||||
if($userquery eq "getreadings") {
|
||||
$sql = "SELECT distinct(reading) FROM current WHERE device = '".$device."'";
|
||||
} elsif($userquery eq "getdevices") {
|
||||
$sql = 'SELECT distinct(device) FROM history';
|
||||
} elsif($userquery eq "timerange") {
|
||||
$sql = 'SELECT '.$xaxis.', VALUE FROM history WHERE READING = "'.$yaxis.'" AND DEVICE = "'.$device.'" AND TIMESTAMP Between "'.$starttime.'" AND "'.$endtime.'";';
|
||||
} elsif($userquery eq "savechart") {
|
||||
$jsonstring = '[{x:"'.$xaxis.'",y:"'.$yaxis.'",device:"'.$device.'",starttime:"'.$starttime.'",endtime:"'.$endtime.'"}]';
|
||||
$sql = "INSERT INTO history (EVENT, READING, VALUE) VALUES('".$jsonstring."', 'savedchart', '".$savename."')";
|
||||
} elsif($userquery eq "getcharts") {
|
||||
$sql = 'SELECT * FROM history WHERE READING = "savedchart"';
|
||||
} else {
|
||||
$sql = "error";
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
################################################################
|
||||
#
|
||||
# Do the query
|
||||
#
|
||||
################################################################
|
||||
sub chartQuery($@) {
|
||||
|
||||
my $sql = prepareSql(@_);
|
||||
|
||||
if ($sql eq "error") {
|
||||
return jsonError("Could not setup SQL String");
|
||||
}
|
||||
|
||||
my ($hash, @a) = @_;
|
||||
my $dbh= $hash->{DBH};
|
||||
|
||||
# prepare the query
|
||||
my $query_handle = $dbh->prepare($sql)
|
||||
or return jsonError("Couldn't prepare statement: " . $dbh->errstr . ", SQL was: " .$sql);
|
||||
|
||||
# execute the query
|
||||
$query_handle->execute()
|
||||
or return jsonError("Couldn't execute statement: " . $query_handle->errstr);
|
||||
|
||||
my $columns = $query_handle->{'NAME'};
|
||||
my $columncnt;
|
||||
|
||||
# When columns are empty but execution was successful, we have done a successful INSERT
|
||||
if($columns) {
|
||||
$columncnt = scalar @$columns;
|
||||
} else {
|
||||
return "{success: true, msg:'Insert ok'}\n";
|
||||
}
|
||||
|
||||
my $i = 0;
|
||||
my $jsonstring = "[";
|
||||
|
||||
while ( my @data = $query_handle->fetchrow_array()) {
|
||||
|
||||
if($i == 0) {
|
||||
$jsonstring .= "{";
|
||||
} else {
|
||||
$jsonstring .= ",{";
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $columncnt; $i++) {
|
||||
$jsonstring .= "'";
|
||||
$jsonstring .= uc($query_handle->{NAME}->[$i]);
|
||||
$jsonstring .= "':'";
|
||||
|
||||
if (defined $data[$i]) {
|
||||
$jsonstring .= $data[$i];
|
||||
}
|
||||
|
||||
if($i == ($columncnt -1)) {
|
||||
$jsonstring .= "'";
|
||||
} else {
|
||||
$jsonstring .= "',";
|
||||
}
|
||||
}
|
||||
$jsonstring .= "}";
|
||||
}
|
||||
$jsonstring .= "]";
|
||||
return $jsonstring;
|
||||
}
|
||||
################################################################
|
||||
|
||||
|
||||
@ -868,6 +991,61 @@ DbLog_Get($@)
|
||||
</ul>
|
||||
<br><br>
|
||||
</ul>
|
||||
|
||||
<b>Get</b> when used for webcharts
|
||||
<ul>
|
||||
<code>get <name> <infile> <outfile> <from>
|
||||
<to> <device> <querytype> <xaxis> <yaxis> <savename> </code>
|
||||
<br><br>
|
||||
Query the Database to retrieve JSON-Formatted Data, which is used by the charting frontend.
|
||||
<br>
|
||||
|
||||
<ul>
|
||||
<li><name><br>
|
||||
The name of the defined DbLog, like it is given in fhem.cfg. For functionality this has to be set to <code>logdb</code>.</li>
|
||||
<li><in><br>
|
||||
A dummy parameter for FileLog compatibility. Always set to <code>-</code></li>
|
||||
<li><out><br>
|
||||
A dummy parameter for FileLog compatibility. Set it to <code>webchart</code>
|
||||
to use the charting related get function.
|
||||
</li>
|
||||
<li><from> / <to><br>
|
||||
Used to select the data. Please use the following timeformat:<br>
|
||||
<ul><code>YYYY-MM-DD_HH24:MI:SS</code></ul></li>
|
||||
<li><device><br>
|
||||
A string which represents the device to query.</li>
|
||||
<li><querytype><br>
|
||||
A string which represents the method the query should use. Actually supported values are: <br>
|
||||
<code>getreadings</code> to retrieve the possible readings for a given device<br>
|
||||
<code>getdevices</code> to retrieve all available devices<br>
|
||||
<code>timerange</code> to retrieve charting data, which requires a given xaxis, yaxis, device, to and from<br>
|
||||
<code>savechart</code> to save a chart configuration in the database. Requires a given xaxis, yaxis, device, to and from, and a 'savename' used to save the chart<br>
|
||||
</li>
|
||||
<li><xaxis><br>
|
||||
A string which represents the xaxis</li>
|
||||
<li><yaxis><br>
|
||||
A string which represents the yaxis</li>
|
||||
<li><savename><br>
|
||||
A string which represents the name a chart will be saved with</li>
|
||||
|
||||
</ul>
|
||||
<br><br>
|
||||
Examples:
|
||||
<ul>
|
||||
<li><code>get logdb - webchart "" "" "" getcharts</code><br>
|
||||
Retrieves all saved charts from the Database</li>
|
||||
<li><code>get logdb - webchart "" "" "" getdevices</code><br>
|
||||
Retrieves all available devices from the Database</li>
|
||||
<li><code>get logdb - webchart "" "" ESA2000_LED_011e getreadings</code><br>
|
||||
Retrieves all available Readings for a given device from the Database</li>
|
||||
<li><code>get logdb - webchart 2013-02-11_00:00:00 2013-02-12_00:00:00 ESA2000_LED_011e timerange TIMESTAMP day_kwh</code><br>
|
||||
Retrieves charting data, which requires a given xaxis, yaxis, device, to and from<br>
|
||||
Will ouput a JSON like this: <code>[{'TIMESTAMP':'2013-02-11 00:10:10','VALUE':'0.22431388090756'},{'TIMESTAMP'.....}]</code></li>
|
||||
<li><code>get logdb - webchart 2013-02-11_00:00:00 2013-02-12_00:00:00 ESA2000_LED_011e savechart TIMESTAMP day_kwh tageskwh</code><br>
|
||||
Will save a chart in the database with the given name and the chart configuration parameters</li>
|
||||
</ul>
|
||||
<br><br>
|
||||
</ul>
|
||||
<a name="DbLogattr"></a>
|
||||
<b>Attributes</b> <ul>N/A</ul><br>
|
||||
</ul>
|
||||
@ -1031,6 +1209,63 @@ DbLog_Get($@)
|
||||
</ul>
|
||||
<br><br>
|
||||
</ul>
|
||||
|
||||
<b>Get</b> für die Nutzung von webcharts
|
||||
<ul>
|
||||
<code>get <name> <infile> <outfile> <from>
|
||||
<to> <device> <querytype> <xaxis> <yaxis> <savename> </code>
|
||||
<br><br>
|
||||
Liest Daten aus der Datenbank aus und gibt diese in JSON formatiert aus. Wird für das Charting Frontend genutzt
|
||||
<br>
|
||||
|
||||
<ul>
|
||||
<li><name><br>
|
||||
Der Name des definierten DbLogs, so wie er in der fhem.cfg angegeben wurde. Muss für Funktionalität auf <code>logdb</code> gesetzt werden.</li>
|
||||
<li><in><br>
|
||||
Ein Dummy Parameter um eine Kompatibilität zum Filelog herzustellen.
|
||||
Dieser Parameter ist immer auf <code>-</code> zu setzen.</li>
|
||||
<li><out><br>
|
||||
Ein Dummy Parameter um eine Kompatibilität zum Filelog herzustellen.
|
||||
Dieser Parameter ist auf <code>webchart</code> zu setzen um die Charting Get Funktion zu nutzen.
|
||||
</li>
|
||||
<li><from> / <to><br>
|
||||
Wird benutzt um den Zeitraum der Daten einzugrenzen. Es ist das folgende
|
||||
Zeitformat zu benutzen:<br>
|
||||
<ul><code>YYYY-MM-DD_HH24:MI:SS</code></ul></li>
|
||||
<li><device><br>
|
||||
Ein String, der das abzufragende Device darstellt.</li>
|
||||
<li><querytype><br>
|
||||
Ein String, der die zu verwendende Abfragemethode darstellt. Zur Zeit unterstützte Werte sind: <br>
|
||||
<code>getreadings</code> um für ein bestimmtes device alle Readings zu erhalten<br>
|
||||
<code>getdevices</code> um alle verfügbaren devices zu erhalten<br>
|
||||
<code>timerange</code> um Chart-Daten abzufragen. Es werden die Parameter 'xaxis', 'yaxis', 'device', 'to' und 'from' benötigt<br>
|
||||
<code>savechart</code> um einen Chart unter Angabe eines 'savename' und seiner zugehörigen Konfiguration abzuspeichern<br>
|
||||
</li>
|
||||
<li><xaxis><br>
|
||||
Ein String, der die X-Achse repräsentiert</li>
|
||||
<li><yaxis><br>
|
||||
Ein String, der die Y-Achse repräsentiert</li>
|
||||
<li><savename><br>
|
||||
Ein String, unter dem ein Chart in der Datenbank gespeichert werden soll</li>
|
||||
</ul>
|
||||
<br><br>
|
||||
Beispiele:
|
||||
<ul>
|
||||
<li><code>get logdb - webchart "" "" "" getcharts</code><br>
|
||||
Liefert alle gespeicherten Charts aus der Datenbank</li>
|
||||
<li><code>get logdb - webchart "" "" "" getdevices</code><br>
|
||||
Liefert alle verfügbaren Devices aus der Datenbank</li>
|
||||
<li><code>get logdb - webchart "" "" ESA2000_LED_011e getreadings</code><br>
|
||||
Liefert alle verfügbaren Readings aus der Datenbank unter Angabe eines Gerätes</li>
|
||||
<li><code>get logdb - webchart 2013-02-11_00:00:00 2013-02-12_00:00:00 ESA2000_LED_011e timerange TIMESTAMP day_kwh</code><br>
|
||||
Liefert Chart-Daten, die auf folgenden Parametern basieren: 'xaxis', 'yaxis', 'device', 'to' und 'from'<br>
|
||||
Die Ausgabe erfolgt als JSON, z.B.: <code>[{'TIMESTAMP':'2013-02-11 00:10:10','VALUE':'0.22431388090756'},{'TIMESTAMP'.....}]</code></li>
|
||||
<li><code>get logdb - webchart 2013-02-11_00:00:00 2013-02-12_00:00:00 ESA2000_LED_011e savechart TIMESTAMP day_kwh tageskwh</code><br>
|
||||
Speichert einen Chart unter Angabe eines 'savename' und seiner zugehörigen Konfiguration</li>
|
||||
</ul>
|
||||
<br><br>
|
||||
</ul>
|
||||
|
||||
<a name="DbLogattr"></a>
|
||||
<b>Attributes</b> <ul>N/A</ul><br>
|
||||
</ul>
|
||||
|
Loading…
x
Reference in New Issue
Block a user