diff --git a/CHANGELOG.md b/CHANGELOG.md index ff0a8467..d512b27d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - Support for TransactionMessageAttempts/-RetryInterval ([#345](https://github.com/matth-x/MicroOcpp/pull/345)) - Heap profiler and custom allocator support ([#350](https://github.com/matth-x/MicroOcpp/pull/350)) - Migration of persistent storage ([#355](https://github.com/matth-x/MicroOcpp/pull/355)) +- Build flag `MO_TX_ATTEMPT_TIMEOUT` to ignore offline attempts ([#398](https://github.com/matth-x/MicroOcpp/pull/398)) ### Removed diff --git a/src/MicroOcpp/Model/ConnectorBase/Connector.cpp b/src/MicroOcpp/Model/ConnectorBase/Connector.cpp index c0911858..d41902e6 100644 --- a/src/MicroOcpp/Model/ConnectorBase/Connector.cpp +++ b/src/MicroOcpp/Model/ConnectorBase/Connector.cpp @@ -1172,6 +1172,7 @@ std::unique_ptr Connector::fetchFrontRequest() { return nullptr; } + const auto attemptNr_capture = transactionFront->getStartSync().getAttemptNr(); transactionFront->getStartSync().advanceAttemptNr(); transactionFront->getStartSync().setAttemptTime(model.getClock().now()); transactionFront->commit(); @@ -1203,6 +1204,19 @@ std::unique_ptr Connector::fetchFrontRequest() { } }); + #if MO_TX_ATTEMPT_TIMEOUT == 0 + //a timeout should not increase the attemptNr. Roll back to previous attemptNr + startTx->setOnTimeoutListener([transactionFront_capture, attemptNr_capture] () { + MO_DBG_DEBUG("StartTx timeout -> roll back attemptNr and try again"); + if (transactionFront_capture) { + transactionFront_capture->getStartSync().setAttemptNr(attemptNr_capture); + transactionFront_capture->commit(); + } + }); + #else + (void)attemptNr_capture; + #endif + return startTx; } @@ -1229,6 +1243,7 @@ std::unique_ptr Connector::fetchFrontRequest() { return nullptr; } + const auto attemptNr_capture = transactionFront->getStopSync().getAttemptNr(); transactionFront->getStopSync().advanceAttemptNr(); transactionFront->getStopSync().setAttemptTime(model.getClock().now()); transactionFront->commit(); @@ -1264,6 +1279,19 @@ std::unique_ptr Connector::fetchFrontRequest() { } }); + #if MO_TX_ATTEMPT_TIMEOUT == 0 + //a timeout should not increase the attemptNr. Roll back to previous attemptNr + stopTx->setOnTimeoutListener([transactionFront_capture, attemptNr_capture] () { + MO_DBG_DEBUG("StopTx timeout -> roll back attemptNr and try again"); + if (transactionFront_capture) { + transactionFront_capture->getStopSync().setAttemptNr(attemptNr_capture); + transactionFront_capture->commit(); + } + }); + #else + (void)attemptNr_capture; + #endif + return stopTx; } } diff --git a/src/MicroOcpp/Model/ConnectorBase/Connector.h b/src/MicroOcpp/Model/ConnectorBase/Connector.h index 541c1f4c..183c4ac9 100644 --- a/src/MicroOcpp/Model/ConnectorBase/Connector.h +++ b/src/MicroOcpp/Model/ConnectorBase/Connector.h @@ -28,6 +28,10 @@ #define MO_REPORT_NOERROR 0 #endif +#ifndef MO_TX_ATTEMPT_TIMEOUT +#define MO_TX_ATTEMPT_TIMEOUT 1 //if the timeout of a tx-related msg should increase its attempt counter +#endif + namespace MicroOcpp { class Context;