statistics: improved robustness against blocking of FHEM

git-svn-id: https://svn.fhem.de/fhem/trunk@11078 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
grompo 2016-03-17 17:25:17 +00:00
parent f8e20ec41a
commit d5f8e5c098

View File

@ -157,7 +157,7 @@ sub statistics_Define($$)
else {$hash->{PREFIX} = "stat";} else {$hash->{PREFIX} = "stat";}
eval { "Hallo" =~ m/^$devName$/ }; eval { "Hallo" =~ m/^$devName$/ };
return "Bad regexp: $@" if($@); return "Bad regexp: $@" if($@);
$hash->{DEV_REGEXP} = $devName; $hash->{DEV_REGEXP} = $devName;
$hash->{STATE} = "Waiting for notifications"; $hash->{STATE} = "Waiting for notifications";
@ -166,7 +166,7 @@ sub statistics_Define($$)
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
InternalTimer( gettimeofday() + 11, "statistics_PeriodChange", $hash, 0); InternalTimer( gettimeofday() + 11, "statistics_PeriodChange", $hash, 0);
$hash->{fhem}{nextPeriodChangeTime} = gettimeofday() + 11;
return undef; return undef;
} }
@ -289,9 +289,12 @@ sub statistics_PeriodChange($)
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $dummy; my $dummy;
my $val; my $val;
my $curPeriodChangeTime = $hash->{fhem}{nextPeriodChangeTime};
$curPeriodChangeTime = gettimeofday() unless defined $curPeriodChangeTime;
my $periodChangePreset = AttrVal($name, "periodChangePreset", 5); my $periodChangePreset = AttrVal($name, "periodChangePreset", 5);
my $isDayChange = ( ReadingsVal($name, "nextPeriodChangeCalc", "") =~ /Day Change/ ); my $isDayChange = ( ReadingsVal($name, "nextPeriodChangeCalc", "") =~ /Day Change/ );
# Determine the next day change time # Determine the next day change time
my @th=localtime(); my @th=localtime();
my $dayChangeDelay = 0; my $dayChangeDelay = 0;
@ -313,7 +316,10 @@ sub statistics_PeriodChange($)
} }
$val = strftime ("%Y-%m-%d %H:%M:%S", localtime($periodEndTime)) . $val; $val = strftime ("%Y-%m-%d %H:%M:%S", localtime($periodEndTime)) . $val;
InternalTimer( $periodEndTime, "statistics_PeriodChange", $hash, 1); InternalTimer( $periodEndTime, "statistics_PeriodChange", $hash, 1);
readingsSingleUpdate($hash, "nextPeriodChangeCalc", $val, 0); readingsSingleUpdate($hash, "nextPeriodChangeCalc", $val, 0);
$hash->{fhem}{nextPeriodChangeTime} = $periodEndTime;
statistics_Log $hash, 4, "Next period change will be calculated at $val"; statistics_Log $hash, 4, "Next period change will be calculated at $val";
return if( AttrVal($name, "disable", 0 ) == 1 ); return if( AttrVal($name, "disable", 0 ) == 1 );
@ -332,12 +338,15 @@ sub statistics_PeriodChange($)
if ($isDayChange) { if ($isDayChange) {
statistics_Log $hash, 4, "Calculating day change"; statistics_Log $hash, 4, "Calculating day change";
($dummy, $dummy, $hourLast, $dayLast, $monthLast, $yearLast) = localtime (gettimeofday() - $dayChangeDelay + $periodChangePreset - 59); # get time values for 50 seconds before a new day
($dummy, $dummy, $hourLast, $dayLast, $monthLast, $yearLast) = localtime ($curPeriodChangeTime - $dayChangeDelay + $periodChangePreset - 59);
# get time values for next day
($dummy, $dummy, $hourNow, $dayNow, $monthNow, $yearNow) = localtime (gettimeofday() + $periodChangePreset); ($dummy, $dummy, $hourNow, $dayNow, $monthNow, $yearNow) = localtime (gettimeofday() + $periodChangePreset);
$periodSwitch = -2;
if ($yearNow != $yearLast) { $periodSwitch = -4; } if ($yearNow != $yearLast) { $periodSwitch = -4; }
elsif ($monthNow != $monthLast) { $periodSwitch = -3; } elsif ($monthNow != $monthLast) { $periodSwitch = -3; }
elsif ($dayNow != $dayLast) { $periodSwitch = -2; } # Positiv Value if periode changes at full hour
if ($dayChangeDelay % 3600 == 0) { $periodSwitch = abs($periodSwitch); } $periodSwitch = abs($periodSwitch) if $dayChangeDelay % 3600 == 0;
} else { } else {
($dummy, $dummy, $hourLast, $dummy, $dummy, $dummy) = localtime (gettimeofday()); ($dummy, $dummy, $hourLast, $dummy, $dummy, $dummy) = localtime (gettimeofday());
($dummy, $dummy, $hourNow, $dummy, $dummy, $dummy) = localtime (gettimeofday() + $periodChangePreset); ($dummy, $dummy, $hourNow, $dummy, $dummy, $dummy) = localtime (gettimeofday() + $periodChangePreset);
@ -1216,7 +1225,7 @@ sub statistics_UpdateDevReading($$$$)
</li><br> </li><br>
<li><code>periodChangePreset &lt;seconds&gt;</code> <li><code>periodChangePreset &lt;seconds&gt;</code>
<br> <br>
Preponed start of the calculation of periodical data. Default is 5 seconds before each full hour. Preponed start of the calculation of periodical data. Default is 5 second before each full hour.
<br> <br>
Allows thus the correct timely assignment within plots. Should be adapted to the CPU speed or load of the server. Allows thus the correct timely assignment within plots. Should be adapted to the CPU speed or load of the server.
<br> <br>