moved Channel::toJson to JsonView (future redesign in View needed)

This commit is contained in:
Steffen Vogel 2010-06-08 02:10:48 +02:00
parent c7a2c4390e
commit b9b639b6a0
4 changed files with 22 additions and 47 deletions

View file

@ -25,7 +25,7 @@ class ChannelController extends Controller {
$this->view->channels = array();
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
$user = User::getByUuid($this->view->request->get['uuid']);
$channels = $user->getChannels();
}
else {
@ -42,13 +42,13 @@ class ChannelController extends Controller {
$jsonChannels = array();
foreach ($channels as $channel) {
$jsonChannel = $channel->toJson(); // TODO fix hardcoded json output
$jsonChannel = $this->view->getChannel($channel);
if ($this->view->request->get['data'] == 'pulses') {
$jsonChannel['pulses'] = array();
if ($this->view->request->get['data'] == 'readings') {
$jsonChannel['readings'] = array();
foreach ($channel->getPulses($from, $to, $groupBy) as $pulse) {
$jsonChannel['pulses'][] = array($pulse['timestamp'], $pulse['value']);
$jsonChannel['readingsgi'][] = array($pulse['timestamp'], $pulse['value']);
}
}
@ -64,9 +64,7 @@ class ChannelController extends Controller {
$channel = Channel::getByUcid($ucid);
if (!($channel instanceof Channel)) { // TODO rework
$channel = Channel::addChannel($ucid);
}
// TODO add channel if it doesn't exist (use $this->add)
$channel->addData($this->view->request->get);
}

View file

@ -190,14 +190,4 @@ abstract class Channel extends DatabaseObject implements ChannelInterface {
$sql .= static::buildFilterCondition($filters, $conjunction);
return $sql;
}
public function toJson() {
return array(
'id' => (int) $this->id,
'ucid' => $this->ucid,
'resolution' => (int) $this->resolution,
'description' => $this->description,
'type' => $this->type,
'costs' => $this->cost);
}
}

View file

@ -61,10 +61,17 @@ class JsonView extends View {
'line' => $exception->getLine(),
'trace' => $exception->getTrace()
);
echo 'bla';
$this->render();
}
public function getChannel(Channel $channel) { // TODO improve view interface
return array('id' => (int) $channel->id,
'ucid' => $channel->ucid,
'resolution' => (int) $channel->resolution,
'description' => $channel->description,
'type' => $channel->type,
'costs' => $channel->cost);
}
}
?>

View file

@ -21,38 +21,18 @@
include '../../backend/init.php';
$user = User::getByEMail($_GET['email']);
$groups = $user->getGroups(true);
$meter = current(Channel::getByFilter(array('id' => 1)));
foreach ($groups as $group) {
$channels = $group->getChannels();
echo $group->description . ' (' . count($channels) . '):<br />';
foreach ($channels as $channel) {
echo '&nbsp;&nbsp;[' . $channel->ucid . '] ' . $channel->description . ': ' . $channel::unit;
echo ' Min: ' . implode('|', $channel->getMin());
echo ' Max: ' . implode('|', $channel->getMax());
echo ' Avg: ' . implode('|', $channel->getAverage());
echo '<br />';
}
$readings = $meter->getData(0, time()*1000, 'hour');
echo '<table>';
foreach ($readings as $i => $reading) {
echo '<tr><td>' . ($i + 1) . '</td><td>' . date('l jS \of F Y h:i:s A', $reading['timestamp']/1000) . '</td><td>' . $reading['value'] . '</td><td>' . $reading['count'] . '</td></tr>';
}
echo '</table>';
echo '<pre>';
var_dump(Database::getConnection());
echo '</pre>';
/*$meter = current(Channel::getByFilter(array('id' => 19)));
$start = microtime(true);
$readings = $meter->getData(0, time(), 'day');
echo microtime(true) - $start;
echo '<br /><table>';
foreach ($readings as $i => $reading) {
echo '<tr><td>' . ($i + 1) . '</td><td>' . date('l jS \of F Y h:i:s A', $reading['timestamp']) . '</td><td>' . $reading['value'] . '</td><td>' . $reading['count'] . '</td></tr>';
}
echo '</table>';
$max = $meter->getAverage();
echo 'Maximal value: ' . date('l jS \of F Y h:i:s A', $max['timestamp']) . ' (' . $max['value'] . ')';*/
?>