From 926f0f6b3ff5f0723caa9a3f699388bebfa9f09f Mon Sep 17 00:00:00 2001 From: Nirgal Date: Sun, 13 Oct 2019 19:03:57 +0000 Subject: [PATCH] Clean up duplicate php code to inc file --- countercron | 31 +++------------ html/ampy.inc.php | 73 ++++++++++++++++++++++++++++++++++++ html/ampy.php | 28 -------------- html/config.php | 1 + html/currentjson.php | 42 +++------------------ html/currentjson_chunked.php | 4 +- 6 files changed, 89 insertions(+), 90 deletions(-) create mode 100644 html/ampy.inc.php delete mode 100644 html/ampy.php diff --git a/countercron b/countercron index 6a5ccb5..635f011 100755 --- a/countercron +++ b/countercron @@ -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); ?> diff --git a/html/ampy.inc.php b/html/ampy.inc.php new file mode 100644 index 0000000..4c9828f --- /dev/null +++ b/html/ampy.inc.php @@ -0,0 +1,73 @@ + 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; +} +?> diff --git a/html/ampy.php b/html/ampy.php deleted file mode 100644 index 88638c4..0000000 --- a/html/ampy.php +++ /dev/null @@ -1,28 +0,0 @@ -'); - #echo('counter='.$counter.'
'); - - $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.'
'); - #echo('hours_since_counter='.((time() - $mtime) / 3600.).'
'); - $wh_since_counter = $watts_since_counter * (time() - $mtime) / 3600.; - #echo('wh_since_counter='.$wh_since_counter.'
'); - $counter += $wh_since_counter; - #echo('newcounter='.($counter/1000).' kWh
'); - - return $counter/1000.; -} -?> diff --git a/html/config.php b/html/config.php index 7bd1ade..6ec064e 100644 --- a/html/config.php +++ b/html/config.php @@ -1,6 +1,7 @@ '); -#echo('counter='.$counter.'
'); - -$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.'
'); -#echo('hours_since_counter='.((time() - $mtime) / 3600.).'
'); -$wh_since_counter = $watts_since_counter * (time() - $mtime) / 3600.; -#echo('wh_since_counter='.$wh_since_counter.'
'); -$newcounter = $counter + $wh_since_counter; -#echo('newcounter='.$newcounter.' kWh
'); +ampy_flush_rrd_daemon(); $info=rrd_lastupdate(RRDFILE); if ($info === FALSE) $result=[ 'last_update' => 'unknown', - 'watts' => 'unknown' + 'watts' => 'unknown', + 'error' => rrd_error(), ]; else $result=[ @@ -46,9 +18,7 @@ else '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)); diff --git a/html/currentjson_chunked.php b/html/currentjson_chunked.php index 41bfb1e..bc62b4a 100644 --- a/html/currentjson_chunked.php +++ b/html/currentjson_chunked.php @@ -6,6 +6,7 @@ * the updates every second. */ require_once('config.php'); +require_once("ampy.inc.php"); $speed=@(int)$_GET['speed']; if ($speed <= 0) @@ -28,7 +29,7 @@ function send_chunk($chunk) } while (True) { - exec("/usr/bin/rrdtool flushcached --daemon ".RRDSOCK." ".RRDFILE); + ampy_flush_rrd_daemon(); $info=rrd_lastupdate(RRDFILE); if ($info === FALSE) @@ -42,6 +43,7 @@ while (True) { 'last_update' => $info['last_update'], 'watts' => (float)$info['data'][0], ]; + $result['counter'] = ampy_get_counter(); send_chunk(json_encode($result)); sleep($speed); } -- 2.30.2