2010-06-06 16:05:46 +02:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 by Justin Otherguy <justin@justinotherguy.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License (either version 2 or
|
|
|
|
* version 3) as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
* For more information on the GPL, please go to:
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ChannelController extends Controller {
|
|
|
|
|
2010-06-07 02:13:05 +02:00
|
|
|
public function __construct(View $view) {
|
|
|
|
parent::__construct($view);
|
2010-06-06 16:05:46 +02:00
|
|
|
}
|
|
|
|
|
2010-06-07 02:13:05 +02:00
|
|
|
public function execute() {
|
|
|
|
switch ($this->view->request->get['action']) {
|
2010-06-06 16:05:46 +02:00
|
|
|
case 'get':
|
|
|
|
$this->get();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'log':
|
|
|
|
$this->log();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new InvalidArgumentException('Invalid action specified!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function get() {
|
2010-06-07 02:13:05 +02:00
|
|
|
if ($this->view->request->get['data'] == 'channels' || $this->view->request->get['data'] == 'pulses') {
|
|
|
|
$this->view->type = 'channels';
|
|
|
|
$this->view->channels = array();
|
2010-06-06 16:05:46 +02:00
|
|
|
|
2010-06-07 02:13:05 +02:00
|
|
|
if ($this->view->request->get['data'] == 'channels') { // get all channels assigned to user
|
|
|
|
$user = current(User::getByFilter(array('id' => 1))); // TODO replace by authentication or session handling
|
2010-06-06 16:05:46 +02:00
|
|
|
$channels = $user->getChannels();
|
|
|
|
}
|
|
|
|
else {
|
2010-06-07 02:13:05 +02:00
|
|
|
$ids = explode(',', trim($this->view->request->get['ids']));
|
2010-06-06 16:05:46 +02:00
|
|
|
$channels = Channel::getByFilter(array('id' => $ids), true, false); // get all channels with id in $ids as an array
|
|
|
|
|
2010-06-07 02:13:05 +02:00
|
|
|
$from = (isset($this->view->request->get['from'])) ? (int) $this->view->request->get['from'] : NULL;
|
|
|
|
$to = (isset($this->view->request->get['to'])) ? (int) $this->view->request->get['to'] : NULL;
|
|
|
|
$groupBy = (isset($this->view->request->get['groupby'])) ? $this->view->request->get['groupby'] : 400;
|
2010-06-06 16:05:46 +02:00
|
|
|
|
2010-06-07 02:13:05 +02:00
|
|
|
$this->view->from = $from; // TODO use min max timestamps from Channel::getData()
|
|
|
|
$this->view->to = $to;
|
2010-06-06 16:05:46 +02:00
|
|
|
}
|
|
|
|
|
2010-06-07 02:13:05 +02:00
|
|
|
$jsonChannels = array();
|
2010-06-06 16:05:46 +02:00
|
|
|
foreach ($channels as $channel) {
|
2010-06-07 02:13:05 +02:00
|
|
|
$jsonChannel = $channel->toJson(); // TODO fix hardcoded json output
|
2010-06-06 16:05:46 +02:00
|
|
|
|
2010-06-07 02:13:05 +02:00
|
|
|
if ($this->view->request->get['data'] == 'pulses') {
|
|
|
|
$this->view->type = 'pulses';
|
2010-06-06 16:05:46 +02:00
|
|
|
$jsonChannel['pulses'] = array();
|
|
|
|
|
|
|
|
foreach ($channel->getPulses($from, $to, $groupBy) as $pulse) {
|
|
|
|
$jsonChannel['pulses'][] = array($pulse['timestamp'], $pulse['value']);
|
2010-06-07 02:13:05 +02:00
|
|
|
}
|
2010-06-06 16:05:46 +02:00
|
|
|
}
|
|
|
|
|
2010-06-07 02:13:05 +02:00
|
|
|
$jsonChannels[] = $jsonChannel;
|
2010-06-06 16:05:46 +02:00
|
|
|
}
|
2010-06-07 02:13:05 +02:00
|
|
|
|
|
|
|
$this->view->channels = $jsonChannels;
|
2010-06-06 16:05:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function log() {
|
2010-06-07 02:13:05 +02:00
|
|
|
$ucid = $this->view->request->get['ucid'];
|
2010-06-06 16:05:46 +02:00
|
|
|
|
|
|
|
$channel = Channel::getByUcid($ucid);
|
|
|
|
|
2010-06-07 00:42:25 +02:00
|
|
|
if (!($channel instanceof Channel)) { // TODO rework
|
2010-06-06 16:05:46 +02:00
|
|
|
$channel = Channel::addChannel($ucid);
|
|
|
|
}
|
|
|
|
|
2010-06-07 02:13:05 +02:00
|
|
|
$channel->addData($this->view->request->get);
|
2010-06-06 16:05:46 +02:00
|
|
|
}
|
2010-06-07 00:42:25 +02:00
|
|
|
|
|
|
|
public function add($ucid) { // TODO rework
|
|
|
|
$channel = new Channel();
|
|
|
|
$channel->ucid = $ucid;
|
|
|
|
|
|
|
|
if (substr($channel->ucid, 0, 19) == OneWireSensor::$ucidPrefix) {
|
|
|
|
$channel->type = 'OneWireSensor';
|
|
|
|
$channel->description = OneWireSensor::getFamilyDescription($channel);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$channel->type = 'Channel';
|
|
|
|
}
|
|
|
|
|
|
|
|
$channel->save();
|
|
|
|
}
|
2010-06-06 16:05:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|