Sat, 30 Aug 2014 13:06:07 +0200
The PID commands have an adjustable delay, default 5 minutes
<?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'); function showunit($unit, $unr) { global $my_style; $outstr = ''; $air_state = $beer_state = 1; $answer = send_cmd("UNIT GET ".$unit); $arr = explode("\r\n", $answer); if (startsWith($arr[0], "213")) { $set_temperature = "NA"; $air_temperature = "NA"; $beer_temperature = "NA"; $profile = ""; $prof_state = "OFF"; $power_led = $cooler_led = $heater_led = $fan_led = "off"; $heater = $cooler = $fan = "no"; $heater_state = $cooler_state = $fan_state = 0; 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_ADDRESS") == 0) && (strcmp($vals[1], "(null)"))) { $heater = "yes"; } if (strcmp($vals[0], "HEATER_STATE") == 0) { $heater_state = $vals[1]; } if ((strcmp($vals[0], "COOLER_ADDRESS") == 0) && (strcmp($vals[1], "(null)"))) { $cooler = "yes"; } if (strcmp($vals[0], "COOLER_STATE") == 0) { $cooler_state = $vals[1]; } if ((strcmp($vals[0], "FAN_ADDRESS") == 0) && (strcmp($vals[1], "(null)"))) { $fan = "yes"; } if (strcmp($vals[0], "FAN_STATE") == 0) { $fan_state = $vals[1]; } if (strcmp($vals[0], "PROFILE") == 0) { $profile = $vals[1]; } if (strcmp($vals[0], "PROF_STATE") == 0) { $prof_state = $vals[1]; } if (($vals[0] == "BEER_SET") && ($mode == "BEER")) { $set_temperature = $vals[1]; } if (($vals[0] == "FRIDGE_SET") && ($mode == "FRIDGE")) { $set_temperature = $vals[1]; } if (($vals[0] == "PROF_TARGET") && ($mode == "PROFILE")) { $set_temperature = $vals[1]; } } $outstr .= ' <script type="text/javascript">'.PHP_EOL; $outstr .= ' $(document).ready(function () {'.PHP_EOL; $outstr .= ' var source'.$unr.' ='.PHP_EOL; $outstr .= ' {'.PHP_EOL; $outstr .= ' datatype: "json",'.PHP_EOL; $outstr .= ' datafields: ['.PHP_EOL; $outstr .= ' { name: "Date" },'.PHP_EOL; $outstr .= ' { name: "Mode" },'.PHP_EOL; $outstr .= ' { name: "Air", type: "float" },'.PHP_EOL; $outstr .= ' { name: "Beer", type: "float" },'.PHP_EOL; $outstr .= ' { name: "Target", type: "float" },'.PHP_EOL; $outstr .= ' { name: "Heater", type: "int" },'.PHP_EOL; $outstr .= ' { name: "Cooler", type: "int" },'.PHP_EOL; $outstr .= ' { name: "Fan", type: "int" },'.PHP_EOL; $outstr .= ' { name: "Door", type: "int" }'.PHP_EOL; $outstr .= ' ],'.PHP_EOL; $outstr .= ' url: \'getlog.php?unit='.$unit.'\''.PHP_EOL; $outstr .= ' };'.PHP_EOL; $outstr .= ' var dataAdapter'.$unr.' = new $.jqx.dataAdapter(source'.$unr.','.PHP_EOL; $outstr .= ' {'.PHP_EOL; $outstr .= ' autoBind: true,'.PHP_EOL; $outstr .= ' async: false,'.PHP_EOL; $outstr .= ' downloadComplete: function () { },'.PHP_EOL; $outstr .= ' loadComplete: function () { },'.PHP_EOL; $outstr .= ' loadError: function () { }'.PHP_EOL; $outstr .= ' });'.PHP_EOL; $outstr .= ' var settings'.$unr.' = {'.PHP_EOL; $outstr .= ' title: "Fermentation history",'.PHP_EOL; $outstr .= ' description: "",'.PHP_EOL; $outstr .= ' padding: { left: 5, top: 5, right: 5, bottom: 5 },'.PHP_EOL; $outstr .= ' titlePadding: { left: 90, top: 0, right: 0, bottom: 10 },'.PHP_EOL; $outstr .= ' source: dataAdapter'.$unr.','.PHP_EOL; $outstr .= ' categoryAxis:'.PHP_EOL; $outstr .= ' {'.PHP_EOL; $outstr .= ' dataField: \'Date\','.PHP_EOL; $outstr .= ' textRotationAngle: 45,'.PHP_EOL; $outstr .= ' showGridLines: false'.PHP_EOL; $outstr .= ' },'.PHP_EOL; $outstr .= ' colorScheme: \'scheme01\','.PHP_EOL; $outstr .= ' seriesGroups:'.PHP_EOL; $outstr .= ' [{'.PHP_EOL; $outstr .= ' type: "stepline",'.PHP_EOL; $outstr .= ' columnsGapPercent: 30,'.PHP_EOL; $outstr .= ' seriesGapPercent: 0,'.PHP_EOL; $outstr .= ' valueAxis:'.PHP_EOL; $outstr .= ' {'.PHP_EOL; $outstr .= ' minValue: 0,'.PHP_EOL; $outstr .= ' maxValue: 4,'.PHP_EOL; $outstr .= ' displayValueAxis: false,'.PHP_EOL; $outstr .= ' description: "Heat/Cool"'.PHP_EOL; $outstr .= ' },'.PHP_EOL; $outstr .= ' series: ['.PHP_EOL; $outstr .= ' { dataField: "Cooler", displayText: "Cool", opacity: 0.5 },'.PHP_EOL; $outstr .= ' { dataField: "Heater", displayText: "Heat", opacity: 0.5 }'.PHP_EOL; $outstr .= ' ]'.PHP_EOL; $outstr .= ' },'.PHP_EOL; $outstr .= ' {'.PHP_EOL; $outstr .= ' type: \'spline\','.PHP_EOL; $outstr .= ' columnsGapPercent: 30,'.PHP_EOL; $outstr .= ' seriesGapPercent: 0,'.PHP_EOL; $outstr .= ' valueAxis:'.PHP_EOL; $outstr .= ' {'.PHP_EOL; $outstr .= ' minValue: 0,'.PHP_EOL; $outstr .= ' description: \'Degrees C\''.PHP_EOL; $outstr .= ' },'.PHP_EOL; $outstr .= ' series: ['.PHP_EOL; $outstr .= ' { dataField: "Air", displayText: "Air" },'.PHP_EOL; $outstr .= ' { dataField: "Beer", displayText: "Beer" },'.PHP_EOL; $outstr .= ' { dataField: "Target", displayText: "Target", opacity: 0.7 }'.PHP_EOL; $outstr .= ' ]'.PHP_EOL; $outstr .= ' }]'.PHP_EOL; $outstr .= ' };'.PHP_EOL; /* Direct draw for the first time */ $outstr .= ' $("#fermentor_chart_'.$unit.'").jqxChart(settings'.$unr.');'.PHP_EOL; /* Regular updates of the chart */ // $outstr .= ' setInterval(function(){'.PHP_EOL; // $outstr .= ' $("#fermentor_chart_'.$unit.'").jqxChart("update");'.PHP_EOL; // $outstr .= ' }, 3000);'.PHP_EOL; $outstr .= ' setInterval(function(){'.PHP_EOL; $outstr .= ' $.getJSON("getstate.php?uuid='.$unit.'", function(data) {'.PHP_EOL; $outstr .= ' $("#load_air_'.$unr.'").html(data.air_temperature);'.PHP_EOL; $outstr .= ' $("#load_beer_'.$unr.'").html(data.beer_temperature);'.PHP_EOL; $outstr .= ' $("#load_target_'.$unr.'").html(data.target_temperature);'.PHP_EOL; $outstr .= ' $("#load_select_'.$unr.'").html(data.mode);'.PHP_EOL; $outstr .= ' $("#load_led1_'.$unr.'").html(data.led1);'.PHP_EOL; $outstr .= ' $("#load_led2_'.$unr.'").html(data.led2);'.PHP_EOL; $outstr .= ' $("#load_led3_'.$unr.'").html(data.led3);'.PHP_EOL; $outstr .= ' $("#load_sw1_'.$unr.'").html(data.sw1);'.PHP_EOL; $outstr .= ' $("#load_sw2_'.$unr.'").html(data.sw2);'.PHP_EOL; $outstr .= ' $("#load_sw3_'.$unr.'").html(data.sw3);'.PHP_EOL; $outstr .= ' });'.PHP_EOL; $outstr .= ' }, 10000);'.PHP_EOL; $outstr .= ' });'.PHP_EOL; $outstr .= ' </script>'.PHP_EOL; $outstr .= ' <div id="fermentor">'.PHP_EOL; $outstr .= ' <div id="fermentor_chart_'.$unit.'" style="width:720px; height:377px; float:left"></div>'.PHP_EOL; $outstr .= ' <div id="fermentor_panel_top">'.PHP_EOL; $outstr .= ' <div id="fermentor_selector">'.PHP_EOL; $outstr .= ' <form id="select_'.$unr.'" action="index.php" method="post">'.PHP_EOL; $outstr .= ' <div id="load_select_'.$unr.'">'.PHP_EOL; strcmp($mode, "OFF") ? $se = "" : $se = " checked"; $outstr .= ' <input type="radio" name="mode" onchange="this.form.submit()" class="select" value="OFF"'.$se.'>Off<br>'.PHP_EOL; strcmp($mode, "NONE") ? $se = "" : $se = " checked"; $outstr .= ' <input type="radio" name="mode" onchange="this.form.submit()" class="select" value="NONE"'.$se.'>None<br>'.PHP_EOL; (strcmp($heater, "no") || strcmp($cooler, "no")) ? $dis = "" : $dis = " disabled"; strcmp($mode, "FRIDGE") ? $se = "" : $se = " checked"; $outstr .= ' <input type="radio" name="mode" onchange="this.form.submit()" class="select" value="FRIDGE"'.$se.$dis.'>Fridge<br>'.PHP_EOL; strcmp($mode, "BEER") ? $se = "" : $se = " checked"; $outstr .= ' <input type="radio" name="mode" onchange="this.form.submit()" class="select" value="BEER"'.$se.$dis.'>Beer<br>'.PHP_EOL; strcmp($mode, "PROFILE") ? $se = "" : $se = " checked"; $outstr .= ' <input type="radio" name="mode" onchange="this.form.submit()" class="select" value="PROFILE"'.$se.$dis.'>Profile'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' <input type="hidden" name="UUID" value="'.$unit.'">'.PHP_EOL; $outstr .= ' </form>'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' <div id="fermentor_powerled">'.PHP_EOL; $outstr .= ' <div class="LEDblue_'.$power_led.'"></div>Pwr'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' <div id="fermentor_mode_control">'.PHP_EOL; if ($mode == "FRIDGE") { $outstr .= ' <form id="set_fridge_'.$unr.'" action="index.php" method="post">'.PHP_EOL; $outstr .= ' <input type="text" name="Fridge" size="5" value="'.$set_temperature.'"><br>'.PHP_EOL; $outstr .= ' <input type="submit" value="Set" name="key">'.PHP_EOL; $outstr .= ' <input type="hidden" value="'.$unit.'" name="UUID">'.PHP_EOL; $outstr .= ' </form>'.PHP_EOL; } if ($mode == "BEER") { $outstr .= ' <form id="set_beer_'.$unr.'" action="index.php" method="post">'.PHP_EOL; $outstr .= ' <input type="text" name="Beer" size="5" value="'.$set_temperature.'">'.PHP_EOL; $outstr .= ' <input type="submit" value="Set" name="key">'.PHP_EOL; $outstr .= ' <input type="hidden" value="'.$unit.'" name="UUID">'.PHP_EOL; $outstr .= ' </form>'.PHP_EOL; } if ($mode == "PROFILE") { /* * First, load a list with available profiles. */ $answer = send_cmd("PROFILE LIST"); $reply = explode("\r\n", $answer); /* * Show loaded profile. */ if ($profile == "(null)") { $prof_name = "None"; }else { if (startsWith($reply[0], "212")) { $i = 1; while (1) { if (strcmp($reply[$i], ".") == 0) break; $f = explode(",", $reply[$i]); if ($f[0] == $profile) { $prof_name = $f[1]; break; } $i++; } } } $outstr .= ' <div style="color: blue; width: 148px; height: 23px; overflow: hidden;">Profile: '.$prof_name.'</div>'.PHP_EOL; $outstr .= ' <div style="color: blue; width: 148px; height: 23px; overflow: hidden;">State: '.$prof_state.'</div>'.PHP_EOL; $outstr .= ' <form id="set_profile_'.$unr.'" action="index.php" method="post">'.PHP_EOL; if ($prof_state == "OFF") { $outstr .= ' <select name="SetProfile" style="width: 130px;">'.PHP_EOL; $outstr .= ' <option value="">None</option>'.PHP_EOL; if (startsWith($reply[0], "212")) { $i = 1; while (1) { if (strcmp($reply[$i], ".") == 0) break; $f = explode(",", $reply[$i]); if ($f[2] > 0) { ($f[0] == $profile) ? $se = " selected" : $se = ""; $outstr .= ' <option value="'.$f[0].'"'.$se.'>'.$f[1].'</option>'.PHP_EOL; } $i++; } } $outstr .= ' </select>'.PHP_EOL; $outstr .= ' <input type="submit" value="Set" name="key">'.PHP_EOL; if ($profile != "(null)") $outstr .= ' <input type="submit" value="Start" name="key">'.PHP_EOL; } else if ($prof_state == "RUN") { $outstr .= ' <input type="hidden" value="'.$profile.'" name="SetProfile">'.PHP_EOL; $outstr .= ' <input type="submit" value="Pause" name="key">'.PHP_EOL; $outstr .= ' <input type="submit" value="Abort" name="key">'.PHP_EOL; } else if ($prof_state == "PAUSE") { $outstr .= ' <input type="hidden" value="'.$profile.'" name="SetProfile">'.PHP_EOL; $outstr .= ' <input type="submit" value="Resume" name="key">'.PHP_EOL; $outstr .= ' <input type="submit" value="Abort" name="key">'.PHP_EOL; } else if ($prof_state == "DONE") { $outstr .= ' <input type="hidden" value="'.$profile.'" name="SetProfile">'.PHP_EOL; $outstr .= ' <input type="submit" value="Off" name="key">'.PHP_EOL; } //$outstr .= ' <input type="text" name="Beer" size="5" value="'.$set_temperature.'">'.PHP_EOL; $outstr .= ' <input type="hidden" value="'.$unit.'" name="UUID">'.PHP_EOL; $outstr .= ' </form>'.PHP_EOL; } $outstr .= ' </div>'.PHP_EOL; $outstr .= ' </div> <!-- fermentor_panel_top -->'.PHP_EOL; $outstr .= ' <div id="fermentor_panel_display">'.PHP_EOL; $outstr .= ' <div id="fermentor_display1">'.PHP_EOL; $outstr .= ' <div id="load_target_'.$unr.'" class="dtemp">'.$set_temperature.'</div>Set'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' <div id="fermentor_display2">'.PHP_EOL; $outstr .= ' <div id="load_air_'.$unr.'" class="dtemp">'.$air_temperature.'</div>Air'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' <div id="fermentor_display3">'.PHP_EOL; $outstr .= ' <div id="load_beer_'.$unr.'" class="dtemp">'.$beer_temperature.'</div>Beer'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' </div> <!-- fermentor_panel_display -->'.PHP_EOL; $outstr .= ' <div id="fermentor_panel_control">'.PHP_EOL; if ($cooler_state == "100") $cooler_led = "on"; $outstr .= ' <div id="fermentor_led1">'.PHP_EOL; $outstr .= ' <div id="load_led1_'.$unr.'"><div class="LEDgreen_'.$cooler_led.'"></div></div>Cool'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; if ($heater_state == "100") $heater_led = "on"; $outstr .= ' <div id="fermentor_led2">'.PHP_EOL; $outstr .= ' <div id="load_led2_'.$unr.'"><div class="LEDgreen_'.$heater_led.'"></div></div>Heat'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; if ($fan_state == "100") $fan_led = "on"; $outstr .= ' <div id="fermentor_led3">'.PHP_EOL; $outstr .= ' <div id="load_led3_'.$unr.'"><div class="LEDgreen_'.$fan_led.'"></div></div>Fan'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' <div id="fermentor_toggle1">'.PHP_EOL; $outstr .= ' <form action="index.php" method="post">'.PHP_EOL; (($mode == "NONE") && ($cooler == "yes")) ? $dis = "" : $dis = " disabled"; ($cooler_state == "0") ? $ch = " checked" : $ch = ""; $outstr .= ' <div id="load_sw1_'.$unr.'">'.PHP_EOL; $outstr .= ' <input type="radio" name="CoolerState" onchange="this.form.submit()" value="0"'.$dis.$ch.'>Off<br>'.PHP_EOL; ($cooler_state == "100") ? $ch = " checked" : $ch = ""; $outstr .= ' <input type="radio" name="CoolerState" onchange="this.form.submit()" value="100"'.$dis.$ch.'>On'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' <input type="hidden" name="UUID" value="'.$unit.'">'.PHP_EOL; $outstr .= ' </form>'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' <div id="fermentor_toggle2">'.PHP_EOL; $outstr .= ' <form action="index.php" method="post">'.PHP_EOL; (($mode == "NONE") && ($heater == "yes")) ? $dis = "" : $dis = " disabled"; ($heater_state == "0") ? $ch = " checked" : $ch = ""; $outstr .= ' <div id="load_sw2_'.$unr.'">'.PHP_EOL; $outstr .= ' <input type="radio" name="HeaterState" onchange="this.form.submit()" value="0"'.$dis.$ch.'>Off<br>'.PHP_EOL; ($heater_state == "100") ? $ch = " checked" : $ch = ""; $outstr .= ' <input type="radio" name="HeaterState" onchange="this.form.submit()" value="100"'.$dis.$ch.'>On'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' <input type="hidden" name="UUID" value="'.$unit.'">'.PHP_EOL; $outstr .= ' </form>'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' <div id="fermentor_toggle3">'.PHP_EOL; $outstr .= ' <form action="index.php" method="post">'.PHP_EOL; (($mode == "NONE") && ($fan== "yes")) ? $dis = "" : $dis = " disabled"; ($fan_state == "0") ? $ch = " checked" : $ch = ""; $outstr .= ' <div id="load_sw3_'.$unr.'">'.PHP_EOL; $outstr .= ' <input type="radio" name="FanState" onchange="this.form.submit()" value="0"'.$dis.$ch.'>Off<br>'.PHP_EOL; ($fan_state == "100") ? $ch = " checked" : $ch = ""; $outstr .= ' <input type="radio" name="FanState" onchange="this.form.submit()" value="100"'.$dis.$ch.'>On'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' <input type="hidden" name="UUID" value="'.$unit.'">'.PHP_EOL; $outstr .= ' </form>'.PHP_EOL; $outstr .= ' </div>'.PHP_EOL; $outstr .= ' </div> <!-- fermentor_panel_control -->'.PHP_EOL; $outstr .= ' </div> <!-- fermentor -->'.PHP_EOL; } return $outstr; } function liveview() { $outstr = PHP_EOL; $answer = send_cmd("LIST"); if (strlen($answer)) { $arr = explode("\r\n", $answer); $outstr .= ' <div id="jqxTabs">'.PHP_EOL; $outstr .= ' <ul>'.PHP_EOL; if (startsWith($arr[0], "212")) { $i = 1; while (1) { if (strcmp($arr[$i], ".") == 0) break; $parts = explode(",", $arr[$i]); $outstr .= ' <li style="margin-left: 20px;">'.$parts[1].'</li>'.PHP_EOL; $i++; } } $outstr .= ' </ul>'.PHP_EOL; if (startsWith($arr[0], "212")) { $i = 1; while (1) { if (strcmp($arr[$i], ".") == 0) break; $parts = explode(",", $arr[$i]); $outstr .= showunit($parts[0], $i); $i++; } } $outstr .= ' </div> <!-- jqxTabs -->'.PHP_EOL; } return $outstr; } ?>