Cleanup
authorNirgal <contact_ampy@nirgal.com>
Mon, 14 Oct 2019 20:06:47 +0000 (20:06 +0000)
committerNirgal <contact_ampy@nirgal.com>
Mon, 14 Oct 2019 22:20:20 +0000 (22:20 +0000)
Comment and code factorisation.
Show server side errors in javascript.

html/ampy.inc.php
html/ampy.js
html/currentjson.php
html/currentjson_chunked.php
html/index_unchuncked.php

index 947fa665448c477fe44a53af1aca6b90854c2d03..050319f53c7db4fa051cf141fbcb21beea6a5b62 100644 (file)
@@ -36,6 +36,7 @@ function ampy_get_old_counter_info() {
 
 /*
  * Returns how many kWh have been spent since a date.
+ * The information is based on what has been recorded in the RRD database.
  * @param mtime seconds since epoch
  * @return float kW.h
  */
@@ -61,6 +62,12 @@ function ampy_get_kwh_since($mtime) {
 
 }
 
+/*
+ * Get counter current value
+ * The value is based on:
+ *  last value stored in counter file + ampy_get_kwh_since
+ * @return float kWh on the counter.
+ */
 function ampy_get_counter() {
        $counter_mtime = ampy_get_old_counter_info();
        $counter = $counter_mtime[0];
@@ -71,4 +78,30 @@ function ampy_get_counter() {
 
        return $counter;
 }
+
+/*
+ * Return all the information.
+ * Make sure you call ampy_flush_rrd_daemon() first if you need real time
+ * value.
+ * @return array['lastupdate'=>timestamps,
+ *               'watts'=>last_watt_value,
+ *               'counter'=>total_kWh,
+ */
+function ampy_get_info() {
+       $info=rrd_lastupdate(RRDFILE);
+       if ($info === FALSE)
+               $result=[
+                       //'last_update' => 'unknown',
+                       //'watts' => 'unknown',
+                       'error' => rrd_error(),
+               ];
+       else
+               $result=[
+                       'last_update' => $info['last_update'],
+                       'watts' => (double)$info['data'][0],
+               ];
+
+       $result['counter'] = ampy_get_counter();
+       return $result;
+}
 ?>
index 0b2ae9883a0e942461f6a0f4549cc1182e1f038e..f372214e6ab336d8e6d789b53068e6dd5a77b0f6 100644 (file)
@@ -4,11 +4,13 @@ function repportError(errortext) {
        if (domError.style.display != 'block')
                domError.style.display = 'block';
 }
+
 function repportSuccess() {
        domError = document.getElementById('errorpannel');
        if (domError.style.display != 'none')
                domError.style.display = 'none';
 }
+
 function updatePage(strdata) {
        try {
                data = JSON.parse(strdata);
@@ -17,7 +19,22 @@ function updatePage(strdata) {
                console.warn("Error decoding json: "+strdata);
                return;
        }
-       document.getElementById('currentpower').innerHTML = data.watts.toFixed(1);
-       document.getElementById('totalpower').innerHTML = data.counter.toFixed(5);
-       repportSuccess();
+       errorfound = false;
+       for (key in data) {
+               value = data[key];
+               switch(key) {
+               case 'watts':
+                       document.getElementById('currentpower').innerHTML = value.toFixed(1);
+                       break;
+               case 'counter':
+                       document.getElementById('totalpower').innerHTML = value.toFixed(5);
+                       break;
+               case 'error':
+                       repportError(value);
+                       errorfound = true;
+                       break;
+               }
+       }
+       if (!errorfound)
+               repportSuccess();
 }
index cdd300dacc213757eda183cfe368e05871ac512e..28e18496da81f4bd4760451c809ac3813099b277 100644 (file)
@@ -3,23 +3,9 @@
 require_once("config.php");
 require_once("ampy.inc.php");
 
-ampy_flush_rrd_daemon();
-
-$info=rrd_lastupdate(RRDFILE);
-if ($info === FALSE)
-       $result=[
-               'last_update' => 'unknown',
-               'watts' => 'unknown',
-               'error' => rrd_error(),
-       ];
-else
-       $result=[
-               'last_update' => $info['last_update'],
-               'watts' => (double)$info['data'][0]
-       ];
+header("Content-Type: text/json");
 
-$result['counter'] = ampy_get_counter();
+ampy_flush_rrd_daemon();
 
-header("Content-Type: text/json");
-echo(json_encode($result));
+echo(json_encode(ampy_get_info()));
 ?>
index 7772479e7ffc7ebec4dd384c2842e24fdab9fb15..19224d7d783de6662ce5fad2e8eabc35e98c9068 100644 (file)
@@ -16,10 +16,11 @@ if ($speed <= 0)
 header("Content-Type: text/json");
 header("Transfer-Encoding: chunked");
 flush();
+ob_flush();
 
 
 // Send chunk to browser
-function send_chunk($chunk)
+function echo_chunk($chunk)
 {
        // The chunk must fill the output buffer or php won't send it
        //$chunk = str_pad($chunk, 4096);
@@ -32,20 +33,8 @@ function send_chunk($chunk)
 while (True) {
        ampy_flush_rrd_daemon();
 
-       $info=rrd_lastupdate(RRDFILE);
-       if ($info === FALSE)
-               $result=[
-                       'last_update' => 'unknown',
-                       'watts' => 'unknown',
-                       'error' => rrd_error(),
-               ];
-       else
-               $result=[
-                       'last_update' => $info['last_update'],
-                       'watts' => (float)$info['data'][0],
-               ];
-       $result['counter'] = ampy_get_counter();
-       send_chunk(json_encode($result));
+       echo_chunk(json_encode(ampy_get_info()));
+
        sleep($speed);
 }
 ?>
index 77d56f4172a6635a1264a577251c1896d4d3089a..452bb064636cf43591ccc9b187da687a3d5461c3 100644 (file)
@@ -4,6 +4,7 @@
 <title>ampy - Le compteur espion qui ne moucharde pas</title>
 <script src='/javascript/jquery/jquery.js'></script>
 <script src=ampy.js></script>
+<style>
 div#errorpannel
 {
     position: absolute;