diff --git a/backend/lib/View/JSON.php b/backend/lib/View/JSON.php index 4d34745..86db8e5 100644 --- a/backend/lib/View/JSON.php +++ b/backend/lib/View/JSON.php @@ -47,30 +47,29 @@ class JSON extends View { /** * constructor */ - public function __construct(HTTP\Request $request, HTTP\Response $response, $padding = FALSE) { + public function __construct(HTTP\Request $request, HTTP\Response $response) { parent::__construct($request, $response); $this->json = new Util\JSON(); $this->json['source'] = 'volkszaehler.org'; $this->json['version'] = VZ_VERSION; - $this->setPadding($padding); - - $this->response->setHeader('Content-type', 'application/json'); + $this->setPadding($request->getParameter('padding')); } public function addChannel(Model\Channel $channel, array $data = NULL) { $jsonChannel = self::convertEntity($channel); + $jsonChannel['type'] = $channel->getType(); if (isset($data)) { $jsonChannel['data'] = self::convertData($data); } - $this->json['channels'][] = $jsonChannel; + $this->json['channel'] = $jsonChannel; } public function addAggregator(Model\Aggregator $aggregator, $recursive = FALSE) { - $this->json['groups'][] = self::convertAggregator($aggregator, $recursive); + $this->json['group'] = self::convertAggregator($aggregator, $recursive); } public function addDebug(Util\Debug $debug) { @@ -108,8 +107,8 @@ class JSON extends View { $jsonEntity = array(); $jsonEntity['uuid'] = (string) $entity->getUuid(); - foreach ($entity->getProperties() as $property) { - $jsonEntity[$property->getName()] = $property->getValue(); + foreach ($entity->getProperties() as $key => $value) { + $jsonEntity[$key] = $value; } return $jsonEntity; @@ -123,10 +122,10 @@ class JSON extends View { } if ($recursive) { - $jsonAggregator['children'] = array(); + $jsonAggregator['groups'] = array(); foreach ($aggregator->getChildren() as $subAggregator) { - $jsonAggregator['children'][] = $this->toJson($subAggregator, $recursive); // recursion + $jsonAggregator['groups'][] = $this->toJson($subAggregator, $recursive); // recursion } } @@ -134,26 +133,21 @@ class JSON extends View { } protected static function convertData($data) { - $jsonData = array(); + array_walk($data, function(&$reading) { + $reading[1] = round($reading[1], View::PRECISSION); + }); - foreach ($data as $reading) { - $jsonData[] = array( - (int) $reading[0], - (float) round($reading[1], View::PRECISSION), - (int) $reading[2] - ); - } - - return $jsonData; + return $data; } - public function renderResponse() { + 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; } diff --git a/backend/lib/View/JpGraph.php b/backend/lib/View/JpGraph.php index 7b39a5e..108dba9 100644 --- a/backend/lib/View/JpGraph.php +++ b/backend/lib/View/JpGraph.php @@ -48,8 +48,8 @@ class JpGraph extends View { protected $channels = array(); - protected $width = 800; - protected $height = 400; + const WIDTH = 800; + const HEIGHT = 400; protected static $colors = array('chartreuse', 'chocolate1', 'cyan', 'blue', 'lightcyan4', 'gold'); @@ -64,7 +64,10 @@ class JpGraph extends View { public function __construct(HTTP\Request $request, HTTP\Response $response, $format = 'png') { parent::__construct($request, $response); - $this->graph = new \Graph($this->width,$this->height); + $width = $this->request->getParameter('width') ? $this->request->getParameter('width') : self::WIDTH; + $height = $this->request->getParameter('height') ? $this->request->getParameter('height') : self::HEIGHT; + + $this->graph = new \Graph($width, $height); $this->graph->img->SetImgFormat($format); @@ -125,7 +128,7 @@ class JpGraph extends View { $this->channels[] = $channel; } else { - throw new \Exception('can\'t plot channels without data!'); + throw new \Exception('Can\'t plot channels without data!'); } } diff --git a/backend/lib/View/View.php b/backend/lib/View/View.php index 4cd2d32..26da2a5 100644 --- a/backend/lib/View/View.php +++ b/backend/lib/View/View.php @@ -28,25 +28,13 @@ use Volkszaehler\View\HTTP; use Volkszaehler\Util; /** - * Interface for all View classes - * - * @author Steffen Vogel - * @package default - */ -interface ViewInterface { - public function addChannel(Model\Channel $channel, array $data = NULL); - function addAggregator(Model\Aggregator $aggregator); - function addDebug(Util\Debug $debug); -} - -/** - * Superclass for all view classes + * Base class for all view formats * * @package default * @author Steffen Vogel * */ -abstract class View implements ViewInterface { +abstract class View { /** * @var integer round all values to x decimals */ @@ -79,6 +67,11 @@ abstract class View implements ViewInterface { set_error_handler(array($this, 'errorHandler')); } + public function __desctruct() { + restore_exception_handler(); + restore_error_handler(); + } + /** * error & exception handling */ @@ -91,22 +84,52 @@ abstract class View implements ViewInterface { $code = ($exception->getCode() == 0 && HTTP\Response::getCodeDescription($exception->getCode())) ? 400 : $exception->getCode(); $this->response->setCode($code); - $this->sendResponse(); + $this->send(); die(); } - public function sendResponse() { + public function send() { if (Util\Debug::isActivated()) { $this->addDebug(Util\Debug::getInstance()); } - $this->renderResponse(); + $this->render(); $this->response->send(); } - protected abstract function renderResponse(); + /** + * + * @param mixed $data + */ + public function add($data) { + if (isset($data)) { + if (is_array($data)) { + array_walk($data, array($this, 'add')); + } + else { + if ($data instanceof Model\Channel) { + $this->addChannel($data); + } + elseif ($data instanceof Model\Aggregator) { + $this->addAggregator($data); + } + elseif ($data instanceof \Exception) { + $this->addException($data); + } + elseif ($data instanceof Util\Debug) { + $this->addDebug($data); + } + else { + throw new \Exception('Can\'t show ' . get_class($data)); + } + } + } + } + + protected abstract function render(); protected abstract function addException(\Exception $exception); + protected abstract function addDebug(Util\Debug $debug); } ?>