* 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);
?>
--- /dev/null
+<?php
+/*
+ * Ampy helpers
+ */
+
+require_once('config.php');
+
+function ampy_flush_rrd_daemon() {
+ exec("/usr/bin/rrdtool flushcached --daemon ".escapeshellarg(RRDSOCK)." ".escapeshellarg(RRDFILE));
+}
+
+/*
+ * Initialized the counter file with an absolute value.
+ * Note that the file mtime is important.
+ */
+function ampy_set_counter($newcounter) {
+ file_put_contents(COUNTERFILE.'.new', $newcounter);
+ rename(COUNTERFILE.'.new', COUNTERFILE);
+}
+
+/*
+ * Returns an array
+ * 0 => kWh counter value (file content)
+ * 1 => last update (file mtime)
+ */
+function ampy_get_old_counter_info() {
+ $fd = fopen(COUNTERFILE, 'r');
+ $mtime = fstat($fd)['mtime'];
+ $counter = (double)fread($fd, 128);
+ fclose($fd);
+ #echo($mtime.'<br>');
+ #echo('counter='.$counter.'<br>');
+ return [$counter, $mtime];
+}
+
+/*
+ * Returns how many kWh have been spent since a date.
+ * @param mtime seconds since epoch
+ * @return float kW.h
+ */
+function ampy_get_kwh_since($mtime) {
+
+ $params = [
+ '--daemon', RRDSOCK,
+ '-s', $mtime,
+ 'DEF:watts='.RRDFILE.':watts:AVERAGE',
+ 'VDEF:avg=watts,AVERAGE',
+ #'VDEF:last=watts,LAST',
+ 'PRINT:avg:%lf',
+ #'PRINT:last:%lf',
+ ];
+ $info=rrd_graph( '-', $params);
+ #print_r($info);
+ $watts_since_counter = (double)$info['calcpr'][0];
+ #echo('watts_since_counter='.$watts_since_counter.'<br>');
+ #echo('hours_since_counter='.((time() - $mtime) / 3600.).'<br>');
+ $wh_since_counter = $watts_since_counter * (time() - $mtime) / 3600.;
+ #echo('wh_since_counter='.$wh_since_counter.'<br>');
+ return $wh_since_counter / 1000.;
+
+}
+
+function ampy_get_counter() {
+ $counter_mtime = ampy_get_old_counter_info();
+ $counter = $counter_mtime[0];
+ $mtime = $counter_mtime[1];
+
+ $counter += ampy_get_kwh_since($mtime);
+ #echo('newcounter='.($counter/1000).' kWh<br>');
+
+ return $counter;
+}
+?>
+++ /dev/null
-<?php
-/*
- * This file is not used!
- */
-$RRDFILE='/home/nirgal/ampy/power.rrd';
-$COUNTERFILE='/home/nirgal/ampy/counter';
-
-function get_counter() {
- $fd = fopen($COUNTERFILE, 'r');
- $mtime = fstat($fd)['mtime'];
- $counter = (double)fread($fd, 128) * 1000;
- fclose($fd);
- #echo($mtime.'<br>');
- #echo('counter='.$counter.'<br>');
-
- $info=rrd_graph( '-', ['-s', $mtime, 'DEF:watts='.$RRDFILE.':watts:AVERAGE', 'VDEF:avg=watts,AVERAGE', 'PRINT:avg:%lf']);
- #print_r($info);
- $watts_since_counter = (double)$info['calcpr'][0];
- #echo('watts_since_counter='.$watts_since_counter.'<br>');
- #echo('hours_since_counter='.((time() - $mtime) / 3600.).'<br>');
- $wh_since_counter = $watts_since_counter * (time() - $mtime) / 3600.;
- #echo('wh_since_counter='.$wh_since_counter.'<br>');
- $counter += $wh_since_counter;
- #echo('newcounter='.($counter/1000).' kWh<br>');
-
- return $counter/1000.;
-}
-?>
<?php
define("RRDSOCK", "/var/run/rrdcached.sock");
define("RRDFILE", "/var/lib/rrdcached/db/power.rrd");
+define("VARDIR", "/var/lib/ampy/");
define("COUNTERFILE", "/var/lib/ampy/counter");
define("IMGWIDTH", 970);
define("IMGHEIGHT", 490);
<?php
-require("config.php");
+require_once("config.php");
+require_once("ampy.inc.php");
-exec("/usr/bin/rrdtool flushcached --daemon ".RRDSOCK." ".RRDFILE);
-
-$fd = fopen(COUNTERFILE, 'r');
-$mtime = fstat($fd)['mtime'];
-$counter = (double)fread($fd, 128) * 1000;
-fclose($fd);
-#echo($mtime.'<br>');
-#echo('counter='.$counter.'<br>');
-
-$params = [
- '--daemon', RRDSOCK,
- '-s', $mtime,
- 'DEF:watts='.RRDFILE.':watts:AVERAGE',
- 'VDEF:avg=watts,AVERAGE',
- #'VDEF:last=watts,LAST',
- 'PRINT:avg:%lf',
- #'PRINT:last:%lf',
-];
-/*header("Content-Type: text/plain");
-foreach ($params as $param) echo(escapeshellarg($param)." ");
-return;*/
-$info=rrd_graph( '-', $params);
-#print_r($info);
-$watts_since_counter = (double)$info['calcpr'][0];
-#$watts_last = (double)$info['calcpr'][1];
-#echo('watts_since_counter='.$watts_since_counter.'<br>');
-#echo('hours_since_counter='.((time() - $mtime) / 3600.).'<br>');
-$wh_since_counter = $watts_since_counter * (time() - $mtime) / 3600.;
-#echo('wh_since_counter='.$wh_since_counter.'<br>');
-$newcounter = $counter + $wh_since_counter;
-#echo('newcounter='.$newcounter.' kWh<br>');
+ampy_flush_rrd_daemon();
$info=rrd_lastupdate(RRDFILE);
if ($info === FALSE)
$result=[
'last_update' => 'unknown',
- 'watts' => 'unknown'
+ 'watts' => 'unknown',
+ 'error' => rrd_error(),
];
else
$result=[
'watts' => (double)$info['data'][0]
];
-#$result=[];
-#$result['watts'] = $watts_last;
-$result['counter'] = $newcounter / 1000.;
+$result['counter'] = ampy_get_counter();
header("Content-Type: text/json");
echo(json_encode($result));
* the updates every second.
*/
require_once('config.php');
+require_once("ampy.inc.php");
$speed=@(int)$_GET['speed'];
if ($speed <= 0)
}
while (True) {
- exec("/usr/bin/rrdtool flushcached --daemon ".RRDSOCK." ".RRDFILE);
+ ampy_flush_rrd_daemon();
$info=rrd_lastupdate(RRDFILE);
if ($info === FALSE)
'last_update' => $info['last_update'],
'watts' => (float)$info['data'][0],
];
+ $result['counter'] = ampy_get_counter();
send_chunk(json_encode($result));
sleep($speed);
}