More tests on http chuncked transfer
authorNirgal <contact_ampy@nirgal.com>
Sun, 13 Oct 2019 11:07:05 +0000 (11:07 +0000)
committerNirgal <contact_ampy@nirgal.com>
Sun, 13 Oct 2019 11:07:05 +0000 (11:07 +0000)
README.rst
html/currentjson_chunked.php
html/index2.php [deleted file]
html/test_chuncked.php [new file with mode: 0644]

index 5bddf100c65bc8b00f137d743494e07caeb8dc91..fd1f1c201c2ca3148e9e947bf0dc0f43d1a46166 100644 (file)
@@ -31,6 +31,7 @@ Dependencies
 
 * apache2
 * libapache2-mod-php | php-cgi | php-fpm
+* libjs-jquery
 * php-rrd
 * python3-pyaudio
 * python3-rrdtool
index a860df51b596268dc1bb6083d1189a5c94c6c837..41bfb1e1f9286d3c8dece39f9fe05ac4c2d3f1d0 100644 (file)
@@ -3,10 +3,15 @@
  * Experimental version that return chunked http response.
  *
  * Pulling the values is no longer needed as the one request will output
- * the updates every 10 seconds.
+ * the updates every second.
  */
 require_once('config.php');
 
+$speed=@(int)$_GET['speed'];
+if ($speed <= 0)
+       $speed=1;  // seconds
+
+
 header("Content-Type: text/json");
 header("Transfer-Encoding: chunked");
 flush();
@@ -23,6 +28,8 @@ function send_chunk($chunk)
 }
 
 while (True) {
+       exec("/usr/bin/rrdtool flushcached --daemon ".RRDSOCK." ".RRDFILE);
+
        $info=rrd_lastupdate(RRDFILE);
        if ($info === FALSE)
                $result=[
@@ -33,9 +40,9 @@ while (True) {
        else
                $result=[
                        'last_update' => $info['last_update'],
-                       'watts' => $info['data'][0]
+                       'watts' => (float)$info['data'][0],
                ];
        send_chunk(json_encode($result));
-       sleep(10);
+       sleep($speed);
 }
 ?>
diff --git a/html/index2.php b/html/index2.php
deleted file mode 100644 (file)
index 9134f27..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-<!DOCTYPE html>
-<meta charset="UTF-8">
-<script src='/javascript/jquery/jquery.js'></script>
-
-<hr>
-<span id=debugg>
-</span>
-<hr>
-
-Puissance instannĂ©e: <span id=currentpower>Unknown</span> watts<br>
-
-<script>
-xhr = new XMLHttpRequest();
-xhr.onreadystatechange = function() {
-       document.getElementById('debugg').innerHTML += ""+xhr.readyState+"<br>";
-       document.getElementById('debugg').innerHTML += ""+xhr.status+" "+xhr.statusText+"<br>";
-       document.getElementById('debugg').innerHTML += ""+xhr.responseText+"<br>";
-       this.response = "";
-}
-xhr.open('GET', 'currentjson_chunked.php', true /*async*/);
-xhr.send(null);
-
-//autorefresh_power = 1;
-//function refresh_power() {
-//     $.ajax({
-//             url : 'currentjson_chunked.php',
-//             success : function(result) {
-//                     document.getElementById('debugg').innerHTML = document.getElementById('debugg').innerHTML + "success<br>";
-//                     alert(result);
-//                     document.getElementById('currentpower').innerHTML = result['watts'];
-//             },
-//             complete : function(textStatus) {
-//                     document.getElementById('debugg').innerHTML = document.getElementById('debugg').innerHTML + "complete<br>";
-//             //      if (autorefresh_power)
-//             //              setTimeout("refresh_power()", 1000);
-//             }
-//     });
-//}
-//refresh_power();
-</script>
-
-
-<img src="graph.php">
diff --git a/html/test_chuncked.php b/html/test_chuncked.php
new file mode 100644 (file)
index 0000000..4a65e9a
--- /dev/null
@@ -0,0 +1,57 @@
+<!DOCTYPE html>
+<meta charset="UTF-8">
+
+<fieldset>
+  <legend>debug messages</legend>
+  <span id=debugg></span>
+</fieldset>
+
+Puissance instannĂ©e: <span id=currentpower>Unknown</span> watts<br>
+
+<script>
+function debug(str) {
+       document.getElementById('debugg').innerHTML += "" + str + "<br>";
+}
+xhr = new XMLHttpRequest();
+xhr.onreadystatechange = function() {
+       switch (xhr.readyState) {
+       case 0: /* unsent */
+       case 1: /* opened */
+       case 2: /* header received */
+               return;
+       case 3:
+               switch(xhr.status) {
+               case 200:
+                       response = xhr.responseText;
+                       //debug("responseText:"+response);
+                       new_text = response.substring(xhr.ampy_len);
+                       xhr.ampy_len = response.length;
+                       //debug("new_text: "+new_text);
+
+                       try {
+                               data = JSON.parse(new_text);
+                       } catch(SyntaxError) {
+                               debug("Error decoding json: "+new_text);
+                               break;
+                       }
+                       document.getElementById('currentpower').innerHTML = data.watts.toFixed(1);
+                       break;
+               default:
+                       debug("XMLHttpRequest.status=="+xhr.status+" ("+xhr.statusText+")");
+                       break;
+               }
+               break;
+       case 4: /* done */
+               debug("XMLHttpRequest.readyState==4. Connection was lost.");
+               /* TODO: reconnect */
+               break;
+       }
+}
+xhr.ampy_len=0;
+xhr.open('GET', 'currentjson_chunked.php', true /*async*/);
+xhr.send(null);
+
+</script>
+
+
+<!--img src="graph.php"-->