next commit for the controller webinterface

This commit is contained in:
Steffen Vogel 2010-09-26 19:57:37 +02:00
parent 78fa8c2129
commit 39414ab9e2
5 changed files with 366 additions and 107 deletions

View file

@ -11,32 +11,30 @@
<h1>volkszaehler.org - controller setup</h1>
<p>Hier können Sie ihren Stromzähler einrichten.</p>
<h2>Kanäle</h2>
<p>Derzeit sind folgende Zähler/Sensoren konfiguriert:</p>
<table id="channels">
<tr><th>#</th><th>UUID</th><th>Typ</th><th>Port</th><th>Value</th></tr>
</table>
<div id="list">
<h2>Kanäle</h2>
<p>Derzeit sind folgende Zähler/Sensoren konfiguriert:</p>
<table></table>
</div>
<h2>Kanal hinzufügen</h2>
<p>Hier können sie einen neuen Kanal hinzufügen:</p>
<fieldset><legend>Allgemeine Eigenschaften</legend>
<table>
<tr>
<td><label for="type">Typ:</label></td>
<td><select name="type"></select></td>
</tr>
</table>
</fieldset>
<fieldset><legend>Spezifische Eigenschaften</legend>
<table id="properties">
</table>
</fieldset>
<hr />
<h5>Debug</h5>
<div style="border: 1px dashed grey; padding: 5px" id="debug"></div>
<div id="new">
<form name="new">
<h2>Kanal hinzufügen</h2>
<p>Hier können sie einen neuen Kanal hinzufügen:</p>
<fieldset><legend>Allgemeine Eigenschaften</legend>
<table>
<tr><td><label for="type">Typ:</label></td><td><select name="type"></select></td></tr>
<tr><td><label for="port">Port:</label></td><td><input type="text" name="port" /></td></tr>
</table>
</fieldset>
<fieldset>
<legend>Spezifische Eigenschaften</legend>
<table id="properties"></table>
<p>(*) required</p>
</fieldset>
<input type="submit" value="hinzufügen" />
</form>
</div>
</body>
</html>

View file

