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
705 lines (610 loc) · 25.1 KB
/
ArduinoOcpp.cpp
File metadata and controls
705 lines (610 loc) · 25.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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// 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/Tasks/Transactions/TransactionStore.h>
#include <ArduinoOcpp/SimpleOcppOperationFactory.h>
#include <ArduinoOcpp/Core/Configuration.h>
#include <ArduinoOcpp/Core/FilesystemAdapter.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};
std::shared_ptr<FilesystemAdapter> filesystem;
FilesystemOpt fileSystemOpt {};
float voltage_eff {230.f};
#ifndef AO_NUMCONNECTORS
#define AO_NUMCONNECTORS 2
#endif
#define OCPP_ID_OF_CP 0
bool 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) {
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) {
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;
#ifndef AO_DEACTIVATE_FLASH
filesystem = makeDefaultFilesystemAdapter(fileSystemOpt);
#endif
AO_DBG_DEBUG("filesystem %s", filesystem ? "loaded" : "error");
configuration_init(filesystem); //call before each other library call
ocppEngine = new OcppEngine(ocppSocket, Clocks::DEFAULT_CLOCK, filesystem);
auto& model = ocppEngine->getOcppModel();
model.setTransactionStore(std::unique_ptr<TransactionStore>(
new TransactionStore(AO_NUMCONNECTORS, filesystem)));
model.setChargePointStatusService(std::unique_ptr<ChargePointStatusService>(
new ChargePointStatusService(*ocppEngine, AO_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 client needs to implement logging
#else
model.setDiagnosticsService(std::unique_ptr<DiagnosticsService>(
new DiagnosticsService(*ocppEngine)));
#endif
#if AO_PLATFORM == AO_PLATFORM_ARDUINO && (defined(ESP32) || defined(ESP8266))
if (!model.getChargePointStatusService()->getExecuteReset())
model.getChargePointStatusService()->setExecuteReset(makeDefaultResetFn());
#endif
ocppEngine->setRunOcppTasks(false); //prevent OCPP classes from doing anything while booting
}
void OCPP_deinitialize() {
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");
return;
}
ocppEngine->loop();
if (!OCPP_booted) {
auto csService = ocppEngine->getOcppModel().getChargePointStatusService();
if (!csService || csService->isBooted()) {
OCPP_booted = true;
ocppEngine->setRunOcppTasks(true);
} else {
return; //wait until the first BootNotification succeeded
}
}
}
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("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
auto credentials = std::unique_ptr<DynamicJsonDocument>(new DynamicJsonDocument(
JSON_OBJECT_SIZE(2) + strlen(chargePointModel) + strlen(chargePointVendor) + 2));
(*credentials)["chargePointModel"] = (char*) chargePointModel;
(*credentials)["chargePointVendor"] = (char*) chargePointVendor;
bootNotification(std::move(credentials), onConf, onAbort, onTimeout, onError, std::move(timeout));
}
void bootNotification(std::unique_ptr<DynamicJsonDocument> payload, OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, std::unique_ptr<Timeout> timeout) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
auto bootNotification = makeOcppOperation(
new BootNotification(std::move(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 authorize(const char *idTag, OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, std::unique_ptr<Timeout> timeout) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //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));
}
bool beginTransaction(const char *idTag, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return false;
}
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 false;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(connectorId);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return false;
}
connector->beginSession(idTag);
return true;
}
bool endTransaction(const char *reason, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return false;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(connectorId);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return false;
}
bool res = connector->getSessionIdTag();
connector->endSession(reason);
return res;
}
bool isTransactionRunning(unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return false;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(connectorId);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return false;
}
return connector->isTransactionRunning();
}
bool ocppPermitsCharge(unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_WARN("Please call OCPP_initialize before");
return false;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(connectorId);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return false;
}
return connector->ocppPermitsCharge();
}
void setConnectorPluggedInput(std::function<bool()> pluggedInput, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(connectorId);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->setConnectorPluggedSampler(pluggedInput);
if (pluggedInput) {
AO_DBG_INFO("Added ConnectorPluggedSampler. Transaction-management is in auto mode now");
} else {
AO_DBG_INFO("Reset ConnectorPluggedSampler. Transaction-management is in manual mode now");
}
}
void setEnergyMeterInput(std::function<float()> energyInput, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
auto& model = ocppEngine->getOcppModel();
if (!model.getMeteringService()) {
model.setMeteringSerivce(std::unique_ptr<MeteringService>(
new MeteringService(*ocppEngine, AO_NUMCONNECTORS, filesystem)));
}
SampledValueProperties meterProperties;
meterProperties.setMeasurand("Energy.Active.Import.Register");
meterProperties.setUnit("Wh");
auto mvs = std::unique_ptr<SampledValueSamplerConcrete<float, SampledValueDeSerializer<float>>>(
new SampledValueSamplerConcrete<float, SampledValueDeSerializer<float>>(
meterProperties,
[energyInput] (ReadingContext) {return energyInput();}
));
model.getMeteringService()->addMeterValueSampler(connectorId, std::move(mvs));
model.getMeteringService()->setEnergySampler(connectorId, energyInput);
}
void setPowerMeterInput(std::function<float()> powerInput, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
auto& model = ocppEngine->getOcppModel();
if (!model.getMeteringService()) {
model.setMeteringSerivce(std::unique_ptr<MeteringService>(
new MeteringService(*ocppEngine, AO_NUMCONNECTORS, filesystem)));
}
SampledValueProperties meterProperties;
meterProperties.setMeasurand("Power.Active.Import");
meterProperties.setUnit("W");
auto mvs = std::unique_ptr<SampledValueSamplerConcrete<float, SampledValueDeSerializer<float>>>(
new SampledValueSamplerConcrete<float, SampledValueDeSerializer<float>>(
meterProperties,
[powerInput] (ReadingContext) {return powerInput();}
));
model.getMeteringService()->addMeterValueSampler(connectorId, std::move(mvs));
model.getMeteringService()->setPowerSampler(connectorId, powerInput);
}
void setSmartChargingOutput(std::function<void(float)> chargingLimitOutput, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
if (connectorId != 1) {
AO_DBG_WARN("Smart charging for multiple connectorId %u not implemented yet", connectorId);
return;
}
auto& model = ocppEngine->getOcppModel();
if (!model.getSmartChargingService()) {
model.setSmartChargingService(std::unique_ptr<SmartChargingService>(
new SmartChargingService(*ocppEngine, 11000.0f, voltage_eff, AO_NUMCONNECTORS, fileSystemOpt))); //default charging limit: 11kW
}
model.getSmartChargingService()->setOnLimitChange(chargingLimitOutput);
}
void setEvReadyInput(std::function<bool()> evReadyInput, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(connectorId);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->setEvRequestsEnergySampler(evReadyInput);
}
void setEvseReadyInput(std::function<bool()> evseReadyInput, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(connectorId);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->setConnectorEnergizedSampler(evseReadyInput);
}
void addErrorCodeInput(std::function<const char *()> errorCodeInput, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(connectorId);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->addConnectorErrorCodeSampler(errorCodeInput);
}
void addMeterValueInput(std::function<float ()> valueInput, const char *measurand, const char *unit, const char *location, const char *phase, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
if (!valueInput) {
AO_DBG_ERR("value undefined");
return;
}
if (!measurand) {
measurand = "Energy.Active.Import.Register";
AO_DBG_WARN("Measurand unspecified; assume %s", measurand);
}
SampledValueProperties properties;
properties.setMeasurand(measurand); //mandatory for AO
if (unit)
properties.setUnit(unit);
if (location)
properties.setLocation(location);
if (phase)
properties.setPhase(phase);
auto valueSampler = std::unique_ptr<ArduinoOcpp::SampledValueSamplerConcrete<float, ArduinoOcpp::SampledValueDeSerializer<float>>>(
new ArduinoOcpp::SampledValueSamplerConcrete<float, ArduinoOcpp::SampledValueDeSerializer<float>>(
properties,
[valueInput] (ArduinoOcpp::ReadingContext) {return valueInput();}));
addMeterValueInput(std::move(valueSampler), connectorId);
}
void addMeterValueInput(std::unique_ptr<SampledValueSampler> valueInput, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
auto& model = ocppEngine->getOcppModel();
if (!model.getMeteringService()) {
model.setMeteringSerivce(std::unique_ptr<MeteringService>(
new MeteringService(*ocppEngine, AO_NUMCONNECTORS, filesystem)));
}
model.getMeteringService()->addMeterValueSampler(connectorId, std::move(valueInput));
}
void setOnResetNotify(std::function<bool(bool)> onResetNotify) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
if (auto csService = ocppEngine->getOcppModel().getChargePointStatusService()) {
csService->setPreReset(onResetNotify);
}
}
void setOnResetExecute(std::function<void(bool)> onResetExecute) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
if (auto csService = ocppEngine->getOcppModel().getChargePointStatusService()) {
csService->setExecuteReset(onResetExecute);
}
}
void setOnUnlockConnectorInOut(std::function<PollResult<bool>()> onUnlockConnectorInOut, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(connectorId);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->setOnUnlockConnector(onUnlockConnectorInOut);
}
void setConnectorLockInOut(std::function<ArduinoOcpp::TxEnableState(ArduinoOcpp::TxTrigger)> lockConnectorInOut, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(connectorId);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->setConnectorLock(lockConnectorInOut);
}
void setTxBasedMeterInOut(std::function<ArduinoOcpp::TxEnableState(ArduinoOcpp::TxTrigger)> txMeterInOut, unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(connectorId);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return;
}
connector->setTxBasedMeterUpdate(txMeterInOut);
}
bool isOperative(unsigned int connectorId) {
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(connectorId);
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);
}
int getTransactionId(unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_WARN("Please call OCPP_initialize before");
return -1;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(connectorId);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return -1;
}
return connector->getTransactionId();
}
const char *getTransactionIdTag(unsigned int connectorId) {
if (!ocppEngine) {
AO_DBG_WARN("Please call OCPP_initialize before");
return nullptr;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(connectorId);
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
OcppEngine *getOcppEngine() {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return nullptr;
}
return ocppEngine;
}
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 setOnResetRequest(OnReceiveReqListener onReceiveReq) {
setOnResetReceiveRequestListener(onReceiveReq);
}
#define OCPP_ID_OF_CONNECTOR 1
bool startTransaction(const char *idTag, OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, std::unique_ptr<Timeout> timeout) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //please call OCPP_initialize before
return false;
}
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 false;
}
auto connector = ocppEngine->getOcppModel().getConnectorStatus(OCPP_ID_OF_CONNECTOR);
if (!connector) {
AO_DBG_ERR("Could not find connector. Ignore");
return false;
}
auto transaction = connector->getTransaction();
if (transaction) {
if (transaction->getStartRpcSync().isRequested()) {
AO_DBG_ERR("Transaction already in progress. Must call stopTransaction()");
return false;
}
transaction->setIdTag(idTag);
} else {
beginTransaction(idTag); //request new transaction object
transaction = connector->getTransaction();
if (!transaction) {
AO_DBG_WARN("Transaction queue full");
return false;
}
}
auto startTransaction = makeOcppOperation(
new StartTransaction(transaction));
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));
return true;
}
bool stopTransaction(OnReceiveConfListener onConf, OnAbortListener onAbort, OnTimeoutListener onTimeout, OnReceiveErrorListener onError, std::unique_ptr<Timeout> timeout) {
if (!ocppEngine) {
AO_DBG_ERR("OCPP uninitialized"); //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;
}
auto transaction = connector->getTransaction();
if (!transaction || !transaction->isRunning()) {
AO_DBG_ERR("No running Tx to stop");
return false;
}
connector->endSession("Local");
const char *idTag = transaction->getIdTag();
if (idTag) {
transaction->setStopIdTag(idTag);
}
transaction->setStopReason("Local");
auto stopTransaction = makeOcppOperation(
new StopTransaction(transaction));
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));
return true;
}