www-thermferm/units.php

Mon, 16 Feb 2015 19:52:42 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 16 Feb 2015 19:52:42 +0100
changeset 312
7b0f819a3805
parent 311
f3b0e9ac9bcb
child 349
1f89da6511a9
permissions
-rw-r--r--

Added more precision to the unit low and high black window margins. Switched to PID routine from Pid without a PhD. The PID compute routine is now in the one second loop. The switch delay times are now in seconds, 0..720.

<?php
/*****************************************************************************
 * Copyright (C) 2014-2015
 *   
 * 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');


/*
 * $arr contains the complete reply of the LIST command.
 */
$answer = send_cmd("UNIT LIST");
$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') {
	send_cmd("UNIT ADD ".$_POST['Name']);
    }

    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') {
	send_cmd("UNIT DEL ".$_POST['UUID']);
    }

    if ($_POST['key'] == 'Save') {
	$cmd = array("UNIT PUT ".$_POST['UUID']);
	$cmd[] = "NAME,".$_POST['Name'];
	$cmd[] = "VOLUME,".$_POST['Volume'];
	$cmd[] = "AIR_ADDRESS,".$_POST['AirAddress'];
	$cmd[] = "BEER_ADDRESS,".$_POST['BeerAddress'];
	$cmd[] = "HEATER_ADDRESS,".$_POST['HeaterAddress'];
	$cmd[] = "HEATER_DELAY,".$_POST['HeaterDelay'];
	$cmd[] = "COOLER_ADDRESS,".$_POST['CoolerAddress'];
	$cmd[] = "COOLER_DELAY,".$_POST['CoolerDelay'];
	$cmd[] = "FAN_ADDRESS,".$_POST['FanAddress'];
	$cmd[] = "LIGHT_ADDRESS,".$_POST['LightAddress'];
	$cmd[] = "LIGHT_DELAY,".$_POST['LightDelay'];
	$cmd[] = "DOOR_ADDRESS,".$_POST['DoorAddress'];
	$cmd[] = "PSU_ADDRESS,".$_POST['PSUAddress'];
	$cmd[] = "TEMP_SET_MIN,".$_POST['TempSetMin'];
	$cmd[] = "TEMP_SET_MAX,".$_POST['TempSetMax'];
	$cmd[] = "IDLE_RANGE_L,".$_POST['IdleRangeL'];
	$cmd[] = "IDLE_RANGE_H,".$_POST['IdleRangeH'];
	$cmd[] = "PID_KP,".$_POST['PID_Kp'];
	$cmd[] = "PID_KD,".$_POST['PID_Kd'];
	$cmd[] = "PID_KI,".$_POST['PID_Ki'];
	$cmd[] = ".";
	send_array($cmd);
    }

    unset($_POST['UUID']);
    unset($_POST['Name']);
    unset($_POST['Volume']);
    unset($_POST['key']);
    unset($_POST['command']);
    unset($_POST['AirAddress']);
    unset($_POST['BeerAddress']);
    unset($_POST['HeaterAddress']);
    unset($_POST['HeaterDelay']);
    unset($_POST['CoolerAddress']);
    unset($_POST['CoolerDelay']);
    unset($_POST['LightAddress']);
    unset($_POST['LightDelay']);
    unset($_POST['FanAddress']);
    unset($_POST['DoorAddress']);
    unset($_POST['PSUAddress']);
    unset($_POST['TempSetMin']);
    unset($_POST['TempSetMax']);
    unset($_POST['IdleRangeL']);
    unset($_POST['IdleRangeH']);
    unset($_POST['PID_Kp']);
    unset($_POST['PID_Kd']);
    unset($_POST['PID_Ki']);
    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
 *         4 = TempSetMax <= TempSetMin
 *         5 = TempSetMax < 25 or > 35
 *         6 = TempSetMin < -5 or > 15
 *         7 = HeaterDelay out of range
 *         8 = CoolerDelay out of range
 *         9 = IdleRangeL out of range
 *        10 = IdleRangeH out of range
 *        11 = LightDelay out of range
 *        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['LightAddress']) &&
	isset($_POST['HeaterDelay']) && isset($_POST['CoolerDelay']) && isset($_POST['LightDelay']) && isset($_POST['PSUAddress']) &&
	isset($_POST['FanAddress']) && isset($_POST['DoorAddress']) && isset($_POST['TempSetMin']) && isset($_POST['TempSetMax']) &&
	isset($_POST['PID_Kp']) && isset($_POST['PID_Kd']) && isset($_POST['PID_Ki']) &&
	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++;
	    }
	}

	if ($_POST['TempSetMax'] <= $_POST['TempSetMin'])
	    return 4;

	if (($_POST['TempSetMax'] < 25) || ($_POST['TempSetMax'] > 35))
	    return 5;

	if (($_POST['TempSetMin'] < -5) || ($_POST['TempSetMin'] > 15))
	    return 6;

	if (($_POST['HeaterDelay'] < 0) || ($_POST['HeaterDelay'] > 720))
	    return 7;

	if (($_POST['CoolerDelay'] < 0) || ($_POST['CoolerDelay'] > 720))
	    return 8;

	if (($_POST['IdleRangeL'] > 0) || ($_POST['IdleRangeL'] < -5))
	    return 9;

	if (($_POST['IdleRangeH'] < 0) || ($_POST['IdleRangeH'] > 5))
	    return 10;

	if (($_POST['LightDelay'] < 0) || ($_POST['LightDelay'] > 720))
	    return 11;

    } 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 4: $error = 'Temperature Maximum cannot be smaller then Temperature Minimum';
		break;
	case 5: $error = 'Temperature Maximum must be between 25 and 35 &deg;C';
		break;
	case 6:	$error = 'Temperature Minimum must be between -5 and 15 &deg;C';
		break;
	case 7: $error = 'Heater Delay must be bewteen 0 and 720 seconds';
		break;
	case 8:	$error = 'Cooler Delay must be bewteen 0 and 720 seconds';
		break;
	case 9: $error = 'Idle Range Low must be between -5 en 0';
		break;
	case 10: $error = 'Idle Range High must be between 0 and 5';
		break;
	case 11: $error = 'Light Delay must be bewteen 0 and 720 seconds';
		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
     */
    $answer = send_cmd("DEVICE LIST");
    $devices = explode("\r\n", $answer);

    /*
     * Get current unit data
     */
    $answer = send_cmd("UNIT GET ".$UUID);
    $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] == "HEATER_DELAY") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Heater Switch Delay</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="HeaterDelay" size="5" value="'.$f[1].'"> seconds (0..720)</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] == "COOLER_DELAY") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Cooler Switch Delay</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="CoolerDelay" size="5" value="'.$f[1].'"> seconds (0..720)</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] == "LIGHT_ADDRESS") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Lights Address</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><select name="LightAddress">'.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] == "LIGHT_DELAY") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Lights Delay</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="LightDelay" size="5" value="'.$f[1].'"> seconds (0..720)</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] == "PSU_ADDRESS") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Power Supply Sensor Address</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><select name="PSUAddress">'.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].'"> &deg;C</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].'"> &deg;C</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="6" value="'.$f[1].'"> &deg;C (Cooler margin)</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="6" value="'.$f[1].'"> &deg;C (Heater margin)</td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "PID_KP") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">PID Kp</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="PID_Kp" size="6" value="'.$f[1].'"> Proportional</td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "PID_KI") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">PID Ki</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="PID_Ki" size="6" value="'.$f[1].'"> Intergral</td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "PID_KD") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">PID Kd</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="PID_Kd" size="6" value="'.$f[1].'"> Derivative</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&amp;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="" name="AirAddress">';
    $outstr .= '<input type="hidden" value="" name="BeerAddress">';
    $outstr .= '<input type="hidden" value="" name="HeaterAddress">';
    $outstr .= '<input type="hidden" value="20" name="HeaterDelay">';
    $outstr .= '<input type="hidden" value="" name="CoolerAddress">';
    $outstr .= '<input type="hidden" value="20" name="CoolerDelay">';
    $outstr .= '<input type="hidden" value="" name="FanAddress">';
    $outstr .= '<input type="hidden" value="" name="LightAddress">';
    $outstr .= '<input type="hidden" value="1" name="LightDelay">';
    $outstr .= '<input type="hidden" value="" name="DoorAddress">';
    $outstr .= '<input type="hidden" value="" name="PSUAdress">';
    $outstr .= '<input type="hidden" value="1.0" name="TempSetMin">';
    $outstr .= '<input type="hidden" value="30.0" name="TempSetMax">';
    $outstr .= '<input type="hidden" value="-1.0" name="IdleRangeL">';
    $outstr .= '<input type="hidden" value="1.0" name="IdleRangeH">';
    $outstr .= '<input type="hidden" value="4.0" name="PID_Kp">';
    $outstr .= '<input type="hidden" value="0.2" name="PID_Kd">';
    $outstr .= '<input type="hidden" value="1.0" name="PID_Ki">';
    $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;
}

mercurial