Tests on php-rrd not using rrdcahed
[ampy.git] / html / ampy.inc.php
1 <?php
2 /*
3  * Ampy helpers
4  */
5
6 require_once('config.php');
7
8 function ampy_flush_rrd_daemon() {
9         exec("/usr/bin/rrdtool flushcached --daemon ".escapeshellarg(RRDSOCK)." ".escapeshellarg(RRDFILE));
10 }
11
12 /*
13  * Initialized the counter file with an absolute value.
14  * Note that the file mtime is important.
15  */
16 function ampy_set_counter($newcounter) {
17         file_put_contents(COUNTERFILE.'.new', $newcounter);
18         rename(COUNTERFILE.'.new', COUNTERFILE);
19         trigger_error("Counter was reset to $newcounter", E_USER_WARNING);
20 }
21
22 /*
23  * Returns an array
24  * 0 => kWh counter value (file content)
25  * 1 => last update (file mtime)
26  */
27 function ampy_get_old_counter_info() {
28         $fd = fopen(COUNTERFILE, 'r');
29         $mtime = fstat($fd)['mtime'];
30         $counter = (double)fread($fd, 128);
31         fclose($fd);
32         #echo($mtime.'<br>');
33         #echo('counter='.$counter.'<br>');
34         return [$counter, $mtime];
35 }
36
37 /*
38  * Returns how many kWh have been spent since a date.
39  * The information is based on what has been recorded in the RRD database.
40  * @param mtime seconds since epoch
41  * @return float kW.h
42  */
43 function ampy_get_kwh_since($mtime) {
44         
45         $params = [
46                 '--daemon', RRDSOCK,
47                 '-s', $mtime,
48                 'DEF:watts='.RRDFILE.':watts:AVERAGE',
49                 'VDEF:avg=watts,AVERAGE',
50                 #'VDEF:last=watts,LAST',
51                 'PRINT:avg:%lf',
52                 #'PRINT:last:%lf',
53         ];
54         $info=rrd_graph( '-', $params);
55         #print_r($info);
56         $watts_since_counter = (double)$info['calcpr'][0];
57         #echo('watts_since_counter='.$watts_since_counter.'<br>');
58         #echo('hours_since_counter='.((time() - $mtime) / 3600.).'<br>');
59         $wh_since_counter = $watts_since_counter * (time() - $mtime) / 3600.;
60         #echo('wh_since_counter='.$wh_since_counter.'<br>');
61         return $wh_since_counter / 1000.;
62
63 }
64
65 /*
66  * Get counter current value
67  * The value is based on:
68  *  last value stored in counter file + ampy_get_kwh_since
69  * @return float kWh on the counter.
70  */
71 function ampy_get_counter() {
72         $counter_mtime = ampy_get_old_counter_info();
73         $counter = $counter_mtime[0];
74         $mtime = $counter_mtime[1];
75
76         $counter += ampy_get_kwh_since($mtime);
77         #echo('newcounter='.($counter/1000).' kWh<br>');
78
79         return $counter;
80 }
81
82 /*
83  * Experimental function that uses rrd_graph to get lastest information
84  * This actually flushes the daemon... so it doesn't work
85  *
86 function my_lastupdate_noflush() {
87         $info=rrd_graph( '-',
88                 '--daemon', RRDSOCK,
89                 'DEF:watts='.RRDFILE.':watts:AVERAGE',
90                 '-s' 'now - 5 seconds',
91                 'DEF:watts=/var/lib/rrdcached/db/power.rrd:watts:AVERAGE',
92                 'VDEF:last=watts,LAST',
93                 'PRINT:last:%lf'
94         );
95 } */
96
97 /*
98  * Return all the information.
99  * Make sure you call ampy_flush_rrd_daemon() first if you need real time
100  * value.
101  * @return array['lastupdate'=>timestamps,
102  *               'watts'=>last_watt_value,
103  *               'counter'=>total_kWh,
104  */
105 function ampy_get_info() {
106         $info=rrd_lastupdate(RRDFILE);
107         if ($info === FALSE)
108                 $result=[
109                         //'last_update' => 'unknown',
110                         //'watts' => 'unknown',
111                         'error' => rrd_error(),
112                 ];
113         else
114                 $result=[
115                         'last_update' => $info['last_update'],
116                         'watts' => (double)$info['data'][0],
117                 ];
118
119         $result['counter'] = ampy_get_counter();
120         return $result;
121 }
122 ?>