. */ namespace Volkszaehler\Interpreter; /** * Sensor interpreter * * @package default * @author Steffen Vogel */ use Volkszaehler\Util; class SensorInterpreter extends Interpreter { protected $consumption = NULL; // in Wms (Watt milliseconds) protected $min = NULL; protected $max = NULL; protected $first = NULL; protected $last = NULL; /** * Calculates the consumption * * @return float total consumption in Wh */ public function getConsumption() { return $this->consumption / 3600000; // convert to Wh } /** * Get minimum * * @return array (0 => timestamp, 1 => value) */ public function getMin() { return ($this->min) ? array_map('floatval', array_slice($this->min, 0 , 2)) : NULL; } /** * Get maximum * * @return array (0 => timestamp, 1 => value) */ public function getMax() { return ($this->max) ? array_map('floatval', array_slice($this->max, 0 , 2)) : NULL; } /** * Get Average * * @return float 3600: 3600 s/h; 1000: ms -> s */ public function getAverage() { if ($consumption = $this->getConsumption()) { $delta = $this->last[0] - $this->first[0]; return (3600 * 1000 * $consumption) / $delta; } else { // prevents division by zero return 0; } } public function processData($callback) { $data = parent::getData(); $tuples = array(); $last = $data->rewind(); $next = $data->next(); while ($data->valid()) { $tuple = $callback(array( (float) $next[0], (float) $next[1] / $next[2], (int) $next[2] )); if (is_null($this->max) || $tuple[1] > $this->max[1]) { $this->max = $tuple; } if (is_null($this->min) || $tuple[1] < $this->min[1]) { $this->min = $tuple; } $this->consumption += $next[1] * ($next[0] - $last[0]); $tuples[] = $tuple; $last = $next; $next = $data->next(); } $this->first = reset($tuples); $this->last = end($tuples); return $tuples; } } ?>