Tue, 05 Aug 2014 15:44:29 +0200
Removing units thread
<?php /***************************************************************************** * Copyright (C) 2014 * * Michiel Broek <mbroek at mbse dot eu> * * This file is part of ThermFerm * * This 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 2, or (at your option) any * later version. * * ThermFerm 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 ThermFerm; see the file COPYING. If not, write to the Free * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *****************************************************************************/ require_once('utilities.php'); $sock = open_socket(); if ($sock == false) { load('index.php'); } socket_write($sock, "UNIT LIST", 4096); $answer = ""; while (1) { $line = socket_read($sock, 4096); if ($line === '') break; $answer .= $line; } socket_close($sock); /* * $arr contains the complete reply of the LIST command. */ $arr = explode("\r\n", $answer); if (isset($_GET['action'])) { switch ($_GET['action']) { case 'edit': unit_edit(); break; default: break; } } elseif (isset($_POST['action'])) { switch ($_POST['action']) { case 'testdata': testdata(); break; default: break; } } else { unit_list(); } exit; /**************************************************************************** * */ /* * Unit add * * @param string $_POST['Name'] The rpofile name */ function unit_add() { if ($_POST['key'] == 'Add') { $cmd = "UNIT ADD ".$_POST['Name']; $sock = open_socket(); if ($sock != false) { /* * Send command and absorb the result. */ socket_write($sock, $cmd, 4096); while (1) { $line = socket_read($sock, 4096); if ($line === '') break; } socket_close($sock); } } unset($_POST['UUID']); unset($_POST['Name']); unset($_POST['key']); unset($_POST['command']); load('units.php'); } /* * Unit update * * @param string $_POST['UUID'] The unit UUID * @param string $_POST['Name'] The unit name * @param string $_POST['key'] The button pressed. */ function unit_update() { /* * Build the update command */ if ($_POST['key'] == 'Delete') { $sock = open_socket(); if ($sock != false) { socket_write($sock, "UNIT DEL ".$_POST['UUID'], 4096); /* Absorb response */ while (1) { $line = socket_read($sock, 4096); if ($line === '') break; } socket_close($sock); } } if ($_POST['key'] == 'Save') { $sock = open_socket(); if ($sock != false) { /* * Send command and absorb the result. */ socket_write($sock, "UNIT PUT ".$_POST['UUID'], 4096); usleep(20000); socket_write($sock, "NAME,".$_POST['Name'], 4096); usleep(20000); socket_write($sock, "VOLUME,".$_POST['Volume'], 4096); usleep(20000); socket_write($sock, "AIR_ADDRESS,".$_POST['AirAddress'], 4096); usleep(20000); socket_write($sock, "BEER_ADDRESS,".$_POST['BeerAddress'], 4096); usleep(20000); socket_write($sock, "HEATER_ADDRESS,".$_POST['HeaterAddress'], 4096); usleep(20000); socket_write($sock, "COOLER_ADDRESS,".$_POST['CoolerAddress'], 4096); usleep(20000); socket_write($sock, "FAN_ADDRESS,".$_POST['FanAddress'], 4096); usleep(20000); socket_write($sock, "DOOR_ADDRESS,".$_POST['DoorAddress'], 4096); usleep(20000); socket_write($sock, "TEMP_SET_MIN,".$_POST['TempSetMin'], 4096); usleep(20000); socket_write($sock, "TEMP_SET_MAX,".$_POST['TempSetMax'], 4096); usleep(20000); socket_write($sock, "IDLE_RANGE_L,".$_POST['IdleRangeL'], 4096); usleep(20000); socket_write($sock, "IDLE_RANGE_H,".$_POST['IdleRangeH'], 4096); usleep(20000); socket_write($sock, ".", 4096); while (1) { $line = socket_read($sock, 4096); if ($line === '') break; } socket_close($sock); } } unset($_POST['UUID']); unset($_POST['Name']); unset($_POST['Volume']); unset($_POST['key']); unset($_POST['command']); load('units.php'); } /* * Test input of a modified or new profile. * * @param string $_POST['UUID'] Unique record UUID * @param string $_POST['Name'] Profile name * @param string $_POST['key'] Key choice, Save or Cancel * @param string $_POST['command'] Command used, 'add' or 'update' * * Return: 0 = Ok * 1 = Missing data * 2 = Name field too short * 3 = Name already in use * 99 = Cancel key */ function test_thedata() { global $arr; if (isset($_POST['UUID']) && isset($_POST['Name']) && isset($_POST['Volume']) && isset($_POST['AirAddress']) && isset($_POST['BeerAddress']) && isset($_POST['HeaterAddress']) && isset($_POST['CoolerAddress']) && isset($_POST['FanAddress']) && isset($_POST['DoorAddress']) && isset($_POST['TempSetMin']) && isset($_POST['TempSetMax']) && isset($_POST['IdleRangeL']) && isset($_POST['IdleRangeH']) && isset($_POST['key']) && isset($_POST['command'])) { if ($_POST['key'] == 'Cancel') return 99; if (strlen($_POST['Name']) < 2) return 2; if (startsWith($arr[0], "212")) { $j = 1; while (1) { if (strcmp($arr[$j], ".") == 0) break; $f = explode(",", $arr[$j]); if (strcmp($f[0], $_POST['UUID']) && (strcmp($f[1], $_POST['Name']) == 0)) { return 3; } $j++; } } } else { return 1; } return 0; } /* * Test result from edit screen and do next action */ function testdata() { $result = test_thedata(); $error = ''; switch ($result) { case 0: if ($_POST['command'] == 'add') { unit_add(); return; } else if ($_POST['command'] == 'update') { unit_update(); return; } break; case 1: $error = 'Missing data'; break; case 2: $error = 'The name is too short'; break; case 3: $error = 'The name is already in use, choose another one'; break; case 99: load('units.php'); break; } if ($_POST['command'] == 'add') { $heading = 'ThermFerm - Add Unit'; } else { $heading = 'ThermFerm - Edit Unit'; } edit_screen($_POST['UUID'], $_POST['command'], $heading, $error); } /* * Unit edit screen. Used by unit_edit(), unit_add() and testdata() * * @param string $UUID The record UUID (fixed). * @param string $command 'add' or 'update' * @param string $heading Page heading title. * @Param string $error_message Blank or previous error. */ function edit_screen($UUID, $command, $heading, $error_message) { /* * Get list of devices, we need it later */ $sock = open_socket(); if ($sock == false) { load("units.php"); return; } socket_write($sock, "DEVICE LIST", 4096); $answer = ""; while (1) { $line = socket_read($sock, 4096); if ($line === '') break; $answer .= $line; } socket_close($sock); $devices = explode("\r\n", $answer); /* * Get current unit data */ $sock = open_socket(); if ($sock == false) { load("units.php"); return; } socket_write($sock, "UNIT GET ".$UUID, 4096); $answer = ""; while (1) { $line = socket_read($sock, 4096); if ($line === '') break; $answer .= $line; } socket_close($sock); $reply = explode("\r\n", $answer); $outstr = build_header($heading); $outstr .= ' <div id="errors">'.PHP_EOL; $outstr .= ' '.$error_message.PHP_EOL; $outstr .= ' </div> <!-- errors -->'.PHP_EOL; $outstr .= ' <div id="etable">'.PHP_EOL; $outstr .= ' <form method="POST" action="units.php">'.PHP_EOL; $outstr .= ' <table class="editor">'.PHP_EOL; if (startsWith($reply[0], "213")) { $i = 1; while (1) { if (strcmp($reply[$i], ".") == 0) break; $f = explode(",", $reply[$i]); if ($f[0] == "NAME") { $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname">Unit Name</td>'.PHP_EOL; $outstr .= ' <td class="editfield"><input type="text" name="Name" size="50" value="'.$f[1].'"></td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; } if ($f[0] == "VOLUME") { $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname">Unit Volume</td>'.PHP_EOL; $outstr .= ' <td class="editfield"><input type="text" name="Volume" size="5" value="'.$f[1].'"></td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; } if ($f[0] == "AIR_ADDRESS") { $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname">Air Sensor Address</td>'.PHP_EOL; $outstr .= ' <td class="editfield"><select name="AirAddress">'.PHP_EOL; $outstr .= ' <option value="">Not Assigned</option>'.PHP_EOL; if (startsWith($devices[0], "212")) { $j = 1; while (1) { if (strcmp($devices[$j], ".") == 0) break; $g = explode(",", $devices[$j]); if ($g[5] == "IN_ANALOG") { ($f[1] == $g[0]) ? $se = " selected" : $se = ""; $outstr .= ' <option value="'.$g[0].'"'.$se.'>'.$g[1].' '.$g[4].'</option>'.PHP_EOL; } $j++; } } $outstr .= ' </select></td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; } if ($f[0] == "BEER_ADDRESS") { $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname">Beer Sensor Address</td>'.PHP_EOL; $outstr .= ' <td class="editfield"><select name="BeerAddress">'.PHP_EOL; $outstr .= ' <option value="">Not Assigned</option>'.PHP_EOL; if (startsWith($devices[0], "212")) { $j = 1; while (1) { if (strcmp($devices[$j], ".") == 0) break; $g = explode(",", $devices[$j]); if ($g[5] == "IN_ANALOG") { ($f[1] == $g[0]) ? $se = " selected" : $se = ""; $outstr .= ' <option value="'.$g[0].'"'.$se.'>'.$g[1].' '.$g[4].'</option>'.PHP_EOL; } $j++; } } $outstr .= ' </select></td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; } if ($f[0] == "HEATER_ADDRESS") { $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname">Heater Switch Address</td>'.PHP_EOL; $outstr .= ' <td class="editfield"><select name="HeaterAddress">'.PHP_EOL; $outstr .= ' <option value="">Not Assigned</option>'.PHP_EOL; if (startsWith($devices[0], "212")) { $j = 1; while (1) { if (strcmp($devices[$j], ".") == 0) break; $g = explode(",", $devices[$j]); if (($g[5] == "OUT_ANALOG") || ($g[5] == "OUT_BIN")) { ($f[1] == $g[0]) ? $se = " selected" : $se = ""; $outstr .= ' <option value="'.$g[0].'"'.$se.'>'.$g[1].' '.$g[4].'</option>'.PHP_EOL; } $j++; } } $outstr .= ' </select></td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; } if ($f[0] == "COOLER_ADDRESS") { $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname">Cooler Switch Address</td>'.PHP_EOL; $outstr .= ' <td class="editfield"><select name="CoolerAddress">'.PHP_EOL; $outstr .= ' <option value="">Not Assigned</option>'.PHP_EOL; if (startsWith($devices[0], "212")) { $j = 1; while (1) { if (strcmp($devices[$j], ".") == 0) break; $g = explode(",", $devices[$j]); if (($g[5] == "OUT_ANALOG") || ($g[5] == "OUT_BIN")) { ($f[1] == $g[0]) ? $se = " selected" : $se = ""; $outstr .= ' <option value="'.$g[0].'"'.$se.'>'.$g[1].' '.$g[4].'</option>'.PHP_EOL; } $j++; } } $outstr .= ' </select></td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; } if ($f[0] == "FAN_ADDRESS") { $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname">Fan Switch Address</td>'.PHP_EOL; $outstr .= ' <td class="editfield"><select name="FanAddress">'.PHP_EOL; $outstr .= ' <option value="">Not Assigned</option>'.PHP_EOL; if (startsWith($devices[0], "212")) { $j = 1; while (1) { if (strcmp($devices[$j], ".") == 0) break; $g = explode(",", $devices[$j]); if (($g[5] == "OUT_ANALOG") || ($g[5] == "OUT_BIN")) { ($f[1] == $g[0]) ? $se = " selected" : $se = ""; $outstr .= ' <option value="'.$g[0].'"'.$se.'>'.$g[1].' '.$g[4].'</option>'.PHP_EOL; } $j++; } } $outstr .= ' </select></td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; } if ($f[0] == "DOOR_ADDRESS") { $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname">Door Sensor Address</td>'.PHP_EOL; $outstr .= ' <td class="editfield"><select name="DoorAddress">'.PHP_EOL; $outstr .= ' <option value="">Not Assigned</option>'.PHP_EOL; if (startsWith($devices[0], "212")) { $j = 1; while (1) { if (strcmp($devices[$j], ".") == 0) break; $g = explode(",", $devices[$j]); if ($g[5] == "IN_BIN") { ($f[1] == $g[0]) ? $se = " selected" : $se = ""; $outstr .= ' <option value="'.$g[0].'"'.$se.'>'.$g[1].' '.$g[4].'</option>'.PHP_EOL; } $j++; } } $outstr .= ' </select></td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; } if ($f[0] == "TEMP_SET_MIN") { $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname">Min. temp setting</td>'.PHP_EOL; $outstr .= ' <td class="editfield"><input type="text" name="TempSetMin" size="5" value="'.$f[1].'"></td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; } if ($f[0] == "TEMP_SET_MAX") { $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname">Max. temp setting</td>'.PHP_EOL; $outstr .= ' <td class="editfield"><input type="text" name="TempSetMax" size="5" value="'.$f[1].'"></td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; } if ($f[0] == "IDLE_RANGE_L") { $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname">Idle Range Low</td>'.PHP_EOL; $outstr .= ' <td class="editfield"><input type="text" name="IdleRangeL" size="5" value="'.$f[1].'"></td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; } if ($f[0] == "IDLE_RANGE_H") { $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname">Idle Range High</td>'.PHP_EOL; $outstr .= ' <td class="editfield"><input type="text" name="IdleRangeH" size="5" value="'.$f[1].'"></td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; } $i++; } } $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname"><input type="submit" value="Save" name="key"></td>'.PHP_EOL; $outstr .= ' <td class="editfield"><input type="submit" value="Cancel" name="key">'; $outstr .= '<input type="submit" value="Delete" name="key" style="margin-left: 100px;">'; $outstr .= '<input type="hidden" value="testdata" name="action">'; $outstr .= '<input type="hidden" value="'.$command.'" name="command">'; $outstr .= '<input type="hidden" value="'.$UUID.'" name="UUID"></td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; $outstr .= ' </table>'.PHP_EOL; $outstr .= ' </form>'.PHP_EOL; $outstr .= ' </div> <!-- etable -->'.PHP_EOL; $outstr .= build_footer(); echo $outstr; } /* * Edit a Unit. Fetches the record data and shows the edit screen. * * @param string $_GET['action'] Must be 'edit'. * @param string $_GET['UUID'] The UUID of the Unit. */ function unit_edit() { if ($_GET['action'] == 'edit') { edit_screen($_GET['UUID'], 'update', 'ThermFerm - Edit Unit', ''); return; } else { load('units.php'); } } /* * @link edit unit * @link add unit */ function unit_list() { global $arr; $outstr = build_header('ThermFerm - Units Maintenance'); $outstr .= ' <div id="errors">'.PHP_EOL; $outstr .= ' </div> <!-- errors -->'.PHP_EOL; $outstr .= ' <div id="etable">'.PHP_EOL; $outstr .= ' <table class="setup">'.PHP_EOL; $outstr .= ' <tr class="trhead">'.PHP_EOL; $outstr .= ' <td class="setup" style="width: 300px;">UUID</td>'.PHP_EOL; $outstr .= ' <td class="setup" style="width: 300px;">Name</td>'.PHP_EOL; $outstr .= ' <td class="setup" style="width: 60px;">Mode</td>'.PHP_EOL; $outstr .= ' <td class="setup" style="width: 40px;">Edit</td>'.PHP_EOL; $outstr .= ' </tr>'.PHP_EOL; if (startsWith($arr[0], "212")) { $j = 1; while (1) { if (strcmp($arr[$j], ".") == 0) break; $f = explode(",", $arr[$j]); $outstr .= ' <tr class="setup">'.PHP_EOL; $outstr .= ' <td class="setup">'.$f[0].'</td>'.PHP_EOL; $outstr .= ' <td class="setup">'.$f[1].'</td>'.PHP_EOL; $outstr .= ' <td class="setup">'.$f[2].'</td>'.PHP_EOL; if (strcmp($f[2], "OFF")) { $outstr .= ' <td class="setup">Busy</td>'.PHP_EOL; } else { $outstr .= ' <td class="setup"><a href="units.php?action=edit&UUID='.$f[0].'">Edit</a></td>'.PHP_EOL; } $outstr .= ' </tr>'.PHP_EOL; $j++; } } $outstr .= ' </table>'.PHP_EOL; $outstr .= ' </div> <!-- etable -->'.PHP_EOL; $outstr .= ' <div id="atable">'.PHP_EOL; $outstr .= ' <form method="POST" action="units.php">'.PHP_EOL; $outstr .= ' <table class="editor">'.PHP_EOL; $outstr .= ' <tr class="trhead"><td colspan="3">Add new unit</td></tr>'.PHP_EOL; $outstr .= ' <tr class="editor">'.PHP_EOL; $outstr .= ' <td class="editname">Unit Name</td>'.PHP_EOL; $outstr .= ' <td class="editfield"><input type="text" name="Name" size="50" value=""></td>'.PHP_EOL; $outstr .= ' <td class="editsub"><input type="submit" value="Add" name="key"></td>'.PHP_EOL; $outstr .= '<input type="hidden" value="0.0" name="Volume">'; $outstr .= '<input type="hidden" value="testdata" name="action">'; $outstr .= '<input type="hidden" value="add" name="command">'; $outstr .= '<input type="hidden" value="00000000-0000-0000-0000-000000000000" name="UUID">'; $outstr .= ' </tr>'.PHP_EOL; $outstr .= ' </table>'.PHP_EOL; $outstr .= ' </form>'.PHP_EOL; $outstr .= ' </div> <!-- atable -->'.PHP_EOL; $outstr .= build_footer(); echo $outstr; }