Pre-release demo version
[ampy.git] / html / currentjson_chunked.php
1 <?php
2 $RRDFILE="/home/nirgal/ampy/power.rrd";
3
4 header("Content-Type: text/json");
5 header("Transfer-Encoding: chunked");
6 flush();
7
8
9 // Send chunk to browser
10 function send_chunk($chunk)
11 {
12         // The chunk must fill the output buffer or php won't send it
13         $chunk = str_pad($chunk, 4096);
14
15         printf("%x\r\n%s\r\n", strlen($chunk), $chunk);
16         flush();
17 }
18
19 while (True) {
20         $info=rrd_lastupdate($RRDFILE);
21         if ($info === FALSE)
22                 $result=[
23                         'last_update' => 'unknown',
24                         'watts' => 'unknown'
25                 ];
26         else
27                 $result=[
28                         'last_update' => $info['last_update'],
29                         'watts' => $info['data'][0]
30                 ];
31         send_chunk(json_encode($result));
32         sleep(10);
33 }
34 ?>