Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add raw context for access to uninterpreted meter data
- Loading branch information
Showing
7 changed files
with
264 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 . ')'; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
?> |