Clean up duplicate php code to inc file
authorNirgal <contact_ampy@nirgal.com>
Sun, 13 Oct 2019 19:03:57 +0000 (19:03 +0000)
committerNirgal <contact_ampy@nirgal.com>
Sun, 13 Oct 2019 19:03:57 +0000 (19:03 +0000)
countercron
html/ampy.inc.php [new file with mode: 0644]
html/ampy.php [deleted file]
html/config.php
html/currentjson.php
html/currentjson_chunked.php

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);
 ?>
diff --git a/html/ampy.inc.php b/html/ampy.inc.php
new file mode 100644 (file)
index 0000000..4c9828f
--- /dev/null
@@ -0,0 +1,73 @@
+<?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;
+}
+?>
diff --git a/html/ampy.php b/html/ampy.php
deleted file mode 100644 (file)
index 88638c4..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-<?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.;
-}
-?>
index 7bd1adee0ba35b250cbe4f41b6786c5707738550..6ec064ec1bb45f7e59d3f4ca35980fd417c89efe 100644 (file)
@@ -1,6 +1,7 @@
 <?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);
index 41b39ed953a7d91bd32bb665e504cccee7d198b2..cdd300dacc213757eda183cfe368e05871ac512e 100644 (file)
@@ -1,44 +1,16 @@
 <?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=[
@@ -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));
index 41bfb1e1f9286d3c8dece39f9fe05ac4c2d3f1d0..bc62b4a39b5bc77283492ea56340d769894191d3 100644 (file)
@@ -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);
 }