From 7a5dbd4a8d4434fc8ead51d151cdd5c8cb02b542 Mon Sep 17 00:00:00 2001 From: tobiasfaust <> Date: Wed, 5 Dec 2012 06:57:04 +0000 Subject: [PATCH] =?UTF-8?q?-=20deutsche=20Doku=20erg=C3=A4nzt=20-=205.Para?= =?UTF-8?q?meter=20in=20GET-Funktion=20in=20der=20column=5Fspec=20hinzugef?= =?UTF-8?q?=C3=BCgt=20=20=20hiermit=20k=C3=B6nnen=20komplexe=20Regexp=20zu?= =?UTF-8?q?r=20Modifizierung=20des=20gelesenen=20Readings=20=C3=BCbergeben?= =?UTF-8?q?=20werden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.fhem.de/fhem/trunk@2267 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/contrib/dblog/93_DbLog.pm | 264 ++++++++++++++++++++++++++------- 1 file changed, 213 insertions(+), 51 deletions(-) diff --git a/fhem/contrib/dblog/93_DbLog.pm b/fhem/contrib/dblog/93_DbLog.pm index 0937baa6f..81c5a5369 100755 --- a/fhem/contrib/dblog/93_DbLog.pm +++ b/fhem/contrib/dblog/93_DbLog.pm @@ -1,5 +1,6 @@ ############################################## +# $Id$ # # 93_DbLog.pm # written by Dr. Boris Neubert 2007-12-30 @@ -121,14 +122,12 @@ DbLog_ParseEvent($$) } # Onewire - elsif(($type eq "OWAD") || ($type eq "OWSWITCH") || - ($type eq "OWMULTI")) { - $reading = "data"; - $value = $event; + ($type eq "OWMULTI")) { + $reading = "data"; + $value = $event; } - # FS20 elsif(($type eq "FS20") || ($type eq "X10")) { @@ -214,7 +213,6 @@ DbLog_ParseEvent($$) if($event =~ m(T:.*)) { $reading= "data"; $value= $event; } if($reading eq "temperature") { $value=~ s/ \(Celsius\)//; $unit= "°C"; } if($reading eq "temperature") { $value=~ s/([-\.\d]+).*/$1/; $unit= "°C"; } #OWTHERM - if($reading eq "humidity") { $value=~ s/ \(\%\)//; $unit= "%"; } if($reading eq "battery") { $value=~ s/ok/1/; @@ -223,7 +221,6 @@ DbLog_ParseEvent($$) } } - # BS elsif($type eq "BS") { if($event =~ m(brightness:.*)) { @@ -475,11 +472,12 @@ DbLog_Get($@) #extract the Device:Reading arguments into @readings array for(my $i = 0; $i < int(@a); $i++) { - @fld = split(":", $a[$i], 4); - $readings[$i][0] = $fld[0]; - $readings[$i][1] = $fld[1]; - $readings[$i][2] = $fld[2]; - $readings[$i][3] = $fld[3]; + @fld = split(":", $a[$i], 5); + $readings[$i][0] = $fld[0]; # Device + $readings[$i][1] = $fld[1]; # Reading + $readings[$i][2] = $fld[2]; # Default + $readings[$i][3] = $fld[3]; # function + $readings[$i][4] = $fld[4]; # regexp } my $dbh= $hash->{DBH}; @@ -531,7 +529,7 @@ DbLog_Get($@) ".$sqlspec{all}." FROM history WHERE 1=1 - AND (DEVICE || '|' || READING) = ('".@readings[$i]->[0]."|".@readings[$i]->[1]."') + AND (DEVICE || '|' || READING) = ('".$readings[$i]->[0]."|".$readings[$i]->[1]."') AND TIMESTAMP > $sqlspec{from_timestamp} AND TIMESTAMP < $sqlspec{to_timestamp} ORDER BY TIMESTAMP"; @@ -551,20 +549,32 @@ DbLog_Get($@) $writeout = 0; $out_value = ""; $out_tstamp = ""; + + ############ Auswerten des 5. Parameters: Regexp ################### + if($readings[$i]->[4]) { + #evaluate + my $val = $sql_value; + eval("$readings[$i]->[4]"); + $sql_value = $val; + if($@) {Log 3, "DbLog: Fehler in der übergebenen Funktion: <".$readings[$i]->[4].">, Fehler: $@";} + $out_tstamp = $sql_timestamp; + $writeout=1; + } + ############ Auswerten des 4. Parameters: function ################### - if(@readings[$i]->[3] eq "int") { - #nur den integerwert uebernehmen falls zb value=15°C + if($readings[$i]->[3] eq "int") { + #nur den integerwert uebernehmen falls zb value=15?C $out_value = $1 if($sql_value =~ m/^(\d+).*/o); $out_tstamp = $sql_timestamp; $writeout=1; - } elsif (@readings[$i]->[3] =~ m/^int(\d+).*/o) { - #übernehme den Dezimalwert mit den angegebenen Stellen an Nachkommastellen + } elsif ($readings[$i]->[3] =~ m/^int(\d+).*/o) { + #?bernehme den Dezimalwert mit den angegebenen Stellen an Nachkommastellen $out_value = $1 if($sql_value =~ m/^([-\.\d]+).*/o); $out_tstamp = $sql_timestamp; $writeout=1; - } elsif (@readings[$i]->[3] eq "delta-h") { + } elsif ($readings[$i]->[3] eq "delta-h") { #Berechnung eines Stundenwertes %tstamp = DbLog_explode_datetime($sql_timestamp, undef); if($lastd[$i] eq "undef") { @@ -580,7 +590,7 @@ DbLog_Get($@) $maxval = -999999; $writeout=1; } - } elsif (@readings[$i]->[3] eq "delta-d") { + } elsif ($readings[$i]->[3] eq "delta-d") { #Berechnung eines Tageswertes %tstamp = DbLog_explode_datetime($sql_timestamp, undef); if($lastd[$i] eq "undef") { @@ -596,15 +606,6 @@ DbLog_Get($@) $maxval = -999999; $writeout=1; } - } elsif(@readings[$i]->[3]) { - #evaluate - my $val = $sql_value; - eval("@readings[$i]->[3]"); - $out_value = $val; - - if($@) {Log 3, "DbLog: Fehler in der übergebenen Funktion: <".@readings[$i]->[3].">, Fehler: $@";} - $out_tstamp = $sql_timestamp; - $writeout=1; } else { $out_value = $sql_value; $out_tstamp = $sql_timestamp; @@ -642,10 +643,10 @@ DbLog_Get($@) } #while fetchrow ######## den letzten Abschlusssatz rausschreiben ########## - if(@readings[$i]->[3] eq "delta-h" || @readings[$i]->[3] eq "delta-d") { + if($readings[$i]->[3] eq "delta-h" || $readings[$i]->[3] eq "delta-d") { $out_value = sprintf("%0.1f", $maxval - $minval); - $out_tstamp = DbLog_implode_datetime($lasttstamp{year}, $lasttstamp{month}, $lasttstamp{day}, $lasttstamp{hour}, "30", "00") if(@readings[$i]->[3] eq "delta-h"); - $out_tstamp = DbLog_implode_datetime($lasttstamp{year}, $lasttstamp{month}, $lasttstamp{day}, "00", "00", "00") if(@readings[$i]->[3] eq "delta-d"); + $out_tstamp = DbLog_implode_datetime($lasttstamp{year}, $lasttstamp{month}, $lasttstamp{day}, $lasttstamp{hour}, "30", "00") if($readings[$i]->[3] eq "delta-h"); + $out_tstamp = DbLog_implode_datetime($lasttstamp{year}, $lasttstamp{month}, $lasttstamp{day}, "00", "00", "00") if($readings[$i]->[3] eq "delta-d"); if(uc($outf) eq "ALL") { $retval .= sprintf("%s: %s %s %s %s %s %s\n", $out_tstamp, $sql_dev, $type, $event, $sql_reading, $out_value, $unit); } else { @@ -654,7 +655,7 @@ DbLog_Get($@) } } # DatenTrenner setzen - $retval .= "#@readings[$i]->[0]:@readings[$i]->[1]:@readings[$i]->[2]:@readings[$i]->[3]\n"; + $retval .= "#$readings[$i]->[0]:$readings[$i]->[1]:$readings[$i]->[2]:$readings[$i]->[3]\n"; } #for @readings #Ueberfuehren der gesammelten Werte in die globale Variable %data @@ -688,7 +689,6 @@ DbLog_Get($@) 1; - =pod =begin html @@ -704,20 +704,20 @@ DbLog_Get($@) Log events to a database. The database connection is defined in <configfilename> (see sample configuration file - db.conf). The configuration is stored in a separate file + contrib/dblog/db.conf). The configuration is stored in a separate file to avoid storing the password in the main configuration file and to have it visible in the output of the list command.

