* This scripts updates the "counter" file based on average consumption and
* last known value.
*
+ * This should optimize computations.
+ *
* It is not used!
*/
include("html/config.php");
+include("html/ampy.inc.php");
-exec("/usr/bin/rrdtool flushcached --daemon ".RRDSOCK." ".RRDFILE);
-
-$fd = fopen(COUNTERFILE, 'r');
-$mtime = fstat($fd)['mtime'];
-$counter = (double)fread($fd, 128);
-fclose($fd);
-echo("oldcounter=$counter\n");
-
-$params = [
- '--daemon', RRDSOCK,
- '-s', $mtime,
- 'DEF:watts='.RRDFILE.':watts:AVERAGE',
- 'VDEF:avg=watts,AVERAGE',
- 'PRINT:avg:%lf',
-];
-$info=rrd_graph( '-', $params);
-$watts_since_counter = (double)$info['calcpr'][0];
-echo("watts_since_counter=$watts_since_counter\n");
-echo('hours_since_counter='.((time() - $mtime) / 3600.)."\n");
-$wh_since_counter = $watts_since_counter * (time() - $mtime) / 3600.;
-echo("wh_since_counter=$wh_since_counter\n");
-$newcounter = $counter + $wh_since_counter / 1000.;
-echo('newcounter='.$newcounter." kWh\n");
+ampy_flush_rrd_daemon();
-file_put_contents(COUNTERFILE.'.new', $newcounter);
-rename(COUNTERFILE.'.new', COUNTERFILE);
+$new_counter = ampy_get_counter();
+ampy_set_counter($new_counter);
?>