www-thermferm/simulator.php

Sun, 07 Jul 2019 14:31:10 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 07 Jul 2019 14:31:10 +0200
branch
stable
changeset 603
fcff55324b84
parent 553
4091d4fe217f
permissions
-rw-r--r--

Merged fix from default

<?php
/*****************************************************************************
 * Copyright (C) 2014-2018
 *   
 * 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 SIMULATOR LIST command
 */
$answer = send_cmd("SIMULATOR LIST");
$arr = explode("\r\n", $answer);


if (isset($_GET['action'])) {
    switch ($_GET['action']) {
	case 'edit':            simulator_edit();
				break;
	default:                break;
    }
} elseif (isset($_POST['action'])) {
    switch ($_POST['action']) {
	case 'testdata':        testdata();
				break;
	default:                break;
    }
} else {
    simulator_list();
}

exit;

/****************************************************************************
 *
 */


/*
 * Simulator add
 *
 * @param string $_POST['Name'] The simulator name
 */
function simulator_add() {
    if ($_POST['key'] == 'Add')
	send_cmd("SIMULATOR ADD ".$_POST['Name']);                                    
    unset($_POST['UUID']);
    unset($_POST['Name']);
    unset($_POST['key']);
    unset($_POST['command']);
    load('simulator.php');
}



/*
 * Simulator update
 *
 * @param string $_POST['UUID'] The simulator UUID
 * @param string $_POST['Name'] The simulator name
 * @param string $_POST['VolumeAir'] The simulator Air volume
 * @param string $_POST['VolumeBeer'] The simulator Beer volume
 * @param string $_POST['RoomTemperature'] The simulator room temp
 * @param string $_POST['RoomHumidity'] The simulator room humidity
 * @param string $_POST['CoolerTemp'] The simulator cold temp
 * @param string $_POST['CoolerTime'] The simulator time to reach this
 * @param string $_POST['CoolerSize'] The simulator plate size
 * @param string $_POST['HeaterTemp'] The simulator heater temp
 * @param string $_POST['HeaterTime'] The simulator time to reach this
 * @param string $_POST['HeaterSize'] The simulator plate size
 * @param string $_POST['FrigoIsolation'] The simulator isolation
 * @param string $_POST['key'] The button pressed.
 */
function simulator_update() {
    /*
     * Build the update command
     */
    if ($_POST['key'] == 'Delete') {
	send_cmd("SIMULATOR DEL ".$_POST['UUID']);
    }

    if ($_POST['key'] == 'Save') {
	$cmd = array("SIMULATOR PUT ".$_POST['UUID']);
	$cmd[] = "NAME,".$_POST['Name'];
	$cmd[] = "VOLUME_AIR,".$_POST['VolumeAir'];
	$cmd[] = "VOLUME_BEER,".$_POST['VolumeBeer'];
	$cmd[] = "ROOM_TEMPERATURE,".$_POST['RoomTemperature'];
	$cmd[] = "ROOM_HUMIDITY,".$_POST['RoomHumidity'];
	$cmd[] = "COOLER_TEMP,".$_POST['CoolerTemp'];
	$cmd[] = "COOLER_TIME,".$_POST['CoolerTime'];
	$cmd[] = "COOLER_SIZE,".$_POST['CoolerSize'];
	$cmd[] = "HEATER_TEMP,".$_POST['HeaterTemp'];
	$cmd[] = "HEATER_TIME,".$_POST['HeaterTime'];
	$cmd[] = "HEATER_SIZE,".$_POST['HeaterSize'];
	$cmd[] = "FRIGO_ISOLATION,".$_POST['FrigoIsolation'];
	$cmd[] = ".";
	send_array($cmd);
    }

    unset($_POST['UUID']);
    unset($_POST['Name']);
    unset($_POST['VolumeAir']);
    unset($_POST['VolumeBeer']);
    unset($_POST['RoomTemperature']);
    unset($_POST['RoomHumidity']);
    unset($_POST['CoolerTemp']);
    unset($_POST['CoolerTime']);
    unset($_POST['CoolerSize']);
    unset($_POST['HeaterTemp']);
    unset($_POST['HeaterTime']);
    unset($_POST['HeaterSize']);
    unset($_POST['FrigoIsolation']);
    unset($_POST['key']);
    unset($_POST['command']);
    load('simulator.php');
}



/*
 * Test input of a modified or new simulator.
 *
 * @param string $_POST['UUID'] Unique record UUID
 * @param string $_POST['Name'] The simulator name
 * @param string $_POST['VolumeAir'] The simulator Air volume
 * @param string $_POST['VolumeBeer'] The simulator Beer volume
 * @param string $_POST['RoomTemperature'] The simulator room temp
 * @param string $_POST['RoomHumidity'] The simulator room humidity
 * @param string $_POST['CoolerTemp'] The simulator cold temp
 * @param string $_POST['CoolerTime'] The simulator time to reach this
 * @param string $_POST['CoolerSize'] The simulator plate size
 * @param string $_POST['HeaterTemp'] The simulator heater temp
 * @param string $_POST['HeaterTime'] The simulator time to reach this
 * @param string $_POST['HeaterSize'] The simulator plate size
 * @param string $_POST['FrigoIsolation'] The simulator isolation
 * @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 too short
 *         3 = Name already in use
 *        99 = Cancel key
 */
