Enabled HTTP chunked transfer encoding
[ampy.git] / html / currentjson_chunked.php
index 34f5c8b059c54ae85abc2af84298de8e8ba38ea3..7772479e7ffc7ebec4dd384c2842e24fdab9fb15 100644 (file)
@@ -1,5 +1,17 @@
 <?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');
+require_once("ampy.inc.php");
+
+$speed=@(int)$_GET['speed'];
+if ($speed <= 0)
+       $speed=1;  // seconds
+
 
 header("Content-Type: text/json");
 header("Transfer-Encoding: chunked");
@@ -10,25 +22,30 @@ flush();
 function send_chunk($chunk)
 {
        // The chunk must fill the output buffer or php won't send it
-       $chunk = str_pad($chunk, 4096);
+       //$chunk = str_pad($chunk, 4096);
 
        printf("%x\r\n%s\r\n", strlen($chunk), $chunk);
        flush();
+       ob_flush();
 }
 
 while (True) {
-       $info=rrd_lastupdate($RRDFILE);
+       ampy_flush_rrd_daemon();
+
+       $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],
                ];
+       $result['counter'] = ampy_get_counter();
        send_chunk(json_encode($result));
-       sleep(10);
+       sleep($speed);
 }
 ?>