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:
tobiasfaust 2013-02-18 18:47:58 +00:00
parent 1fdccf139a
commit cd4a584c60

View File

@ -469,6 +469,9 @@ DbLog_Get($@)
if($outf eq "INT") { if($outf eq "INT") {
$outf = "-"; $outf = "-";
$internal = 1; $internal = 1;
} elsif (uc($outf) eq "WEBCHART") {
# redirect the get request to the chartQuery function
return chartQuery($hash, @_);
} }
my @readings = (); my @readings = ();
@ -713,6 +716,126 @@ DbLog_Get($@)
} }
return $retval; 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> </ul>
<br><br> <br><br>
</ul> </ul>
<b>Get</b> when used for webcharts
<ul>
<code>get &lt;name&gt; &lt;infile&gt; &lt;outfile&gt; &lt;from&gt;
&lt;to&gt; &lt;device&gt; &lt;querytype&gt; &lt;xaxis&gt; &lt;yaxis&gt; &lt;savename&gt; </code>
<br><br>
Query the Database to retrieve JSON-Formatted Data, which is used by the charting frontend.
<br>
<ul>
<li>&lt;name&gt;<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>&lt;in&gt;<br>
A dummy parameter for FileLog compatibility. Always set to <code>-</code></li>
<li>&lt;out&gt;<br>
A dummy parameter for FileLog compatibility. Set it to <code>webchart</code>
to use the charting related get function.
</li>
<li>&lt;from&gt; / &lt;to&gt;<br>
Used to select the data. Please use the following timeformat:<br>
<ul><code>YYYY-MM-DD_HH24:MI:SS</code></ul></li>
<li>&lt;device&gt;<br>
A string which represents the device to query.</li>
<li>&lt;querytype&gt;<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>&lt;xaxis&gt;<br>
A string which represents the xaxis</li>
<li>&lt;yaxis&gt;<br>
A string which represents the yaxis</li>
<li>&lt;savename&gt;<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> <a name="DbLogattr"></a>
<b>Attributes</b> <ul>N/A</ul><br> <b>Attributes</b> <ul>N/A</ul><br>
</ul> </ul>
@ -1031,6 +1209,63 @@ DbLog_Get($@)
</ul> </ul>
<br><br> <br><br>
</ul> </ul>
<b>Get</b> für die Nutzung von webcharts
<ul>
<code>get &lt;name&gt; &lt;infile&gt; &lt;outfile&gt; &lt;from&gt;
&lt;to&gt; &lt;device&gt; &lt;querytype&gt; &lt;xaxis&gt; &lt;yaxis&gt; &lt;savename&gt; </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>&lt;name&gt;<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>&lt;in&gt;<br>
Ein Dummy Parameter um eine Kompatibilität zum Filelog herzustellen.
Dieser Parameter ist immer auf <code>-</code> zu setzen.</li>
<li>&lt;out&gt;<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>&lt;from&gt; / &lt;to&gt;<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>&lt;device&gt;<br>
Ein String, der das abzufragende Device darstellt.</li>
<li>&lt;querytype&gt;<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>&lt;xaxis&gt;<br>
Ein String, der die X-Achse repräsentiert</li>
<li>&lt;yaxis&gt;<br>
Ein String, der die Y-Achse repräsentiert</li>
<li>&lt;savename&gt;<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> <a name="DbLogattr"></a>
<b>Attributes</b> <ul>N/A</ul><br> <b>Attributes</b> <ul>N/A</ul><br>
</ul> </ul>