forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoOcpp.cpp
More file actions
462 lines (388 loc) · 17.1 KB
/
ArduinoOcpp.cpp
File metadata and controls
462 lines (388 loc) · 17.1 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// matth-x/ArduinoOcpp
// Copyright Matthias Akstaller 2019 - 2021
// MIT License
#include "ArduinoOcpp.h"
#include "Variants.h"
#if USE_FACADE
#include <ArduinoOcpp/Core/OcppEngine.h>
#include <ArduinoOcpp/Core/OcppSocket.h>
#include <ArduinoOcpp/Tasks/Metering/MeteringService.h>
#include <ArduinoOcpp/Tasks/SmartCharging/SmartChargingService.h>
#include <ArduinoOcpp/Tasks/ChargePointStatus/ChargePointStatusService.h>
#include <ArduinoOcpp/Tasks/Heartbeat/HeartbeatService.h>
#include <ArduinoOcpp/Tasks/FirmwareManagement/FirmwareService.h>
#include <ArduinoOcpp/Tasks/Diagnostics/DiagnosticsService.h>
#include <ArduinoOcpp/SimpleOcppOperationFactory.h>
#include <ArduinoOcpp/Core/Configuration.h>
#include <ArduinoOcpp/MessagesV16/Authorize.h>
#include <ArduinoOcpp/MessagesV16/BootNotification.h>
#include <ArduinoOcpp/MessagesV16/StartTransaction.h>
#include <ArduinoOcpp/MessagesV16/StopTransaction.h>
#include <ArduinoOcpp/Core/OcppOperationTimeout.h>
namespace ArduinoOcpp {
namespace Facade {
#ifndef AO_CUSTOM_WS
WebSocketsClient webSocket;
#endif
OcppSocket *ocppSocket;
MeteringService *meteringService;
PowerSampler powerSampler;
EnergySampler energySampler;
std::function<bool()> evRequestsEnergySampler = NULL; //bool (*evRequestsEnergySampler)() = NULL;
bool evRequestsEnergyLastState = false;
std::function<bool()> connectorEnergizedSampler = NULL;
bool connectorEnergizedLastState = false;
SmartChargingService *smartChargingService;
ChargePointStatusService *chargePointStatusService;
HeartbeatService *heartbeatService;
FirmwareService *firmwareService = NULL;
DiagnosticsService *diagnosticsServce = NULL;
OnLimitChange onLimitChange;
OcppTime *ocppTime;
#define OCPP_NUMCONNECTORS 2
#define OCPP_ID_OF_CONNECTOR 1
#define OCPP_ID_OF_CP 0
boolean OCPP_initialized = false;
boolean OCPP_booted = false; //if BootNotification succeeded
#if 0 //moved to OcppConnection
/*
Called by Websocket library on incoming message on the internet link
*/
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch (type) {
case WStype_DISCONNECTED:
Serial.print(F("[WSc] Disconnected!\n"));
break;
case WStype_CONNECTED:
Serial.printf("[WSc] Connected to url: %s\n", payload);
break;
case WStype_TEXT:
if (DEBUG_OUT || TRAFFIC_OUT) Serial.printf("[WSc] get text: %s\n", payload);
if (!processWebSocketEvent((const char *) payload, length)) { //forward message to OcppEngine
Serial.print(F("[WSc] Processing WebSocket input event failed!\n"));
}
break;
case WStype_FRAGMENT_TEXT_START: //fragments are not supported
Serial.print(F("[WSc] Fragments are not supported\n"));
if (!processWebSocketUnsupportedEvent((const char *) payload, length)) { //forward message to OcppEngine
Serial.print(F("[WSc] Processing WebSocket input event failed!\n"));
}
break;
case WStype_BIN:
Serial.print(F("[WSc] Incoming binary data stream not supported"));
break;
case WStype_PING:
// pong will be send automatically
Serial.print(F("[WSc] get ping\n"));
break;
case WStype_PONG:
// answer to a ping we send
Serial.print(F("[WSc] get pong\n"));
break;
default:
Serial.print(F("[WSc] Unsupported WebSocket event type\n"));
break;
}
}
#endif //moved to OcppConnection
} //end namespace ArduinoOcpp::Facade
} //end namespace ArduinoOcpp
using namespace ArduinoOcpp;
using namespace ArduinoOcpp::Facade;
using namespace ArduinoOcpp::Ocpp16;
#ifndef AO_CUSTOM_WS
void OCPP_initialize(String CS_hostname, uint16_t CS_port, String CS_url, float V_eff, ArduinoOcpp::FilesystemOpt fsOpt, ArduinoOcpp::OcppClock system_time) {
if (OCPP_initialized) {
Serial.print(F("[ArduinoOcpp] Error: cannot call OCPP_initialize() two times! If you want to reconfigure the library, please restart your ESP\n"));
return;
}
// server address, port and URL
webSocket.begin(CS_hostname, CS_port, CS_url, "ocpp1.6");
// event handler
//webSocket.onEvent(webSocketEvent); //will be set in ocppEngine_initialize()
// use HTTP Basic Authorization this is optional remove if not needed
// webSocket.setAuthorization("user", "Password");
// try ever 5000 again if connection has failed
webSocket.setReconnectInterval(5000);
// start heartbeat (optional)
// ping server every 15000 ms
// expect pong from server within 3000 ms
// consider connection disconnected if pong is not received 2 times
webSocket.enableHeartbeat(15000, 3000, 2); //comment this one out to for specific OCPP servers
ocppSocket = new EspWiFi::OcppClientSocket(&webSocket);
OCPP_initialize(ocppSocket, V_eff, fsOpt);
}
#endif
void OCPP_initialize(OcppSocket *ocppSocket, float V_eff, ArduinoOcpp::FilesystemOpt fsOpt, ArduinoOcpp::OcppClock system_time) {
if (OCPP_initialized) {
Serial.print(F("[ArduinoOcpp] Error: cannot call OCPP_initialize() two times! If you want to reconfigure the library, please restart your ESP\n"));
return;
}
if (!ocppSocket) {
Serial.print(F("[ArduinoOcpp] OCPP_initialize(ocppSocket): ocppSocket cannot be NULL!\n"));
return;
}
configuration_init(fsOpt); //call before each other library call
ocppEngine_initialize(ocppSocket);
ocppTime = new OcppTime(system_time);
ocppEngine_setOcppTime(ocppTime);
smartChargingService = new SmartChargingService(11000.0f, V_eff, OCPP_NUMCONNECTORS, ocppTime, fsOpt); //default charging limit: 11kW
chargePointStatusService = new ChargePointStatusService(OCPP_NUMCONNECTORS, ocppTime); //Constructor adds instance to ocppEngine in constructor
meteringService = new MeteringService(OCPP_NUMCONNECTORS, ocppTime);
heartbeatService = new HeartbeatService();
#if !defined(AO_CUSTOM_UPDATER) && !defined(AO_CUSTOM_WEBSOCKET)
firmwareService = EspWiFi::makeFirmwareService("12345789"); //instantiate FW service + ESP installation routine
#else
firmwareService = new FirmwareService(); //only instantiate FW service
#endif
setFirmwareService(firmwareService);
#if !defined(AO_CUSTOM_DIAGNOSTICS) && !defined(AO_CUSTOM_WEBSOCKET)
diagnosticsServce = EspWiFi::makeDiagnosticsService(); //will only return "Rejected" because logging is not implemented yet
#else
diagnosticsServce = new DiagnosticsService();
#endif
setDiagnosticsService(diagnosticsServce);
OCPP_initialized = true;
}
void OCPP_loop() {
if (!OCPP_initialized) {
Serial.print(F("[ArduinoOcpp] Error: you must call OCPP_initialize before calling the loop() function!\n"));
delay(200); //Prevent this error message from flooding the Serial monitor.
return;
}
//webSocket.loop(); //moved to Core/OcppSocket
ocppEngine_loop(); //mandatory
if (!OCPP_booted) {
if (chargePointStatusService->isBooted()) {
OCPP_booted = true;
} else {
return; //wait until the first BootNotification succeeded
}
}
if (onLimitChange != NULL) {
smartChargingService->loop(); //optional
}
chargePointStatusService->loop(); //optional
if (powerSampler != NULL || energySampler != NULL) {
meteringService->loop(); //optional
}
heartbeatService->loop();
if (firmwareService != NULL) {
firmwareService->loop();
}
if (diagnosticsServce != NULL) {
diagnosticsServce->loop();
}
bool evRequestsEnergyNewState = true;
if (evRequestsEnergySampler != NULL) {
evRequestsEnergyNewState = evRequestsEnergySampler();
} else {
if (powerSampler != NULL) {
evRequestsEnergyNewState = powerSampler() >= 5.f;
}
}
if (!evRequestsEnergyLastState && evRequestsEnergyNewState) {
evRequestsEnergyLastState = true;
chargePointStatusService->getConnector(OCPP_ID_OF_CONNECTOR)->startEvDrawsEnergy();
} else if (evRequestsEnergyLastState && !evRequestsEnergyNewState) {
evRequestsEnergyLastState = false;
chargePointStatusService->getConnector(OCPP_ID_OF_CONNECTOR)->stopEvDrawsEnergy();
}
bool connectorEnergizedNewState = true;
if (connectorEnergizedSampler != NULL) {
connectorEnergizedNewState = connectorEnergizedSampler();
} else {
connectorEnergizedNewState = getTransactionId() >= 0;
}
if (!connectorEnergizedLastState && connectorEnergizedNewState) {
connectorEnergizedLastState = true;
chargePointStatusService->getConnector(OCPP_ID_OF_CONNECTOR)->startEnergyOffer();
} else if (connectorEnergizedLastState && !connectorEnergizedNewState) {
connectorEnergizedLastState = false;
chargePointStatusService->getConnector(OCPP_ID_OF_CONNECTOR)->stopEnergyOffer();
}
}
void setPowerActiveImportSampler(std::function<float()> power) {
powerSampler = power;
meteringService->setPowerSampler(OCPP_ID_OF_CONNECTOR, powerSampler); //connectorId=1
}
void setEnergyActiveImportSampler(std::function<float()> energy) {
energySampler = energy;
meteringService->setEnergySampler(OCPP_ID_OF_CONNECTOR, energySampler); //connectorId=1
}
void setEvRequestsEnergySampler(std::function<bool()> evRequestsEnergy) {
evRequestsEnergySampler = evRequestsEnergy;
}
void setConnectorEnergizedSampler(std::function<bool()> connectorEnergized) {
connectorEnergizedSampler = connectorEnergized;
}
void setConnectorPluggedSampler(std::function<bool()> connectorPlugged) {
ConnectorStatus *connector = getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (connector) {
connector->setConnectorPluggedSampler(connectorPlugged);
} else {
Serial.print(F("[ArduinoOcpp] Error: called setConnectorPluggedSampler before initializing the library!\n"));
}
}
//void setConnectorFaultedSampler(std::function<bool()> connectorFaulted) {
// ConnectorStatus *connector = getConnectorStatus(OCPP_ID_OF_CONNECTOR);
// if (connector) {
// connector->setConnectorFaultedSampler(connectorFaulted);
// } else {
// Serial.print(F("[ArduinoOcpp] Error: called setConnectorFaultedSampler before initializing the library!\n"));
// }
//}
void addConnectorErrorCodeSampler(std::function<const char *()> connectorErrorCode) {
ConnectorStatus *connector = getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (connector) {
connector->addConnectorErrorCodeSampler(connectorErrorCode);
} else {
Serial.print(F("[ArduinoOcpp] Error: called addConnectorErrorCodeSampler before initializing the library!\n"));
}
}
void setOnChargingRateLimitChange(std::function<void(float)> chargingRateChanged) {
onLimitChange = chargingRateChanged;
smartChargingService->setOnLimitChange(onLimitChange);
}
void setOnUnlockConnector(std::function<bool()> unlockConnector) {
ConnectorStatus *connector = getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (connector) {
connector->setOnUnlockConnector(unlockConnector);
} else {
Serial.print(F("[ArduinoOcpp] Error: called setOnUnlockConnector before initializing the library!\n"));
}
}
void setOnSetChargingProfileRequest(OnReceiveReqListener onReceiveReq) {
setOnSetChargingProfileRequestListener(onReceiveReq);
}
void setOnRemoteStartTransactionSendConf(OnSendConfListener onSendConf) {
setOnRemoteStartTransactionSendConfListener(onSendConf);
}
void setOnRemoteStopTransactionReceiveReq(OnReceiveReqListener onReceiveReq) {
setOnRemoteStopTransactionReceiveRequestListener(onReceiveReq);
}
void setOnRemoteStopTransactionSendConf(OnSendConfListener onSendConf) {
setOnRemoteStopTransactionSendConfListener(onSendConf);
}
void setOnResetSendConf(OnSendConfListener onSendConf) {
setOnResetSendConfListener(onSendConf);
}
void setOnResetReceiveReq(OnReceiveReqListener onReceiveReq) {
setOnResetReceiveRequestListener(onReceiveReq);
}
void authorize(String &idTag, OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, Timeout *timeout) {
OcppOperation *authorize = makeOcppOperation(
new Authorize(idTag));
initiateOcppOperation(authorize);
if (onConf)
authorize->setOnReceiveConfListener(onConf);
if (onAbort)
authorize->setOnAbortListener(onAbort);
if (onTimeout)
authorize->setOnTimeoutListener(onTimeout);
if (onError)
authorize->setOnReceiveErrorListener(onError);
if (timeout)
authorize->setTimeout(timeout);
else
authorize->setTimeout(new FixedTimeout(20000));
}
void bootNotification(String chargePointModel, String chargePointVendor, OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, Timeout *timeout) {
OcppOperation *bootNotification = makeOcppOperation(
new BootNotification(chargePointModel, chargePointVendor));
initiateOcppOperation(bootNotification);
if (onConf)
bootNotification->setOnReceiveConfListener(onConf);
if (onAbort)
bootNotification->setOnAbortListener(onAbort);
if (onTimeout)
bootNotification->setOnTimeoutListener(onTimeout);
if (onError)
bootNotification->setOnReceiveErrorListener(onError);
if (timeout)
bootNotification->setTimeout(timeout);
else
bootNotification->setTimeout(new SuppressedTimeout());
}
void bootNotification(String &chargePointModel, String &chargePointVendor, String &chargePointSerialNumber, OnReceiveConfListener onConf) {
OcppOperation *bootNotification = makeOcppOperation(
new BootNotification(chargePointModel, chargePointVendor, chargePointSerialNumber));
initiateOcppOperation(bootNotification);
bootNotification->setOnReceiveConfListener(onConf);
bootNotification->setTimeout(new SuppressedTimeout());
}
void bootNotification(DynamicJsonDocument *payload, OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, Timeout *timeout) {
OcppOperation *bootNotification = makeOcppOperation(
new BootNotification(payload));
initiateOcppOperation(bootNotification);
if (onConf)
bootNotification->setOnReceiveConfListener(onConf);
if (onAbort)
bootNotification->setOnAbortListener(onAbort);
if (onTimeout)
bootNotification->setOnTimeoutListener(onTimeout);
if (onError)
bootNotification->setOnReceiveErrorListener(onError);
if (timeout)
bootNotification->setTimeout(timeout);
else
bootNotification->setTimeout(new SuppressedTimeout());
}
void startTransaction(OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, Timeout *timeout) {
OcppOperation *startTransaction = makeOcppOperation(
new StartTransaction(OCPP_ID_OF_CONNECTOR));
initiateOcppOperation(startTransaction);
if (onConf)
startTransaction->setOnReceiveConfListener(onConf);
if (onAbort)
startTransaction->setOnAbortListener(onAbort);
if (onTimeout)
startTransaction->setOnTimeoutListener(onTimeout);
if (onError)
startTransaction->setOnReceiveErrorListener(onError);
if (timeout)
startTransaction->setTimeout(timeout);
else
startTransaction->setTimeout(new SuppressedTimeout());
}
void startTransaction(String &idTag, OnReceiveConfListener onConf) {
OcppOperation *startTransaction = makeOcppOperation(
new StartTransaction(OCPP_ID_OF_CONNECTOR, idTag));
initiateOcppOperation(startTransaction);
startTransaction->setOnReceiveConfListener(onConf);
startTransaction->setTimeout(new SuppressedTimeout());
}
void stopTransaction(OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, Timeout *timeout) {
OcppOperation *stopTransaction = makeOcppOperation(
new StopTransaction(OCPP_ID_OF_CONNECTOR));
initiateOcppOperation(stopTransaction);
if (onConf)
stopTransaction->setOnReceiveConfListener(onConf);
if (onAbort)
stopTransaction->setOnAbortListener(onAbort);
if (onTimeout)
stopTransaction->setOnTimeoutListener(onTimeout);
if (onError)
stopTransaction->setOnReceiveErrorListener(onError);
if (timeout)
stopTransaction->setTimeout(timeout);
else
stopTransaction->setTimeout(new SuppressedTimeout());
}
//void startEvDrawsEnergy() {
// chargePointStatusService->getConnector(OCPP_ID_OF_CONNECTOR)->startEvDrawsEnergy();
//}
//void stopEvDrawsEnergy() {
// chargePointStatusService->getConnector(OCPP_ID_OF_CONNECTOR)->stopEvDrawsEnergy();
//}
int getTransactionId() {
return chargePointStatusService->getConnector(OCPP_ID_OF_CONNECTOR)->getTransactionId();
}
bool existsUnboundIdTag() {
return chargePointStatusService->existsUnboundAuthorization();
}
bool isAvailable() {
return (chargePointStatusService->getConnector(OCPP_ID_OF_CP)->getAvailability() != AVAILABILITY_INOPERATIVE)
&& (chargePointStatusService->getConnector(OCPP_ID_OF_CONNECTOR)->getAvailability() != AVAILABILITY_INOPERATIVE);
}
#endif