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
508 lines (443 loc) · 17.6 KB
/
Copy pathArduinoOcpp.cpp
File metadata and controls
508 lines (443 loc) · 17.6 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// matth-x/ArduinoOcpp
// Copyright Matthias Akstaller 2019 - 2022
// MIT License
#include "ArduinoOcpp.h"
#include <ArduinoOcpp/Core/OcppEngine.h>
#include <ArduinoOcpp/Core/OcppModel.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/Debug.h>
namespace ArduinoOcpp {
namespace Facade {
#ifndef AO_CUSTOM_WS
WebSocketsClient *webSocket {nullptr};
OcppSocket *ocppSocket {nullptr};
#endif
OcppEngine *ocppEngine {nullptr};
FilesystemOpt fileSystemOpt {};
float voltage_eff {230.f};
#define OCPP_NUMCONNECTORS 2
#define OCPP_ID_OF_CONNECTOR 1
#define OCPP_ID_OF_CP 0
boolean OCPP_booted = false; //if BootNotification succeeded
} //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(const char *CS_hostname, uint16_t CS_port, const char *CS_url, float V_eff, ArduinoOcpp::FilesystemOpt fsOpt, ArduinoOcpp::OcppClock system_time) {
if (ocppEngine) {
AO_DBG_WARN("Can't be called two times. Either restart ESP, or call OCPP_deinitialize() before");
return;
}
if (!webSocket)
webSocket = new WebSocketsClient();
// server address, port and URL
webSocket->begin(CS_hostname, CS_port, CS_url, "ocpp1.6");
// 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
delete ocppSocket;
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 (ocppEngine) {
AO_DBG_WARN("Can't be called two times. To change the credentials, either restart ESP, or call OCPP_deinitialize() before");
return;
}
voltage_eff = V_eff;
fileSystemOpt = fsOpt;
configuration_init(fileSystemOpt); //call before each other library call
ocppEngine = new OcppEngine(ocppSocket, system_time);
auto& model = ocppEngine->getOcppModel();
model.setChargePointStatusService(std::unique_ptr<ChargePointStatusService>(
new ChargePointStatusService(*ocppEngine, OCPP_NUMCONNECTORS)));
model.setHeartbeatService(std::unique_ptr<HeartbeatService>(
new HeartbeatService(*ocppEngine)));
#if !defined(AO_CUSTOM_UPDATER) && !defined(AO_CUSTOM_WS)
model.setFirmwareService(std::unique_ptr<FirmwareService>(
EspWiFi::makeFirmwareService(*ocppEngine, "1234578901"))); //instantiate FW service + ESP installation routine
#else
model.setFirmwareService(std::unique_ptr<FirmwareService>(
new FirmwareService(*ocppEngine))); //only instantiate FW service
#endif
#if !defined(AO_CUSTOM_DIAGNOSTICS) && !defined(AO_CUSTOM_WS)
model.setDiagnosticsService(std::unique_ptr<DiagnosticsService>(
EspWiFi::makeDiagnosticsService(*ocppEngine))); //will only return "Rejected" because logging is not implemented yet
#else
model.setDiagnosticsService(std::unique_ptr<DiagnosticsService>(
new DiagnosticsService(*ocppEngine)));
#endif
ocppEngine->setRunOcppTasks(false); //prevent OCPP classes from doing anything while booting
}
void OCPP_deinitialize() {
AO_DBG_DEBUG("Still experimental function. If you find problems, it would be great if you publish them on the GitHub page");
delete ocppEngine;
ocppEngine = nullptr;
#ifndef AO_CUSTOM_WS
delete ocppSocket;
ocppSocket = nullptr;
delete webSocket;
webSocket = nullptr;
#endif
simpleOcppFactory_deinitialize();
fileSystemOpt = FilesystemOpt();
voltage_eff = 230.f;
OCPP_booted = false;
}
void OCPP_loop() {
if (!ocppEngine) {
AO_DBG_WARN("Please call OCPP_initialize before");
delay(200); //Prevent this message from flooding the Serial monitor.
return;
}
ocppEngine->loop();
auto& model = ocppEngine->getOcppModel();
if (!OCPP_booted) {
auto csService = model.getChargePointStatusService();
if (!csService || csService->isBooted()) {
OCPP_booted = true;
ocppEngine->setRunOcppTasks(true);
} else {
return; //wait until the first BootNotification succeeded
}
}
}
void setPowerActiveImportSampler(std::function<float()> power) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
auto& model = ocppEngine->getOcppModel();
if (!model.getMeteringService()) {
model.setMeteringSerivce(std::unique_ptr<MeteringService>(
new MeteringService(*ocppEngine, OCPP_NUMCONNECTORS)));
}
model.getMeteringService()->setPowerSampler(OCPP_ID_OF_CONNECTOR, power); //connectorId=1
}
void setEnergyActiveImportSampler(std::function<float()> energy) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
auto& model = ocppEngine->getOcppModel();
if (!model.getMeteringService()) {
model.setMeteringSerivce(std::unique_ptr<MeteringService>(
new MeteringService(*ocppEngine, OCPP_NUMCONNECTORS)));
}
model.getMeteringService()->setEnergySampler(OCPP_ID_OF_CONNECTOR, energy); //connectorId=1
}
void setEvRequestsEnergySampler(std::function<bool()> evRequestsEnergy) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->setEvRequestsEnergySampler(evRequestsEnergy);
}
void setConnectorEnergizedSampler(std::function<bool()> connectorEnergized) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->setConnectorEnergizedSampler(connectorEnergized);
}
void setConnectorPluggedSampler(std::function<bool()> connectorPlugged) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->setConnectorPluggedSampler(connectorPlugged);
}
void addConnectorErrorCodeSampler(std::function<const char *()> connectorErrorCode) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->addConnectorErrorCodeSampler(connectorErrorCode);
}
void setOnChargingRateLimitChange(std::function<void(float)> chargingRateChanged) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
auto& model = ocppEngine->getOcppModel();
if (!model.getSmartChargingService()) {
model.setSmartChargingService(std::unique_ptr<SmartChargingService>(
new SmartChargingService(*ocppEngine, 11000.0f, voltage_eff, OCPP_NUMCONNECTORS, fileSystemOpt))); //default charging limit: 11kW
}
model.getSmartChargingService()->setOnLimitChange(chargingRateChanged);
}
void setOnUnlockConnector(std::function<bool()> unlockConnector) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->setOnUnlockConnector(unlockConnector);
}
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(const char *idTag, OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, std::unique_ptr<Timeout> timeout) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
if (!idTag || strnlen(idTag, IDTAG_LEN_MAX + 2) > IDTAG_LEN_MAX) {
AO_DBG_ERR("idTag format violation. Expect c-style string with at most %u characters", IDTAG_LEN_MAX);
return;
}
auto authorize = makeOcppOperation(
new Authorize(idTag));
if (onConf)
authorize->setOnReceiveConfListener(onConf);
if (onAbort)
authorize->setOnAbortListener(onAbort);
if (onTimeout)
authorize->setOnTimeoutListener(onTimeout);
if (onError)
authorize->setOnReceiveErrorListener(onError);
if (timeout)
authorize->setTimeout(std::move(timeout));
else
authorize->setTimeout(std::unique_ptr<Timeout>(new FixedTimeout(20000)));
ocppEngine->initiateOperation(std::move(authorize));
}
void bootNotification(const char *chargePointModel, const char *chargePointVendor, OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, std::unique_ptr<Timeout> timeout) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
auto bootNotification = makeOcppOperation(
new BootNotification(chargePointModel, chargePointVendor));
if (onConf)
bootNotification->setOnReceiveConfListener(onConf);
if (onAbort)
bootNotification->setOnAbortListener(onAbort);
if (onTimeout)
bootNotification->setOnTimeoutListener(onTimeout);
if (onError)
bootNotification->setOnReceiveErrorListener(onError);
if (timeout)
bootNotification->setTimeout(std::move(timeout));
else
bootNotification->setTimeout(std::unique_ptr<Timeout> (new SuppressedTimeout()));
ocppEngine->initiateOperation(std::move(bootNotification));
}
void bootNotification(DynamicJsonDocument *payload, OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, std::unique_ptr<Timeout> timeout) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
auto bootNotification = makeOcppOperation(
new BootNotification(payload));
if (onConf)
bootNotification->setOnReceiveConfListener(onConf);
if (onAbort)
bootNotification->setOnAbortListener(onAbort);
if (onTimeout)
bootNotification->setOnTimeoutListener(onTimeout);
if (onError)
bootNotification->setOnReceiveErrorListener(onError);
if (timeout)
bootNotification->setTimeout(std::move(timeout));
else
bootNotification->setTimeout(std::unique_ptr<Timeout>(new SuppressedTimeout()));
ocppEngine->initiateOperation(std::move(bootNotification));
}
void startTransaction(const char *idTag, OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, std::unique_ptr<Timeout> timeout) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
if (!idTag || strnlen(idTag, IDTAG_LEN_MAX + 2) > IDTAG_LEN_MAX) {
AO_DBG_ERR("idTag format violation. Expect c-style string with at most %u characters", IDTAG_LEN_MAX);
return;
}
auto startTransaction = makeOcppOperation(
new StartTransaction(OCPP_ID_OF_CONNECTOR, idTag));
if (onConf)
startTransaction->setOnReceiveConfListener(onConf);
if (onAbort)
startTransaction->setOnAbortListener(onAbort);
if (onTimeout)
startTransaction->setOnTimeoutListener(onTimeout);
if (onError)
startTransaction->setOnReceiveErrorListener(onError);
if (timeout)
startTransaction->setTimeout(std::move(timeout));
else
startTransaction->setTimeout(std::unique_ptr<Timeout>(new SuppressedTimeout()));
ocppEngine->initiateOperation(std::move(startTransaction));
}
void stopTransaction(OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, std::unique_ptr<Timeout> timeout) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
auto stopTransaction = makeOcppOperation(
new StopTransaction(OCPP_ID_OF_CONNECTOR));
if (onConf)
stopTransaction->setOnReceiveConfListener(onConf);
if (onAbort)
stopTransaction->setOnAbortListener(onAbort);
if (onTimeout)
stopTransaction->setOnTimeoutListener(onTimeout);
if (onError)
stopTransaction->setOnReceiveErrorListener(onError);
if (timeout)
stopTransaction->setTimeout(std::move(timeout));
else
stopTransaction->setTimeout(std::unique_ptr<Timeout>(new SuppressedTimeout()));
ocppEngine->initiateOperation(std::move(stopTransaction));
}
int getTransactionId() {
if (!ocppEngine) {
AO_DBG_WARN("Please call OCPP_initialize before");
return -1;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return -1;
}
return connector->getTransactionId();
}
bool ocppPermitsCharge() {
if (!ocppEngine) {
AO_DBG_WARN("Please call OCPP_initialize before");
return false;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return false;
}
return connector->ocppPermitsCharge();
}
bool isAvailable() {
if (!ocppEngine) {
AO_DBG_WARN("Please call OCPP_initialize before");
return true; //assume "true" as default state
}
auto& model = ocppEngine->getOcppModel();
auto chargePoint = model.getConnectorStatus(OCPP_ID_OF_CP);
auto connector = model.getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (!chargePoint || !connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return true; //assume "true" as default state
}
return (chargePoint->getAvailability() != AVAILABILITY_INOPERATIVE)
&& (connector->getAvailability() != AVAILABILITY_INOPERATIVE);
}
void beginSession(const char *idTag) {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
if (!idTag || strnlen(idTag, IDTAG_LEN_MAX + 2) > IDTAG_LEN_MAX) {
AO_DBG_ERR("idTag format violation. Expect c-style string with at most %u characters", IDTAG_LEN_MAX);
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->beginSession(idTag);
}
void endSession() {
if (!ocppEngine) {
AO_DBG_ERR("Please call OCPP_initialize before");
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->endSession();
}
bool isInSession() {
return getSessionIdTag() != nullptr;
}
const char *getSessionIdTag() {
if (!ocppEngine) {
AO_DBG_WARN("Please call OCPP_initialize before");
return nullptr;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return nullptr;
}
return connector->getSessionIdTag();
}
#if defined(AO_CUSTOM_UPDATER) || defined(AO_CUSTOM_WS)
ArduinoOcpp::FirmwareService *getFirmwareService() {
auto& model = ocppEngine->getOcppModel();
return model.getFirmwareService();
}
#endif
#if defined(AO_CUSTOM_DIAGNOSTICS) || defined(AO_CUSTOM_WS)
ArduinoOcpp::DiagnosticsService *getDiagnosticsService() {
auto& model = ocppEngine->getOcppModel();
return model.getDiagnosticsService();
}
#endif