. */ namespace Volkszaehler\Interpreter; /** * Meter interpreter * * @package default * @author Steffen Vogel (info@steffenvogel.de) * */ use Volkszaehler; use Volkszaehler\Util; class MeterInterpreter extends Interpreter { /** * Calculates the consumption for interval speciefied by $from and $to * * @todo untested */ public function getConsumption() { $sql = 'SELECT SUM(value) AS count FROM data WHERE channel_id = ' . (int) $this->id . ' && ' . self::buildTimeFilterSQL($this->from, $this->to) . ' GROUP BY channel_id'; $result = $this->dbh->query($sql)->rewind(); return $result['count'] / $this->resolution / 1000; // returns Wh } /** * @return array (0 => timestamp, 1 => value) */ public function getMin() { $data = $this->getData(); $min = current($data); foreach ($data as $reading) { if ($reading['value '] < $min['value']) { $min = $reading; } } return $min; } /** * @return array (0 => timestamp, 1 => value) */ public function getMax() { $data = $this->getData(); $max = current($data); foreach ($data as $reading) { if ($reading['value '] > $max['value']) { $max = $reading; } } return $max; } /** * @return float */ public function getAverage() { return $this->getConsumption() / ($this->to - $this->from) / 1000; // return W } /** * Just a passthrough of raw data * * @deprecated */ public function getPulses($groupBy = NULL) { return parent::getData($groupBy); } /** * Raw pulses to power conversion * * @todo untested * @return array with timestamp and values in [W] */ public function getValues($groupBy = NULL) { $pulses = parent::getData($groupBy); $values = array(); foreach ($pulses as $pulse) { if (isset($last)) { $values[] = $this->raw2differential($last, $pulse); $last = $pulse; } else { $last = $pulse; } } return $values; } /** * Calculates the differential quotient of two consecutive pulses * * @param array $last the last pulse * @param array $next the next pulse */ protected function raw2differential(array $last, array $next) { $delta = $next[0] - $last[0]; return array( ($next[0] - $delta / 2), // timestamp $next[1] * (3600000 / (($this->channel->getProperty('resolution') / 1000) * $delta)), // value (isset($next[2])) ? $next[2] : 1 ); } } ?>