2010-07-18 17:12:00 +02:00
|
|
|
<?php
|
2010-07-21 12:44:01 +02:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2010, The volkszaehler.org project
|
2010-07-22 10:32:51 +02:00
|
|
|
* @package default
|
2010-07-21 12:44:01 +02:00
|
|
|
* @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
|
2010-07-22 16:21:26 +02:00
|
|
|
*/
|
|
|
|
/*
|
2010-07-21 12:44:01 +02:00
|
|
|
* This file is part of volkzaehler.org
|
|
|
|
*
|
|
|
|
* volkzaehler.org is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* volkzaehler.org is distributed in the hope that it will be useful,
|
2010-07-18 17:12:00 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2010-07-21 12:44:01 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2010-07-18 17:12:00 +02:00
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2010-07-21 12:44:01 +02:00
|
|
|
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
|
2010-07-18 17:12:00 +02:00
|
|
|
*/
|
|
|
|
|
2010-07-24 13:03:34 +02:00
|
|
|
namespace Volkszaehler\View;
|
2010-07-18 17:12:00 +02:00
|
|
|
|
2010-07-21 12:44:01 +02:00
|
|
|
use Volkszaehler\View\HTTP;
|
2010-07-18 17:12:00 +02:00
|
|
|
use Volkszaehler\Util;
|
2010-07-24 13:03:34 +02:00
|
|
|
use Volkszaehler\Model;
|
2010-07-18 17:12:00 +02:00
|
|
|
|
2010-07-22 10:32:51 +02:00
|
|
|
/**
|
|
|
|
* JSON view
|
|
|
|
*
|
|
|
|
* @package default
|
|
|
|
* @author Steffen Vogel <info@steffenvogel.de>
|
|
|
|
*/
|
2010-07-24 13:03:34 +02:00
|
|
|
class JSON extends View {
|
2010-08-24 15:33:55 +02:00
|
|
|
protected $json;
|
2010-07-18 17:12:00 +02:00
|
|
|
|
2010-07-24 13:03:34 +02:00
|
|
|
protected $padding = FALSE;
|
|
|
|
|
2010-07-21 12:44:01 +02:00
|
|
|
/**
|
2010-07-18 17:12:00 +02:00
|
|
|
* constructor
|
|
|
|
*/
|
2010-07-21 12:44:01 +02:00
|
|
|
public function __construct(HTTP\Request $request, HTTP\Response $response) {
|
2010-07-18 17:12:00 +02:00
|
|
|
parent::__construct($request, $response);
|
|
|
|
|
2010-08-24 15:33:55 +02:00
|
|
|
$this->json = new Util\JSON();
|
|
|
|
|
2010-07-18 17:12:00 +02:00
|
|
|
$this->json['source'] = 'volkszaehler.org';
|
2010-08-24 15:33:55 +02:00
|
|
|
$this->json['version'] = VZ_VERSION;
|
2010-07-19 17:33:26 +02:00
|
|
|
|
2010-07-18 17:12:00 +02:00
|
|
|
$this->response->setHeader('Content-type', 'application/json');
|
2010-07-28 00:42:28 +02:00
|
|
|
|
|
|
|
$this->padding = $this->request->getParameter('padding');
|
2010-07-18 17:12:00 +02:00
|
|
|
}
|
|
|
|
|
2010-07-24 13:03:34 +02:00
|
|
|
public function setPadding($padding) { $this->padding = $padding; }
|
|
|
|
|
2010-07-25 23:15:49 +02:00
|
|
|
public function addChannel(Model\Channel $channel, array $data = NULL) {
|
2010-07-24 13:03:34 +02:00
|
|
|
$jsonChannel['uuid'] = (string) $channel->getUuid();
|
2010-08-24 15:33:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
foreach ($channel->getProperties() as $property) {
|
|
|
|
$jsonChannel[$property->getName()] = $property->getValue();
|
2010-07-29 00:04:33 +02:00
|
|
|
}
|
2010-07-24 13:03:34 +02:00
|
|
|
|
|
|
|
if (isset($data)) {
|
2010-07-29 00:04:33 +02:00
|
|
|
$jsonChannel['data'] = self::convertData($data);
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->json['channels'][] = $jsonChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addGroup(Model\Group $group, $recursive = FALSE) {
|
2010-07-29 00:04:33 +02:00
|
|
|
$this->json['groups'][] = self::convertJson($group, $recursive);
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addDebug(Util\Debug $debug) {
|
|
|
|
$this->json['debug'] = array(
|
|
|
|
'time' => $debug->getExecutionTime(),
|
|
|
|
'messages' => $debug->getMessages(),
|
|
|
|
'database' => array(
|
|
|
|
'driver' => Util\Configuration::read('db.driver'),
|
|
|
|
'queries' => $debug->getQueries()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function addException(\Exception $exception) {
|
|
|
|
$this->json['exception'] = array(
|
|
|
|
'type' => get_class($exception),
|
|
|
|
'message' => $exception->getMessage(),
|
|
|
|
'code' => $exception->getCode(),
|
|
|
|
'file' => $exception->getFile(),
|
|
|
|
'line' => $exception->getLine(),
|
|
|
|
'trace' => $exception->getTrace()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-07-29 00:04:33 +02:00
|
|
|
protected static function convertGroup(Model\Group $group, $recursive = FALSE) {
|
2010-07-24 13:03:34 +02:00
|
|
|
$jsonGroup = array();
|
|
|
|
|
|
|
|
$jsonGroup['uuid'] = (string) $group->getUuid();
|
|
|
|
$jsonGroup['name'] = $group->getName();
|
|
|
|
$jsonGroup['description'] = $group->getDescription();
|
|
|
|
$jsonGroup['channels'] = array();
|
|
|
|
|
|
|
|
foreach ($group->getChannels() as $channel) {
|
|
|
|
$jsonGroup['channels'][] = (string) $channel->getUuid();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($recursive) {
|
|
|
|
$jsonGroup['children'] = array();
|
|
|
|
|
2010-07-25 23:15:49 +02:00
|
|
|
foreach ($group->getChildren() as $subGroup) {
|
|
|
|
$jsonGroup['children'][] = $this->toJson($subGroup, $recursive); // recursion
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $jsonGroup;
|
|
|
|
}
|
|
|
|
|
2010-07-29 00:04:33 +02:00
|
|
|
protected static function convertData($data) {
|
|
|
|
$jsonData = array();
|
|
|
|
|
|
|
|
foreach ($data as $reading) {
|
|
|
|
$jsonData[] = array(
|
|
|
|
(int) $reading[0],
|
2010-07-31 15:43:49 +02:00
|
|
|
(float) round($reading[1], View::PRECISSION),
|
2010-07-29 00:04:33 +02:00
|
|
|
(int) $reading[2]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $jsonData;
|
|
|
|
}
|
|
|
|
|
2010-07-24 13:03:34 +02:00
|
|
|
public function renderResponse() {
|
2010-08-24 15:33:55 +02:00
|
|
|
$json = $this->json->encode((Util\Debug::isActivated()) ? JSON_PRETTY : 0);
|
2010-07-20 21:27:53 +02:00
|
|
|
|
2010-07-24 13:03:34 +02:00
|
|
|
if ($this->padding) {
|
2010-07-28 00:42:28 +02:00
|
|
|
$json = 'if (self.' . $this->padding . ') { ' . $this->padding . '(' . $json . '); }';
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
2010-07-21 12:44:01 +02:00
|
|
|
|
2010-07-24 13:03:34 +02:00
|
|
|
echo $json;
|
2010-07-19 17:33:26 +02:00
|
|
|
}
|
2010-07-18 17:12:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|