-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData.php
executable file
·201 lines (160 loc) · 6.13 KB
/
Data.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
<?php
header('Content-Type: application/json');
error_reporting(0);
// settings
$accountNumber = "<energy australia costumer id>";
$username = "<energy australia email>";
$password = "<energy australia password>";
$inverterIp = "<fronius ip/name>";
$fallbackStartDate = "<first date to fetch data>";
// http init
require_once 'HttpRequester.php';
$browser = new HttpRequester;
$browser->postfollowredirs = true;
function login($username, $password) {
return $GLOBALS['browser']->post(
'https://www.energyaustralia.com.au/myaccount/login',
"username=".$username."&password=".$password
);
}
function getPeriod() {
$response = $GLOBALS['browser']->get(
"https://www.energyaustralia.com.au/myaccount/api/usage/".$GLOBALS['accountNumber']."/summary?meterType=SMART_MANUAL"
);
if($response == null) {
$startDate = new DateTime($GLOBALS['fallbackStartDate']);
$endDate = new DateTime(date("Y-m-d"));
$endDate->modify('-2 day');
} else {
$dates = [$response["earliestDataDate"] , $response["latestDataDate"] ];
$startDate = new DateTime(implode("-", $dates[0]));
$endDate = new DateTime(implode("-", $dates[1]));
$endDate->modify('+1 day');
}
return new DatePeriod(
$startDate,
new DateInterval('P1D'),
$endDate
);
}
function saveInverterData($period) {
foreach ($period as $key => $value) {
$date = $value->format('Y-m-d');
$filepath = "./data/inverter/".$date.'.json';
$json_string = file_get_contents($filepath);
if ($json_string === false) {
$result = $GLOBALS['browser']->get(
"http://".$GLOBALS['inverterIp']."/solar_api/v1/GetArchiveData.cgi?Scope=Device&DeviceClass=Inverter&StartDate=".$date."&EndDate=".$date."&DeviceId=1&Channel=TimeSpanInSec&Channel=EnergyReal_WAC_Sum_Produced&Channel=EnergyReal_WAC_Sum_Consumed&Channel=InverterEvents&Channel=InverterErrors&Channel=Current_DC_String_1&Channel=Current_DC_String_2&Channel=Voltage_DC_String_1&Channel=Voltage_DC_String_2&Channel=Temperature_Powerstage&Channel=Voltage_AC_Phase_1&Channel=Voltage_AC_Phase_2&Channel=Voltage_AC_Phase_3&Channel=Current_AC_Phase_1&Channel=Current_AC_Phase_2&Channel=Current_AC_Phase_3&Channel=PowerReal_PAC_Sum&Channel=EnergyReal_WAC_Minus_Absolute&Channel=EnergyReal_WAC_Plus_Absolute&Channel=Meter_Location_Current&Channel=Temperature_Channel_1&Channel=Temperature_Channel_2&Channel=Digital_Channel_1&Channel=Digital_Channel_2&Channel=Radiation&Channel=Digital_PowerManagementRelay_Out_1&Channel=Digital_PowerManagementRelay_Out_2&Channel=Digital_PowerManagementRelay_Out_3&Channel=Digital_PowerManagementRelay_Out_4"
);
if($result !== null) {
$json_string = json_encode($result);
file_put_contents($filepath, $json_string);
}
}
}
}
function convertInverterDataToHourly($period) {
foreach ($period as $key => $value) {
$date = $value->format('Y-m-d');
$filepath = "./data/inverter/".$date.".json";
$hourlyfilepath = "./data/hourly/".$date.".json";
$json_string = file_get_contents($hourlyfilepath);
if ($json_string === false) {
$json = null;
$json_string = file_get_contents($filepath);
$json = json_decode($json_string, true)['Body']["Data"]["inverter/1"]["Data"]["PowerReal_PAC_Sum"]["Values"];
if($json) {
$hourlyData = array(
0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 11 => 0,
12 => 0, 13 => 0, 14 => 0, 15 => 0, 16 => 0, 17 => 0, 18 => 0, 19 => 0, 20 => 0, 21 => 0, 22 => 0, 23 => 0
);
foreach ((array) $json as $k => $v) {
if($v > 0) {
$hour = floor($k/60/60);
$hourlyData[$hour] = $hourlyData[$hour] + ($v/12/1000);
}
}
$json_string = json_encode($hourlyData);
file_put_contents($hourlyfilepath , $json_string);
}
}
}
}
function saveEAData( $period) {
foreach ($period as $key => $value) {
$date = $value->format('Y-m-d');
$filepath = "./data/ea/".$date.'.json';
$json_string = file_get_contents($filepath);
if ($json_string === false) {
$result = $GLOBALS['browser']->get(
"https://www.energyaustralia.com.au/myaccount/api/usage/".$GLOBALS['accountNumber']."/hourly?forDay=".$date."&ts=" . time()
)[0];
if($result !== null) {
$json_string = json_encode($result);
file_put_contents($filepath, $json_string);
}
}
}
}
function mergeData($period) {
$result = array();
foreach ($period as $key => $value) {
$date = $value->format('Y-m-d');
$mergedpath = "./data/merged/".$date.'.json';
$json = json_decode(file_get_contents($mergedpath), true);;
if($json ) {
array_push($result, $json );
} else {
$hourlyfilepath = "./data/hourly/".$date.".json";
$eafilepath = "./data/ea/".$date.'.json';
$solarKw = json_decode(file_get_contents($hourlyfilepath), true);
$ea = json_decode(file_get_contents($eafilepath), true);
if($solarKw && $ea) {
$day = array();
$sums = array(
"interval" => 0,
"bought" => 0,
"sold" => 0,
"produced" => 0,
"consumed" => 0
);
foreach ($ea as $i) {
$hour = $i['interval'];
$solar = $solarKw[$hour];
$sold = - $i['soldToGrid'];
$bought = $i['consumption'];
$consumed = - $bought - ($solar + $sold);
// EA does not return null for missing values
// it is quite unlikely that I will not buy or sell anything.
$hasData = $bought != 0 || $sold != 0;
$sums = array(
"interval" => $sums["interval"] + $hour,
"bought" => $sums["bought"] + $bought,
"sold" => $sums["sold"] + $sold,
"produced" => $sums["produced"] + $solar,
"consumed" => $sums["consumed"] + ($hasData ? $consumed : 0)
);
$day[$hour] = array(
"interval" => $hour,
"bought" => $hasData ? $bought : null,
"sold" => $hasData ? $sold : null,
"produced" => $solar,
"consumed" => $hasData ? $consumed : null
);
}
$data = array( "date"=> $date, "data"=> $day, "sum" => $sums );
file_put_contents($mergedpath, json_encode($data));
array_push($result, $data);
}
}
}
return $result;
}
login( $username, $password);
$period = getPeriod();
saveEAData( $period);
saveInverterData( $period);
convertInverterDataToHourly($period);
$result = mergeData($period);
echo json_encode($result);
?>