diff --git a/CHANGED b/CHANGED index f1e173859..d5a6f7be0 100644 --- a/CHANGED +++ b/CHANGED @@ -1,5 +1,7 @@ # 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_DbLog: V2.17.1, optional UTF-8 support for MySQL database + (additional parameter in db.conf neccesary for activation) - feature: YAMAHA_AVR: new set commands / readings to control HDMI outputs (only for models with two HDMI outputs) - featere: exclude_from_update can check the source too (Forum #73275) diff --git a/FHEM/93_DbLog.pm b/FHEM/93_DbLog.pm index f18dd3a4a..a67ec0140 100644 --- a/FHEM/93_DbLog.pm +++ b/FHEM/93_DbLog.pm @@ -10,12 +10,14 @@ # # reduceLog() created by Claudiu Schuster (rapster) # -# redesigned 2017 by DS_Starter with credits by +# redesigned 2016/2017 by DS_Starter with credits by # JoeAllb, DeeSpe # ############################################################################################################################################ # Versions History done by DS_Starter & DeeSPe: # +# 2.17.1 17.06.2017 fix log-entries "utf8 enabled" if SVG's called, commandref revised, enable UTF8 for DbLog_get +# 2.17.0 15.06.2017 enable UTF8 for MySQL (entry in configuration file necessary) # 2.16.11 03.06.2017 execmemcache changed for SQLite avoid logging if deleteOldDaysNbl or reduceLogNbL is running # 2.16.10 15.05.2017 commandref revised # 2.16.9.1 11.05.2017 set userCommand changed - @@ -128,8 +130,9 @@ eval "use DBI;1" or my $DbLogMMDBI = "DBI"; use Data::Dumper; use Blocking; use Time::HiRes qw(gettimeofday tv_interval); +use Encode qw(encode_utf8); -my $DbLogVersion = "2.16.11"; +my $DbLogVersion = "2.17.1"; my %columns = ("DEVICE" => 64, "TYPE" => 64, @@ -230,7 +233,7 @@ sub DbLog_Define($@) $hash->{cache}{index} = 0; # read configuration data - my $ret = _DbLog_readCfg($hash); + my $ret = DbLog_readCfg($hash); return $ret if ($ret); # return on error while reading configuration # set used COLUMNS @@ -490,7 +493,7 @@ sub DbLog_Set($@) { $dbh->commit() if(!$dbh->{AutoCommit}); $dbh->disconnect(); } - $ret = _DbLog_readCfg($hash); + $ret = DbLog_readCfg($hash); return $ret if $ret; DbLog_ConnectPush($hash); $ret = "Rereadcfg executed."; @@ -1653,6 +1656,7 @@ sub DbLog_PushAsync(@) { my $dbpassword = $attr{"sec$name"}{secret}; my $DbLogType = AttrVal($name, "DbLogType", "History"); my $supk = AttrVal($name, "noSupportPK", 0); + my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0; my $errorh = 0; my $errorc = 0; my $error = 0; @@ -1666,7 +1670,7 @@ sub DbLog_PushAsync(@) { # Background-Startzeit my $bst = [gettimeofday]; - eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1 });}; + eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, mysql_enable_utf8 => $utf8 });}; if ($@) { $error = encode_base64($@,""); @@ -2049,7 +2053,7 @@ sub DbLog_implode_datetime($$$$$$) { ################################################################################### # Verbindungen zur DB aufbauen ################################################################################### -sub _DbLog_readCfg($){ +sub DbLog_readCfg($){ my ($hash)= @_; my $name = $hash->{NAME}; @@ -2082,6 +2086,11 @@ sub _DbLog_readCfg($){ Log3 $hash->{NAME}, 3, "Only Mysql, Postgresql, Oracle, SQLite are fully supported."; Log3 $hash->{NAME}, 3, "It may cause SQL-Erros during generating plots."; } + + if($hash->{DBMODEL} eq "MYSQL") { + $hash->{UTF8} = defined($dbconfig{utf8})?$dbconfig{utf8}:0; + } + return; } @@ -2092,9 +2101,10 @@ sub DbLog_ConnectPush($;$$) { my $dbconn = $hash->{dbconn}; my $dbuser = $hash->{dbuser}; my $dbpassword = $attr{"sec$name"}{secret}; + my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0; Log3 $hash->{NAME}, 3, "DbLog $name: Creating Push-Handle to database $dbconn with user $dbuser" if(!$get); - my $dbhp = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0 }); + my $dbhp = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, mysql_enable_utf8 => $utf8 }); if(!$dbhp) { RemoveInternalTimer($hash, "DbLog_ConnectPush"); @@ -2106,6 +2116,7 @@ sub DbLog_ConnectPush($;$$) { } Log3 $hash->{NAME}, 3, "DbLog $name: Push-Handle to db $dbconn created" if(!$get); + Log3 $hash->{NAME}, 3, "DbLog $name: UTF8 support enabled" if($utf8 && $hash->{DBMODEL} eq "MYSQL" && !$get); readingsSingleUpdate($hash, 'state', 'connected', 1) if(!$get); $hash->{DBHP}= $dbhp; @@ -2127,9 +2138,10 @@ sub DbLog_ConnectNewDBH($) { my $dbconn = $hash->{dbconn}; my $dbuser = $hash->{dbuser}; my $dbpassword = $attr{"sec$name"}{secret}; + my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0; my $dbh; - eval { $dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0 }); }; + eval { $dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, mysql_enable_utf8 => $utf8 }); }; if($@) { Log3($name, 2, "DbLog $name: - $@"); @@ -2191,11 +2203,10 @@ sub DbLog_ExecSQL1($$$) # outfile: [-|ALL|INT|WEBCHART] # ################################################################ -sub -DbLog_Get($@) -{ +sub DbLog_Get($@) { my ($hash, @a) = @_; my $name = $hash->{NAME}; + my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0; my $dbh; return dbReadings($hash,@a) if $a[1] =~ m/^Readings/; @@ -2701,6 +2712,8 @@ DbLog_Get($@) return @ReturnArray; } else { + $retval = Encode::encode_utf8($retval) if($utf8); + # Log3 $name, 5, "DbLog $name -> Result of get:\n$retval"; return $retval; } } @@ -4035,8 +4048,50 @@ sub checkUsePK ($$){

