www-thermferm/units.php

changeset 196
4d7a96c5d1ff
parent 175
b73490398368
child 243
ab75ff8e26e1
--- 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);

mercurial