2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 12:49:34 +00:00

: minor code improvements

git-svn-id: https://svn.fhem.de/fhem/trunk@21571 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
betateilchen 2020-04-01 11:09:00 +00:00
parent f15ba5748d
commit 661fc4d4c5

View File

@ -162,49 +162,51 @@ use MIME::Base64;
##################################################
# Forward declarations for functions in fhem.pl
#
## no critic
sub AnalyzeCommandChain($$;$);
sub GetDefAndAttr($;$);
sub Log($$);
sub Log3($$$);
sub createUniqueId();
## use critic
##################################################
# Forward declarations inside this library
#
sub cfgDB_AttrRead($);
sub cfgDB_Init();
sub cfgDB_FileRead($);
sub cfgDB_FileUpdate($);
sub cfgDB_Fileversion($$);
sub cfgDB_FileWrite($@);
sub cfgDB_FW_fileList($$@);
sub cfgDB_Read99();
sub cfgDB_ReadAll($);
sub cfgDB_SaveCfg(;$);
sub cfgDB_SaveState();
sub cfgDB_svnId();
sub cfgDB_AttrRead;
sub cfgDB_ReadAll;
sub cfgDB_Init;
sub cfgDB_FileRead;
sub cfgDB_FileUpdate;
sub cfgDB_Fileversion;
sub cfgDB_FileWrite;
sub cfgDB_FW_fileList;
sub cfgDB_Read99;
sub cfgDB_SaveCfg;
sub cfgDB_SaveState;
sub cfgDB_svnId;
sub _cfgDB_binFileimport($$;$);
sub _cfgDB_Connect();
sub _cfgDB_DeleteTemp();
sub _cfgDB_Diff($$);
sub __cfgDB_Diff($$$$);
sub _cfgDB_InsertLine($$$$);
sub _cfgDB_Execute($@);
sub _cfgDB_Filedelete($);
sub _cfgDB_Fileexport($;$);
sub _cfgDB_Filelist(;$);
sub _cfgDB_Info($);
sub _cfgDB_Migrate();
sub _cfgDB_ReadCfg(@);
sub _cfgDB_ReadState(@);
sub _cfgDB_Recover($);
sub _cfgDB_Reorg(;$$);
sub _cfgDB_Rotate($$);
sub _cfgDB_Search($$;$);
sub _cfgDB_Uuid();
sub _cfgDB_table_exists($$);
sub _cfgDB_dump($);
sub _cfgDB_binFileimport;
sub _cfgDB_Connect;
sub _cfgDB_DeleteTemp;
sub _cfgDB_Diff;
sub __cfgDB_Diff;
sub _cfgDB_InsertLine;
sub _cfgDB_Execute;
sub _cfgDB_Filedelete;
sub _cfgDB_Fileexport;
sub _cfgDB_Filelist;
sub _cfgDB_Info;
sub _cfgDB_Migrate;
sub _cfgDB_ReadCfg;
sub _cfgDB_ReadState;
sub _cfgDB_Recover;
sub _cfgDB_Reorg;
sub _cfgDB_Rotate;
sub _cfgDB_Search;
sub _cfgDB_Uuid;
sub _cfgDB_table_exists;
sub _cfgDB_dump;
##################################################
# Read configuration file for DB connection
@ -223,15 +225,6 @@ foreach my $line (@c) {
push (@config,$line) if($line !~ m/^#/ && length($line) > 0);
}
#while (<CONFIG>){
# my $line = $_;
# $line =~ s/^\s+|\s+$//g; # remove whitespaces etc.
# $line =~ s/;$/;;/; # duplicate ; at end-of-line
# push (@config,$line) if($line !~ m/^#/ && length($line) > 0);
#}
#close CONFIG;
use vars qw(%configDB);
my %dbconfig;
@ -290,7 +283,7 @@ $count = undef;
#
# initialize database, create tables if necessary
sub cfgDB_Init() {
sub cfgDB_Init {
##################################################
# Create non-existing database tables
# Create default config entries if necessary
@ -346,7 +339,7 @@ sub cfgDB_Init() {
}
# read attributes
sub cfgDB_AttrRead($) {
sub cfgDB_AttrRead {
my ($readSpec) = @_;
my ($row, $sql, @line, @rets);
my $fhem_dbh = _cfgDB_Connect;
@ -368,7 +361,7 @@ sub cfgDB_AttrRead($) {
}
# generic file functions called from fhem.pl
sub cfgDB_FileRead($) {
sub cfgDB_FileRead {
my ($filename) = @_;
Log3(undef, 4, "configDB reading file: $filename");
@ -391,7 +384,7 @@ sub cfgDB_FileRead($) {
return ($err, @ret);
}
sub cfgDB_FileWrite($@) {
sub cfgDB_FileWrite {
my ($filename,@content) = @_;
Log3(undef, 4, "configDB writing file: $filename");
my $fhem_dbh = _cfgDB_Connect;
@ -404,7 +397,7 @@ sub cfgDB_FileWrite($@) {
return;
}
sub cfgDB_FileUpdate($) {
sub cfgDB_FileUpdate {
my ($filename) = @_;
my $fhem_dbh = _cfgDB_Connect;
my $id = $fhem_dbh->selectrow_array("SELECT filename from fhemb64filesave where filename = '$filename'");
@ -418,7 +411,7 @@ sub cfgDB_FileUpdate($) {
}
# read and execute fhemconfig and fhemstate
sub cfgDB_ReadAll($) {
sub cfgDB_ReadAll { ## prototype used in fhem.pl
my ($cl) = @_;
my ($ret, @dbconfig);
@ -444,7 +437,7 @@ sub cfgDB_ReadAll($) {
}
# save running configuration to version 0
sub cfgDB_SaveCfg(;$) {
sub cfgDB_SaveCfg { ## prototype used in fhem.pl
my ($internal) = shift;
$internal = defined($internal) ? $internal : 0;
@ -503,7 +496,7 @@ sub cfgDB_SaveCfg(;$) {
}
# save statefile
sub cfgDB_SaveState() {
sub cfgDB_SaveState {
my ($out,$val,$r,$rd,$t,@rowList);
$t = localtime;
@ -558,7 +551,7 @@ sub cfgDB_SaveState() {
}
# import existing files during migration
sub cfgDB_MigrationImport() {
sub cfgDB_MigrationImport {
my ($ret, $filename, @files, @def);
my $modpath = AttrVal("global","modpath",".");
@ -659,12 +652,12 @@ sub cfgDB_MigrationImport() {
}
# return SVN Id, called by fhem's CommandVersion
sub cfgDB_svnId() {
sub cfgDB_svnId {
return "# ".'$Id$'
}
# return filelist depending on directory and regexp
sub cfgDB_FW_fileList($$@) {
sub cfgDB_FW_fileList {
my ($dir,$re,@ret) = @_;
my @files = split(/\n/, _cfgDB_Filelist('notitle'));
foreach my $f (@files) {
@ -677,7 +670,7 @@ sub cfgDB_FW_fileList($$@) {
}
# read filelist containing 99_ files in database
sub cfgDB_Read99() {
sub cfgDB_Read99 {
my $ret = "";
my $fhem_dbh = _cfgDB_Connect;
my $sth = $fhem_dbh->prepare( "SELECT filename FROM fhemb64filesave WHERE filename like '%/99_%.pm' group by filename" );
@ -693,7 +686,7 @@ sub cfgDB_Read99() {
}
# return SVN Id from file stored in database
sub cfgDB_Fileversion($$) {
sub cfgDB_Fileversion {
my ($file,$ret) = @_;
$ret = "No Id found for $file";
my ($err,@in) = cfgDB_FileRead($file);
@ -707,7 +700,7 @@ sub cfgDB_Fileversion($$) {
#
# connect do database
sub _cfgDB_Connect() {
sub _cfgDB_Connect {
my $fhem_dbh = DBI->connect(
"dbi:$cfgDB_dbconn",
$cfgDB_dbuser,
@ -718,7 +711,7 @@ sub _cfgDB_Connect() {
}
# add configuration entry into fhemconfig
sub _cfgDB_InsertLine($$$$) {
sub _cfgDB_InsertLine {
my ($fhem_dbh, $uuid, $line, $counter) = @_;
my ($c,$d,$p1,$p2) = split(/ /, $line, 4);
my $sth = $fhem_dbh->prepare('INSERT INTO fhemconfig values (?, ?, ?, ?, ?, ?)');
@ -727,7 +720,7 @@ sub _cfgDB_InsertLine($$$$) {
}
# pass command table to AnalyzeCommandChain
sub _cfgDB_Execute($@) {
sub _cfgDB_Execute {
my ($cl, @dbconfig) = @_;
my (@ret);
@ -743,7 +736,7 @@ sub _cfgDB_Execute($@) {
# read all entries from fhemconfig
# and add them to command table for execution
sub _cfgDB_ReadCfg(@) {
sub _cfgDB_ReadCfg {
my (@dbconfig) = @_;
my $fhem_dbh = _cfgDB_Connect;
my ($sth, @line, $row);
@ -773,7 +766,7 @@ sub _cfgDB_ReadCfg(@) {
# read all entries from fhemstate
# and add them to command table for execution
sub _cfgDB_ReadState(@) {
sub _cfgDB_ReadState {
my (@dbconfig) = @_;
my $fhem_dbh = _cfgDB_Connect;
my ($sth, $row);
@ -789,7 +782,7 @@ sub _cfgDB_ReadState(@) {
# rotate all versions to versionnum + 1
# return uuid for new version 0
sub _cfgDB_Rotate($$) {
sub _cfgDB_Rotate {
my ($fhem_dbh,$newversion) = @_;
my $uuid = _cfgDB_Uuid;
$fhem_dbh->do("UPDATE fhemversions SET VERSION = VERSION+1 where VERSION >= 0") if $newversion == 0;
@ -798,11 +791,11 @@ sub _cfgDB_Rotate($$) {
}
# 2015-01-12 use the fhem default function
sub _cfgDB_Uuid() {
sub _cfgDB_Uuid {
return createUniqueId();
}
sub _cfgDB_filesize_str($) {
sub _cfgDB_filesize_str {
my ($size) = @_;
if ($size > 1099511627776) # TiB: 1024 GiB
@ -833,7 +826,7 @@ sub _cfgDB_filesize_str($) {
#
# migrate existing fhem config into database
sub _cfgDB_Migrate() {
sub _cfgDB_Migrate {
my $ret;
$ret = "Starting migration...\n";
Log3('configDB',4,'Starting migration');
@ -856,7 +849,7 @@ sub _cfgDB_Migrate() {
}
# show database statistics
sub _cfgDB_Info($) {
sub _cfgDB_Info {
my ($info2) = @_;
$info2 //= 'unknown';
my ($l, @r, $f);
@ -939,7 +932,7 @@ sub _cfgDB_Info($) {
}
# recover former config from database archive
sub _cfgDB_Recover($) {
sub _cfgDB_Recover {
my ($version) = @_;
my ($cmd, $count, $ret);
@ -982,7 +975,7 @@ sub _cfgDB_Recover($) {
}
# delete old configurations
sub _cfgDB_Reorg(;$$) {
sub _cfgDB_Reorg {
my ($lastversion,$quiet) = @_;
$lastversion = (defined($lastversion)) ? $lastversion : 3;
Log3('configDB', 4, "DB Reorg started, keeping last $lastversion versions.");
@ -1003,7 +996,7 @@ sub _cfgDB_Reorg(;$$) {
}
# delete temporary version
sub _cfgDB_DeleteTemp() {
sub _cfgDB_DeleteTemp {
Log3('configDB', 4, "configDB: delete temporary Version -1");
my $fhem_dbh = _cfgDB_Connect;
$fhem_dbh->do("delete FROM fhemconfig where versionuuid in (select versionuuid from fhemversions where version = -1)");
@ -1014,7 +1007,7 @@ sub _cfgDB_DeleteTemp() {
}
# search for device or fulltext in db
sub _cfgDB_Search($$;$) {
sub _cfgDB_Search {
my ($search,$searchversion,$dsearch) = @_;
return 'Syntax error.' if(!(defined($search)));
my $fhem_dbh = _cfgDB_Connect;
@ -1047,7 +1040,7 @@ sub _cfgDB_Search($$;$) {
}
# called from cfgDB_Diff
sub __cfgDB_Diff($$$$) {
sub __cfgDB_Diff {
my ($fhem_dbh,$search,$searchversion,$svinternal) = @_;
my ($sql, $sth, @line, $ret);
if($svinternal != -1) {
@ -1066,7 +1059,7 @@ if($svinternal != -1) {
}
# compare device configurations from 2 versions
sub _cfgDB_Diff($$) {
sub _cfgDB_Diff {
my ($search,$searchversion) = @_;
my ($ret, $v0, $v1);
@ -1091,7 +1084,7 @@ sub _cfgDB_Diff($$) {
}
# find DEF, input supports devspec definitions
sub _cfgDB_findDef($;$) {
sub _cfgDB_findDef {
my ($search,$internal) = @_;
$internal = 'DEF' unless defined($internal);
@ -1105,11 +1098,11 @@ sub _cfgDB_findDef($;$) {
return @ret;
}
sub _cfgDB_type() {
sub _cfgDB_type {
return "$cfgDB_dbtype (b64)";
}
sub _cfgDB_dump($) {
sub _cfgDB_dump {
my ($param1) = @_;
$param1 //= '';
@ -1177,7 +1170,7 @@ sub _cfgDB_dump($) {
#
# delete file from database
sub _cfgDB_Filedelete($) {
sub _cfgDB_Filedelete {
my ($filename) = @_;
my $fhem_dbh = _cfgDB_Connect;
my $ret = $fhem_dbh->do("delete from fhemb64filesave where filename = '$filename'");
@ -1188,7 +1181,7 @@ sub _cfgDB_Filedelete($) {
}
# export file from database to filesystem
sub _cfgDB_Fileexport($;$) {
sub _cfgDB_Fileexport {
my ($filename,$raw) = @_;
my $fhem_dbh = _cfgDB_Connect;
my $sth = $fhem_dbh->prepare( "SELECT content FROM fhemb64filesave WHERE filename = '$filename'" );
@ -1209,7 +1202,7 @@ sub _cfgDB_Fileexport($;$) {
}
# import file into database
sub _cfgDB_binFileimport($$;$) {
sub _cfgDB_binFileimport {
my ($filename,$filesize,$doDelete) = @_;
$doDelete = (defined($doDelete)) ? 1 : 0;
@ -1241,7 +1234,7 @@ sub _cfgDB_binFileimport($$;$) {
}
# list all files stored in database
sub _cfgDB_Filelist(;$) {
sub _cfgDB_Filelist {
my ($notitle) = @_;
my $ret = "Files found in database:\n".
"------------------------------------------------------------\n";