Log events to a database. The database connection is defined in - <configfilename> (see sample configuration file - contrib/dblog/db.conf). The configuration is stored in a separate file + <configfilename> + + In contrib/dblog an example configuration and scripts to create needed tables in the + different databases are provided.

+ + The configuration file should be copied e.g. to /opt/fhem and has the following structure you have to customize + suitable to your conditions (decomment the appropriate raws and adjust it):

+ +
+    ####################################################################################
+    # database configuration file                                    
+    #                                                                
+    ## for MySQL                                                     
+    ####################################################################################
+    #%dbconfig= (                                                    
+    #    connection => "mysql:database=fhem;host=db;port=3306",       
+    #    user => "fhemuser",                                          
+    #    password => "fhempassword",
+    #    # optional enable(1) / disable(0) UTF-8 support (at least V 4.042 is necessary) 	
+    #    utf8 => 1,   
+    #);                                                              
+    ####################################################################################
+    #                                                                
+    ## for PostgreSQL                                                
+    ####################################################################################
+    #%dbconfig= (                                                   
+    #    connection => "Pg:database=fhem;host=localhost",        
+    #    user => "fhemuser",                                     
+    #    password => "fhempassword"                              
+    #);                                                              
+    ####################################################################################
+    #                                                                
+    ## for SQLite (username and password stay empty for SQLite)      
+    ####################################################################################
+    #%dbconfig= (                                                   
+    #    connection => "SQLite:dbname=/opt/fhem/fhem.db",        
+    #    user => "",                                             
+    #    password => ""                                          
+    #);                                                              
+    ####################################################################################
+	
+
+ + 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.

