Pre-release demo version
[ampy.git] / html / ampy.php
1 <?php
2 $RRDFILE='/home/nirgal/ampy/power.rrd';
3 $COUNTERFILE='/home/nirgal/ampy/counter';
4
5 function get_counter() {
6         $fd = fopen($COUNTERFILE, 'r');
7         $mtime = fstat($fd)['mtime'];
8         $counter = (double)fread($fd, 128) * 1000;
9         fclose($fd);
10         #echo($mtime.'<br>');
11         #echo('counter='.$counter.'<br>');
12         
13         $info=rrd_graph( '-', ['-s', $mtime, 'DEF:watts='.$RRDFILE.':watts:AVERAGE', 'VDEF:avg=watts,AVERAGE', 'PRINT:avg:%lf']);
14         #print_r($info);
15         $watts_since_counter = (double)$info['calcpr'][0];
16         #echo('watts_since_counter='.$watts_since_counter.'<br>');
17         #echo('hours_since_counter='.((time() - $mtime) / 3600.).'<br>');
18         $wh_since_counter = $watts_since_counter * (time() - $mtime) / 3600.;
19         #echo('wh_since_counter='.$wh_since_counter.'<br>');
20         $counter += $wh_since_counter;
21         #echo('newcounter='.($counter/1000).' kWh<br>');
22
23         return $counter/1000.;
24 }
25 ?>