Skip to content

Commit f825cad

Browse files
committed
revise charging process
1 parent 5ebeecf commit f825cad

12 files changed

Lines changed: 55 additions & 103 deletions

File tree

src/ArduinoOcpp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,8 @@ bool stopTransaction(OnReceiveConfListener onConf, OnAbortListener onAbort, OnTi
527527
return false;
528528
}
529529

530+
connector->endSession("Local");
531+
530532
connector->releaseTransaction();
531533

532534
const char *idTag = transaction->getIdTag();

src/ArduinoOcpp/Core/OcppConnection.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ void OcppConnection::loop(OcppSocket& ocppSock) {
3939
if (inited) {
4040
bool timeout = inited->sendReq(ocppSock); //The only reason to dequeue elements here is when a timeout occurs. Normally
4141
if (timeout){ //the Conf msg processing routine dequeues finished elements
42-
inited->finalize();
4342
initiatedOcppOperations.pop_front();
4443
}
4544
}
@@ -61,7 +60,6 @@ void OcppConnection::loop(OcppSocket& ocppSock) {
6160
(!(*cached)->getStorageHandler() || (*cached)->getStorageHandler()->getOpNr() < 0)) {
6261
AO_DBG_INFO("Discarding cached due to timeout:");
6362
(*cached)->print_debug();
64-
(*cached)->finalize();
6563
cached = initiatedOcppOperations.erase_tail(cached);
6664
} else {
6765
++cached;
@@ -77,7 +75,6 @@ void OcppConnection::loop(OcppSocket& ocppSock) {
7775
while (received != receivedOcppOperations.end()){
7876
bool success = (*received)->sendConf(ocppSock);
7977
if (success){
80-
(*received)->finalize();
8178
received = receivedOcppOperations.erase(received);
8279
} else {
8380
//There will be another attempt to send this conf message in a future loop call.
@@ -193,7 +190,6 @@ void OcppConnection::handleConfMessage(JsonDocument& json) {
193190
[&json, &success] (std::unique_ptr<OcppOperation>& operation) {
194191
bool match = operation->receiveConf(json);
195192
if (match) {
196-
operation->finalize();
197193
success = true;
198194
//operation will be deleted by the surrounding drop_if
199195
}
@@ -234,7 +230,6 @@ void OcppConnection::handleErrMessage(JsonDocument& json) {
234230
[&json, &success] (std::unique_ptr<OcppOperation>& operation) {
235231
bool match = operation->receiveError(json);
236232
if (match) {
237-
operation->finalize();
238233
success = true;
239234
//operation will be deleted by the surrounding drop_if
240235
}

src/ArduinoOcpp/Core/OcppOperation.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ bool OcppOperation::sendReq(OcppSocket& ocppSocket){
142142
bool success = ocppSocket.sendTXT(out);
143143

144144
timeout->tick(success);
145-
145+
146146
if (success) {
147147
AO_DBG_TRAFFIC_OUT(out.c_str());
148148
retry_start = ao_tick_ms();
@@ -354,8 +354,7 @@ bool OcppOperation::restore(std::unique_ptr<StoredOperationHandler> opStorage, s
354354
opStore = std::move(opStorage);
355355

356356
auto rpcData = opStore->getRpc();
357-
auto payloadData = opStore->getPayload();
358-
if (!rpcData || !payloadData) {
357+
if (!rpcData) {
359358
AO_DBG_ERR("corrupted storage");
360359
return false;
361360
}
@@ -371,47 +370,42 @@ bool OcppOperation::restore(std::unique_ptr<StoredOperationHandler> opStorage, s
371370
int parsedMessageID = -1;
372371
if (sscanf(messageID.c_str(), "%d", &parsedMessageID) == 1) {
373372
if (parsedMessageID > unique_id_counter) {
374-
unique_id_counter = parsedMessageID;
375-
AO_DBG_VERBOSE("restore unique_id_counter = %d", unique_id_counter);
373+
AO_DBG_DEBUG("restore unique_id_counter with %d", parsedMessageID);
374+
unique_id_counter = parsedMessageID + 1; //next unique value is parsedId + 1
376375
}
377376
} else {
378377
AO_DBG_ERR("cannot set unique msgID counter");
379378
(void)0;
380379
//skip this step but don't abort restore
381380
}
382381

383-
std::unique_ptr<OcppMessage> msg;
384-
385382
if (!strcmp(opType.c_str(), "StartTransaction")) { //TODO this will get a nicer solution
386-
msg = std::unique_ptr<OcppMessage>(new Ocpp16::StartTransaction());
383+
ocppMessage = std::unique_ptr<OcppMessage>(new Ocpp16::StartTransaction());
387384
} else if (!strcmp(opType.c_str(), "StopTransaction")) {
388-
msg = std::unique_ptr<OcppMessage>(new Ocpp16::StopTransaction());
385+
ocppMessage = std::unique_ptr<OcppMessage>(new Ocpp16::StopTransaction());
389386
}
390387

391-
if (!msg) {
388+
if (!ocppMessage) {
392389
AO_DBG_ERR("cannot create msg");
393390
return false;
394391
}
395392

396-
msg->setOcppModel(oModel);
393+
ocppMessage->setOcppModel(oModel);
397394

398-
bool success = msg->restore(opStore.get());
395+
bool success = ocppMessage->restore(opStore.get());
399396
opStore->releaseBuffer();
400397

401-
if (!success) {
402-
AO_DBG_ERR("restore error");
398+
if (success) {
399+
AO_DBG_DEBUG("restored opNr %i: %s", opStore->getOpNr(), ocppMessage->getOcppOperationType());
400+
(void)0;
401+
} else {
402+
AO_DBG_ERR("restore opNr %i error", opStore->getOpNr());
403403
(void)0;
404404
}
405405

406406
return success;
407407
}
408408

409-
void OcppOperation::finalize() {
410-
if (opStore) {
411-
opStore->confirm();
412-
}
413-
}
414-
415409
void OcppOperation::setOnReceiveConfListener(OnReceiveConfListener onReceiveConf){
416410
if (onReceiveConf)
417411
onReceiveConfListener = onReceiveConf;

src/ArduinoOcpp/Core/OcppOperation.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ class OcppOperation {
107107

108108
StoredOperationHandler *getStorageHandler() {return opStore.get();}
109109

110-
void finalize(); //called whenever this operation is completed or aborted right before the destructor
111-
112110
void setOnReceiveConfListener(OnReceiveConfListener onReceiveConf);
113111

114112
/**

src/ArduinoOcpp/Core/OperationStore.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,6 @@ bool StoredOperationHandler::commit() {
5555
return true;
5656
}
5757

58-
void StoredOperationHandler::confirm() {
59-
if (isPersistent && opNr < 0) {
60-
AO_DBG_ERR("invalid state");
61-
return;
62-
}
63-
if (isPersistent) {
64-
context.confirm((unsigned int) opNr);
65-
}
66-
}
67-
6858
bool StoredOperationHandler::restore(unsigned int opNrToLoad) {
6959
if (isPersistent) {
7060
AO_DBG_ERR("cannot restore after commit");
@@ -96,8 +86,8 @@ bool StoredOperationHandler::restore(unsigned int opNrToLoad) {
9686
return false;
9787
}
9888

99-
JsonObject rpc_restore = (*doc)["rpc"];
100-
JsonObject payload_restore = (*doc)["payload"];
89+
JsonVariant rpc_restore = (*doc)["rpc"];
90+
JsonVariant payload_restore = (*doc)["payload"];
10191

10292
rpc = std::unique_ptr<DynamicJsonDocument>(new DynamicJsonDocument(rpc_restore.memoryUsage()));
10393
payload = std::unique_ptr<DynamicJsonDocument>(new DynamicJsonDocument(payload_restore.memoryUsage()));
@@ -157,14 +147,14 @@ unsigned int OperationStore::reserveOpNr() {
157147
return res;
158148
}
159149

160-
void OperationStore::confirm(unsigned int opNr) {
150+
void OperationStore::advanceOpNr(unsigned int oldOpNr) {
161151
if (!opBegin || *opBegin < 0) {
162152
AO_DBG_ERR("init failure");
163153
return;
164154
}
165155

166-
if (opNr != (unsigned int) *opBegin) {
167-
if (opNr - (unsigned int) *opBegin < 100) {
156+
if (oldOpNr != (unsigned int) *opBegin) {
157+
if (oldOpNr - (unsigned int) *opBegin < 100) {
168158
AO_DBG_ERR("synchronization failure - try to fix");
169159
(void)0;
170160
} else {
@@ -173,8 +163,8 @@ void OperationStore::confirm(unsigned int opNr) {
173163
}
174164
}
175165

176-
opNr++;
177-
opNr %= AO_MAX_OPNR;
166+
unsigned int opNr = (oldOpNr + 1) % AO_MAX_OPNR;
167+
178168
AO_DBG_DEBUG("advance opBegin: %u", opNr);
179169
*opBegin = opNr;
180170
configuration_save();

src/ArduinoOcpp/Core/OperationStore.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class StoredOperationHandler {
3737

3838
bool commit();
3939
void releaseBuffer() {rpc.release(); payload.release();}
40-
void confirm();
4140

4241
bool restore(unsigned int opNr);
4342

@@ -58,7 +57,7 @@ class OperationStore {
5857
std::unique_ptr<StoredOperationHandler> fetchOpHandler(unsigned int opNr);
5958

6059
unsigned int reserveOpNr();
61-
void confirm(unsigned int opNr);
60+
void advanceOpNr(unsigned int oldOpNr);
6261

6362
unsigned int getOpBegin();
6463
};

src/ArduinoOcpp/Core/OperationsQueue.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ OcppOperation *OperationsQueue::front() {
3434
void OperationsQueue::pop_front() {
3535

3636
if (head && head->getStorageHandler() && head->getStorageHandler()->getOpNr() >= 0) {
37-
if ((unsigned int) head->getStorageHandler()->getOpNr() != opStore.getOpBegin()) {
38-
AO_DBG_ERR("wrong order, removed op from queue before increasing opBegin");
39-
(void)0;
40-
}
37+
opStore.advanceOpNr(head->getStorageHandler()->getOpNr());
38+
AO_DBG_DEBUG("advanced %i to %u", head->getStorageHandler()->getOpNr(), opStore.getOpBegin());
4139
}
4240

4341
head.release();
@@ -87,6 +85,11 @@ void OperationsQueue::pop_front() {
8785
AO_DBG_ERR("could not restore operation");
8886
fetched.release();
8987
}
88+
89+
if (!fetched->isFullyConfigured()) {
90+
AO_DBG_ERR("stored op initialization failure");
91+
fetched.release();
92+
}
9093
}
9194

9295
if (fetched) {

src/ArduinoOcpp/MessagesV16/StopTransaction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ bool StopTransaction::initiate(StoredOperationHandler *opStore) {
6363
return false; //execute legacy initiate instead
6464
}
6565

66-
auto payload = std::unique_ptr<DynamicJsonDocument>(new DynamicJsonDocument(JSON_OBJECT_SIZE(1)));
66+
auto payload = std::unique_ptr<DynamicJsonDocument>(new DynamicJsonDocument(JSON_OBJECT_SIZE(2)));
67+
(*payload)["connectorId"] = transaction->getConnectorId();
6768
(*payload)["txNr"] = transaction->getTxNr();
6869

6970
opStore->setPayload(std::move(payload));

src/ArduinoOcpp/Tasks/ChargePointStatus/ConnectorStatus.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ OcppMessage *ConnectorStatus::loop() {
142142
saveState();
143143
}
144144

145+
txProcess.evaluateProcessSteps();
146+
145147
if (transaction) { //begin exclusively transaction-related operations
146148

147149
if (connectorPluggedSampler) {
@@ -175,7 +177,6 @@ OcppMessage *ConnectorStatus::loop() {
175177
}
176178
}
177179

178-
txProcess.evaluateProcessSteps(transaction->getTxNr());
179180
auto txEnable = txProcess.getState();
180181

181182
/*

src/ArduinoOcpp/Tasks/Transactions/TransactionProcess.cpp

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,7 @@
1111
using namespace ArduinoOcpp;
1212

1313
TransactionProcess::TransactionProcess(uint connectorId) {
14-
char key [30] = {'\0'};
15-
if (snprintf(key, 30, "AO_txNrRef_%u", connectorId) < 0) {
16-
AO_DBG_ERR("Invalid key");
17-
(void)0;
18-
}
19-
txNrRef = declareConfiguration<int>(key, 0, AO_TXPROC_FN, false, false, true, false);
20-
if (!txNrRef || *txNrRef < 0) {
21-
AO_DBG_ERR("Initialization failure");
22-
}
14+
2315
}
2416

2517
/*
@@ -29,7 +21,7 @@ TransactionProcess::TransactionProcess(uint connectorId) {
2921
*
3022
* txEnable is the output variable. See getState() for getting the result.
3123
*/
32-
void TransactionProcess::evaluateProcessSteps(uint txNr) {
24+
void TransactionProcess::evaluateProcessSteps() {
3325

3426
#if AO_DBG_LEVEL >= AO_DL_DEBUG
3527
//print transitions to debug console
@@ -51,11 +43,6 @@ void TransactionProcess::evaluateProcessSteps(uint txNr) {
5143
txTrigger = TxTrigger::Inactive;
5244
}
5345

54-
//Check if the current process is obsolete
55-
if (txNrRef && (uint) *txNrRef != txNr) {
56-
txTrigger = TxTrigger::Inactive;
57-
}
58-
5946
//Determine if
6047
// - No trigger is active -> activeTriggerExists = false, txTrigger = Inactive
6148
// - All triggers are active -> activeTriggerExists = true, txTrigger = Active
@@ -93,15 +80,6 @@ void TransactionProcess::evaluateProcessSteps(uint txNr) {
9380
}
9481
}
9582

96-
//If the current process is obsolete: Check if updating the tx reference is possible
97-
if (txNrRef && (uint) *txNrRef != txNr) {
98-
if (txEnable == TxEnableState::Inactive) {
99-
AO_DBG_DEBUG("Upgrade to next tx: %u", txNr);
100-
*txNrRef = txNr;
101-
configuration_save();
102-
}
103-
}
104-
10583
#if AO_DBG_LEVEL >= AO_DL_DEBUG
10684
if (txEnableBefore != txEnable) {
10785
AO_DBG_DEBUG("Transition from %s to %s",
@@ -111,10 +89,3 @@ void TransactionProcess::evaluateProcessSteps(uint txNr) {
11189
#endif
11290

11391
}
114-
115-
uint TransactionProcess::getTxNrRef() {
116-
if (txNrRef) {
117-
return (uint) *txNrRef;
118-
}
119-
return 0;
120-
}

0 commit comments

Comments
 (0)