# HG changeset patch # User Michiel Broek # Date 1407576542 -7200 # Node ID 4d7a96c5d1ff24e98c50a00b86dcf4f3888d355c # Parent b34a1b2421fb788884e6dc7df7c5cc7de6d9532d Code cleanup diff -r b34a1b2421fb -r 4d7a96c5d1ff www-thermferm/devices.php --- a/www-thermferm/devices.php Fri Aug 08 23:07:44 2014 +0200 +++ b/www-thermferm/devices.php Sat Aug 09 11:29:02 2014 +0200 @@ -23,26 +23,10 @@ require_once('utilities.php'); - -$sock = open_socket(); -if ($sock == false) { - echo ""; - return; -} - -socket_write($sock, "DEVICE LIST", 4096); -$answer = ""; -while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - $answer .= $line; -} -socket_close($sock); - /* * $arr contains the complete reply of the LIST BUS command */ +$answer = send_cmd("DEVICE LIST"); $arr = explode("\r\n", $answer); @@ -124,17 +108,7 @@ * Build the update command */ if ($_POST['key'] == 'Delete') { - $sock = open_socket(); - if ($sock != false) { - socket_write($sock, "DEVICE DEL ".$_POST['UUID'], 4096); - /* Absorb response */ - while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - } - socket_close($sock); - } + send_cmd("DEVICE DEL ".$_POST['UUID']); } if ($_POST['key'] == 'Save') { @@ -310,20 +284,7 @@ */ function edit_screen($UUID, $command, $heading, $error_message) { - $sock = open_socket(); - if ($sock == false) { - load('devices.php'); - } - - socket_write($sock, "DEVICE GET ".$UUID, 4096); - $answer = ""; - while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - $answer .= $line; - } - socket_close($sock); + $answer = send_cmd("DEVICE GET ".$UUID); $reply = explode("\r\n", $answer); $outstr = build_header($heading); diff -r b34a1b2421fb -r 4d7a96c5d1ff www-thermferm/getlog.php --- a/www-thermferm/getlog.php Fri Aug 08 23:07:44 2014 +0200 +++ b/www-thermferm/getlog.php Sat Aug 09 11:29:02 2014 +0200 @@ -24,21 +24,7 @@ require_once('utilities.php'); -$sock = open_socket(); -if ($sock == false) { - echo ""; - return; -} - -socket_write($sock, "LIST LOG ".$_GET["unit"], 4096); -$answer = ""; -while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - $answer .= $line; -} -socket_close($sock); +$answer = send_cmd("LIST LOG ".$_GET["unit"]); $arr = explode("\r\n", $answer); diff -r b34a1b2421fb -r 4d7a96c5d1ff www-thermferm/getstate.php --- a/www-thermferm/getstate.php Fri Aug 08 23:07:44 2014 +0200 +++ b/www-thermferm/getstate.php Sat Aug 09 11:29:02 2014 +0200 @@ -28,22 +28,7 @@ else $uuid = "0e261929-486d-4117-897a-2b23509157c6"; -$sock = open_socket(); -if ($sock == false) { - echo ""; - return; -} - -socket_write($sock, 'UNIT GET '.$uuid, 4096); -$answer = ""; -while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - $answer .= $line; -} -socket_close($sock); - +$answer = send_cmd('UNIT GET '.$uuid); $arr = explode("\r\n", $answer); $air_temperature = "NA"; diff -r b34a1b2421fb -r 4d7a96c5d1ff www-thermferm/liveview.php --- a/www-thermferm/liveview.php Fri Aug 08 23:07:44 2014 +0200 +++ b/www-thermferm/liveview.php Sat Aug 09 11:29:02 2014 +0200 @@ -32,21 +32,7 @@ $outstr = ''; $air_state = $beer_state = 1; - - $sock = open_socket(); - if ($sock == false) { - return ''; - } - - socket_write($sock, "UNIT GET ".$unit, 4096); - $answer = ""; - while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - $answer .= $line; - } - socket_close($sock); + $answer = send_cmd("UNIT GET ".$unit); $arr = explode("\r\n", $answer); if (startsWith($arr[0], "213")) { @@ -235,19 +221,8 @@ /* * First, load a list with available profiles. */ - $sock = open_socket(); - if ($sock == true) { - socket_write($sock, "PROFILE LIST", 4096); - $answer = ""; - while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - $answer .= $line; - } - socket_close($sock); - $reply = explode("\r\n", $answer); - } + $answer = send_cmd("PROFILE LIST"); + $reply = explode("\r\n", $answer); /* * Show loaded profile. @@ -379,21 +354,8 @@ { $outstr = PHP_EOL; - $sock = open_socket(); - if ($sock !== false) { - socket_write($sock, "LIST", 4096); - - /* - * Multiple reads until the remote closed the connection - */ - $answer = ""; - while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - $answer .= $line; - } - socket_close($sock); + $answer = send_cmd("LIST"); + if (strlen($answer)) { $arr = explode("\r\n", $answer); $outstr .= '
'.PHP_EOL; diff -r b34a1b2421fb -r 4d7a96c5d1ff www-thermferm/profiles.php --- a/www-thermferm/profiles.php Fri Aug 08 23:07:44 2014 +0200 +++ b/www-thermferm/profiles.php Sat Aug 09 11:29:02 2014 +0200 @@ -25,24 +25,9 @@ /* * Each time this page is loaded, get the profiles from the server. - */ -$sock = open_socket(); -if ($sock == false) { - load('index.php'); -} - -socket_write($sock, "PROFILE LIST", 4096); -$answer = ""; -while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - $answer .= $line; -} -socket_close($sock); -/* * $arr contains the complete reply of he PROFILE LIST command. */ +$answer = send_cmd("PROFILE LIST"); $arr = explode("\r\n", $answer); @@ -93,20 +78,7 @@ 8 => array("steptime" => 0, "resttime" => 1, "target" => 20.0 ), ); - $sock = open_socket(); - if ($sock == false) { - load('profiles.php'); - } - - socket_write($sock, "PROFILE GETS ".$UUID, 4096); - $answer = ""; - while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - $answer .= $line; - } - socket_close($sock); + $answer = send_cmd("PROFILE GETS ".$UUID); $psteps = explode("\r\n", $answer); if (startsWith($arr[0], "212")) { @@ -185,22 +157,7 @@ function profile_add() { if ($_POST['key'] == 'Add') { - - $cmd = "PROFILE ADD ".$_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); - } + send_cmd("PROFILE ADD ".$_POST['Name']); } unset($_POST['UUID']); unset($_POST['Name']); @@ -225,17 +182,7 @@ * Build the update command */ if ($_POST['key'] == 'Delete') { - $sock = open_socket(); - if ($sock != false) { - socket_write($sock, "PROFILE DEL ".$_POST['UUID'], 4096); - /* Absorb response */ - while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - } - socket_close($sock); - } + send_cmd("PROFILE DEL ".$_POST['UUID']); } @@ -290,7 +237,7 @@ global $arr; - print_r($_POST); +// print_r($_POST); for ($i = 1; $i <= 8; $i++) { if ((! isset($_POST['steptime'.$i])) || (! isset($_POST['resttime'.$i])) || (! isset($_POST['target'.$i]))) @@ -491,21 +438,7 @@ /* * Get current profile data */ - $sock = open_socket(); - if ($sock == false) { - load("profiles.php"); - return; - } - - socket_write($sock, "PROFILE GET ".$UUID, 4096); - $answer = ""; - while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - $answer .= $line; - } - socket_close($sock); + $answer = send_cmd("PROFILE GET ".$UUID); $reply = explode("\r\n", $answer); $outstr = build_header($heading); diff -r b34a1b2421fb -r 4d7a96c5d1ff www-thermferm/units.php --- a/www-thermferm/units.php Fri Aug 08 23:07:44 2014 +0200 +++ b/www-thermferm/units.php Sat Aug 09 11:29:02 2014 +0200 @@ -24,24 +24,10 @@ require_once('utilities.php'); -$sock = open_socket(); -if ($sock == false) { - load('index.php'); -} - -socket_write($sock, "UNIT LIST", 4096); -$answer = ""; -while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - $answer .= $line; -} -socket_close($sock); - /* * $arr contains the complete reply of the LIST command. */ +$answer = send_cmd("UNIT LIST"); $arr = explode("\r\n", $answer); @@ -75,22 +61,7 @@ function unit_add() { if ($_POST['key'] == 'Add') { - - $cmd = "UNIT ADD ".$_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); - } + send_cmd("UNIT ADD ".$_POST['Name']); } unset($_POST['UUID']); @@ -114,17 +85,7 @@ * Build the update command */ if ($_POST['key'] == 'Delete') { - $sock = open_socket(); - if ($sock != false) { - socket_write($sock, "UNIT DEL ".$_POST['UUID'], 4096); - /* Absorb response */ - while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - } - socket_close($sock); - } + send_cmd("UNIT DEL ".$_POST['UUID']); } if ($_POST['key'] == 'Save') { @@ -283,41 +244,13 @@ /* * Get list of devices, we need it later */ - $sock = open_socket(); - if ($sock == false) { - load("units.php"); - return; - } - - socket_write($sock, "DEVICE LIST", 4096); - $answer = ""; - while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - $answer .= $line; - } - socket_close($sock); + $answer = send_cmd("DEVICE LIST"); $devices = explode("\r\n", $answer); /* * Get current unit data */ - $sock = open_socket(); - if ($sock == false) { - load("units.php"); - return; - } - - socket_write($sock, "UNIT GET ".$UUID, 4096); - $answer = ""; - while (1) { - $line = socket_read($sock, 4096); - if ($line === '') - break; - $answer .= $line; - } - socket_close($sock); + $answer = send_cmd("UNIT GET ".$UUID); $reply = explode("\r\n", $answer); $outstr = build_header($heading); diff -r b34a1b2421fb -r 4d7a96c5d1ff www-thermferm/utilities.php --- a/www-thermferm/utilities.php Fri Aug 08 23:07:44 2014 +0200 +++ b/www-thermferm/utilities.php Sat Aug 09 11:29:02 2014 +0200 @@ -27,7 +27,6 @@ $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if (!($sock === false)) { -// if (socket_connect($sock, "rpi02", 6554)) { if (socket_connect($sock, "localhost", 6554)) { socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 15, 'usec' => 0)); } else { @@ -39,6 +38,50 @@ +/* + * @param string $command to send to the server. + * Return: string with the complete reply from the + * server. This can be a multiline reply. + */ +function send_cmd($command) +{ + $sock = open_socket(); + if ($sock == false) { + return ""; + } + socket_write($sock, $command, 4096); + + $answer = ""; + while (1) { + $line = socket_read($sock, 4096); + if ($line === '') + break; + $answer .= $line; + } + socket_close($sock); + + return $answer; +} + + + +/* + * @param string $command to send to the server. + * Return: 0 = Ok. + * 1 = Server responden with an error. + */ +function send_cmd_check($command) +{ + $answer = send_cmd($command); + + if (strlen($answer) && (($answer[0] == '1') || ($answer[0] == '2'))) + return 0; + + return 1; +} + + + function startsWith($haystack, $needle) { return !strncmp($haystack, $needle, strlen($needle)); @@ -95,23 +138,3 @@ } -/* - * Send command - */ -function send_cmd($cmd) { - $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); - } -} - -