. */ 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 reimplement according to new env */ public function getConsumption() { $sql = 'SELECT COUNT(*) FROM `data` WHERE `channel_id` = ' . $this->channel->getId() . parent::buildDateTimeFilterSQL($this->from, $this->to); return $this->conn->fetchColumn($sql, array($this->channel->getId()), 0)*$this->channel->getProperty('resolution'); // return KWh } /** * @return array (0 => timestamp, 1 => value) * @todo reimplement according to new env */ 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) * @todo reimplement according to new env */ public function getMax() { /*$data = $this->getData(); $max = current($data); foreach ($data as $reading) { if ($reading['value '] > $max['value']) { $max = $reading; } } return $max;*/ } /** * @return float * @todo reimplement according to new env */ public function getAverage() { return round(3600*1000*1000*$this->getConsumption() / ($this->to - $this->from), 10); // return W // 3600: 3600 s/h; 1000: ms -> s; 1000: KW -> W } /** * @return float * @todo reimplement according to new env */ public function getLast() { //return $this->getConsumption() / ($this->to - $this->from) / 1000; // return W } /** * Raw pulses to power conversion * * @todo untested * @return array with timestamp, values, and pulse count */ public function getValues($tuples = NULL, $groupBy = NULL) { $pulses = parent::getData($tuples, $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 $next[2] ); } } ?>