forked from DeltaVetal26/SteVe-OCPP-HTTP-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.php
348 lines (319 loc) · 12.3 KB
/
main.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
// Steve OCPP HTTP client emulator (v1.5)
// NOTE: Do not use the & symbol in login, steve password, and AuthKey. It won't work.
// Configuration
$steveServerAddres = 'http://00.00.000.00:8080';
$steveLogin = '';
$stevePass = '';
$authKey = '';
$ocppProtocol = 'JSON'; // or JSON
$ocppVersion = 'v1.6'; // or 1.6
$supervision = 'steve';
// Only for SOAP use - charge point endpoint url
// Write here your charge point endpoint url
$endpointURL = 'http://localhost:9090/ocpp'; // (ex: http://localhost:9090/ocpp)
// Steve commands path array
$stevePathArray = array(
// Local cmd (not use)
'signin' => '/' . $supervision . '/manager/signin',
'getTransaction' => '/' . $supervision . '/manager/transactions',
'getConnectorState' => '/' . $supervision . '/manager/home/connectorStatus',
// OCPP cmd
'ReserveNow' => '/' . $supervision . '/manager/operations/' . $ocppVersion . '/ReserveNow',
'RemoteStartTransaction' => '/' . $supervision . '/manager/operations/' . $ocppVersion . '/RemoteStartTransaction',
'RemoteStopTransaction' => '/' . $supervision . '/manager/operations/' . $ocppVersion . '/RemoteStopTransaction',
'UnlockConnector' => '/' . $supervision . '/manager/operations/' . $ocppVersion . '/UnlockConnector',
'DataTransfer' => '/' . $supervision . '/manager/operations/' . $ocppVersion . '/DataTransfer',
'Reset' => '/' . $supervision . '/manager/operations/' . $ocppVersion . '/Reset',
'SetChargingProfile' => '/' . $supervision . '/manager/operations/' . $ocppVersion . '/SetChargingProfile'
);
// Variables
$getData = $_GET;
$curl = "";
// Using endpoint? (for SOAP only)
if($ocppProtocol == 'JSON') { $endpointURL = '-'; }
// Functions
// # cURL init
function curlConnectionInit($curl) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3');
return $curl;
}
// # Connect to URL
function curlConnectTo($steveServerAddres, $stevePathArray) {
global $curl;
$steveServerURL = $steveServerAddres . $stevePathArray;
curl_setopt($curl, CURLOPT_URL, $steveServerURL);
$content = curl_exec($curl);
//echo $content;
return $content;
}
// # Get CSRF token
function getCSRFToken($content) {
preg_match('/<input type="hidden" name="_csrf" value="(.*)"/Uis', $content, $csrf);
$csrf = $csrf[1];
if($csrf != NULL) {
return $csrf;
} else {
return NULL;
}
}
// # SignIn to Steve panel
function steveSignIn($login, $pass) {
global $curl, $steveServerAddres, $stevePathArray;
$content = curlConnectTo($steveServerAddres, $stevePathArray['signin']);
$token = getCSRFToken($content);
$form = "username=".$login."&password=".$pass."&_csrf=".$token."";
curl_setopt($curl, CURLOPT_POSTFIELDS, $form);
curl_exec($curl);
}
// # Get element by class (for HTML DOM parse)
function getElementsByClass(&$parentNode, $tagName, $className) {
$nodes = array();
$childNodeList = $parentNode->getElementsByTagName($tagName);
for($i = 0; $i < $childNodeList->length; $i++) {
$temp = $childNodeList->item($i);
if(stripos($temp->getAttribute('class'), $className) !== false) {
return $temp;
}
}
}
// # Parse html (Get data)
function htmlParser($getData, $mode) {
global $curl, $content, $steveServerAddres, $stevePathArray;
// NULL ChargeBoxID protection
if($getData['ChargeBoxID'] == NULL) {
return 'NullBoxID';
}
// Null mode protectio
if($mode == NULL) {
return 'NullMode';
}
// Get content
// Not used for DataTransfer (DataTransfer connected before calling this function)
if($mode != 'getDataTransferResponse') {
$content = curlConnectTo($steveServerAddres, $stevePathArray[$mode]);
}
// Create DOM element
$domdoc = new DOMDocument();
libxml_use_internal_errors(true);
$domdoc->loadHTML($content);
$domdoc->saveHTML();
// Get table (for DataTransfer respond)
if($mode == 'getDataTransferResponse') {
$domdoc = getElementsByClass($domdoc, 'table', 'res');
}
// Get 'th' array
$thArray = $tdArray = $array = array();
$th = $domdoc->getElementsByTagName('th');
foreach ($th as $th) {
$thArray[] = $th->nodeValue;
}
$count = count($thArray);
// Get 'td' and create result array
$dataArray = [];
foreach($domdoc->getElementsByTagName('tr') as $tr) {
$data = $tr->getElementsByTagName('td');
if(count($data) > 0) {
$tdArray = [];
foreach($data as $td) {
$tdArray[] = $td->textContent;
}
$dataArray[] = @array_combine($thArray, $tdArray);
}
}
// Select mode and get values
// GetTransaction
if($mode == 'getTransaction') {
// Search transaction ID
foreach ($dataArray as $key) {
if(($key['ChargeBox ID'] == $getData['ChargeBoxID']) && ($key['Connector ID'] == $getData['ConnectorID'])) {
// Return transaction ID
return $key['Transaction ID'];
}
}
return 'TransactionNotExist';
}
// Get connector state
else if($mode == 'getConnectorState') {
// Search last connector state
foreach ($dataArray as $key) {
if(($key['ChargeBox ID'] == $getData['ChargeBoxID']) && ($key['Connector ID'] == $getData['ConnectorID'])) {
// Return transaction ID
return $key['Status'];
}
}
return 'StateNotExist';
}
// Get DataTransfer respose
else if($mode == 'getDataTransferResponse') {
$response = $dataArray[1]['Response'];
if($response == NULL) {
return 'ResponseNotFound';
}
return $response;
}
}
// # Command selector
function cmdInputSelector($getData) {
global $curl, $content, $steveServerAddres, $stevePathArray, $ocppProtocol, $endpointURL;
// Set path
$stevePath = $stevePathArray[$getData['cmd']];
// Select command
switch ($getData['cmd']) {
case 'getConnectorState':
$allow = true; // Allow command?
if($allow) {
// Get connector state
$connectorState = htmlParser($getData, 'getConnectorState');
return $connectorState;
}
break;
case 'ReserveNow':
$allow = true; // Allow command?
if($allow) {
// Redirect to ReserveNow page
$content = curlConnectTo($steveServerAddres, $stevePath);
// Get token
$token = getCSRFToken($content);
// Prepare form
$form = "chargePointSelectList=".$ocppProtocol.";".$getData['ChargeBoxID'].";".$endpointURL."&connectorId=".$getData['ConnectorID']."&expiry=".$getData['Expiry']."&idTag=".$getData['idTag']."&_csrf=".$token."";
// Send form
curl_setopt($curl, CURLOPT_POSTFIELDS, $form);
curl_exec($curl);
return 'Ok';
}
break;
case 'RemoteStartTransaction':
$allow = true; // Allow command?
if($allow) {
// Redirect to RemoteStartTransaction page
$content = curlConnectTo($steveServerAddres, $stevePath);
// Get token
$token = getCSRFToken($content);
// Prepare form
$form = "chargePointSelectList=".$ocppProtocol.";".$getData['ChargeBoxID'].";".$endpointURL."&connectorId=".$getData['ConnectorID']."&idTag=".$getData['idTag']."&_csrf=".$token."";
// Send form
curl_setopt($curl, CURLOPT_POSTFIELDS, $form);
curl_exec($curl);
return 'Ok';
}
break;
case 'RemoteStopTransaction':
$allow = true; // Allow command?
if($allow) {
// Get transaction ID
$steveTransactionID = htmlParser($getData, 'getTransaction');
// Redirect to RemoteStopTransaction page
$content = curlConnectTo($steveServerAddres, $stevePath);
// Get token
$token = getCSRFToken($content);
// Prepare form
$form = "chargePointSelectList=".$ocppProtocol.";".$getData['ChargeBoxID'].";".$endpointURL."&transactionId=".$steveTransactionID."&_csrf=".$token."";
// Send form
curl_setopt($curl, CURLOPT_POSTFIELDS, $form);
curl_exec($curl);
return 'Ok';
}
break;
case 'UnlockConnector':
$allow = true; // Allow command?
if($allow) {
// Redirect to RemoteStartTransaction page
$content = curlConnectTo($steveServerAddres, $stevePath);
// Get token
$token = getCSRFToken($content);
// Prepare form
$form = "chargePointSelectList=".$ocppProtocol.";".$getData['ChargeBoxID'].";".$endpointURL."&connectorId=".$getData['ConnectorID']."&_csrf=".$token."";
// Send form
curl_setopt($curl, CURLOPT_POSTFIELDS, $form);
curl_exec($curl);
return 'Ok';
}
break;
case 'DataTransfer':
$allow = true; // Allow command?
if($allow) {
// Redirect to RemoteStartTransaction page
$content = curlConnectTo($steveServerAddres, $stevePath);
// Get token
$token = getCSRFToken($content);
// Prepare form
$form = "chargePointSelectList=".$ocppProtocol.";".$getData['ChargeBoxID'].";".$endpointURL."&vendorId=".$getData['VendorID']."&messageId=".$getData['MessageID']."&data=".$getData['Data']."&_csrf=".$token."";
// Send form
curl_setopt($curl, CURLOPT_POSTFIELDS, $form);
curl_exec($curl);
// Sleep.. (Wait response)
sleep(5);
// Get response
$content = curl_exec($curl);
// Parse response
$response = htmlParser($getData, 'getDataTransferResponse');
// Return response
return $response;
}
break;
case 'Reset':
$allow = true; // Allow command?
if($allow) {
// Redirect to Reset page
$content = curlConnectTo($steveServerAddres, $stevePath);
// Get token
$token = getCSRFToken($content);
// Prepare form
$cbid = explode(";",$getData['ChargeBoxID']);
$toReset = "";
for($i = 0; $i < count($cbid); $i++) {
$toReset = $toReset . "chargePointSelectList=".$ocppProtocol.";" .$cbid[$i] . ";".$endpointURL."&";
}
$form = $toReset . "_chargePointSelectList=1&resetType=HARD&_csrf=".$token."";
// Send form
curl_setopt($curl, CURLOPT_POSTFIELDS, $form);
curl_exec($curl);
return 'Ok';
}
break;
case 'SetChargingProfile':
$allow = true; // Allow command?
if($allow) {
// Redirect to Reset page
$content = curlConnectTo($steveServerAddres, $stevePath);
// Get token
$token = getCSRFToken($content);
// Prepare form
$form = "chargePointSelectList=".$ocppProtocol.";".$getData['ChargeBoxID'].";".$endpointURL."&connectorId=".$getData['ConnectorID']."&chargingProfilePk=".$getData['ChargingProfileID']."&_csrf=".$token."";
// Send form
curl_setopt($curl, CURLOPT_POSTFIELDS, $form);
curl_exec($curl);
return 'Ok';
}
break;
default:
// Unknown command
return 'Unknown command';
break;
}
// Command not allow?
if(!$allow) {
return 'Command NotAllow';
}
}
/*** Сode execution ***/
// Auth
if($getData['key'] != $authKey) {
return;
}
// Init connection
$curl = curlConnectionInit($curl);
// SignIn Steve panel
steveSignIn($steveLogin, $stevePass);
// Select page and send cmd
$result = cmdInputSelector($getData);
echo $result;
?>