From 6c8942c904789e8c67bae28c60dee41370b2304e Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 22 Jul 2011 12:29:16 +0200 Subject: [PATCH] fixes #73 (wrong calculation of consumption & average) --- lib/Interpreter/MeterInterpreter.php | 4 ++-- lib/Interpreter/SensorInterpreter.php | 12 +++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/Interpreter/MeterInterpreter.php b/lib/Interpreter/MeterInterpreter.php index b242c1e..60dccfc 100644 --- a/lib/Interpreter/MeterInterpreter.php +++ b/lib/Interpreter/MeterInterpreter.php @@ -76,8 +76,8 @@ class MeterInterpreter extends Interpreter { * @return float 3600: 3600 s/h; 1000: ms -> s */ public function getAverage() { - if ($consumption = $this->getConsumption()) { - return (3600 * 1000 * $consumption) / ($this->last[0] - $this->first[0]); + if ($this->pulseCount) { + return (1000 * $this->pulseCount / $this->resolution) / ($this->last[0] - $this->first[0]); } else { // prevents division by zero return 0; diff --git a/lib/Interpreter/SensorInterpreter.php b/lib/Interpreter/SensorInterpreter.php index 675ea55..d5408d5 100644 --- a/lib/Interpreter/SensorInterpreter.php +++ b/lib/Interpreter/SensorInterpreter.php @@ -72,9 +72,9 @@ class SensorInterpreter extends Interpreter { * @return float 3600: 3600 s/h; 1000: ms -> s */ public function getAverage() { - if ($consumption = $this->getConsumption()) { + if ($this->consumption) { $delta = $this->last[0] - $this->first[0]; - return (3600 * 1000 * $consumption) / $delta; + return $this->consumption / $delta; } else { // prevents division by zero return 0; @@ -103,7 +103,13 @@ class SensorInterpreter extends Interpreter { $this->min = $tuple; } - $this->consumption += $next[1] * ($next[0] - $last[0]); + /* + * Workaround for #73 + * Due to the "overfetching"" at the boundary regions + */ + if ($last[0] > $this->from && $next[0] < $this->to) { + $this->consumption += $next[1] * ($next[0] - $last[0]); + } $tuples[] = $tuple; $last = $next;