added getMin(), getMax(), getAverage() and getData() to Meter class (backend based calculations)
This commit is contained in:
parent
3cf30a3dc0
commit
1dae152814
1 changed files with 36 additions and 40 deletions
|
@ -20,11 +20,6 @@
|
|||
*/
|
||||
|
||||
abstract class Meter extends Channel {
|
||||
|
||||
public function getData($from = NULL, $to = NULL, $groupBy = NULL) {
|
||||
return parent::getData($from, $to, $groupBy);
|
||||
}
|
||||
|
||||
public function getConsumption($from = NULL, $to = NULL) { // TODO untested
|
||||
$sql = 'SELECT SUM(value) AS count
|
||||
FROM data
|
||||
|
@ -38,48 +33,32 @@ abstract class Meter extends Channel {
|
|||
return $result['count'] / $this->resolution;
|
||||
}
|
||||
|
||||
private function getDerivative($diffrence, $count = 1) {
|
||||
return ($count / $this->resolution) / $diffrence;
|
||||
}
|
||||
|
||||
public function getMin($from = NULL, $to = NULL) { // TODO fix draft
|
||||
return array();
|
||||
$result = $this->getData($from, $to); // get all pulses
|
||||
$ts = 0;
|
||||
public function getMin($from = NULL, $to = NULL) {
|
||||
$data = $this->getData($from, $to);
|
||||
|
||||
foreach ($result as $pulse) {
|
||||
$last = $new;
|
||||
$new = $pulse['timestamp'];
|
||||
|
||||
if ($last - $new < $return['diffrence']) {
|
||||
$return['diffrence'] = $last - $new;
|
||||
$return['count'] = $pulse['value'];
|
||||
$return['timestamp'] = $new + $return['diffrence'] / 2; // calculate new ts between $ts and $lastTs
|
||||
$min = current($data);
|
||||
foreach ($data as $reading) {
|
||||
if ($reading['value '] < $min['value']) {
|
||||
$min = $reading;
|
||||
}
|
||||
}
|
||||
|
||||
$return['value'] = $this->getDerivative($return['diffrence'], $return['count']);
|
||||
return $return;
|
||||
return $min;
|
||||
}
|
||||
|
||||
public function getMax($from = NULL, $to = NULL) { // TODO fix draft
|
||||
return array();
|
||||
|
||||
}
|
||||
|
||||
public function getAverage($from = NULL, $to = NULL) { // TODO fix draft
|
||||
return array();
|
||||
|
||||
$result = $this->getData($from, $to); // get all pulses
|
||||
$sum = $count = 0;
|
||||
public function getMax($from = NULL, $to = NULL) {
|
||||
$data = $this->getData($from, $to);
|
||||
|
||||
foreach ($result as $pulse) {
|
||||
$sum += $pulse['value'];
|
||||
$min = current($data);
|
||||
foreach ($data as $reading) {
|
||||
if ($reading['value '] > $min['value']) {
|
||||
$min = $reading;
|
||||
}
|
||||
}
|
||||
|
||||
$return['value'] = $this->getDerivative(0, $sum);
|
||||
|
||||
return $return;
|
||||
return $min;
|
||||
}
|
||||
|
||||
public function getAverage($from, $to) {
|
||||
return $this->getConsumption($from, $to) / ($to - $from) * 1000;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -88,4 +67,21 @@ abstract class Meter extends Channel {
|
|||
public function getPulses($from = NULL, $to = NULL, $groupBy = NULL) {
|
||||
return parent::getData($from, $to, $groupBy);
|
||||
}
|
||||
|
||||
/*
|
||||
* raw pulses to power conversion
|
||||
*/
|
||||
public function getData($from = NULL, $to = NULL, $groupBy = NULL) {
|
||||
$pulses = parent::getData($from, $to, $groupBy);
|
||||
$pulseCount = count($pulses);
|
||||
|
||||
for ($i = 1; $i < $pulseCount; $i++) {
|
||||
$delta = $pulses[$i]['timestamp'] - $pulses[$i-1]['timestamp'];
|
||||
|
||||
$pulses[$i]['timestamp'] -= $delta/2;
|
||||
$pulses[$i]['value'] *= 3600000/(($this->resolution / 1000) * $delta);
|
||||
}
|
||||
|
||||
return $pulses;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue