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.'
');
#echo('counter='.$counter.'
');
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.'
');
#echo('hours_since_counter='.((time() - $mtime) / 3600.).'
');
$wh_since_counter = $watts_since_counter * (time() - $mtime) / 3600.;
#echo('wh_since_counter='.$wh_since_counter.'
');
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
');
return $counter;
}
?>