Skip to content

Commit

Permalink
Add raw context for access to uninterpreted meter data
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Mar 8, 2015
1 parent d02f245 commit 4a7edf4
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 9 deletions.
1 change: 0 additions & 1 deletion lib/Volkszaehler/Controller/DataController.php
Expand Up @@ -60,7 +60,6 @@ public function get(Model\Channel $entity = null) {
$to = $this->request->parameters->get('to');
$tuples = $this->request->parameters->get('tuples');
$groupBy = $this->request->parameters->get('group');
$tsFmt = $this->request->parameters->get('tsfmt');

// single entity interpreter
if ($entity) {
Expand Down
102 changes: 102 additions & 0 deletions lib/Volkszaehler/Controller/RawController.php
@@ -0,0 +1,102 @@
<?php
/**
* @copyright Copyright (c) 2011, 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\Controller;

use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;

use Volkszaehler\Model;
use Volkszaehler\Util;
use Volkszaehler\Interpreter\RawInterpreter;

/**
* Raw data controller
* Allow read-only access to raw database values
*
* @author Andreas Goetz <cpuidle@gmx.de>
* @package default
*/
class RawController extends Controller {

protected $ec; // EntityController instance

public function __construct(Request $request, EntityManager $em) {
parent::__construct($request, $em);

$this->ec = new EntityController($this->request, $this->em);
}

/**
* Query for data by given channel or group or multiple channels
*
* @param Model\Entity $entity - can be null
*/
public function get($entity) {
$from = $this->request->parameters->get('from');
$to = $this->request->parameters->get('to');

if ($this->request->parameters->has('tuples')) {
throw new \Exception('Invalid argument tuples');
}
if ($this->request->parameters->has('group')) {
throw new \Exception('Invalid argument group');
}

// single entity
if ($entity) {
return new RawInterpreter($entity, $this->em, $from, $to);
}

// multiple UUIDs
if ($uuids = self::makeArray($this->request->parameters->get('uuid'))) {
$interpreters = array();

foreach ($uuids as $uuid) {
$entity = $this->ec->getSingleEntity($uuid, true); // from cache
$interpreters[] = $this->get($entity);
}

return $interpreters;
}
}

/**
* Add single or multiple tuples
*
* @param Model\Channel $channel
*/
public function add($channel) {
throw new \Exception('Invalid operation');
}

/**
* Run operation
*/
public function run($operation, $uuid = null) {
$entity = isset($uuid) ? $this->ec->getSingleEntity($uuid, true) : null; // from cache if GET
return $this->{$operation}($entity);
}
}

?>
6 changes: 3 additions & 3 deletions lib/Volkszaehler/Interpreter/DataIterator.php
Expand Up @@ -96,8 +96,8 @@ public function next() {
// special cases - auxilary information for specific interpreters
$package[3] = max($package[3], $tuple[1]); // CounterInterpreter
$package[4] += $tuple[1] * ($tuple[0] - $this->lastTimestamp); // SensorInterpreter
$this->lastTimestamp = $tuple[0];

$this->lastTimestamp = $tuple[0];
$this->rowKey++;
}

Expand All @@ -108,7 +108,7 @@ public function next() {
$package[4] /= $this->lastTimestamp - $firstTimestamp; // weighed average for SensorInterpreter
}

return $this->current = $package;
$this->current = $package;
}

/**
Expand All @@ -124,7 +124,7 @@ public function firstValue() { return $this->firstValue; }
public function rewind() {
$this->key = $this->rowKey = 0;
$this->lastTimestamp = $this->from;
return $this->next(); // fetch first tuple
$this->next(); // fetch first tuple
}

public function valid() {
Expand Down
99 changes: 99 additions & 0 deletions lib/Volkszaehler/Interpreter/RawInterpreter.php
@@ -0,0 +1,99 @@
<?php
/**
* @copyright Copyright (c) 2011, 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\Interpreter;

/**
* Raw data interpreter
*
* @package default
* @author Andreas Götz <cpuidle@gmx.de>
*/

class RawInterpreter extends Interpreter {

/**
* Initialize data iterator
*/
public function rewind() {
$this->key = 0;
$this->rows = $this->getData();
$this->rows->rewind();
}

/**
* Iterate over result set
*/
public function current() {
$row = $this->rows->current();

$tuple = array(
(float) $row[0], // raw data
(float) $row[1], // raw data
(int) $row[2] // raw data
);

if (is_null($this->max) || $tuple[1] > $this->max[1]) {
$this->max = $tuple;
}

if (is_null($this->min) || $tuple[1] < $this->min[1]) {
$this->min = $tuple;
}

return $tuple;
}

/**
* Calculates the consumption
*
* @return float total consumption in Wh
*/
public function getConsumption() {
return NULL;
}

/**
* Get Average
*
* @return float average in W
*/
public function getAverage() {
return NULL;
}

/**
* Return sql grouping expression
*
* Override Interpreter->groupExpr
*
* @author Andreas Götz <cpuidle@gmx.de>
* @param string $expression sql parameter
* @return string grouped sql expression
*/
public static function groupExprSQL($expression) {
return '(' . $expression . ')';
}
}

?>
1 change: 1 addition & 0 deletions lib/Volkszaehler/Router.php
Expand Up @@ -82,6 +82,7 @@ class Router implements HttpKernelInterface {
'aggregator' => 'Volkszaehler\Controller\AggregatorController',
'entity' => 'Volkszaehler\Controller\EntityController',
'data' => 'Volkszaehler\Controller\DataController',
'raw' => 'Volkszaehler\Controller\RawController',
'capabilities' => 'Volkszaehler\Controller\CapabilitiesController'
);

Expand Down
10 changes: 5 additions & 5 deletions test/Tests/Data.php
Expand Up @@ -67,15 +67,15 @@ protected function addTuple($ts, $value, $uuid = null) {
protected function getTuplesByUrl($url, $from = null, $to = null, $group = null, $tuples = null, $options = null) {
$params = array();

if ($from)
if (isset($from))
$params['from'] = $from;
if ($to)
if (isset($to))
$params['to'] = $to;
if ($group)
if (isset($group))
$params['group'] = $group;
if ($tuples)
if (isset($tuples))
$params['tuples'] = $tuples;
if ($options)
if (isset($options))
$params['options'] = $options;

$this->getJson($url, $params);
Expand Down
54 changes: 54 additions & 0 deletions test/Tests/RawTest.php
@@ -0,0 +1,54 @@
<?php
/**
* Meter tests
*
* @package Test
* @author Andreas Götz <cpuidle@gmx.de>
*/

namespace Tests;

class RawTest extends Data
{
public function channelDataProvider() {
return array(
array('power', round(rand(2, 1000))),
array('electric meter', round(rand(2, 1000))),
array("powersensor", round(rand(2, 1000))),
);
}

/**
* @dataProvider channelDataProvider
*/
function testAddAndGetRawTuples($type, $resolution) {
self::$uuid = self::createChannel('Test', $type, $resolution);

$data = array(
array('ts' => 1000, 'value' => 1),
array('ts' => 2000, 'value' => 2),
array('ts' => 3000, 'value' => 3),
);

foreach ($data as $tuple) {
$this->addTuple($tuple['ts'], $tuple['value'], self::$uuid);
}

$url = '/raw/' . self::$uuid . '.json';

$this->assertTrue(isset(
$this->getTuplesByUrl($url, 0, PHP_INT_MAX)->data)
);

$this->assertEquals(count($data)-1, count($this->json->data->tuples));

for ($i = 1; $i < count($data); $i++) {
$tuple = array_slice($this->json->data->tuples[$i-1], 0, 2);
$this->assertEquals(array_values($data[$i]), $tuple);
}

self::deleteChannel(self::$uuid);
}
}

?>

0 comments on commit 4a7edf4

Please sign in to comment.