mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-05-04 22:19:38 +00:00
93_DbRep: diffValue - recognize if diff is 0 or no value available
git-svn-id: https://svn.fhem.de/fhem/trunk@25280 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
e5850b4b3e
commit
e4ceb4777a
@ -1,5 +1,6 @@
|
||||
# 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_DbRep: diffValue - recognize if diff is 0 or no value available
|
||||
- change: 72_FB_CALLLIST: reading "count-missed-calls" only counts incoming
|
||||
missed calls
|
||||
- bugfix: 88_HMCCU: Fixed device detection bug
|
||||
|
@ -57,6 +57,7 @@ no if $] >= 5.017011, warnings => 'experimental::smartmatch';
|
||||
|
||||
# Version History intern
|
||||
my %DbRep_vNotesIntern = (
|
||||
"8.44.1" => "27.11.2021 change diffValue: recognize if diff is 0 or no value available ",
|
||||
"8.44.0" => "21.11.2021 new attr numDecimalPlaces ",
|
||||
"8.43.1" => "02.11.2021 fix SQL statement if devspec can't be resolved, Forum:https://forum.fhem.de/index.php/topic,53584.msg1184155.html#msg1184155 ",
|
||||
"8.43.0" => "22.09.2021 consider attr device, reading in sqlCmd, remove length limit of attr device, reading ",
|
||||
@ -3997,7 +3998,7 @@ return;
|
||||
# nichtblockierende DB-Abfrage diffValue
|
||||
####################################################################################################
|
||||
sub diffval_DoParse {
|
||||
my ($string) = @_;
|
||||
my $string = shift;
|
||||
my ($name,$device,$reading,$prop,$ts) = split("\\§", $string);
|
||||
my $hash = $defs{$name};
|
||||
my $dbloghash = $defs{$hash->{HELPER}{DBLOGDEVICE}};
|
||||
@ -4006,18 +4007,22 @@ sub diffval_DoParse {
|
||||
my $dblogname = $dbloghash->{NAME};
|
||||
my $dbmodel = $dbloghash->{MODEL};
|
||||
my $dbpassword = $attr{"sec$dblogname"}{secret};
|
||||
|
||||
my ($dbh,$sql,$sth,$err,$selspec);
|
||||
|
||||
# Background-Startzeit
|
||||
my $bst = [gettimeofday];
|
||||
|
||||
eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
|
||||
|
||||
if ($@) {
|
||||
$err = encode_base64($@,"");
|
||||
eval { $dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0,
|
||||
RaiseError => 1,
|
||||
AutoInactiveDestroy => 1
|
||||
}
|
||||
); 1;
|
||||
}
|
||||
or do { $err = encode_base64($@,"");
|
||||
Log3 ($name, 2, "DbRep $name - $@");
|
||||
return "$name|''|$device|$reading|''|''|''|$err|''";
|
||||
}
|
||||
};
|
||||
|
||||
# only for this block because of warnings if details of readings are not set
|
||||
no warnings 'uninitialized';
|
||||
@ -4033,7 +4038,8 @@ sub diffval_DoParse {
|
||||
#vorbereiten der DB-Abfrage, DB-Modell-abhaengig
|
||||
if($dbmodel eq "MYSQL") {
|
||||
$selspec = "TIMESTAMP,VALUE, if(VALUE-\@V < 0 OR \@RB = 1 , \@diff:= 0, \@diff:= VALUE-\@V ) as DIFF, \@V:= VALUE as VALUEBEFORE, \@RB:= '0' as RBIT ";
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$selspec = "TIMESTAMP,VALUE";
|
||||
}
|
||||
|
||||
@ -4063,9 +4069,11 @@ sub diffval_DoParse {
|
||||
|
||||
if ($IsTimeSet || $IsAggrSet) {
|
||||
$sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",'ORDER BY TIMESTAMP');
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,undef,undef,'ORDER BY TIMESTAMP');
|
||||
}
|
||||
|
||||
Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
|
||||
|
||||
eval{ $sth = $dbh->prepare($sql);
|
||||
@ -4076,11 +4084,12 @@ sub diffval_DoParse {
|
||||
Log3 ($name, 2, "DbRep $name - $@");
|
||||
$dbh->disconnect;
|
||||
return "$name|''|$device|$reading|''|''|''|$err|''";
|
||||
}
|
||||
|
||||
} else {
|
||||
if($dbmodel eq "MYSQL") {
|
||||
@array = map { $runtime_string." ".$_ -> [0]." ".$_ -> [1]." ".$_ -> [2]."\n" } @{ $sth->fetchall_arrayref() };
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
@array = map { $runtime_string." ".$_ -> [0]." ".$_ -> [1]."\n" } @{ $sth->fetchall_arrayref() };
|
||||
|
||||
if (@array) {
|
||||
@ -4088,18 +4097,21 @@ sub diffval_DoParse {
|
||||
my $dse = 0;
|
||||
my $vold;
|
||||
my @sqlite_array;
|
||||
foreach my $row (@array) {
|
||||
|
||||
for my $row (@array) {
|
||||
@sp = split("[ \t][ \t]*", $row, 4);
|
||||
my $runtime_string = $sp[0];
|
||||
my $timestamp = $sp[2] ? $sp[1]." ".$sp[2] : $sp[1];
|
||||
my $vnew = $sp[3];
|
||||
$vnew =~ tr/\n//d;
|
||||
|
||||
$dse = ($vold && (($vnew-$vold) > 0))?($vnew-$vold):0;
|
||||
$dse = $vold && (($vnew-$vold) > 0) ? ($vnew-$vold) : 0;
|
||||
@sp = $runtime_string." ".$timestamp." ".$vnew." ".$dse."\n";
|
||||
$vold = $vnew;
|
||||
|
||||
push(@sqlite_array, @sp);
|
||||
}
|
||||
|
||||
@array = @sqlite_array;
|
||||
}
|
||||
}
|
||||
@ -4109,17 +4121,19 @@ sub diffval_DoParse {
|
||||
if($aval eq "hour") {
|
||||
my @rsf = split(/[ :]/,$runtime_string_first);
|
||||
@array = ($runtime_string." ".$rsf[0]."_".$rsf[1]."\n");
|
||||
} elsif($aval eq "minute") {
|
||||
}
|
||||
elsif($aval eq "minute") {
|
||||
my @rsf = split(/[ :]/,$runtime_string_first);
|
||||
@array = ($runtime_string." ".$rsf[0]."_".$rsf[1]."-".$rsf[2]."\n");
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
my @rsf = split(" ",$runtime_string_first);
|
||||
@array = ($runtime_string." ".$rsf[0]."\n");
|
||||
}
|
||||
}
|
||||
|
||||
push(@row_array, @array);
|
||||
}
|
||||
}
|
||||
|
||||
# SQL-Laufzeit ermitteln
|
||||
my $rt = tv_interval($st);
|
||||
@ -4133,8 +4147,9 @@ sub diffval_DoParse {
|
||||
# Berechnung diffValue aus Selektionshash
|
||||
my %rh = (); # Ergebnishash, wird alle Ergebniszeilen enthalten
|
||||
my %ch = (); # counthash, enthält die Anzahl der verarbeiteten Datasets pro runtime_string
|
||||
my $lastruntimestring;
|
||||
my $i = 1;
|
||||
my $max = ($#row_array)+1; # Anzahl aller Listenelemente
|
||||
my $lastruntimestring;
|
||||
my $lval; # immer der letzte Wert von $value
|
||||
my $rslval; # runtimestring von lval
|
||||
my $uediff; # Übertragsdifferenz (Differenz zwischen letzten Wert einer Aggregationsperiode und dem ersten Wert der Folgeperiode)
|
||||
@ -4142,7 +4157,6 @@ sub diffval_DoParse {
|
||||
my $diff_before; # Differenzwert vorheriger Datensatz
|
||||
my $rejectstr; # String der ignorierten Differenzsätze
|
||||
my $diff_total; # Summenwert aller berücksichtigten Teildifferenzen
|
||||
my $max = ($#row_array)+1; # Anzahl aller Listenelemente
|
||||
|
||||
Log3 ($name, 5, "DbRep $name - data of row_array result assigned to fields:\n");
|
||||
|
||||
@ -4198,13 +4212,12 @@ sub diffval_DoParse {
|
||||
# neuer Zeitabschnitt beginnt, ersten Value-Wert erfassen und Übertragsdifferenz bilden
|
||||
$lastruntimestring = $runtime_string;
|
||||
$i = 1;
|
||||
|
||||
$uediff = $value - $lval if($value > $lval);
|
||||
$diff = $uediff;
|
||||
$lval = $value if($value); # Übetrag über Perioden mit value = 0 hinweg !
|
||||
$rslval = $runtime_string;
|
||||
Log3 ($name, 4, "DbRep $name - balance difference of $uediff between $rslval and $runtime_string");
|
||||
|
||||
Log3 ($name, 4, "DbRep $name - balance difference of $uediff between $rslval and $runtime_string");
|
||||
|
||||
$diff_total = $diff ? $diff : 0 if($diff <= $difflimit);
|
||||
$rh{$runtime_string} = $runtime_string."|".$diff_total."|".$timestamp;
|
||||
@ -4212,6 +4225,7 @@ sub diffval_DoParse {
|
||||
|
||||
$uediff = 0;
|
||||
}
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
@ -4242,13 +4256,16 @@ sub diffval_DoParse {
|
||||
|
||||
# Ergebnisse in Datenbank schreiben
|
||||
my ($wrt,$irowdone);
|
||||
|
||||
if($prop =~ /writeToDB/) {
|
||||
($wrt,$irowdone,$err) = DbRep_OutputWriteToDB($name,$device,$reading,$rows,"diff");
|
||||
|
||||
if ($err) {
|
||||
Log3 $hash->{NAME}, 2, "DbRep $name - $err";
|
||||
$err = encode_base64($err,"");
|
||||
return "$name|''|$device|$reading|''|''|''|$err|''";
|
||||
}
|
||||
|
||||
$rt = $rt+$wrt;
|
||||
}
|
||||
|
||||
@ -4267,7 +4284,7 @@ sub diffval_DoParse {
|
||||
# Auswertungsroutine der nichtblockierenden DB-Abfrage diffValue
|
||||
####################################################################################################
|
||||
sub diffval_ParseDone {
|
||||
my ($string) = @_;
|
||||
my $string = shift;
|
||||
my @a = split("\\|",$string);
|
||||
my $hash = $defs{$a[0]};
|
||||
my $name = $hash->{NAME};
|
||||
@ -4312,8 +4329,9 @@ sub diffval_ParseDone {
|
||||
|
||||
my %ncp = split("§", $ncpslist);
|
||||
my $ncpstr;
|
||||
|
||||
if(%ncp) {
|
||||
foreach my $ncpkey (sort(keys(%ncp))) {
|
||||
for my $ncpkey (sort(keys(%ncp))) {
|
||||
$ncpstr .= $ncpkey." || ";
|
||||
}
|
||||
}
|
||||
@ -4322,15 +4340,17 @@ sub diffval_ParseDone {
|
||||
my %rh = split("§", $rowlist);
|
||||
|
||||
Log3 ($name, 4, "DbRep $name - result of diffValue calculation after decoding:");
|
||||
foreach my $key (sort(keys(%rh))) {
|
||||
for my $key (sort(keys(%rh))) {
|
||||
Log3 ($name, 4, "DbRep $name - runtimestring Key: $key, value: ".$rh{$key});
|
||||
}
|
||||
|
||||
my $state = $erread?$erread:"done";
|
||||
readingsBeginUpdate($hash);
|
||||
|
||||
foreach my $key (sort(keys(%rh))) {
|
||||
for my $key (sort(keys(%rh))) {
|
||||
my $valid = 0; # Datensatz hat kein Ergebnis als default
|
||||
my @k = split("\\|",$rh{$key});
|
||||
$valid = 1 if($k[2] =~ /(\d{4})-(\d{2})-(\d{2})_(\d{2}):(\d{2}):(\d{2})/x); # Datensatz hat einen Wert wenn kompletter Timestamp ist enthalten
|
||||
my $rts = $k[2]."__";
|
||||
$rts =~ s/:/-/g; # substituieren unsupported characters -> siehe fhem.pl
|
||||
|
||||
@ -4345,8 +4365,7 @@ sub diffval_ParseDone {
|
||||
}
|
||||
my $rv = $k[1];
|
||||
|
||||
ReadingsBulkUpdateValue ($hash, $reading_runtime_string, $rv ? sprintf("%.${ndp}f", $rv) : "-");
|
||||
|
||||
ReadingsBulkUpdateValue ($hash, $reading_runtime_string, (!$valid ? "-" : defined $rv ? sprintf "%.${ndp}f", $rv : "-"));
|
||||
}
|
||||
|
||||
ReadingsBulkUpdateValue ($hash, "db_lines_processed", $irowdone) if($hash->{LASTCMD} =~ /writeToDB/);
|
||||
|
Loading…
x
Reference in New Issue
Block a user