-
Notifications
You must be signed in to change notification settings - Fork 6
/
agile.php
237 lines (179 loc) · 8.66 KB
/
agile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
$dir = dirname(__FILE__);
chdir("$dir/www");
$admin_userid = 2;
require "Lib/load_database.php";
require("Modules/user/user_model.php");
$user = new User($mysqli, false);
require ("Modules/system/system_model.php");
$system = new System($mysqli);
require ("Modules/system/system_stats_model.php");
$system_stats = new SystemStats($mysqli,$system);
$data = $system->list_admin();
$i = 0;
$date = new DateTime();
$date->setTimezone(new DateTimeZone('Europe/London'));
$date->modify("midnight");
$end = $date->getTimestamp();
$starts = array();
$date->modify("-7 days");
$starts['last7'] = $date->getTimestamp();
$date->setTimestamp($end);
$date->modify("-30 days");
$starts['last30'] = $date->getTimestamp();
$date->setTimestamp($end);
$date->modify("-90 days");
$starts['last90'] = $date->getTimestamp();
$date->setTimestamp($end);
$date->modify("-365 days");
$starts['last365'] = $date->getTimestamp();
// Half hour interval
$interval = 1800;
// Set start time to the start of the last 30 days
$start = $starts['last365'];
// Round to nearest interval
$start = floor($start/$interval)*$interval;
$end = ceil($end/$interval)*$interval;
$average = 0;
$delta = 1;
$timeformat = "notime";
// Load agile data feedid 473228 (AGILE-22-07-22:K_Southern_Wales)
$url = "https://emoncms.org/feed/data.json?id=473228&start=$start&end=$end&interval=$interval&average=0&delta=0&skipmissing=0&timeformat=$timeformat";
$agile = json_decode(file_get_contents($url));
// Generate Octopus Cosy data
// Day rate: 24.51 p/kWh
// Cosy rate 12.01 p/kWh (04:00-07:00 & 13:00-16:00)
// Peak rate 35.55 p/kWh (16:00-19:00)
$cosy = array();
for ($j=0; $j<count($agile); $j++) {
$time = $start + $j*$interval;
$date->setTimestamp($time);
$hour = $date->format('G');
if ($hour>=4 && $hour<7) $cosy[$j] = 12.01;
else if ($hour>=13 && $hour<16) $cosy[$j] = 12.01;
else if ($hour>=16 && $hour<19) $cosy[$j] = 35.55;
else $cosy[$j] = 24.51;
}
// Generate Octopus Go data
// Day rate: 27.12 p/kWh
// Night rate 9.00 p/kWh (00:30-04:30)
$go = array();
for ($j=0; $j<count($agile); $j++) {
$time = $start + $j*$interval;
$date->setTimestamp($time);
// get hour + minutes / 60
$hour = $date->format('G') + $date->format('i')/60;
if ($hour>=0.5 && $hour<4.5) $go[$j] = 9.00;
else $go[$j] = 27.12;
}
foreach ($data as $row) {
$userid = (int) $row->userid;
$systemid = (int) $row->id;
//if ($systemid!=150) continue;
print $systemid."\n";
$config = $system_stats->get_system_config_with_meta($userid, $systemid);
if (is_array($config) && isset($config['success'])) {
print "Error: ".$config['message']."\n";
print $row->url."\n";
continue;
}
if (isset($config->feeds)) {
// print heatpump_elec_kwh
if (isset($config->feeds->heatpump_elec_kwh)) {
// append apikey if set
$apikeystr = "";
if ($config->apikey) $apikeystr = "&apikey=".$config->apikey;
// Load heatpump_elec_kwh feed data
$url = "$config->server/feed/data.json?id=".$config->feeds->heatpump_elec_kwh->feedid."&start=$start&end=$end&interval=$interval&average=0&delta=1&skipmissing=0&timeformat=$timeformat".$apikeystr;
$result = json_decode(file_get_contents($url));
// Test for feed count mismatch
if (count($result)!=count($agile)) {
print "Feed count mismatch\n";
continue;
}
$sum_kwh = array();
$sum_cost_agile = array();
$sum_cost_cosy = array();
$sum_cost_go = array();
$months_kwh = array();
$months_agile_cost = array();
$months_cosy_cost = array();
$months_go_cost = array();
$periods = array('last7','last30','last90','last365');
foreach ($periods as $period) {
$sum_kwh[$period] = 0;
$sum_cost_agile[$period] = 0;
$sum_cost_cosy[$period] = 0;
$sum_cost_go[$period] = 0;
}
for ($j=0; $j<count($result); $j++) {
$time = $start + $j*$interval;
$kwh = $result[$j];
if ($kwh!=null) {
$unit_cost_agile = $agile[$j]*0.01;
$unit_cost_cosy = $cosy[$j]*0.01;
$unit_cost_go = $go[$j]*0.01;
foreach ($periods as $period) {
if ($time>=$starts[$period]) {
$sum_kwh[$period] += $kwh;
$sum_cost_agile[$period] += $kwh*$unit_cost_agile;
$sum_cost_cosy[$period] += $kwh*$unit_cost_cosy;
$sum_cost_go[$period] += $kwh*$unit_cost_go;
}
}
// Allocate to months by start time
$date->setTimestamp($time);
// get timestamp of start of month
$date->modify("midnight first day of this month");
// 00:00:00
$date->setTime(0,0,0);
$month = $date->getTimestamp();
if (!isset($months_kwh[$month])) {
$months_kwh[$month] = 0;
$months_agile_cost[$month] = 0;
$months_cosy_cost[$month] = 0;
$months_go_cost[$month] = 0;
}
$months_kwh[$month] += $kwh;
$months_agile_cost[$month] += $kwh*$unit_cost_agile;
$months_cosy_cost[$month] += $kwh*$unit_cost_cosy;
$months_go_cost[$month] += $kwh*$unit_cost_go;
}
}
foreach ($periods as $period) {
if ($sum_kwh[$period]>0) {
$unit_cost_agile = $sum_cost_agile[$period]/$sum_kwh[$period];
$unit_cost_agile_vat = $unit_cost_agile*1.05*100;
$unit_cost_cosy = $sum_cost_cosy[$period]/$sum_kwh[$period]*100;
$unit_cost_go = $sum_cost_go[$period]/$sum_kwh[$period]*100;
print "$period Agile: £".number_format($sum_cost_agile[$period],2).", ".number_format($sum_kwh[$period],2)." kWh, ".number_format($unit_cost_agile_vat,3)." p/kWh\n";
print "$period Cosy: £".number_format($sum_cost_cosy[$period],2).", ".number_format($sum_kwh[$period],2)." kWh, ".number_format($unit_cost_cosy,3)." p/kWh\n";
print "$period Go: £".number_format($sum_cost_go[$period],2).", ".number_format($sum_kwh[$period],2)." kWh, ".number_format($unit_cost_go,3)." p/kWh\n";
$mysqli->query("UPDATE system_stats_".$period."_v2 SET `unit_rate_agile` = '$unit_cost_agile_vat' WHERE `id` = $systemid");
$mysqli->query("UPDATE system_stats_".$period."_v2 SET `unit_rate_cosy` = '$unit_cost_cosy' WHERE `id` = $systemid");
$mysqli->query("UPDATE system_stats_".$period."_v2 SET `unit_rate_go` = '$unit_cost_go' WHERE `id` = $systemid");
}
}
// Monthly data
foreach ($months_kwh as $month => $kwh) {
if ($kwh>0) {
$unit_cost_agile = $months_agile_cost[$month]/$kwh;
$unit_cost_agile_vat = $unit_cost_agile*1.05*100;
$unit_cost_cosy = $months_cosy_cost[$month]/$kwh*100;
$unit_cost_go = $months_go_cost[$month]/$kwh*100;
$date->setTimestamp($month);
$monthstr = $date->format('M Y');
print "$monthstr Agile: £".number_format($months_agile_cost[$month],2).", ".number_format($kwh,2)." kWh, ".number_format($unit_cost_agile_vat,3)." p/kWh\n";
print "$monthstr Cosy: £".number_format($months_cosy_cost[$month],2).", ".number_format($kwh,2)." kWh, ".number_format($unit_cost_cosy,3)." p/kWh\n";
print "$monthstr Go: £".number_format($months_go_cost[$month],2).", ".number_format($kwh,2)." kWh, ".number_format($unit_cost_go,3)." p/kWh\n";
$mysqli->query("UPDATE system_stats_monthly_v2 SET `unit_rate_agile` = '$unit_cost_agile_vat' WHERE `id` = $systemid AND `timestamp` = $month");
$mysqli->query("UPDATE system_stats_monthly_v2 SET `unit_rate_cosy` = '$unit_cost_cosy' WHERE `id` = $systemid AND `timestamp` = $month");
$mysqli->query("UPDATE system_stats_monthly_v2 SET `unit_rate_go` = '$unit_cost_go' WHERE `id` = $systemid AND `timestamp` = $month");
}
}
print "\n";
}
}
$i++;
// if ($i>10) break;
}