/**
  * Add single or multiple tuples
  *
  * @todo replace by pluggable api parser
  * @param Model\Channel $channel
  */
 public function add($channel)
 {
     try {
         /* to parse new submission protocol */
         $rawPost = $this->request->getContent();
         // file_get_contents('php://input')
         $json = Util\JSON::decode($rawPost);
         if (isset($json['data'])) {
             throw new \Exception('Can only add data for a single channel at a time');
             /* backed out b111cfa2 */
         }
         // convert nested ArrayObject to plain array with flattened tuples
         $data = array_reduce($json->getArrayCopy(), function ($carry, $tuple) {
             return array_merge($carry, $tuple);
         }, array());
     } catch (Util\JSONException $e) {
         /* fallback to old method */
         $timestamp = $this->request->parameters->get('ts');
         $value = $this->request->parameters->get('value');
         if (is_null($timestamp)) {
             $timestamp = (double) round(microtime(TRUE) * 1000);
         } else {
             $timestamp = Interpreter::parseDateTimeString($timestamp);
         }
         if (is_null($value)) {
             $value = 1;
         }
         // same structure as JSON request result
         $data = array($timestamp, $value);
     }
     $sql = 'INSERT ' . (in_array(self::OPT_SKIP_DUPLICATES, $this->options) ? 'IGNORE ' : '') . 'INTO data (channel_id, timestamp, value) ' . 'VALUES ' . implode(', ', array_fill(0, count($data) >> 1, '(' . $channel->getId() . ',?,?)'));
     $rows = $this->em->getConnection()->executeUpdate($sql, $data);
     return array('rows' => $rows);
 }
 /**
  * Sporadic test/demo implemenation
  *
  * @todo replace by pluggable api parser
  */
 public function add(Model\Channel $channel)
 {
     try {
         /* to parse new submission protocol */
         $rawPost = file_get_contents('php://input');
         $json = Util\JSON::decode($rawPost);
         foreach ($json as $tuple) {
             $channel->addData(new Model\Data($channel, (double) round($tuple[0]), $tuple[1]));
         }
     } catch (Util\JSONException $e) {
         /* fallback to old method */
         $timestamp = $this->view->request->getParameter('ts');
         $value = $this->view->request->getParameter('value');
         if (is_null($timestamp)) {
             $timestamp = (double) round(microtime(TRUE) * 1000);
         }
         if (is_null($value)) {
             $value = 1;
         }
         $channel->addData(new Model\Data($channel, $timestamp, $value));
     }
     $this->em->flush();
 }
Exemple #3
0
 /**
  * check weather a axis for the indicator of $channel exists
  *
  * @param \Volkszaehler\Model\Channel $channel
  */
 protected function getAxisIndex(\Volkszaehler\Model\Channel $channel)
 {
     $type = $channel->getType();
     if (!array_key_exists($type, $this->axes)) {
         $count = count($this->axes);
         if ($count == 0) {
             $this->axes[$type] = -1;
             $yaxis = $this->graph->yaxis;
         } else {
             $this->axes[$type] = $count - 1;
             $this->graph->SetYScale($this->axes[$type], 'lin');
             $yaxis = $this->graph->ynaxis[$this->axes[$type]];
         }
         $yaxis->title->Set($channel->getDefinition()->getUnit());
         $yaxis->SetFont(FF_ARIAL);
         $yaxis->title->SetFont(FF_ARIAL);
         $yaxis->SetTitleMargin('50');
     }
     return $this->axes[$type];
 }
setlocale(LC_ALL, Util\Configuration::read('locale'));
// define include dirs for vendor libs
define('DOCTRINE_DIR', Util\Configuration::read('lib.doctrine') ? Util\Configuration::read('lib.doctrine') : 'Doctrine');
define('JPGRAPH_DIR', Util\Configuration::read('lib.jpgraph') ? Util\Configuration::read('lib.jpgraph') : 'JpGraph');
$classLoaders = array(new Util\ClassLoader('Doctrine', DOCTRINE_DIR), new Util\ClassLoader('Volkszaehler', VZ_DIR . '/lib'));
foreach ($classLoaders as $loader) {
    $loader->register();
    // register on SPL autoload stack
}
$_SERVER['REQUEST_METHOD'] = "get";
$_SERVER['PATH_INFO'] = "bla.json";
$r = new Router();
$class = "Volkszaehler\\Controller\\ChannelController";
//$r->view->request->parameters["get"]["type"]="heat";
$controller = new $class($r->view, $r->em);
$channel = new Model\Channel("heat");
//$controller->setProperties($channel, $parameters);
$channel->setProperty("title", "TestCounterHeat");
$channel->setProperty("public", "1");
$channel->setProperty("resolution", "1000");
$r->em->persist($channel);
$r->em->flush();
$from_ts = 1230764400000.0;
# 2009-01-01
$to_ts = 1320102000000.0;
# 2011-11-01
$delta_ts = 3 * 60 * 1000;
# every 3 Minutes
$ts = $from_ts;
$i = 0;
while ($ts < $to_ts) {