- You must have 93_DbLog.pm in the FHEM subdirectory - to make this work. Additionally, the modules DBI and - DBD::<dbtype> need to be installed (use - cpan -i <module> if your distribution does not have it). + The modules DBI and DBD::<dbtype> + need to be installed (use cpan -i <module> + if your distribution does not have it).

<regexp> is the same as in FileLog.

- Sample code to create a MySQL database is in fhemdb_create.sql. + Sample code to create a MySQL/PostgreSQL database is in + <DBType>_create.sql. The database contains two tables: current and history. The latter contains all events whereas the former only contains the last event for any given reading and device. @@ -779,7 +779,7 @@ DbLog_Get($@)
  • <column_spec>
    For each column_spec return a set of data separated by a comment line on the current connection.
    - Syntax: <device>:<reading>:<default>:<fn>
    + Syntax: <device>:<reading>:<default>:<fn>:<regexp>
  • +
  • <regexp>
    + The string is evaluated as a perl expression. $val is the + current value returned from the Database. The regexp is executed + before <fn> parameter.
    + Note: The string/perl expression cannot contain spaces, + as the part after the space will be considered as the + next column_spec. +


  • Examples: @@ -833,6 +836,165 @@ DbLog_Get($@) =end html =begin html_DE + +

    DbLog

    + =end html_DE =cut +