function test_thedata() {

    global $arr;

    if (isset($_POST['UUID']) && isset($_POST['Name']) && isset($_POST['VolumeAir']) && isset($_POST['VolumeBeer']) && 
	isset($_POST['RoomTemperature']) && isset($_POST['RoomHumidity']) && isset($_POST['CoolerTemp']) && isset($_POST['CoolerTime']) &&
	isset($_POST['CoolerSize']) && isset($_POST['HeaterTemp']) && isset($_POST['HeaterTime']) && isset($_POST['HeaterSize']) && 
	isset($_POST['FrigoIsolation']) && 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']) && ($f[1] == $_POST['Name'])) {
		    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') {
		    simulator_add();
		    return;
		} else if ($_POST['command'] == 'update') {
		    simulator_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('simulator.php');
		break;
    }

    if ($_POST['command'] == 'add') {
	$heading = 'ThermFerm - Add Simulator';
    } else {
	$heading = 'ThermFerm - Edit Simulator';
    }

    edit_screen($_POST['UUID'], $_POST['command'], $heading, $error);
}



/*
 * Simulator edit screen. Used by simulator_edit(), simulator_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) {

    $answer = send_cmd("SIMULATOR 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="simulator.php">'.PHP_EOL;
    $outstr .= '      <table class="editor">'.PHP_EOL;

    if (startsWith($reply[0], "213")) {
	$j = 1;
	while (1) {
	    if (strcmp($reply[$j], ".") == 0)
		break;
	    $f = explode(",", $reply[$j]);
	    if ($f[0] == "NAME") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">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_AIR") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Volume Air</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="VolumeAir" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;                  
	    }
	    if ($f[0] == "VOLUME_BEER") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Volume Beer</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="VolumeBeer" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "ROOM_TEMPERATURE") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Room Temperature</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="RoomTemperature" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "ROOM_HUMIDITY") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Room Humidity</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="RoomHumidity" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "COOLER_TEMP") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Cooler Temperature</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="CoolerTemp" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "COOLER_TIME") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Cooler Time</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="CoolerTime" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "COOLER_SIZE") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Cooler Size</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="CoolerSize" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "HEATER_TEMP") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Heater Temperature</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="HeaterTemp" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "HEATER_TIME") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Heater Time</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="HeaterTime" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "HEATER_SIZE") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Heater Size</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="HeaterSize" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "FRIGO_ISOLATION") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Frigo Isolation</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="FrigoIsolation" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    $j++;
	}
    }

    $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 .= '    <script type="text/javascript">'.PHP_EOL;
    $outstr .= '     $(document).ready(function () {'.PHP_EOL;
    $outstr .= '      $("#maintenance").jqxButton({ width: 150, height: 25, theme: \'ui-redmond\' });'.PHP_EOL;
    $outstr .= '     });'.PHP_EOL;
    $outstr .= '    </script>'.PHP_EOL;
    $outstr .= build_footer();
    echo $outstr;
}



/*
 * Edit a Simulator. 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 simulator_edit() {
    if ($_GET['action'] == 'edit') {
	edit_screen($_GET['UUID'], 'update', 'ThermFerm - Edit Simulator', '');
	return;
    } else {
	load('simulator.php');
    }
}



/*
 * @link edit simulator
 * @link add simulator
 */
function simulator_list() {
    global $arr;

    $outstr  = build_header("ThermFerm - Simulator Setup");
    $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: 35px;">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"><a href="simulator.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="simulator.php">'.PHP_EOL;
    $outstr .= '      <table class="editor">'.PHP_EOL;
    $outstr .= '      <tr class="trhead"><td colspan="3">Add new Simulator</td></tr>'.PHP_EOL;
    $outstr .= '       <tr class="editor">'.PHP_EOL;
    $outstr .= '        <td class="editname">Simulator 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 .= '<input type="hidden" value="150" name="VolumeAir">';
    $outstr .= '<input type="hidden" value="50" name="VolumeBeer">';
    $outstr .= '<input type="hidden" value="20.0" name="RoomTemperature">';
    $outstr .= '<input type="hidden" value="48.5" name="RoomHumidity">';
    $outstr .= '<input type="hidden" value="-3.0" name="CoolerTemp">';
    $outstr .= '<input type="hidden" value="720" name="CoolerTime">';
    $outstr .= '<input type="hidden" value="0.8" name="CoolerSize">';
    $outstr .= '<input type="hidden" value="150" name="HeaterTemp">';
    $outstr .= '<input type="hidden" value="12" name="HeaterTime">';
    $outstr .= '<input type="hidden" value="0.01" name="HeaterSize">';
    $outstr .= '<input type="hidden" value="0.04" name="FrigoIsolation">';
    $outstr .= '       </tr>'.PHP_EOL;
    $outstr .= '      </table>'.PHP_EOL;
    $outstr .= '     </form>'.PHP_EOL;
    $outstr .= '    </div> <!-- atable -->'.PHP_EOL;

    $outstr .= '    <script type="text/javascript">'.PHP_EOL;
    $outstr .= '     $(document).ready(function () {'.PHP_EOL;
    $outstr .= '      $("#maintenance").jqxButton({ width: 150, height: 25, theme: \'ui-redmond\' });'.PHP_EOL;
    $outstr .= '     });'.PHP_EOL;
    $outstr .= '    </script>'.PHP_EOL;
    $outstr .= build_footer();

    echo $outstr;
}

mercurial