Ajax screen updates now use a single call to the server

Fri, 01 Aug 2014 21:40:02 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 01 Aug 2014 21:40:02 +0200
changeset 171
15f92e5eecef
parent 170
3cb99272b84b
child 172
f45c291c6331

Ajax screen updates now use a single call to the server

www-thermferm/getdata.php file | annotate | diff | comparison | revisions
www-thermferm/getstate.php file | annotate | diff | comparison | revisions
www-thermferm/liveview.php file | annotate | diff | comparison | revisions
--- a/www-thermferm/getdata.php	Fri Aug 01 16:13:55 2014 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,50 +0,0 @@
-<?php
-/*****************************************************************************
- * Copyright (C) 2014
- *   
- * 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');
-
-$what = $_GET['what'];
-$uuid = $_GET["uuid"];
-
-$sock = open_socket();
-if ($sock == false) {
-    echo "";
-    return;
-}
-
-socket_write($sock, 'GET '.$what.' '.$uuid, 4096);
-$answer = "";
-while (1) {
-    $line = socket_read($sock, 4096);
-    if ($line === '')
-	break;
-    $answer .= $line;
-}
-socket_close($sock);
-
-$arr = explode(",", $answer);
-
-
-echo $arr[1];
-
-?>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/www-thermferm/getstate.php	Fri Aug 01 21:40:02 2014 +0200
@@ -0,0 +1,123 @@
+<?php
+/*****************************************************************************
+ * Copyright (C) 2014
+ *   
+ * 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($_GET["uuid"]))
+	$uuid = $_GET["uuid"];
+else
+	$uuid = "0e261929-486d-4117-897a-2b23509157c6";
+
+$sock = open_socket();
+if ($sock == false) {
+    echo "";
+    return;
+}
+
+socket_write($sock, 'UNIT '.$uuid, 4096);
+$answer = "";
+while (1) {
+    $line = socket_read($sock, 4096);
+    if ($line === '')
+	break;
+    $answer .= $line;
+}
+socket_close($sock);
+
+$sock = open_socket();
+if ($sock == false) {
+    echo "";
+    return;
+}
+
+socket_write($sock, 'LIST UNIT', 4096);
+$answer = "";
+while (1) {
+    $line = socket_read($sock, 4096);
+    if ($line === '')
+	break;
+    $answer .= $line;
+}
+socket_close($sock);
+
+$arr = explode("\r\n", $answer);
+
+$air_temperature = "NA";
+$air_state = "NA";
+$beer_temperature = "NA";
+$beer_state = "NA";
+$target_temperature = "NA";
+$led1 = $led2 = $led3 = $sw1 = $sw2 = $sw3 = 0;
+$fridge_set = "NA";
+$beer_set = "NA";
+
+if (startsWith($arr[0], "213")) {
+    $j = 1;
+    while (1) {
+	if (strcmp($arr[$j], ".") == 0)
+	    break;
+	$f = explode(",", $arr[$j]);
+
+	if ($f[0] == "AIR_STATE")
+		$air_state = $f[1];
+	if (($f[0] == "AIR_TEMPERATURE") && ($air_state == "OK"))
+		$air_temperature = $f[1];
+	if ($f[0] == "BEER_STATE")
+		$beer_state = $f[1];
+	if (($f[0] == "BEER_TEMPERATURE") && ($beer_state == "OK"))
+		$beer_temperature = $f[1];
+	if ($f[0] == "MODE")
+		$mode = $f[1];
+	if ($f[0] == "COOLER_STATE")
+		$led1 = $f[1];
+	if ($f[0] == "HEATER_STATE")
+		$led2 = $f[1];
+	if ($f[0] == "FAN_STATE")
+		$led3 = $f[1];
+	if (($f[0] == "BEER_SET") && ($mode == "BEER"))
+		$target_temperature = $f[1];
+	if (($f[0] == "FRIDGE_SET") && ($mode == "FRIDGE"))
+		$target_temperature = $f[1];
+	if (($f[0] == "PROF_TARGET") && ($mode == "PROFILE"))
+		$target_temperature = $f[1];
+	$j++;
+    }
+}
+
+
+$reply = array (
+    'air_temperature'     => $air_temperature,
+    'beer_temperature'    => $beer_temperature,
+    'target_temperature'  => $target_temperature,
+    'mode'                => $mode,
+    'led1'                => $led1,
+    'led2'                => $led2,
+    'led3'                => $led3,
+    'sw1'                 => $sw1,
+    'sw2'                 => $sw2,
+    'sw3'                 => $sw3
+);
+
+
+echo json_encode($reply);
+?>
--- a/www-thermferm/liveview.php	Fri Aug 01 16:13:55 2014 +0200
+++ b/www-thermferm/liveview.php	Fri Aug 01 21:40:02 2014 +0200
@@ -160,18 +160,13 @@
 	$outstr .= '           }]'.PHP_EOL;
 	$outstr .= '       };'.PHP_EOL;
 	$outstr .= '       $("#fermentor_chart_'.$unit.'").jqxChart(settings'.$unr.');'.PHP_EOL;
-	$outstr .= '       var target_refresh_'.$unr.' = setInterval('.PHP_EOL;
-	$outstr .= '         function () {'.PHP_EOL;
-	$outstr .= '           $("#load_target_'.$unr.'").load("getdata.php?uuid='.$unit.'&what=TARGET").fadeIn("slow");'.PHP_EOL;
-	$outstr .= '         }, 65000);'.PHP_EOL;
-	$outstr .= '       var air_refresh_'.$unr.' = setInterval('.PHP_EOL;
-	$outstr .= '         function () {'.PHP_EOL;
-	$outstr .= '           $("#load_air_'.$unr.'").load("getdata.php?uuid='.$unit.'&what=AIR").fadeIn("slow");'.PHP_EOL;
-	$outstr .= '         }, 10001);'.PHP_EOL;
-	$outstr .= '       var beer_refresh_'.$unr.' = setInterval('.PHP_EOL;
-	$outstr .= '         function () {'.PHP_EOL;
-	$outstr .= '           $("#load_beer_'.$unr.'").load("getdata.php?uuid='.$unit.'&what=BEER").fadeIn("slow");'.PHP_EOL;
-	$outstr .= '         }, 10100);'.PHP_EOL;
+	$outstr .= '       setInterval(function(){'.PHP_EOL;
+	$outstr .= '           $.getJSON("getstate.php?uuid='.$unit.'", function(data) {'.PHP_EOL;
+	$outstr .= '             $("#load_air_'.$unr.'").html(data.air_temperature);'.PHP_EOL;
+	$outstr .= '             $("#load_beer_'.$unr.'").html(data.beer_temperature);'.PHP_EOL;
+	$outstr .= '             $("#load_target_'.$unr.'").html(data.target_temperature);'.PHP_EOL;
+        $outstr .= '           });'.PHP_EOL;
+	$outstr .= '         }, 10000);'.PHP_EOL;
 	$outstr .= '     });'.PHP_EOL;
 	$outstr .= '    </script>'.PHP_EOL;
 	$outstr .= '    <div id="fermentor">'.PHP_EOL;

mercurial