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