Wed, 30 Jul 2014 19:05:11 +0200
Added the framework for the units editor. In the dashboard the radio selector buttons are converteed to jqwidgets radiobuttons.
www-thermferm/liveview.php | file | annotate | diff | comparison | revisions | |
www-thermferm/units.php | file | annotate | diff | comparison | revisions |
--- a/www-thermferm/liveview.php Wed Jul 30 15:27:37 2014 +0200 +++ b/www-thermferm/liveview.php Wed Jul 30 19:05:11 2014 +0200 @@ -66,6 +66,42 @@ if (startsWith($arr[0], "213")) { + $set_temperature = "N/A"; + $air_temperature = "N/A"; + $beer_temperature = "N/A"; + $power_led = $cooler_led = $heater_led = $fan_led = "off"; + $heater = $cooler = $fan = "no"; + + foreach($arr as $l) { + $vals = explode(",", $l); + if (strcmp($vals[0], "MODE") == 0) { + $mode = $vals[1]; + if (strcmp($mode, "OFF")) + $power_led = "on"; + } + if (strcmp($vals[0], "AIR_STATE") == 0) { + $air_state = $vals[1]; + } + if ((strcmp($vals[0], "AIR_TEMPERATURE") == 0) && ($air_state == 0)) { + $air_temperature = $vals[1]; + } + if (strcmp($vals[0], "BEER_STATE") == 0) { + $beer_state = $vals[1]; + } + if ((strcmp($vals[0], "BEER_TEMPERATURE") == 0) && ($beer_state == 0)) { + $beer_temperature = $vals[1]; + } + if ((strcmp($vals[0], "HEATER") == 0) && (strcmp($vals[1], "YES"))) { + $heater = "yes"; + } + if ((strcmp($vals[0], "COOLER") == 0) && (strcmp($vals[1], "YES"))) { + $cooler = "yes"; + } + if ((strcmp($vals[0], "FAN") == 0) && (strcmp($vals[1], "YES"))) { + $fan = "yes"; + } + } + $outstr .= ' <script type="text/javascript">'.PHP_EOL; $outstr .= ' $(document).ready(function () {'.PHP_EOL; @@ -124,51 +160,29 @@ $outstr .= ' }]'.PHP_EOL; $outstr .= ' };'.PHP_EOL; $outstr .= ' $("#fermentor_chart_'.$unr.'").jqxChart(settings'.$unr.');'.PHP_EOL; + strcmp($mode, "OFF") ? $se = "" : $se = ", checked: true"; + $outstr .= ' $("#selButton1_'.$unit.'").jqxRadioButton({ groupName: "'.$unit.'", width: 150, height: 18'.$se.'});'.PHP_EOL; + strcmp($mode, "NONE") ? $se = "" : $se = ", checked: true"; + $outstr .= ' $("#selButton2_'.$unit.'").jqxRadioButton({ groupName: "'.$unit.'", width: 150, height: 18'.$se.'});'.PHP_EOL; + strcmp($mode, "FRIDGE") ? $se = "" : $se = ", checked: true"; + $outstr .= ' $("#selButton3_'.$unit.'").jqxRadioButton({ groupName: "'.$unit.'", width: 150, height: 18'.$se.', disabled: true});'.PHP_EOL; + strcmp($mode, "BEER") ? $se = "" : $se = ", checked: true"; + $outstr .= ' $("#selButton4_'.$unit.'").jqxRadioButton({ groupName: "'.$unit.'", width: 150, height: 18'.$se.', disabled: true});'.PHP_EOL; + strcmp($mode, "PROFILE") ? $se = "" : $se = ", checked: true"; + $outstr .= ' $("#selButton5_'.$unit.'").jqxRadioButton({ groupName: "'.$unit.'", width: 150, height: 18'.$se.', disabled: true});'.PHP_EOL; + $outstr .= ' });'.PHP_EOL; $outstr .= ' </script>'.PHP_EOL; $outstr .= ' <div id="fermentor">'.PHP_EOL; $outstr .= ' <div id="fermentor_chart_'.$unr.'" style="width:720px; height:377px; float:left"></div>'.PHP_EOL; - $set_temperature = "N/A"; - $air_temperature = "N/A"; - $beer_temperature = "N/A"; - $power_led = $cooler_led = $heater_led = $fan_led = "off"; - - foreach($arr as $l) { - $vals = explode(",", $l); - if (strcmp($vals[0], "MODE") == 0) { - $mode = $vals[1]; - if (strcmp($mode, "OFF")) - $power_led = "on"; - } - if (strcmp($vals[0], "AIR_STATE") == 0) { - $air_state = $vals[1]; - } - if ((strcmp($vals[0], "AIR_TEMPERATURE") == 0) && ($air_state == 0)) { - $air_temperature = $vals[1]; - } - if (strcmp($vals[0], "BEER_STATE") == 0) { - $beer_state = $vals[1]; - } - if ((strcmp($vals[0], "BEER_TEMPERATURE") == 0) && ($beer_state == 0)) { - $beer_temperature = $vals[1]; - } - } $outstr .= ' <div id="fermentor_panel_top">'.PHP_EOL; $outstr .= ' <div id="fermentor_selector">'.PHP_EOL; - $outstr .= ' Mode '.$mode.PHP_EOL; - $outstr .= ' <form>'.PHP_EOL; - strcmp($mode, "OFF") ? $se = "" : $se = " checked"; - $outstr .= ' <input type="radio" name="mode'.$unr.'" value="OFF"'.$se.'>Off<br>'.PHP_EOL; - strcmp($mode, "NONE") ? $se = "" : $se = " checked"; - $outstr .= ' <input type="radio" name="mode'.$unr.'" value="NONE"'.$se.'>None<br>'.PHP_EOL; - strcmp($mode, "FRIDGE") ? $se = "" : $se = " checked"; - $outstr .= ' <input type="radio" name="mode'.$unr.'" value="FRIDGE"'.$se.' disabled>Fridge<br>'.PHP_EOL; - strcmp($mode, "BEER") ? $se = "" : $se = " checked"; - $outstr .= ' <input type="radio" name="mode'.$unr.'" value="BEER"'.$se.' disabled>Beer<br>'.PHP_EOL; - strcmp($mode, "PROFILE") ? $se = "" : $se = " checked"; - $outstr .= ' <input type="radio" name="mode'.$unr.'" value="PROFILE"'.$se.' disabled>Profile'.PHP_EOL; - $outstr .= ' </form>'.PHP_EOL; + $outstr .= ' <div style="margin-top: 10px;" id="selButton1_'.$unit.'">Off</div>'.PHP_EOL; + $outstr .= ' <div style="margin-top: 10px;" id="selButton2_'.$unit.'">None</div>'.PHP_EOL; + $outstr .= ' <div style="margin-top: 10px;" id="selButton3_'.$unit.'">Fridge</div>'.PHP_EOL; + $outstr .= ' <div style="margin-top: 10px;" id="selButton4_'.$unit.'">Beer</div>'.PHP_EOL; + $outstr .= ' <div style="margin-top: 10px;" id="selButton5_'.$unit.'">Profile</div>'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' <div id="fermentor_powerled">'.PHP_EOL; $outstr .= ' <img src="images/led_green_'.$power_led.'.png"><br>Pwr'.PHP_EOL;
--- a/www-thermferm/units.php Wed Jul 30 15:27:37 2014 +0200 +++ b/www-thermferm/units.php Wed Jul 30 19:05:11 2014 +0200 @@ -26,8 +26,7 @@ $sock = open_socket(); if ($sock == false) { - echo ""; - return; + load('index.php'); } socket_write($sock, "LIST", 4096); @@ -39,35 +38,333 @@ $answer .= $line; } socket_close($sock); + +/* + * $arr contains the complete reply of the LIST command. + */ $arr = explode("\r\n", $answer); -$outstr = build_header(); -$outstr .= ' <div id="errors">'.PHP_EOL; -$outstr .= ' </div> <!-- errors -->'.PHP_EOL; -$outstr .= ' <div id="etable">'.PHP_EOL; +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 = "ADD UNIT ".$_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') + $update_cmd = "DEL UNIT ".$_POST['UUID']; + else + $update_cmd = "XXXXXXX ".$_POST['UUID'].",".$_POST['Name']; + + $sock = open_socket(); + if ($sock != false) { + /* + * Send command and absorb the result. + */ + socket_write($sock, $update_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'); +} + + + +/* + * 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['key']) && isset($_POST['command'])) { + + if ($_POST['key'] == 'Cancel') + return 99; + + if (strlen($_POST['Name']) < 2) + return 2; -$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 .= ' </tr>'.PHP_EOL; + 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['Name'], $_POST['command'], $heading, $error); +} + + -if (startsWith($arr[0], "212")) { - $j = 1; - while (1) { - if (strcmp($arr[$j], ".") == 0) - break; - $f = explode(",", $arr[$j]); - $outstr .= ' <tr class="setup"><td class="setup">'.$f[0].'</td><td class="setup">'.$f[1].'</td><td class="setup">'.$f[2].'</td></tr>'.PHP_EOL; - $j++; +/* + * Unit edit screen. Used by unit_edit(), unit_add() and testdata() + * + * @param string $UUID The record UUID (fixed). + * @param string $Name The Unit Name. + * @param string $command 'add' or 'update' + * @param string $heading Page heading title. + * @Param string $error_message Blank or previous error. + */ +function edit_screen($UUID, $Name, $command, $heading, $error_message) { + + $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; + $outstr .= ' <tr class="editor">'.PHP_EOL; + $outstr .= ' <td class="editname">UUID</td>'.PHP_EOL; + $outstr .= ' <td class="editfield">'.$UUID.'</td>'.PHP_EOL; + $outstr .= ' </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="'.$Name.'"></td>'.PHP_EOL; + $outstr .= ' </tr>'.PHP_EOL; + $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() { + + global $arr; + + if ($_GET['action'] == 'edit') { + + if (startsWith($arr[0], "212")) { + $j = 1; + while (1) { + if (strcmp($arr[$j], ".") == 0) + break; + $f = explode(",", $arr[$j]); + if (strcmp($f[0], $_GET['UUID']) == 0) { + edit_screen($f[0], $f[1], 'update', 'ThermFerm - Edit Unit', ''); + return; + } + $j++; + } + } else { + load('units.php'); + } + + } else { + load('units.php'); } } -$outstr .= ' </table>'.PHP_EOL; -$outstr .= ' </div> <!-- etable -->'.PHP_EOL; -$outstr .= build_footer(); + + +/* + * @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; -echo $outstr; + 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="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; +} +