Clean up duplicate php code to inc file
[ampy.git] / countercron
index 6a5ccb59fec6234bc875c2b6da7e10111123a7e5..635f0119218eb002f59bb8d3af2e86f608ba8283 100755 (executable)
@@ -4,34 +4,15 @@
  * 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);
 ?>