added & integrated new Logger API

some small bugfixes
This commit is contained in:
Steffen Vogel 2010-07-28 00:39:23 +02:00
parent 9df45bc4a1
commit b23a36bad5
9 changed files with 246 additions and 64 deletions

View file

@ -23,6 +23,8 @@
namespace Volkszaehler\Controller;
use Volkszaehler\Model;
use Volkszaehler\Util;
/**
@ -41,18 +43,31 @@ class DataController extends Controller {
* @todo use uuids for groups or channels
*/
public function get() {
$ids = explode(',', trim($this->view->request->getParameter('ids')));
if ($ucid = $this->view->request->getParameter('ucid')) {
$entity = $this->em->getRepository('Volkszaehler\Model\Channel')->findOneBy(array('uuid' => $ucid));
}
elseif ($ugid = $this->view->request->getParameter('ugid')) {
$entity = $this->em->getRepository('Volkszaehler\Model\Group')->findOneBy(array('uuid' => $ugid));
}
else {
throw new \Exception('you have to specifiy an ugid or ucid paramter');
}
$q = $this->em->createQuery('SELECT c FROM Volkszaehler\Model\Channel c WHERE c.id IN (' . implode(', ', $ids) . ')');
$channels = $q->execute();
if ($entity === FALSE) {
throw new \Exception('no group/channel found');
}
$from = ($this->view->request->getParameter('from')) ? (int) $this->view->request->getParameter('from') : NULL;
$to = ($this->view->request->getParameter('to')) ? (int) $this->view->request->getParameter('to') : NULL;
$groupBy = ($this->view->request->getParameter('groupBy')) ? $this->view->request->getParameter('groupBy') : NULL; // get all readings by default
$from = $this->view->request->getParameter('from');
$to = $this->view->request->getParameter('to');
$groupBy = ($this->view->request->getParameter('groupBy')); // get all readings by default
foreach ($channels as $channel) {
$interpreter = $channel->getInterpreter($this->em);
$this->view->addChannel($channel, $interpreter->getValues($from, $to, $groupBy));
$data = $entity->getInterpreter($this->em, $from, $to)->getValues($groupBy);
if ($entity instanceof Model\Group) {
$this->view->addGroup($entity, $data);
}
elseif ($entity instanceof Model\Channel) {
$this->view->addChannel($entity, $data);
}
}
@ -62,41 +77,13 @@ class DataController extends Controller {
* @todo authentification/indentification
*/
public function add() {
$ucid = $this->view->request->getParameter('ucid');
$channel = $this->em->getRepository('Volkszaehler\Model\Channel\Channel')->findOneBy(array('uuid' => $ucid));
$value = (float) $this->view->request->getParameter('value');
$ts = (int) $this->view->request->getParameter('timestamp');
if ($ts == 0) {
$ts = microtime(TRUE) * 1000;
$loggerClassName = 'Volkszaehler\Logger\\' . ucfirst($this->view->request->getParameter('logger')) . 'Logger';
if (!(Util\ClassLoader::classExists($loggerClassName)) || !is_subclass_of($loggerClassName, '\Volkszaehler\Logger\Logger')) {
throw new \Exception('\'' . $loggerClassName . '\' is not a valid controller');
}
$logger = new $loggerClassName($this->view->request, $this->em);
$data = new \Volkszaehler\Model\Data($channel, $value, $ts);
$channel->addData($data);
$this->em->persist($data);
$this->em->flush();
}
/**
* prune data from database
*
* @todo authentification/indentification
*/
public function delete() {
$dql = 'DELETE FROM \Volkszaehler\Model\Data WHERE channel_id = ' . $this->id;
if ($this->view->request->getParameter('from')) {
$dql .= ' && timestamp > ' . (int) $this->view->request->getParameter('from');
}
if ($this->view->request->getParameter('to')) {
$dql .= ' && timestamp < ' . $this->view->request->getParameter('to');
}
$q = $em->createQuery($dql);
$q->execute();
$logger->log();
}
}

View file

@ -42,7 +42,7 @@ class GroupController extends Controller {
$dql = 'SELECT g, c, d FROM Volkszaehler\Model\Group g LEFT JOIN g.children c LEFT JOIN g.channels d';
// TODO fix this (depending on DDC-719)
if ($recursion = $this->view->request->getParameter('recursion')) {
if ($recursion = $this->view->request->getParameter('recursive')) {
//$dql .= ' WHERE g.parents IS EMPTY';
}

View file

@ -32,7 +32,13 @@ namespace Volkszaehler\Logger;
* @author Steffen Vogel <info@steffenvogel.de>
* @todo to be implemented
*/
class FluksoLogger implements Logger {
class FluksoLogger extends Logger {
/**
* @return array of Model\Data
*/
public function getData();
public function getVersion();
}

View file

@ -24,6 +24,8 @@
namespace Volkszaehler\Logger;
use Volkszaehler\View\HTTP;
use Doctrine\ORM;
use Volkszaehler\Model;
/**
* interface for parsing diffrent logging APIs (google, flukso etc..)
@ -32,13 +34,38 @@ use Volkszaehler\View\HTTP;
* @package default
* @todo to be implemented
*/
interface Logger {
public function __construct(HTTP\Request $request);
interface LoggerInterface {
public function __construct(HTTP\Request $request, ORM\EntityManager $em);
/**
* @return \Volkszaehler\Model\Data $data the parsed data
* @return array of Model\Data
*/
public function getData();
public function getVersion();
}
abstract class Logger implements LoggerInterface {
protected $request;
protected $em;
public function __construct(HTTP\Request $request, ORM\EntityManager $em) {
$this->request = $request;
$this->em = $em;
}
public function log() {
$data = $this->getData();
if (!is_array($data)) {
$data = array($data);
}
foreach ($data as $reading) {
$this->em->persist($reading);
}
$this->em->flush();
}
}
?>

View file

@ -0,0 +1,79 @@
<?php
/**
* @copyright Copyright (c) 2010, The volkszaehler.org project
* @package default
* @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
*/
/*
* 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Volkszaehler\Logger;
use Volkszaehler\Model;
use Doctrine\ORM;
/**
* logger for the the original volkszaehler.org prototype based on ethersex's watchasync
*
* @package default
* @link http://github.com/ethersex/ethersex/blob/master/services/watchasync
* @author Steffen Vogel <info@steffenvogel.de>
* @todo to be implemented
*/
class PrototypeLogger extends Logger {
/**
* @return array of Model\Data
*/
public function getData() {
$uuid = $this->request->getParameter('uuid');
$port = $this->request->getParameter('port');
$channel = $this->em->getRepository('Volkszaehler\Model\Channel')->findOneBy(array(
'description' => $uuid,
'name' => $port
));
if ($channel) {
if (!($time = $this->request->getParameter('time'))) {
$time = (int) (microtime(TRUE) * 1000);
}
return new Model\Data($channel, 1, $time);
}
else {
return FALSE;
}
}
/**
* the prototyp protocol doesn't have a version
*/
public function getVersion() {
return FALSE;
}
}
/*
* just some documentation
*
* /httplog/httplog.php?port=<port>&uuid=<uuid>&time=<unixtimestamp>
*
* <port> = <prefix:PC><no#>
* <unixstimestamp> = timestamp in ms since 1970
*
*/
?>

View file

@ -0,0 +1,59 @@
<?php
/**
* @package default
* @copyright Copyright (c) 2010, The volkszaehler.org project
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
*/
/*
* 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Volkszaehler\Logger;
use Volkszaehler\Model;
use Doctrine\ORM;
/**
*
* @author Steffen Vogel <info@steffenvogel.de>
* @package default
*
*/
class VzLogger extends Logger {
/**
* @return array of Model\Data
*/
public function getData() {
$ucid = $this->view->request->getParameter('ucid');
$channel = $this->em->getRepository('Volkszaehler\Model\Channel\Channel')->findOneBy(array('uuid' => $ucid));
$value = (float) $this->view->request->getParameter('value');
$ts = (int) $this->view->request->getParameter('timestamp');
if ($ts == 0) {
$ts = microtime(TRUE) * 1000;
}
$data = new Model\Data($channel, $value, $ts);
}
public function getVersion() {
return $this->request->getParameter('version');
}
}
?>

View file

@ -62,10 +62,10 @@ class Channel extends Entity {
* indicator => interpreter, unit mapping
*/
protected static $indicators = array(
'power' => array('meter', 'kW/h'),
'gas' => array('meter', 'qm/h'),
'water' => array('meter', 'qm/h'),
'temperature' => array('sensor', C'),
'power' => array('meter', 'W'),
'gas' => array('meter', ''),
'water' => array('meter', ''),
'temperature' => array('sensor', C'),
'pressure' => array('sensor', 'hPa'),
'humidity' => array('sensor', '%')
);
@ -76,7 +76,7 @@ class Channel extends Entity {
public function __construct($indicator) {
parent::__construct();
if (!in_array($indicator, self::$indicators)) {
if (!in_array($indicator, array_keys(self::$indicators))) {
throw new \Exception($indicator . ' is no known indicator');
}
@ -96,12 +96,9 @@ class Channel extends Entity {
/**
* obtain channels data interpreter to calculate statistical information
*/
public function getInterpreter(\Doctrine\ORM\EntityManager $em) {
$interpreterClassName = 'Volkszaehler\Interpreter\\' . ucfirst(self::$indicators[$this->indicator][0]) . 'Interpreter';
if (!(\Volkszaehler\Util\ClassLoader::classExists($interpreterClassName)) || !is_subclass_of($interpreterClassName, '\Volkszaehler\Interpreter\Interpreter')) {
throw new \Exception('\'' . $interpreterClassName . '\' is not a valid Interpreter');
}
return new $interpreterClassName($this, $em);
public function getInterpreter(\Doctrine\ORM\EntityManager $em, $from, $to) {
$interpreterClassName = 'Volkszaehler\Interpreter\\' . ucfirst($this->getType()) . 'Interpreter';
return new $interpreterClassName($this, $em, $from, $to);
}
/**
@ -111,12 +108,15 @@ class Channel extends Entity {
public function setName($name) { $this->name = $name; }
public function getDescription() { return $this->description; }
public function setDescription($description) { $this->description = $description; }
public function getUnit() { return self::$indicators[$this->indicator][1]; }
public function getIndicator() { return $this->indicator; }
public function getResolution() { return $this->resolution; }
public function setResolution($resolution) { $this->resolution = $resolution; }
public function getCost() { return $this->cost; }
public function setCost($cost) { $this->cost = $cost; }
public function getType() { return self::$indicators[$this->indicator][0]; }
public function getUnit() { return self::$indicators[$this->indicator][1]; }
public function getIndicator() { return $this->indicator; }
}
?>

View file

@ -24,6 +24,7 @@
namespace Volkszaehler\Model;
use Doctrine\Common\Collections\ArrayCollection;
use Volkszaehler\Model;
/**
* Data entity
@ -56,12 +57,18 @@ class Data {
*/
protected $channel;
public function __construct(Channel\Channel $channel, $value, $timestamp) {
public function __construct(Model\Channel $channel, $value, $timestamp) {
$channel->addData($this); // bidirectional association
$this->channel = $channel;
$this->value = $value;
$this->timestamp = $timestamp;
}
public function toArray() {
return array('channel' => $this->channel, 'timestamp' => $this->timestamp, 'value' => $this->value);
}
/**
* setter & getter
*/

View file

@ -23,6 +23,10 @@
namespace Volkszaehler\Model;
use Volkszaehler\Interpreter;
use Doctrine\ORM;
use Doctrine\Common\Collections;
use Doctrine\Common\Collections\ArrayCollection;
@ -83,21 +87,31 @@ class Group extends Entity {
* @param Group $child
* @todo check against endless recursion
* @todo check if the group is already member of the group
* @todo add bidirectional association
*/
public function addGroup(Group $child) {
$this->children->add($child);
}
public function removeGroup(Group $child) {
$this->children->removeElement($child);
}
/**
* adds channel as new child
*
* @param Channel $child
* @todo check if the channel is already member of the group
* @todo add bidrection association
*/
public function addChannel(Channel $child) {
$this->channels->add($child);
}
public function removeChannel(Channel $child) {
$this->channels->removeElement($child);
}
/**
* getter & setter
*/
@ -105,9 +119,12 @@ class Group extends Entity {
public function setName($name) { $this->name = $name; }
public function getDescription() { return $this->description; }
public function setDescription($description) { $this->description = $description; }
public function getChildren() { return $this->children; }
public function getParents() { return $this->parents; }
public function getChannels() { return $this->channels; }
public function getChildren() { return $this->children->toArray(); }
public function getParents() { return $this->parents->toArray(); }
public function getChannels() { return $this->channels->toArray(); }
public function getInterpreter(ORM\EntityManager $em) { return new Interpreter\GroupInterpreter($this, $em); }
}
?>