-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpsoliscontrol.php
272 lines (182 loc) · 6.71 KB
/
phpsoliscontrol.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
<?php
// phpsoliscontrol
// version 0.0.1
// standalone php script to set charge/discharge settings for Solis Inverters via SolisCloud API
/*
Commands based on details from
https://github.com/stevegal/solis_control
http://www.adsar.co.uk/soliscloud-php
*/
// error reporting
error_reporting(1); error_reporting(E_ERROR | E_WARNING | E_PARSE);
//-------------------- SETTINGS TO CONNECT TO SOLISCLOUD --------------------//
$keyID = '<your API Key provided by SolisAPI>'; //i.e. '13000000000000000'
$keySecret = '<your API SECRET provided by SolisAPI>'; //i.e. 'aabbccddeff001122334455'
$keyAccount = '<SolisCloud Web Account>'; //i.e. 'blahblah@hotmail.com';
$keyPass = '<SolisCloud Web Pass>'; // i.e.'sldkfjslkdfjslkdfjlsk';
//-------------------- SETTINGS TO CONNECT TO SOLISCLOUD --------------------//
//Overwrites settings from external file, if present
if (file_exists("config.php"))
require "config.php";
// if RESET is set AND TRUE, it will setup the charge-discharge to 00:00 all over so nothing will charge or discharge
//$RESET=TRUE;
// php phpsoliscontrol.php reset
// will force RESET and clear all values to no operation
if ((isset($argv)) && (in_array("reset",$argv))) $RESET=TRUE;
//Some inverter models ONLY use the FIRST charge/discharge values,
//Repeating the setting in the three chargeSettings
//Setup here the values you want to push to the Inverter
$chargeCurrent=10;
$DISchargeCurrent=10;
$chargeSettings = [
[
'chargeCurrent' => $chargeCurrent,
'dischargeCurrent' => $DISchargeCurrent,
'chargeStartTime' => "06:00",
'chargeEndTime' => "07:58",
'dischargeStartTime' => "00:00",
'dischargeEndTime' => "00:00"
],
[
'chargeCurrent' => $chargeCurrent,
'dischargeCurrent' => $DISchargeCurrent,
'chargeStartTime' => "00:00",
'chargeEndTime' => "00:00",
'dischargeStartTime' => "00:00",
'dischargeEndTime' => "00:00"
],
[
'chargeCurrent' => $chargeCurrent,
'dischargeCurrent' => $DISchargeCurrent,
'chargeStartTime' => "00:00",
'chargeEndTime' => "00:00",
'dischargeStartTime' => "00:00",
'dischargeEndTime' => "00:00"
]
];
// RESET
if (
(isset($RESET))
&&
($RESET===TRUE)
)
{
$chargeCurrent=10;
$DISchargeCurrent=10;
$chargeSettings = [
[
'chargeCurrent' => $chargeCurrent,
'dischargeCurrent' => $DISchargeCurrent,
'chargeStartTime' => "00:00",
'chargeEndTime' => "00:00",
'dischargeStartTime' => "00:00",
'dischargeEndTime' => "00:00"
],
[
'chargeCurrent' => $chargeCurrent,
'dischargeCurrent' => $DISchargeCurrent,
'chargeStartTime' => "00:00",
'chargeEndTime' => "00:00",
'dischargeStartTime' => "00:00",
'dischargeEndTime' => "00:00"
],
[
'chargeCurrent' => $chargeCurrent,
'dischargeCurrent' => $DISchargeCurrent,
'chargeStartTime' => "00:00",
'chargeEndTime' => "00:00",
'dischargeStartTime' => "00:00",
'dischargeEndTime' => "00:00"
]
];
}
//Generic function to API call SolisCloud endpoints
function soliscurl($keyAccount,$keyPass,$keyID,$keySecret,$endPoint,$body,$extraHeaders = array()) {
$contentMD5 = base64_encode(md5($body, true));
$contentType = "application/json";
$gmdate = gmdate("D, j M Y H:i:s") . " GMT";
$chain = "POST\n{$contentMD5}\n{$contentType}\n{$gmdate}\n{$endPoint}";
$signature = base64_encode(pack('H*',hash_hmac('sha1', $chain , $keySecret, false)));
$authorization = "API_" . $keyID . ":" . $signature;
$headers = array(
'Content-MD5: ' . $contentMD5,
'Authorization: ' . $authorization,
'Content-Type: ' . $contentType,
'Date: ' . $gmdate
);
//Add extra headers if any
if (is_array($extraHeaders))
foreach ($extraHeaders as $hkey => $hvalue)
$headers[$hkey]=$headers[$hvalue];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://www.soliscloud.com:13333'.$endPoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => $headers
));
//$response = json_decode(curl_exec($curl),TRUE);
//Fixes Solis returning malformed jsons as per https://github.com/stevegal/solis_control/issues/21
$response = json_decode(str_replace(",
},","},",curl_exec($curl)),TRUE);
curl_close($curl);
if (
(!empty($response['status']))
&&
($response['status']==403)
)
die("\n\nSomething went wrong, please check your configuration settings!\n\n");
return $response;
}
$jouts=array();
// Get Battery percentage and Inverter ID (needed to set the new timings)
$endPoint = "/v1/api/inverterList";
$body = '{"pageNo":1,"pageSize":10}';
$response=soliscurl($keyAccount,$keyPass,$keyID,$keySecret,$endPoint,$body);
// Confirm inverter id has returned values and batteryCapacitySoc is integer from 1 to 100
if (
(empty($response['data']['page']['records'][0]['id']))
||
(!ctype_digit((string) $response['data']['page']['records'][0]['id']))
||
(empty($response['data']['page']['records'][0]['batteryCapacitySoc']))
||
(!ctype_digit((string) $response['data']['page']['records'][0]['batteryCapacitySoc']))
||
(
!
(
($response['data']['page']['records'][0]['batteryCapacitySoc']>0)
&&
($response['data']['page']['records'][0]['batteryCapacitySoc']<=100)
)
)
)
die("\n\nInverter id and batteryCapacitySoc retrieved must be an integer to confirm we can connect to SolisCloud. Review your settings!\n\n");
//Battery percentage (just the integer)
$jouts['battery']=$response['data']['page']['records'][0]['batteryCapacitySoc'];
//inverter ID
$jouts['inverter']=$response['data']['page']['records'][0]['id'];
//Get Token
$endPoint = "/v2/api/login";
$body = '{"userInfo":"' . $keyAccount . '","passWord":"' . md5($keyPass) . '"}';
$response=soliscurl($keyAccount,$keyPass,$keyID,$keySecret,$endPoint,$body);
if (
(empty($response['csrfToken']))
||
(strpos($response['csrfToken'],"token_")!==0) //does not start with "token_"
)
die("\n\nUnable to retrieve a token for Inverter Control. Are your username and password correctly set?\n\n");
$jouts['token']=$response['csrfToken'];
//Send Control Command with extra token header and values from $chargeSettings
$endPoint = "/v2/api/control";
$body = '{"inverterId":"' . $jouts['inverter'] . '", "cid":"103",';
$body .= '"value":"' . implode(",",$chargeSettings[0]) . "," . implode(",",$chargeSettings[1]) . "," . implode(",",$chargeSettings[2]);
$body .= '"}';
$response=soliscurl($keyAccount,$keyPass,$keyID,$keySecret,$endPoint,$body,array("token" => $jouts['token']));
print_R($response);
?>