/*
* 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
*/
}
+/*
+ * 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];
return $counter;
}
+
+/*
+ * Experimental function that uses rrd_graph to get lastest information
+ * This actually flushes the daemon... so it doesn't work
+ *
+function my_lastupdate_noflush() {
+ $info=rrd_graph( '-',
+ '--daemon', RRDSOCK,
+ 'DEF:watts='.RRDFILE.':watts:AVERAGE',
+ '-s' 'now - 5 seconds',
+ 'DEF:watts=/var/lib/rrdcached/db/power.rrd:watts:AVERAGE',
+ 'VDEF:last=watts,LAST',
+ 'PRINT:last:%lf'
+ );
+} */
+
+/*
+ * 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;
+}
?>