Documentation & cleanup
[ampy.git] / countercron
1 #!/usr/bin/php -f
2 <?php
3 include("html/config.php");
4
5 exec("/usr/bin/rrdtool flushcached --daemon ".RRDSOCK." ".RRDFILE);
6
7 $fd = fopen(COUNTERFILE, 'r');
8 $mtime = fstat($fd)['mtime'];
9 $counter = (double)fread($fd, 128);
10 fclose($fd);
11 echo("oldcounter=$counter\n");
12
13 $params = [
14         '--daemon', RRDSOCK,
15         '-s', $mtime,
16         'DEF:watts='.RRDFILE.':watts:AVERAGE',
17         'VDEF:avg=watts,AVERAGE',
18         'PRINT:avg:%lf',
19 ];
20 $info=rrd_graph( '-', $params);
21 $watts_since_counter = (double)$info['calcpr'][0];
22 echo("watts_since_counter=$watts_since_counter\n");
23 echo('hours_since_counter='.((time() - $mtime) / 3600.)."\n");
24 $wh_since_counter = $watts_since_counter * (time() - $mtime) / 3600.;
25 echo("wh_since_counter=$wh_since_counter\n");
26 $newcounter = $counter + $wh_since_counter / 1000.;
27 echo('newcounter='.$newcounter." kWh\n");
28
29 file_put_contents(COUNTERFILE.'.new', $newcounter);
30 rename(COUNTERFILE.'.new', COUNTERFILE);
31 ?>