www-thermferm/global.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 534
92b546d4a839
child 693
3518c07737d8
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');


if (isset($_POST['action'])) {
    if ($_POST['action'] == "testdata")
	testdata();
} else {
    edit_screen("");
}


exit;


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


function global_update() {

    if ($_POST['key'] == 'Save') {
	$cmd = array("GLOBAL PUT");
	$cmd[] = "NAME,".$_POST['Name'];
	$cmd[] = "PORT,".$_POST['Port'];
	$cmd[] = "TEMP_ADDRESS,".$_POST['TempAddress'];
	$cmd[] = "HUM_ADDRESS,".$_POST['HumAddress'];
	$cmd[] = "TEMP_HUM_IDX,".$_POST['TempHumIdx'];
	if (isset($_POST['LCDcols']))
	    $cmd[] = "LCD_COLS,".$_POST['LCDcols'];
	if (isset($_POST['LCDrows']))
		$cmd[] = "LCD_ROWS,".$_POST['LCDrows'];
	$cmd[] = "MQTT_HOST,".$_POST['MQTThost'];
	$cmd[] = "MQTT_POST,".$_POST['MQTTport'];
	$cmd[] = "MQTT_USER,".$_POST['MQTTuser'];
	$cmd[] = "MQTT_PASS,".$_POST['MQTTpass'];
	$cmd[] = ".";
	send_array($cmd);
    }

    unset($_POST['Name']);
    unset($_POST['Port']);
    unset($_POST['TempAddress']);
    unset($_POST['HumAddress']);
    unset($_POST['TempHumIdx']);
    unset($_POST['LCDcols']);
    unset($_POST['LCDrows']);
    unset($_POST['MQTThost']);
    unset($_POST['MQTTport']);
    unset($_POST['MQTTuser']);
    unset($_POST['MQTTpass']);
    unset($_POST['key']);
    load('maintenance.php');
}



function test_thedata() {

    if (isset($_POST['Name']) && isset($_POST['Port']) &&
	isset($_POST['TempAddress']) && isset($_POST['HumAddress']) &&
	isset($_POST['TempHumIdx']) && isset($_POST['key']) &&
	isset($_POST['MQTThost']) && isset($_POST['MQTTport']) &&
	isset($_POST['MQTTuser']) && isset($_POST['MQTTpass'])) {

	if ($_POST['key'] == 'Cancel')
	    return 99;

	if (isset($_POST['LCDcols']) && (($_POST['LCDcols'] != 16) && ($_POST['LCDcols'] != 20)))
	    return 2;

	if (isset($_POST['LCDrows']) && (($_POST['LCDrows'] != 2) && ($_POST['LCDrows'] != 4)))
	    return 3;

    } else {
	return 1;
    }

    return 0;
}



function testdata()
{
    $result = test_thedata();
    $error = '';

    switch ($result) {
	case 0:	global_update();
		return;
		break;
	case 1:	$error = 'Missing data';
		break;
	case 2: $error = 'LCD columns must be 16 or 20';
		break;
	case 3: $error = 'LCD rows must be 2 or 4';
		break;
	case 99:
		load('maintenance.php');
		break;
    }

    edit_screen($error);
}



function edit_screen($error_message)
{
    /*
     * Get list of devices, we need it later
     */
    $answer = send_cmd("DEVICE LIST");
    $devices = explode("\r\n", $answer);

    /*
     * Get current global data
     */
    $answer = send_cmd("GLOBAL GET");
    $reply = explode("\r\n", $answer);

    $outstr  = build_header("ThermFerm - Global Setup");
    $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="global.php">'.PHP_EOL;
    $outstr .= '      <table class="editor">'.PHP_EOL;
    $outstr .= '      <tr class="trhead">'.PHP_EOL;
    $outstr .= '       <td class="setup" colspan="2" style="text-align: center; height: 20px; padding-top: 5px;">Global Setup</td>'.PHP_EOL;
    $outstr .= '      </tr>'.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">System 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] == "PORT") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Telnet port</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="Port" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "TEMP_ADDRESS") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Room temperature sensor</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><select name="TempAddress">'.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] == "HUM_ADDRESS") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Room humidity sensor</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><select name="HumAddress">'.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] == "TEMP_HUM_IDX") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">Domoticz TH index</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="TempHumIdx" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "LCD_COLS") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">LCD columns</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="LCDcols" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "LCD_ROWS") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">LCD rows</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="LCDrows" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "MQTT_HOST") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">MQTT host</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="MQTThost" size="50" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "MQTT_PORT") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">MQTT port</td>'.PHP_EOL;
		$outstr .= '        <td class="editfield"><input type="text" name="MQTTport" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "MQTT_USER") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">MQTT username</td>'.PHP_EOL;
		if (strcmp($f[1], "(null)") == 0) {
		    $f[1] = "";
		}
		$outstr .= '        <td class="editfield"><input type="text" name="MQTTuser" size="50" value="'.$f[1].'"></td>'.PHP_EOL;
		$outstr .= '       </tr>'.PHP_EOL;
	    }
	    if ($f[0] == "MQTT_PASS") {
		$outstr .= '       <tr class="editor">'.PHP_EOL;
		$outstr .= '        <td class="editname">MQTT password</td>'.PHP_EOL;
		if (strcmp($f[1], "(null)") == 0) {
		    $f[1] = "";
		}
		$outstr .= '        <td class="editfield"><input type="text" name="MQTTpass" size="50" 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="hidden" value="testdata" name="action">';
    $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;
}

mercurial