2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-16 16:56:04 +00:00

configDB - added new commands fileimport and fileexport

git-svn-id: https://svn.fhem.de/fhem/trunk@5563 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
betateilchen 2014-04-18 20:53:42 +00:00
parent 401371d01c
commit 714ebae901
3 changed files with 86 additions and 1 deletions

View File

@ -1,6 +1,7 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # 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. # Do not insert empty lines here, update check depends on it.
- SVN - SVN
- feature: configDB: added commands fileimport and fileexport
- feature: 36_JeeLink: added LaCrosse, ETH200comfort, CUL_IR, - feature: 36_JeeLink: added LaCrosse, ETH200comfort, CUL_IR,
HX2272 and FS20 modes from ulli HX2272 and FS20 modes from ulli
added AliRF added AliRF

View File

@ -180,6 +180,16 @@ sub CommandConfigdb($$) {
$ret = _cfgDB_Diff($param1, $param2); $ret = _cfgDB_Diff($param1, $param2);
} }
when ('fileexport') {
return "\n Syntax: configdb fileexport <pathToFile>" if @a != 2;
$ret = _cfgDB_Fileexport $param1;
}
when ('fileimport') {
return "\n Syntax: configdb fileimport <pathToFile>" if @a != 2;
$ret = _cfgDB_Fileimport $param1;
}
when ('info') { when ('info') {
Log3('configdb', 4, "info requested."); Log3('configdb', 4, "info requested.");
$ret = _cfgDB_Info; $ret = _cfgDB_Info;
@ -378,7 +388,7 @@ sub CommandConfigdb($$) {
from current version 0 with version &lt;version&gt;<br/> from current version 0 with version &lt;version&gt;<br/>
Example for valid request:<br/> Example for valid request:<br/>
<br/> <br/>
<code>get configDB telnetPort 1</code><br/> <code>configdb diff telnetPort 1</code><br/>
<br/> <br/>
will show a result like this: will show a result like this:
<pre> <pre>
@ -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.<br/> The target file can be imported again, if needed.<br/>
<br/> <br/>
<li><code>configdb fileexport &lt;targetFilename&gt;</code></li><br/>
Exports specified fhem file from database into filesystem.
Example:<br/>
<br/>
<code>configdb fileexport FHEM/99_myUtils.pm</code><br/>
<br/>
<br/>
<li><code>configdb fileimport &lt;sourceFilename&gt;</code></li><br/>
Imports specified fhem file from from filesystem into database.
Example:<br/>
<br/>
<code>configdb fileimport FHEM/99_myUtils.pm</code><br/>
<br/>
<br/>
<li><code>configdb info</code></li><br/> <li><code>configdb info</code></li><br/>
Returns some database statistics<br/> Returns some database statistics<br/>
<pre> <pre>
@ -637,6 +663,22 @@ compare device: telnetPort in current version 0 (left) to version: 1 (right)
Die Zieldatei kann sp&auml;ter f&uuml;r die Wiederherstellung verwendet werden.<br/> Die Zieldatei kann sp&auml;ter f&uuml;r die Wiederherstellung verwendet werden.<br/>
<br/> <br/>
<li><code>configdb fileexport &lt;zielDatei&gt;</code></li><br/>
Schreibt die angegebene Datei aus der Datenbank in das Dateisystem.
Beispiel:<br/>
<br/>
<code>configdb fileexport FHEM/99_myUtils.pm</code><br/>
<br/>
<br/>
<li><code>configdb fileimport &lt;quellDatei&gt;</code></li><br/>
Liest die angegbene Datei aus dem Dateisystem und schreibt den Inhalt in die Datenbank.
Beispiel:<br/>
<br/>
<code>configdb fileimport FHEM/99_myUtils.pm</code><br/>
<br/>
<br/>
<li><code>configdb info</code></li><br/> <li><code>configdb info</code></li><br/>
Liefert eine Datenbankstatistik<br/> Liefert eine Datenbankstatistik<br/>
<pre> <pre>

View File

@ -153,6 +153,9 @@ sub cfgDB_Init {
# create TABLE fhemstate if nonexistent # create TABLE fhemstate if nonexistent
$fhem_dbh->do("CREATE TABLE IF NOT EXISTS fhemstate(stateString TEXT)"); $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 # close database connection
$fhem_dbh->commit(); $fhem_dbh->commit();
$fhem_dbh->disconnect(); $fhem_dbh->disconnect();
@ -615,6 +618,45 @@ sub _cfgDB_Diff($$) {
return $ret; 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 (<in>){
$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; 1;