diff --git a/backend/lib/View/JSON.php b/backend/lib/View/JSON.php index 1982818..241cd19 100644 --- a/backend/lib/View/JSON.php +++ b/backend/lib/View/JSON.php @@ -23,6 +23,8 @@ namespace Volkszaehler\View; +use Volkszaehler\Interpreter; + use Volkszaehler\View\HTTP; use Volkszaehler\Util; use Volkszaehler\Model; @@ -57,22 +59,45 @@ class JSON extends View { $this->setPadding($request->getParameter('padding')); } - public function addChannel(Model\Channel $channel, array $data = NULL) { - $jsonChannel = self::convertEntity($channel); - $jsonChannel['type'] = $channel->getType(); + /** + * Process, encode and print output + */ + protected function render() { + $json = $this->json->encode((Util\Debug::isActivated()) ? JSON_PRETTY : 0); - if (isset($data)) { - $jsonChannel['data'] = self::convertData($data); + if ($this->padding) { + $json = 'if (self.' . $this->padding . ') { ' . $this->padding . '(' . $json . '); }'; } - $this->json['channel'] = $jsonChannel; + $this->response->setHeader('Content-type', 'application/json'); + echo $json; } - public function addAggregator(Model\Aggregator $aggregator, $recursive = FALSE) { + /** + * Add channel to output queue + * + * @param Model\Channel $channel + */ + protected function addChannel(Model\Channel $channel) { + $this->json['channel'] = self::convertEntity($channel); + } + + /** + * Add aggregator to output queue + * + * @param Model\Aggregator $aggregator + * @param boolean $recursive + */ + protected function addAggregator(Model\Aggregator $aggregator, $recursive = FALSE) { $this->json['group'] = self::convertAggregator($aggregator, $recursive); } - public function addDebug(Util\Debug $debug) { + /** + * Add debugging information include queries and messages to output queue + * + * @param Util\Debug $debug + */ + protected function addDebug(Util\Debug $debug) { $this->json['debug'] = array( 'time' => $debug->getExecutionTime(), 'messages' => $debug->getMessages(), @@ -83,6 +108,12 @@ class JSON extends View { ); } + /** + * Add exception to output queue + * + * @param \Exception $exception + * @param boolean $debug + */ protected function addException(\Exception $exception, $debug = FALSE) { $exceptionInfo = array( 'type' => get_class($exception), @@ -103,9 +134,26 @@ class JSON extends View { } } + /** + * Add data to output queue + * + * @param Interpreter\InterpreterInterface $interpreter + */ + protected function addData(Interpreter\InterpreterInterface $interpreter) { + $this->json['data'][$interpreter->getUuid()] = $interpreter->getValues($this->request->getParameter('groupBy')); + } + + /** + * Converts entity to array for json_encode() + * + * @param Model\Entity $entity + * @return array + */ protected static function convertEntity(Model\Entity $entity) { $jsonEntity = array(); $jsonEntity['uuid'] = (string) $entity->getUuid(); + $jsonEntity['type'] = $entity->getType(); + foreach ($entity->getProperties() as $key => $value) { $jsonEntity[$key] = $value; @@ -114,43 +162,27 @@ class JSON extends View { return $jsonEntity; } - protected static function convertAggregator(Model\Aggregator $aggregator, $recursive = FALSE) { + /** + * Converts aggregator to array for json_encode + * + * @param Model\Aggregator $aggregator + * @param boolean $recursive + * @return array + */ + protected static function convertAggregator(Model\Aggregator $aggregator) { $jsonAggregator = self::convertEntity($aggregator); foreach ($aggregator->getChannels() as $channel) { - $jsonAggregator['channels'][] = (string) $channel->getUuid(); + $jsonAggregator['channels'][] = self::convertEntity($channel); } - if ($recursive) { - $jsonAggregator['groups'] = array(); - - foreach ($aggregator->getChildren() as $subAggregator) { - $jsonAggregator['groups'][] = $this->toJson($subAggregator, $recursive); // recursion - } + foreach ($aggregator->getChildren() as $subAggregator) { + $jsonAggregator['groups'][] = self::convertAggregator($subAggregator); // recursion } return $jsonAggregator; } - protected static function convertData($data) { - array_walk($data, function(&$reading) { - $reading[1] = round($reading[1], View::PRECISSION); - }); - - return $data; - } - - public function render() { - $json = $this->json->encode((Util\Debug::isActivated()) ? JSON_PRETTY : 0); - - if ($this->padding) { - $json = 'if (self.' . $this->padding . ') { ' . $this->padding . '(' . $json . '); }'; - } - - $this->response->setHeader('Content-type', 'application/json'); - echo $json; - } - /* * Setter & getter */ diff --git a/backend/lib/View/JpGraph.php b/backend/lib/View/JpGraph.php index 108dba9..3e51f45 100644 --- a/backend/lib/View/JpGraph.php +++ b/backend/lib/View/JpGraph.php @@ -23,6 +23,8 @@ namespace Volkszaehler\View; +use Volkszaehler\Interpreter; + use Volkszaehler\Model; use Volkszaehler\Util; @@ -39,6 +41,7 @@ require_once VZ_BACKEND_DIR . '/lib/vendor/JpGraph/jpgraph_date.php'; * @author Steffen Vogel * @link http://jpgraph.net/ * @todo add caching + * @todo rework */ class JpGraph extends View { /** @@ -96,8 +99,10 @@ class JpGraph extends View { * @param $obj * @param $data */ - public function addChannel(Model\Channel $channel, array $data = NULL){ - if (isset($data) && count($data) > 0) { + public function addData(Interpreter\Interpreter $interpreter){ + $data = $interpreter->getValues(800); + + if (count($data) > 0) { $count = count($this->channels); $xData = $yData = array(); @@ -109,7 +114,7 @@ class JpGraph extends View { // Create the scatter plot $plot = new \ScatterPlot($yData, $xData); - $plot->setLegend($channel->getProperty('name')->getValue() . ': ' . $channel->getProperty('description')->getValue() . ' [' . $channel->getDefinition()->getUnit() . ']'); + $plot->setLegend($interpreter->getChannel()->getProperty('title') . ': [' . $interpreter->getChannel()->getDefinition()->getUnit() . ']'); $plot->SetLinkPoints(TRUE, self::$colors[$count]); $plot->mark->SetColor(self::$colors[$count]); @@ -117,7 +122,7 @@ class JpGraph extends View { $plot->mark->SetType(MARK_DIAMOND); $plot->mark->SetWidth(1); - $axis = $this->getAxisIndex($channel); + $axis = $this->getAxisIndex($interpreter->getChannel()); if ($axis >= 0) { $this->graph->AddY($axis, $plot); } @@ -125,30 +130,25 @@ class JpGraph extends View { $this->graph->Add($plot); } - $this->channels[] = $channel; - } - else { - throw new \Exception('Can\'t plot channels without data!'); + $this->channels[] = $interpreter->getChannel(); } } - /** - * adds all channel of group as new plots to the graph - * - * @param Model\Aggregator $aggregator - */ + public function addChannel(Model\Channel $channel) { + throw new \Exception(get_class($this) . ' cant show ' . get_class($channel)); + } + public function addAggregator(Model\Aggregator $aggregator) { - foreach ($aggregator->getChannels() as $child) { - $this->addChannel($child); - } + throw new \Exception(get_class($this) . ' cant show ' . get_class($aggregator)); } public function addDebug(Util\Debug $debug) { - throw new \Exception(get_class($this) . ' cant show debugging information'); + throw new \Exception(get_class($this) . ' cant show ' . get_class($debug)); } /** - * shows exception + * Shows exception + * * @todo avoid graph plotting and set content-type to text/plain * @param \Exception $exception */ @@ -160,22 +160,22 @@ class JpGraph extends View { * check weather a axis for the indicator of $channel exists * * @param \Volkszaehler\Model\Channel $channel - * @todo call getType() only once */ protected function getAxisIndex(\Volkszaehler\Model\Channel $channel) { - if (!in_array($channel->getType(), array_keys($this->axes))) { - $count =count($this->axes); + $type = $channel->getType(); + + if (!array_key_exists($type, $this->axes)) { + $count = count($this->axes); if ($count == 0) { - $this->axes[$channel->getType()] = -1; + $this->axes[$type] = -1; $yaxis = $this->graph->yaxis; } else { - $this->axes[$channel->getType()] = $count - 1; + $this->axes[$type] = $count - 1; + $this->graph->SetYScale($this->axes[$type],'lin'); - $this->graph->SetYScale($this->axes[$channel->getType()],'lin'); - - $yaxis = $this->graph->ynaxis[$this->axes[$channel->getType()]]; + $yaxis = $this->graph->ynaxis[$this->axes[$type]]; } $yaxis->title->Set($channel->getDefinition()->getUnit()); @@ -186,7 +186,7 @@ class JpGraph extends View { $yaxis->SetTitleMargin('50'); } - return $this->axes[$channel->getType()]; + return $this->axes[$type]; } /** @@ -194,11 +194,11 @@ class JpGraph extends View { * * headers has been set automatically */ - protected function renderResponse() { + protected function render() { $this->graph->SetMargin(75, (count($this->axes) - 1) * 65 + 10, 20, 90); // Display the graph - $this->graph->Stroke(); + //$this->graph->Stroke(); } } diff --git a/backend/lib/View/View.php b/backend/lib/View/View.php index 26da2a5..524c088 100644 --- a/backend/lib/View/View.php +++ b/backend/lib/View/View.php @@ -23,6 +23,10 @@ namespace Volkszaehler\View; +use Volkszaehler\Interpreter; + +use Volkszaehler\Iterator; + use Volkszaehler\Model; use Volkszaehler\View\HTTP; use Volkszaehler\Util; @@ -42,8 +46,7 @@ abstract class View { /** * @var HTTP\Request - * @todo do we need this? - * @todo why public? not via getter? + * @todo do we need this? why public? not via getter? */ public $request; @@ -108,7 +111,10 @@ abstract class View { array_walk($data, array($this, 'add')); } else { - if ($data instanceof Model\Channel) { + if ($data instanceof Interpreter\InterpreterInterface) { + $this->addData($data); + } + elseif ($data instanceof Model\Channel) { $this->addChannel($data); } elseif ($data instanceof Model\Aggregator) { @@ -128,6 +134,10 @@ abstract class View { } protected abstract function render(); + + protected abstract function addData(Interpreter\InterpreterInterface $data); + protected abstract function addChannel(Model\Channel $channel); + protected abstract function addAggregator(Model\Aggregator $aggregator); protected abstract function addException(\Exception $exception); protected abstract function addDebug(Util\Debug $debug); } diff --git a/backend/lib/View/XML.php b/backend/lib/View/XML.php index e0638fe..4e21fce 100644 --- a/backend/lib/View/XML.php +++ b/backend/lib/View/XML.php @@ -23,6 +23,8 @@ namespace Volkszaehler\View; +use Volkszaehler\Interpreter; + use Volkszaehler\View\HTTP; use Volkszaehler\Util; use Volkszaehler\Model; @@ -38,6 +40,7 @@ class XML extends View { protected $xmlRoot = NULL; protected $xmlChannels = NULL; protected $xmlAggregators = NULL; + protected $xmlDatas = NULL; public function __construct(HTTP\Request $request, HTTP\Response $response) { parent::__construct($request, $response); @@ -53,7 +56,30 @@ class XML extends View { $this->response->setHeader('Content-type', 'application/xml; charset=UTF-8'); } - public function addChannel(Model\Channel $channel, array $data = NULL) { + protected function addData(Interpreter\Interpreter $interpreter) { + $data = $interpreter->getValues(); + + $xmlData = $this->xmlDoc->createElement('data'); + + foreach ($data as $reading) { + $xmlReading = $this->xmlDoc->createElement('reading'); + + $xmlReading->setAttribute('timestamp', $reading[0]); // hardcoded data fields for performance optimization + $xmlReading->setAttribute('value', $reading[1]); + $xmlReading->setAttribute('count', $reading[2]); + + $xmlData->appendChild($xmlReading); + } + + if (!isset($this->xmlDatas)) { + $this->xmlDatas = $this->xmlDoc->createElement('datas'); + $this->xmlRoot->appendChild($this->xmlDatas); + } + + $this->xmlDatas->appendChild($xmlData); + } + + public function addChannel(Model\Channel $channel) { $xmlChannel = $this->xmlDoc->createElement('channel'); $xmlChannel->setAttribute('uuid', $channel->getUuid()); @@ -64,22 +90,6 @@ class XML extends View { $xmlChannel->appendChild($this->xmlDoc->createElement('resolution', (int) $channel->getResolution())); $xmlChannel->appendChild($this->xmlDoc->createElement('cost', (float) $channel->getCost())); - if (isset($data)) { - $xmlData = $this->xmlDoc->createElement('data'); - - foreach ($data as $reading) { - $xmlReading = $this->xmlDoc->createElement('reading'); - - $xmlReading->setAttribute('timestamp', $reading['timestamp']); // hardcoded data fields for performance optimization - $xmlReading->setAttribute('value', $reading['value']); - $xmlReading->setAttribute('count', $reading['count']); - - $xmlData->appendChild($xmlReading); - } - - $xmlChannel->appendChild($xmlData); - } - if (!isset($this->xmlChannels)) { $this->xmlChannels = $this->xmlDoc->createElement('channels'); $this->xmlRoot->appendChild($this->xmlChannels); @@ -139,7 +149,7 @@ class XML extends View { $this->xmlRoot->appendChild($xmlException); } - protected function renderResponse() { + protected function render() { echo $this->xmlDoc->saveXML(); }