-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlive-server-data-electra-dsmr.php
282 lines (264 loc) · 9.89 KB
/
live-server-data-electra-dsmr.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
//
// versie: 1.69.2
// auteur: Jos van der Zande based on model from André Rijkeboer
//
// datum: 7-08-2020
// omschrijving: ophalen van de P1meter informatie uit DSMR server en SolarEdge gegeven om ze samen in 1 grafiek te laten zien
//
//~ URL tbv live data p1 Meter: live-server-data-electra-dsmr.php/period=c
//~ ==========================================================================
//~ De verwachte JSON output is voor "period=c" (current data) aantal= wordt niet gebruikt
//~ [{
//~ "ServerTime" : "2019-03-13 11:48:40",
//~ "CounterDelivToday" : "1.349 kWh",
//~ "CounterToday" : "3.893 kWh",
//~ "Usage" : "167 Watt",
//~ "UsageDeliv" : "0 Watt",
//~ }]
//~ URL tbv dag grafiek p1 Meter: live-server-data-electra-dsmr.php/period=d&aantal=60
//~ ======================================================================================
//~ De verwachte JSON output is voor "period=d&aantal=xx"
//~ [
//~ {"idate":"2019-02-10","serie":"2019-02-10","prod":0,"v1":10.94,"v2":0,"r1":0,"r2":0},
//~ {"idate":"2019-02-11","serie":"2019-02-11","prod":0,"v1":3.68,"v2":9.92,"r1":0,"r2":0},
//~ {"idate":"2019-02-12","serie":"2019-02-12","prod":0,"v1":3.45,"v2":8.49,"r1":0,"r2":0}
//~ ]
//~ URL tbv maand grafiek p1 Meter: live-server-data-electra-dsmr.php/period=m&aantal=13
//~ ========================================================================================
//~ De verwachte JSON output is voor "period=m&aantal=xx"
//~ [
//~ {"idate":"2019-01-01","serie":"2019-01","prod":0,"v1":186.47,"v2":181.2,"r1":0,"r2":0},
//~ {"idate":"2019-02-01","serie":"2019-02","prod":137.72,"v1":163.95,"v2":154.13,"r1":36.64,"r2":71.46},
//~ {"idate":"2019-03-01","serie":"2019-03","prod":128.23,"v1":63.63,"v2":34.71,"r1":15.8,"r2":72.11}
//~ ]
include('config.php');
$limit = array_key_exists('aantal', $_GET) ? $_GET['aantal'] : "";
$period = array_key_exists('period', $_GET) ? $_GET['period'] : "";
$d1 = array_key_exists('date', $_GET) ? $_GET['date'] : "";
if($d1 == '') { $d1 = date("d-m-Y H:i:s", time()); }
if($period == '' || $period == 'd' ) {
$SQLdatefilter = '"%Y-%m-%d"';
$JSON_SUM = "Y-m-d";
$JSON_period = "day";
if($limit == '') { $limit = '30'; }
$periods = $limit-1;
} elseif ($period == 'm') {
$SQLdatefilter = '"%Y-%m"';
$JSON_SUM = "Y-m";
$JSON_period = "month";
if($limit == '') { $limit = '12'; }
$periods = $limit*31-1;
}
$date = (new DateTime("today " . date("Y-m-d 00:00:00", strtotime($d1))))->getTimestamp();
$tomorrow = (new DateTime("tomorrow " . date("Y-m-d 00:00:00", strtotime($d1))))->getTimestamp();
$today = new DateTime();
$total = array();
$dsmrdata = array();
$dsmrrec = array();
$diff = array();
// Get current info for P1_ElectriciteitsMeter from DSMR server
if ($period == 'c' ) {
//Get current info for P1_ElectriciteitsMeter from DSMR server
$response = file_get_contents($dsmr_url.'/api/v2/consumption/electricity-live', null, stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => array('X-AUTHKEY:'.$dsmr_apikey))
)
));
if (!$response) {
echo "\nUnable to reach server for P1 information\nERROR:";
print_r(error_get_last()['message']);
exit();
}
$dsmr_restc = json_decode($response,true);
//Get today info for P1_ElectriciteitsMeter from DSMR server
$response = file_get_contents($dsmr_url.'/api/v2/consumption/today', null, stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => array('X-AUTHKEY:'.$dsmr_apikey))
)
));
$dsmr_restd = json_decode($response,true);
$diff['ServerTime'] = date("d-m-Y H:i:s",strtotime($dsmr_restc['timestamp']));
$diff['CounterToday'] = round((floatval($dsmr_restd['electricity1'])+floatval($dsmr_restd['electricity2'])),3);
$diff['CounterDelivToday'] = round((floatval($dsmr_restd['electricity1_returned'])+floatval($dsmr_restd['electricity2_returned'])),3);
$diff['Usage'] = round(floatval($dsmr_restc['currently_delivered']),3);
$diff['UsageDeliv'] = round(floatval($dsmr_restc['currently_returned']),3);
array_push($total, $diff);
} else {
// ============================================================================================
// Laad de data van het laatste jaar voor de P1_ElectriciteitsMeter uit DSMR server
$sdate=date("Y-m-d", strtotime("-$periods day",$date));
$response = file_get_contents($dsmr_url.'/api/v2/statistics/day?day__gte='.$sdate.'&limit='.($periods+1).'', null, stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => array('X-AUTHKEY:'.$dsmr_apikey))
)
));
if (!$response) {
echo "\nUnable to reach server for P1 information\nERROR:";
print_r(error_get_last()['message']);
exit();
}
$dsmr_rest = json_decode($response,true);
$dsmr_data_cy = $dsmr_rest['results'];
// ==================================================================================================
$checkdate = date($JSON_SUM, strtotime("-$limit $JSON_period",$date));
$checkdates = date($JSON_SUM, strtotime("-$limit $JSON_period",$date));
$checkdatee = date("Y-m-d", $date);
$i=0;
$savedate=0;
// process all DSMR data and sum for the requested period
foreach($dsmr_data_cy as $item) {
$compdate = date($JSON_SUM,strtotime($item['day']));
$compdate2 = date("Y-m-d",strtotime($item['day']));
if($compdate>=$checkdates && $compdate2<=$checkdatee){
if($savedate!=$compdate) {
if ($savedate!=0) {
$dsmrrec['d']= $td;
$dsmrrec['v1']= $tv1;
$dsmrrec['v2']= $tv2;
$dsmrrec['r1']= $tr1;
$dsmrrec['r2']= $tr2;
array_push($dsmrdata, $dsmrrec);
}
$savedate=$compdate;
$td = $item['day'];
$tv1 = $item['electricity1'];
$tv2 = $item['electricity2'];
$tr1 = $item['electricity1_returned'];
$tr2 = $item['electricity2_returned'];
} else {
$tv1 += $item['electricity1'];
$tv2 += $item['electricity2'];
$tr1 += $item['electricity1_returned'];
$tr2 += $item['electricity2_returned'];
}
}
}
$tcompdate = date("Y-m-d");
if($tcompdate<=$checkdatee){
//Get today info for P1_ElectriciteitsMeter from DSMR server
$response = file_get_contents($dsmr_url.'/api/v2/consumption/today', null, stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => array('X-AUTHKEY:'.$dsmr_apikey))
)
));
// get todays info from DSMR since that is not part of the previous call
$dsmr_rest = json_decode($response,true);
$compdate = date($JSON_SUM,strtotime($dsmr_rest['day']));
if ($savedate != 0){
if ($compdate != $savedate) {
$dsmrrec['d']= $td;
$dsmrrec['v1']= $tv1;
$dsmrrec['v2']= $tv2;
$dsmrrec['r1']= $tr1;
$dsmrrec['r2']= $tr2;
array_push($dsmrdata, $dsmrrec);
//echo "Write1a: " . $td." v1:".$tv1." v2:".$tv2."\n";
$tv1 = 0;
$tv2 = 0;
$tr1 = 0;
$tr2 = 0;
}
}
$dsmrrec['d']= $compdate;
$dsmrrec['v1']= $tv1 + round(floatval($dsmr_rest['electricity1']),3);
$dsmrrec['v2']= $tv2 + round(floatval($dsmr_rest['electricity2']),3);
$dsmrrec['r1']= $tr1 + round(floatval($dsmr_rest['electricity1_returned']),3);
$dsmrrec['r2']= $tr2 + round(floatval($dsmr_rest['electricity2_returned']),3);
array_push($dsmrdata, $dsmrrec);
} else {
if ($savedate != 0){
$dsmrrec['d']= $td;
$dsmrrec['v1']= $tv1;
$dsmrrec['v2']= $tv2;
$dsmrrec['r1']= $tr1;
$dsmrrec['r2']= $tr2;
array_push($dsmrdata, $dsmrrec);
}
}
//-------------------------------------------------------------
//open MySQL database
$mysqli = new mysqli($host, $user, $passwd, $db, $port);
// haal gegevens van de inverter op
$diff = array();
$p1revrow = ["se_day" => 0];
$table = $inverter == 1 ? "telemetry_inverter" : "telemetry_inverter_3phase";
if ($period == 'd' ) {
$checkdate = date($JSON_SUM, strtotime("-$limit $JSON_period",$date));
} else {
$checkdate = date($JSON_SUM, strtotime("-$limit $JSON_period",$date))."-01";
}
$query = 'SELECT DATE_FORMAT(DATE(FROM_UNIXTIME(timestamp)), '.$SQLdatefilter.') as iDate, (max(e_total)-min(e_total))/1000 as prod' .
' FROM ' . $table .
' WHERE timestamp <= UNIX_TIMESTAMP("' . date('Y-m-d', $date) . ' 23:59:59") AND timestamp >= UNIX_TIMESTAMP("' . $checkdate . '")' .
' GROUP BY iDate' .
' ORDER by iDate';
// haal de gegevens van de inverter op $SQL_datefilter = 'DATE_FORMAT(t2.d, "%Y-%m-%d")';
$inverter_data = $mysqli->query($query);
$thread_id = $mysqli->thread_id;
// Sluit DB
$mysqli->kill($thread_id);
$mysqli->close();
// ================================================================================
// loop through the dates and merge the data from DSMR server and the Converter arrays
//
for ($i=0; $i<=$limit-1; $i++) {
$pnum=$limit-$i-1;
$datafound = 0;
if ($period == 'm') {
$checkdate = date($JSON_SUM, strtotime(date( 'Y-m' )." -$pnum $JSON_period",$date));
} else {
$checkdate = date($JSON_SUM, strtotime("-$pnum $JSON_period",$date));
}
$diff['idate'] = date("Y-m-d",strtotime(date($checkdate)));
$diff['serie'] = date($JSON_SUM,strtotime(date($checkdate)));
$diff['prod'] = 0;
$diff['v1'] = 0;
$diff['v2'] = 0;
$diff['r1'] = 0;
$diff['r2'] = 0;
// get inverter data for that period
$diff['prod'] = 0;
foreach($inverter_data as $j => $row){
$compdate = date($JSON_SUM,strtotime(date($row['iDate'])));
if ($compdate==$checkdate) {
$diff['prod'] = round($row["prod"],2);
$datafound = 1;
}
if ($datafound == 0) {
$diff['prod'] = 0.00;
}
}
// Get&Sum DSMR server data for this period
$tv1 = 0;
$tv2 = 0;
$tr1 = 0;
$tr2 = 0;
$diff['v1'] = 0;
$diff['v2'] = 0;
$diff['r1'] = 0;
$diff['r2'] = 0;
foreach($dsmrdata as $item) { //foreach element in $arr
$compdate = date($JSON_SUM,strtotime($item['d']));
if ($compdate==$checkdate) {
$diff['v1'] = round($item['v1'],2);
$diff['v2'] = round($item['v2'],2);
$diff['r1'] = round($item['r1'],2);
$diff['r2'] = round($item['r2'],2);
$datafound = 1;
}
}
//voeg het resultaat toe aan de total-array
if ($datafound == 1) {
array_push($total, $diff);
}
}
}
//Output totale resultaat als JSON
echo json_encode($total);
echo "\n";
?>