diff --git a/controller/index.html b/controller/index.html index 2cc8346..81d5f6f 100644 --- a/controller/index.html +++ b/controller/index.html @@ -11,32 +11,30 @@

volkszaehler.org - controller setup

Hier können Sie ihren Stromzähler einrichten.

-

Kanäle

-

Derzeit sind folgende Zähler/Sensoren konfiguriert:

- - -
#UUIDTypPortValue
+
+

Kanäle

+

Derzeit sind folgende Zähler/Sensoren konfiguriert:

+
+
-

Kanal hinzufügen

-

Hier können sie einen neuen Kanal hinzufügen:

- -
Allgemeine Eigenschaften - - - - - -
-
- -
Spezifische Eigenschaften - -
-
- - -
-
Debug
-
+
+
+

Kanal hinzufügen

+

Hier können sie einen neuen Kanal hinzufügen:

+
Allgemeine Eigenschaften + + + +
+
+ +
+ Spezifische Eigenschaften +
+

(*) required

+
+ +
+
\ No newline at end of file diff --git a/controller/php/channel.php b/controller/php/channel.php index 733740b..f820fdb 100644 --- a/controller/php/channel.php +++ b/controller/php/channel.php @@ -1,34 +1,62 @@ + * @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 . + */ + 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, ';'); } diff --git a/controller/php/channels.csv b/controller/php/channels.csv deleted file mode 100644 index bb2252e..0000000 --- a/controller/php/channels.csv +++ /dev/null @@ -1,2 +0,0 @@ -1234;power;PD0;; -12345;power;PD0 diff --git a/controller/php/log.php b/controller/php/log.php new file mode 100644 index 0000000..084e709 --- /dev/null +++ b/controller/php/log.php @@ -0,0 +1,53 @@ + + * @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 . + */ + +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); + +?> \ No newline at end of file diff --git a/controller/script.js b/controller/script.js index c543252..2640189 100644 --- a/controller/script.js +++ b/controller/script.js @@ -1,61 +1,175 @@ -var backend = '../backend'; -var controller = 'php/channel.php'; +/** + * Javascript controller webinterface + * + * @author Steffen Vogel + * @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 . + */ + +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(''); - } + 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(''); + } + }); + + // 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('' + input + '') + $('#properties').append('' + getPropertyForm(property) + '(*)'); } + }); + + $.each(entity.optional, function(index, property) { + var property = getDefinition(properties, property); + + if (property) { + $('#properties').append('' + getPropertyForm(property) + ''); + } + }); +} + +/** + * @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 = ''; - break; - - case 'text': - input = ''; - break; - - case 'boolean': - input = ''; - - case 'multiple': - for (var k in property.options) { - options.push('