From 714ebae901566fc8e180583a8ee1b8910d1af0a4 Mon Sep 17 00:00:00 2001 From: betateilchen <> Date: Fri, 18 Apr 2014 20:53:42 +0000 Subject: [PATCH] configDB - added new commands fileimport and fileexport git-svn-id: https://svn.fhem.de/fhem/trunk@5563 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/98_configdb.pm | 44 +++++++++++++++++++++++++++++++++++++++- fhem/configDB.pm | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index dc5443a94..161d83d79 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,6 +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. - SVN + - feature: configDB: added commands fileimport and fileexport - feature: 36_JeeLink: added LaCrosse, ETH200comfort, CUL_IR, HX2272 and FS20 modes from ulli added AliRF diff --git a/fhem/FHEM/98_configdb.pm b/fhem/FHEM/98_configdb.pm index a30f7f99c..1e102d1c9 100644 --- a/fhem/FHEM/98_configdb.pm +++ b/fhem/FHEM/98_configdb.pm @@ -180,6 +180,16 @@ sub CommandConfigdb($$) { $ret = _cfgDB_Diff($param1, $param2); } + when ('fileexport') { + return "\n Syntax: configdb fileexport " if @a != 2; + $ret = _cfgDB_Fileexport $param1; + } + + when ('fileimport') { + return "\n Syntax: configdb fileimport " if @a != 2; + $ret = _cfgDB_Fileimport $param1; + } + when ('info') { Log3('configdb', 4, "info requested."); $ret = _cfgDB_Info; @@ -378,7 +388,7 @@ sub CommandConfigdb($$) { from current version 0 with version <version>
Example for valid request:

- get configDB telnetPort 1
+ configdb diff telnetPort 1

will show a result like this:
@@ -394,6 +404,22 @@ compare device: telnetPort in current version 0 (left) to version: 1 (right)
 			The target file can be imported again, if needed.

+
  • configdb fileexport <targetFilename>

  • + Exports specified fhem file from database into filesystem. + Example:
    +
    + configdb fileexport FHEM/99_myUtils.pm
    +
    +
    + +
  • configdb fileimport <sourceFilename>

  • + Imports specified fhem file from from filesystem into database. + Example:
    +
    + configdb fileimport FHEM/99_myUtils.pm
    +
    +
    +
  • configdb info

  • Returns some database statistics
    @@ -637,6 +663,22 @@ compare device: telnetPort in current version 0 (left) to version: 1 (right)
     			Die Zieldatei kann später für die Wiederherstellung verwendet werden.

    +
  • configdb fileexport <zielDatei>

  • + Schreibt die angegebene Datei aus der Datenbank in das Dateisystem. + Beispiel:
    +
    + configdb fileexport FHEM/99_myUtils.pm
    +
    +
    + +
  • configdb fileimport <quellDatei>

  • + Liest die angegbene Datei aus dem Dateisystem und schreibt den Inhalt in die Datenbank. + Beispiel:
    +
    + configdb fileimport FHEM/99_myUtils.pm
    +
    +
    +
  • configdb info

  • Liefert eine Datenbankstatistik
    diff --git a/fhem/configDB.pm b/fhem/configDB.pm
    index 2d90debc5..48a8073b2 100644
    --- a/fhem/configDB.pm
    +++ b/fhem/configDB.pm
    @@ -153,6 +153,9 @@ sub cfgDB_Init {
     #	create TABLE fhemstate if nonexistent
     	$fhem_dbh->do("CREATE TABLE IF NOT EXISTS fhemstate(stateString TEXT)");
     
    +#	create TABLE fhemfilesave if nonexistent
    +	$fhem_dbh->do("CREATE TABLE IF NOT EXISTS fhemfilesave(filename TEXT, line TEXT)");
    +
     #	close database connection
     	$fhem_dbh->commit();
     	$fhem_dbh->disconnect();
    @@ -615,6 +618,45 @@ sub _cfgDB_Diff($$) {
     	return $ret;
     }
     
    +sub _cfgDB_Fileimport($) {
    +	my ($filename) = @_;
    +	my $path = $attr{global}{modpath};
    +	$path .= "/$filename";
    +	my $counter = 0;
    +	my $fhem_dbh = _cfgDB_Connect;
    +	$fhem_dbh->do("delete from fhemfilesave where filename = '$path'");
    +	my $sth = $fhem_dbh->prepare('INSERT INTO fhemfilesave values (?, ?)');
    +	open (in,"<$path") || die $!;
    +	while (){
    +		$counter++;
    +		my $line = substr($_,0,length($_)-1);
    +		$sth->execute($path, $line);
    +	}
    +	close in;
    +	$sth->finish();
    +	$fhem_dbh->commit();
    +	$fhem_dbh->disconnect();
    +	return "$counter lines written from file $filename to database";
    +}
    +
    +sub _cfgDB_Fileexport($) {
    +	my ($filename) = @_;
    +	my $path = $attr{global}{modpath};
    +	$path .= "/$filename";
    +	my $counter = 0;
    +	my $fhem_dbh = _cfgDB_Connect;
    +	my $sth = $fhem_dbh->prepare( "SELECT * FROM fhemfilesave WHERE filename = '$path'" );  
    +	$sth->execute();
    +	open( FILE, ">$path" );
    +	while (my @line = $sth->fetchrow_array()) {
    +		$counter++;
    +		print FILE $line[1], "\n";
    +	}
    +	close ( FILE );
    +	$sth->finish();
    +	$fhem_dbh->disconnect();
    +	return "$counter lines read from database into file $filename";
    +}
     
     1;