<?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");
}
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);
}
?>