More tests on http chuncked transfer
[ampy.git] / html / currentjson_chunked.php
index 34f5c8b059c54ae85abc2af84298de8e8ba38ea3..41bfb1e1f9286d3c8dece39f9fe05ac4c2d3f1d0 100644 (file)
@@ -1,5 +1,16 @@
 <?php
-$RRDFILE="/home/nirgal/ampy/power.rrd";
+/*
+ * Experimental version that return chunked http response.
+ *
+ * Pulling the values is no longer needed as the one request will output
+ * 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");
@@ -17,18 +28,21 @@ function send_chunk($chunk)
 }
 
 while (True) {
-       $info=rrd_lastupdate($RRDFILE);
+       exec("/usr/bin/rrdtool flushcached --daemon ".RRDSOCK." ".RRDFILE);
+
+       $info=rrd_lastupdate(RRDFILE);
        if ($info === FALSE)
                $result=[
                        'last_update' => 'unknown',
-                       'watts' => 'unknown'
+                       'watts' => 'unknown',
+                       'error' => rrd_error(),
                ];
        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);
 }
 ?>