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); - } -} - -