@ -1,34 +1,62 @@
<?php
/**
* PHP implementation of the controller API
*
* @author Steffen Vogel <info@steffenvogel.de>
* @copyright Copyright (c) 2010, The volkszaehler.org project
* @package controller
* @license http://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/>.
*/
header('Content-type: application/json');
$filename = 'channels.csv';
$channels = array();
define('FILE', 'channels.csv');
$methodMapping = array(
'POST' => 'add',
'DELETE' => 'delete',
'GET' => 'get'
);
$channels = array();
$operation = (isset($_GET['operation'])) ? $_GET['operation'] : $methodMapping[$_SERVER['REQUEST_METHOD']];
// create file if it doesnt exist
touch(FILE);
// read channels
$fd = fopen($filename, 'r') or die('cant open file');
$fd = fopen(FILE, 'r') or die('cant open file');
while (($data = fgetcsv($fd, 100, ';')) !== FALSE) {
$channels[] = array(
'uuid' => $data[0],
'type' => $data[1],
'port' => $data[2],
'last_value' => $data[3],
'last_timestamp' => $data[4]
);
$channels[] = array_combine(array('uuid', 'type', 'port', 'last_value', 'last_timestamp'), $data);
}
fclose($fd);
if ($operation == 'add') {
// TODO check for duplicate uuid
// TODO validate input
$channels[] = array(
'uuid' => $_GET['uuid'],
'type' => $_GET['type'],
'port' => $_GET['port']
'uuid' => $_GET['uuid'],
'type' => $_GET['type'],
'port' => $_GET['port'],
'value' => '',
'timestamp' => ''
);
}
elseif ($operation == 'delete') {
@ -37,8 +65,9 @@ elseif ($operation == 'delete') {
});
}
// save channels
if (in_array($operation, array('delete', 'add'))) {
$fd = fopen($filename, 'w') or die('cant open file');
$fd = fopen(FILE, 'w') or die('cant open file');
foreach ($channels as $channel) {
fputcsv($fd, $channel, ';');
}

View file

@ -1,2 +0,0 @@
1234;power;PD0;;
12345;power;PD0
1 1234;power;PD0;;
2 12345;power;PD0

53
controller/php/log.php Normal file
View file

@ -0,0 +1,53 @@
<?php
/**
* PHP implementation of the controller API
*
* This file should be called by cron every 5-10 minutes
*
* @author Steffen Vogel <info@steffenvogel.de>
* @copyright Copyright (c) 2010, The volkszaehler.org project
* @package controller
* @license http://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/>.
*/
if (php_sapi_name() != 'cli') {
header('HTTP/1.1 405 Method Not Allowed');
die();
}
// read channels
$fd = fopen(FILE, 'r') or die('cant open file');
while (($data = fgetcsv($fd, 100, ';')) !== FALSE) {
$channels[] = array_combine(array('uuid', 'type', 'port', 'last_value', 'last_timestamp'), $data);
}
fclose($fd);
// log data
foreach ($channels as $channel) {
// TODO log data according to type and port
}
// save channels
$fd = fopen(FILE, 'w') or die('cant open file');
foreach ($channels as $channel) {
fputcsv($fd, $channel, ';');
}
fclose($fd);
?>

View file

@ -1,61 +1,175 @@
var backend = '../backend';
var controller = 'php/channel.php';
/**
* Javascript controller webinterface
*
* @author Steffen Vogel <info@steffenvogel.de>
* @copyright Copyright (c) 2010, The volkszaehler.org project
* @package controller
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
* @todo use prototypes
*/
/*
* 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/>.
*/
const backend = '../backend';
const controller = 'php/channel.php';
var entities;
var properties;
var channels;
$(document).ready(function() {
// get entities from backend
$.getJSON(backend + '/capabilities/definition/entity.json', function(data) {
entities = data.definition.entity;
for (var i in entities) {
$('select, [name=type]').append('<option value="' + entities[i].name + '">' + entities[i].de_name + '</option>');
}
fetchEntities();
fetchProperties();
fetchChannels();
// bind type dropdown to showEntityForm()
$('#new select').change(function(event) { showEntityForm(event.target.value); });
// bind button to addChannel()
$('#new form').submit(function(event) {
event.preventDefault();
addChannel(this);
return false;
});
// get properties from backend
$.getJSON(backend + '/capabilities/definition/property.json', function(data) {
properties = data.definition.property;
});
// get channels from controller
$.getJSON(controller, showChannels);
$('select').change(function(event) { showEntityForm(event.target.value); });
});
/**
* Get entities from backend
*/
function fetchEntities() {
$.getJSON(backend + '/capabilities/definition/entity.json', function(data) {
entities = data.definition.entity;
// Add channel types to dropdown
$('#new select').empty();
$.each(entities, function(index, entity) {
if (entity.model == 'Volkszaehler\\Model\\Channel') {
$('#new select').append('<option value="' + entity.name + '">' + entity.translation.de + '</option>');
}
});
// show related properties
$('#new select').trigger('change');
});
}
/**
* Get properties from backend
*/
function fetchProperties() {
$.getJSON(backend + '/capabilities/definition/property.json', function(data) {
properties = data.definition.property;
// show related properties
$('#new select').trigger('change');
});
}
/**
* Get channels from controller
*/
function fetchChannels() {
$.getJSON(controller, function(data) {
channels = data;
// add fetched channels to table
showChannels();
});
}
/**
* Show from for new Channel
*
* @param type
* @return
*/
function showEntityForm(type) {
$('#properties').empty();
var type = getDefinition(entities, type);
var entity = getDefinition(entities, type);
for (var i in type.optional) {
var property = getDefinition(properties, type.optional[i]);
$.each(entity.required, function(index, property) {
var property = getDefinition(properties, property);
if (property) {
var input = getPropertyForm(property);
$('#properties').append('<tr><td><label for="' + property.name + '">' + property.de_name + ':</label></td><td>' + input + '</td></tr>')
$('#properties').append('<tr class="required"><td><label for="' + property.name + '">' + property.translation.de + ':</label></td><td>' + getPropertyForm(property) + '</td><td>(*)</td></tr>');
}
});
$.each(entity.optional, function(index, property) {
var property = getDefinition(properties, property);
if (property) {
$('#properties').append('<tr class="optional"><td><label for="' + property.name + '">' + property.translation.de + ':</label></td><td>' + getPropertyForm(property) + '</td></tr>');
}
});
}
/**
* @param uuid
* @return
*/
function deleteChannel(uuid) {
$.getJSON(controller, { operation: 'delete', uuid: uuid }, function(data) {
channels = data;
showChannels();
});
}
function addChannel(form) {
var uuid = false;
if (validateChannel(form)) {
//if (uuid = addChannelBackend(form)) {
if (addChannelController(form, randomUUID)) { //uuid)) {
fetchChannels();
return true;
}
else {
//removeChannelBackend(uuid);
alert('Error: adding channel to controller');
}
/*}
else {
alert('Error: adding channel to backend');
}*/
}
else {
alert('Please correct your input');
}
}
function deleteChannel(uuid) {
$.get(controller, {operation: 'delete', uuid: uuid}, function(data) {
function addChannelController(form, uuid) {
$.getJSON(controller, { operation: 'add', uuid: uuid, port: form.port.value, type: form.type.value }, function(data) {
channels = data;
showChannels();
});
return true; // TODO
}
function addChannelBackend(form) {
$.getJSON(backend + '/channel.json', { operation: 'add' }, function(data) {
})
}
function addChannel() {
}
function addChannelBackend(channel, backendUrl) {
}
function addChannelController(uuid) {
});
return true; // TODO
}
function getDefinition(definition, type) {
@ -67,39 +181,106 @@ function getDefinition(definition, type) {
}
function getPropertyForm(property) {
var input;
switch (property.type) {
case 'string':
case 'float':
case 'integer':
input = '<input type="text" name="' + property.name + '" maxlength="' + property.max + '" />';
break;
case 'text':
input = '<textarea name="' + property.name + '"></textarea>';
break;
case 'boolean':
input = '<input type="checkbox" name="' + property.name + '" value="true" />';
case 'multiple':
for (var k in property.options) {
options.push('<option>' + property.options[k] + '<\option>');
}
input = '<select name="' + property.name + '">' + options.join() + '</select>';
break;
case 'string':
case 'float':
case 'integer':
return '<input type="text" name="' + property.name + '" ' + ((property.type == 'string') ? 'maxlength="' + property.max + '" ' : '') + '/>';
case 'text':
return '<textarea name="' + property.name + '"></textarea>';
case 'boolean':
return '<input type="checkbox" name="' + property.name + '" value="1" />';
case 'multiple':
$.each(property.options, function(index, option) {
options.push('<option value="' + option + '">' + option + '<\option>');
});
return '<select name="' + property.name + '">' + options.join() + '</select>';
default:
input = 'Error: unknown property type!';
alert('Error: unknown property!');
}
return input;
}
function showChannels(data) {
channels = data;
for (var i in channels) {
$('#channels').append('<tr><td>' + i + '</td><td>' + channels[i].uuid + '</td><td>' + channels[i].type + '</td><td>' + channels[i].port + '</td><td>' + channels[i].value + '</td><td><input type="button" value="löschen" /></td></tr>');
function showChannels() {
$('#list table').empty();
$('#list table').append('<tr><th>#</th><th>UUID</th><th>Typ</th><th>Port</th><th>Value</th></tr>');
$.each(channels, function(index, channel) {
$('#list table').append('<tr><td>' + index + '</td><td>' + channel.uuid + '</td><td>' + channel.type + '</td><td>' + channel.port + '</td><td>' + channel.value + '</td><td><input type="button" value="löschen" onclick="deleteChannel(\'' + channel.uuid + '\')" /></td></tr>');
});
}
function validateChannel(form) {
var entity = getDefinition(entities, form.type.value);
$.each(entity.required, function(index, property) {
var property = getDefinition(properties, property);
if (!validateProperty(property, form.elements[property.name].value)) {
alert('Error: invalid property: ' + property.name + ' = ' + form.elements[property.name].value);
return false;
}
});
$.each(entity.optional, function(index, property) {
var property = getDefinition(properties, property);
});
return true;
}
function validateProperty(property, value) {
switch (property.type) {
case 'string':
case 'text':
// TODO check pattern
// TODO check string length
return true;
case 'float':
// TODO check format
// TODO check min/max
return true;
case 'integer':
// TODO check format
// TODO check min/max
return true;
case 'boolean':
return value == '1' || value == '';
case 'multiple':
return $.inArray(value, property.options);
default:
alert('Error: unknown property!');
}
}
/**
* Create and return a "version 4" RFC-4122 UUID string
*
* @todo remove after got backend handling working
*/
function randomUUID() {
var s = [], itoh = '0123456789ABCDEF';
// make array of random hex digits. The UUID only has 32 digits in it, but we
// allocate an extra items to make room for the '-'s we'll be inserting.
for (var i = 0; i <36; i++) s[i] = Math.floor(Math.random()*0x10);
// conform to RFC-4122, section 4.4
s[14] = 4; // Set 4 high bits of time_high field to version
s[19] = (s[19] & 0x3) | 0x8; // Specify 2 high bits of clock sequence
// convert to hex chars
for (var i = 0; i <36; i++) s[i] = itoh[s[i]];
// insert '-'s
s[8] = s[13] = s[18] = s[23] = '-';
return s.join('');
}