From 2e48407cefe583c737c1baa914fd7e22cacd49cd Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Tue, 15 Mar 2011 11:52:42 +0100 Subject: [PATCH] fixed smaller issues, added comments --- htdocs/frontend/javascripts/entity.js | 42 +++++++++++++++++---------- lib/Interpreter/DataIterator.php | 29 +++++++++++------- lib/Interpreter/Interpreter.php | 1 - lib/Interpreter/MeterInterpreter.php | 10 +++---- lib/View/JSON.php | 31 +++++++++++++------- 5 files changed, 69 insertions(+), 44 deletions(-) diff --git a/htdocs/frontend/javascripts/entity.js b/htdocs/frontend/javascripts/entity.js index b55ab6e..2827a5a 100644 --- a/htdocs/frontend/javascripts/entity.js +++ b/htdocs/frontend/javascripts/entity.js @@ -206,23 +206,33 @@ Entity.prototype.loadData = function() { success: function(json) { this.data = json.data; - if (this.data.min !== null && this.data.min[1] < vz.options.plot.yaxis.min) { // allow negative values for temperature sensors - vz.options.plot.yaxis.min = null; - } + if (this.data.count > 0) { + if (this.data.min[1] < vz.options.plot.yaxis.min) { // allow negative values for temperature sensors + vz.options.plot.yaxis.min = null; + } - // update entity table - var unit = ' ' + this.definition.unit; - $('#entity-' + this.uuid + ' .min') - .text( - (this.data.min !== null) ? vz.wui.formatNumber(this.data.min[1]) + unit : '-') - .attr('title', (this.data.min !== null) ? $.plot.formatDate(new Date(this.data.min[0]), '%d. %b %h:%M:%S', vz.options.plot.xaxis.monthNames) : ''); - $('#entity-' + this.uuid + ' .max') - .text((this.data.max !== null) ? vz.wui.formatNumber(this.data.max[1]) + unit : '-') - .attr('title', (this.data.max !== null) ? $.plot.formatDate(new Date(this.data.max[0]), '%d. %b %h:%M:%S', vz.options.plot.xaxis.monthNames) : ''); - $('#entity-' + this.uuid + ' .average').text((this.data.average !== null) ? vz.wui.formatNumber(this.data.average) + unit : ''); - $('#entity-' + this.uuid + ' .last').text((this.data.tuples) ? vz.wui.formatNumber(this.data.tuples.last()[1]) + unit : ''); - if (this.definition.interpreter == 'Volkszaehler\\Interpreter\\MeterInterpreter') { // sensors have no consumption - $('#entity-' + this.uuid + ' .consumption').text(vz.wui.formatNumber(this.data.consumption) + unit + 'h'); + // update entity table + var unit = ' ' + this.definition.unit; + $('#entity-' + this.uuid + ' .min') + .text(vz.wui.formatNumber(this.data.min[1]) + unit) + .attr('title', $.plot.formatDate(new Date(this.data.min[0]), '%d. %b %h:%M:%S', vz.options.plot.xaxis.monthNames)); + $('#entity-' + this.uuid + ' .max') + .text(vz.wui.formatNumber(this.data.max[1]) + unit) + .attr('title', $.plot.formatDate(new Date(this.data.max[0]), '%d. %b %h:%M:%S', vz.options.plot.xaxis.monthNames)); + $('#entity-' + this.uuid + ' .average') + .text(vz.wui.formatNumber(this.data.average) + unit); + $('#entity-' + this.uuid + ' .last') + .text(vz.wui.formatNumber(this.data.tuples.last()[1]) + unit); + if (this.definition.interpreter == 'Volkszaehler\\Interpreter\\MeterInterpreter') { // sensors have no consumption + $('#entity-' + this.uuid + ' .consumption').text(vz.wui.formatNumber(this.data.consumption) + unit + 'h'); + } + } + else { // no data available, clear table + $('#entity-' + this.uuid + ' .min').text('').attr('title', ''); + $('#entity-' + this.uuid + ' .max').text('').attr('title', ''); + $('#entity-' + this.uuid + ' .average').text(''); + $('#entity-' + this.uuid + ' .last').text(''); + $('#entity-' + this.uuid + ' .consumption').text(''); } } }); diff --git a/lib/Interpreter/DataIterator.php b/lib/Interpreter/DataIterator.php index 2c74816..7bce138 100644 --- a/lib/Interpreter/DataIterator.php +++ b/lib/Interpreter/DataIterator.php @@ -55,12 +55,14 @@ class DataIterator implements \Iterator, \Countable { $this->stmt = $stmt; $this->stmt->setFetchMode(\PDO::FETCH_NUM); - if ($this->rowCount > $this->tupleCount) { + if ($this->rowCount > $this->tupleCount) { // sumarize $this->packageSize = floor($this->rowCount / $this->tupleCount); $this->tupleCount = floor($this->rowCount / $this->packageSize) + 1; } - else { + else { // passthrough $this->packageSize = 1; + $this->tupleCount = $this->rowCount; + } } @@ -68,15 +70,20 @@ class DataIterator implements \Iterator, \Countable { * Aggregate data */ public function next() { - $package = array(0, 0, 0); - for ($i = 0; $i < $this->packageSize && $this->valid(); $i++) { - $tuple = $this->stmt->fetch(); + if ( $this->packageSize == 1) { // return each row as single tuple + $package = $this->stmt->fetch(); + } + else { // summarize rows + $package = array(0, 0, 0); + for ($i = 0; $i < $this->packageSize && $this->rowKey < $this->rowCount; $i++) { + $tuple = $this->stmt->fetch(); - $package[0] = $tuple[0]; - $package[1] += $tuple[1]; - $package[2] += $tuple[2]; + $package[0] = $tuple[0]; + $package[1] += $tuple[1]; + $package[2] += $tuple[2]; - $this->rowKey++; + $this->rowKey++; + } } $this->key++; @@ -91,11 +98,11 @@ class DataIterator implements \Iterator, \Countable { */ public function rewind() { $this->key = $this->rowKey = 0; - return $this->next(); + return $this->next(); // fetch first tuple } public function valid() { - return $this->rowKey < $this->rowCount; + return $this->key < $this->tupleCount; } /** diff --git a/lib/Interpreter/Interpreter.php b/lib/Interpreter/Interpreter.php index f7b2bc9..869baed 100644 --- a/lib/Interpreter/Interpreter.php +++ b/lib/Interpreter/Interpreter.php @@ -205,7 +205,6 @@ abstract class Interpreter { */ public function getEntity() { return $this->channel; } - public function getTupleCount() { return $this->tupleCount; } } ?> diff --git a/lib/Interpreter/MeterInterpreter.php b/lib/Interpreter/MeterInterpreter.php index b544914..c5379b1 100644 --- a/lib/Interpreter/MeterInterpreter.php +++ b/lib/Interpreter/MeterInterpreter.php @@ -92,8 +92,7 @@ class MeterInterpreter extends Interpreter { $this->pulseCount = 0; $tuples = array(); - $pulses->rewind(); - $last = $pulses->next(); + $last = $pulses->rewind(); $next = $pulses->next(); while ($pulses->valid()) { @@ -127,9 +126,10 @@ class MeterInterpreter extends Interpreter { $delta = $next[0] - $last[0]; return array( - ($next[0] - $delta / 2), // timestamp - $next[1] * (3600000 / (($this->resolution / 1000) * $delta)), // value - $next[2] // num of pulses + //($next[0] - $delta / 2), // timestamp in the middle + $next[0], // timestamp at the end + $next[1] * (3600000 / (($this->resolution / 1000) * $delta)), // value + $next[2] // num of pulses ); } } diff --git a/lib/View/JSON.php b/lib/View/JSON.php index 5ac087c..e25cd66 100644 --- a/lib/View/JSON.php +++ b/lib/View/JSON.php @@ -199,9 +199,7 @@ class JSON extends View { * @param $interpreter */ protected function addData($interpreter) { - $this->json['data']['uuid'] = $interpreter->getEntity()->getUuid(); - - $data = $interpreter->processData( + $data = $interpreter->processData( // iterate through PDO resultset function($tuple) { return array( $tuple[0], @@ -210,17 +208,25 @@ class JSON extends View { ); } ); + + $this->json['data']['uuid'] = $interpreter->getEntity()->getUuid(); + $this->json['data']['count'] = count($data); + + $min = $interpreter->getMin(); + $max = $interpreter->getMax(); + $average = View::formatNumber($interpreter->getAverage()); - $this->json['data']['min'] = $interpreter->getMin(); - $this->json['data']['max'] = $interpreter->getMax(); - $this->json['data']['average'] = View::formatNumber($interpreter->getAverage()); - - if ($interpreter instanceof Interpreter\MeterInterpreter) { + if (isset($min)) + $this->json['data']['min'] = $min; + if (isset($max)) + $this->json['data']['max'] = $max; + if (isset($average)) + $this->json['data']['average'] = $average; + if ($interpreter instanceof Interpreter\MeterInterpreter) $this->json['data']['consumption'] = View::formatNumber($interpreter->getConsumption()); + if (count($data) > 0) { + $this->json['data']['tuples'] = $data; } - - $this->json['data']['count'] = $interpreter->getTupleCount(); - if (count($data) > 0) $this->json['data']['tuples'] = $data; } /** @@ -234,6 +240,9 @@ class JSON extends View { elseif ($value instanceof Model\Entity) { $refNode[$index] = self::convertEntity($value); } + elseif (is_numeric($value)) { + $refNode[$index] = View::formatNumber($value); + } else { $refNode[$index] = $value; }