@@ -4303,7 +4358,7 @@ sub checkUsePK ($$){
  • get myDbLog - - 2012-11-10 2012-11-20 KS300:temperature
  • get myDbLog current ALL - - %:temperature

  • you will get all actual readings "temperature" from all logged devices. - Be carful by using "history" as inputfile because a long execution time will be expected! + Be careful by using "history" as inputfile because a long execution time will be expected!
  • get myDbLog - - 2012-11-10_10 2012-11-10_20 KS300:temperature::int1
    like from 10am until 08pm at 10.11.2012
  • get myDbLog - all 2012-11-10 2012-11-20 KS300:temperature
  • @@ -4766,9 +4821,50 @@ sub checkUsePK ($$){

    Speichert Events in eine Datenbank. Die Datenbankverbindungsparameter werden - definiert in <configfilename>. (Vergleiche - Beipspielkonfigurationsdatei in contrib/dblog/db.conf).
    - Die Konfiguration ist in einer sparaten Datei abgelegt um das Datenbankpasswort + definiert in <configfilename>.
    + + In contrib/dblog sind eine Beispielkonfiguation und Scripts zum Anlegen der benötigten Tabellen + der verschiedenen Datenbanktypen bereitgestellt.

    + + Die Konfigurationsdatei wird z.B. nach /opt/fhem kopiert und hat folgenden Aufbau, den man an seine Umgebung + anpassen muß (entsprechende Zeilen entkommentieren und anpassen):

    + +
    +    ####################################################################################
    +    # database configuration file                                    
    +    #                                                                
    +    ## for MySQL                                                     
    +    ####################################################################################
    +    #%dbconfig= (                                                    
    +    #    connection => "mysql:database=fhem;host=db;port=3306",       
    +    #    user => "fhemuser",                                          
    +    #    password => "fhempassword",
    +    #    # optional enable(1) / disable(0) UTF-8 support (at least V 4.042 is necessary) 	
    +    #    utf8 => 1,   
    +    #);                                                              
    +    ####################################################################################
    +    #                                                                
    +    ## for PostgreSQL                                                
    +    ####################################################################################
    +    #%dbconfig= (                                                   
    +    #    connection => "Pg:database=fhem;host=localhost",        
    +    #    user => "fhemuser",                                     
    +    #    password => "fhempassword"                              
    +    #);                                                              
    +    ####################################################################################
    +    #                                                                
    +    ## for SQLite (username and password stay empty for SQLite)      
    +    ####################################################################################
    +    #%dbconfig= (                                                   
    +    #    connection => "SQLite:dbname=/opt/fhem/fhem.db",        
    +    #    user => "",                                             
    +    #    password => ""                                          
    +    #);                                                              
    +    ####################################################################################
    +	
    +
    + + Die Konfiguration ist in einer sparaten Datei abgelegt um das Datenbankpasswort nicht in Klartext in der FHEM-Haupt-Konfigurationsdatei speichern zu müssen. Ansonsten wäre es mittels des list Befehls einfach auslesbar. @@ -4967,7 +5063,7 @@ sub checkUsePK ($$){ <to> <column_spec>

    Liesst Daten aus der Datenbank. Wird durch die Frontends benutzt um Plots - zu generieren ohne selbst auf die Datenank zugreifen zu mössen. + zu generieren ohne selbst auf die Datenank zugreifen zu müssen.
  • <regexp>
    - Diese Zeichenkette wird als Perl Befehl ausgewertet. Die regexp wird vor dem angegebenen <fn> Parameter ausgeföhrt. + Diese Zeichenkette wird als Perl Befehl ausgewertet. + Die regexp wird vor dem angegebenen <fn> Parameter ausgeführt.
    Bitte zur Beachtung: Diese Zeichenkette darf keine Leerzeichen enthalten da diese sonst als <column_spec> Trennung interpretiert werden und alles nach dem Leerzeichen als neue <column_spec> gesehen wird.
    - Schlüsselwörter + + Schlüsselwörter
  • $val ist der aktuelle Wert die die Datenbank für ein Device/Reading ausgibt.
  • $ts ist der aktuelle Timestamp des Logeintrages.
  • Wird als $val das Schlüsselwort "hide" zurückgegeben, so wird dieser Logeintrag nicht - ausgegeben, trotzdem aber för die Zeitraumberechnung verwendet.
  • + ausgegeben, trotzdem aber für die Zeitraumberechnung verwendet.
  • Wird als $val das Schlüsselwort "ignore" zurückgegeben, so wird dieser Logeintrag - nicht för eine Folgeberechnung verwendet.
  • + nicht für eine Folgeberechnung verwendet. @@ -5061,22 +5159,29 @@ sub checkUsePK ($$){ Beispiele:

    @@ -5092,20 +5197,25 @@ sub checkUsePK ($$){

    @@ -5138,16 +5254,21 @@ sub checkUsePK ($$){

    diff --git a/contrib/dblog/db.conf b/contrib/dblog/db.conf index 72d511c9f..8fe123664 100644 --- a/contrib/dblog/db.conf +++ b/contrib/dblog/db.conf @@ -8,6 +8,8 @@ # connection => "mysql:database=fhem;host=db;port=3306", # user => "fhemuser", # password => "fhempassword", +# # optional enable(1) / disable(0) UTF-8 support of MySQL (at least V 4.042 necessary) +# utf8 => 1, #); ################################################################ #