Skip to content

Commit 91b0738

Browse files
committed
port TxNotification to v201
1 parent be4f092 commit 91b0738

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

src/MicroOcpp.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,12 @@ void setTxNotificationOutput(std::function<void(MicroOcpp::Transaction*,TxNotifi
11511151
MO_DBG_ERR("OCPP uninitialized"); //need to call mocpp_initialize before
11521152
return;
11531153
}
1154+
#if MO_ENABLE_V201
1155+
if (context->getVersion().major == 2) {
1156+
MO_DBG_ERR("only supported in v16");
1157+
return;
1158+
}
1159+
#endif
11541160
auto connector = context->getModel().getConnector(connectorId);
11551161
if (!connector) {
11561162
MO_DBG_ERR("could not find connector");
@@ -1159,6 +1165,30 @@ void setTxNotificationOutput(std::function<void(MicroOcpp::Transaction*,TxNotifi
11591165
connector->setTxNotificationOutput(notificationOutput);
11601166
}
11611167

1168+
#if MO_ENABLE_V201
1169+
void setTxNotificationOutputV201(std::function<void(MicroOcpp::Ocpp201::Transaction*,TxNotification)> notificationOutput, unsigned int connectorId) {
1170+
if (!context) {
1171+
MO_DBG_ERR("OCPP uninitialized"); //need to call mocpp_initialize before
1172+
return;
1173+
}
1174+
1175+
if (context->getVersion().major != 2) {
1176+
MO_DBG_ERR("only supported in v201");
1177+
return;
1178+
}
1179+
1180+
TransactionService::Evse *evse = nullptr;
1181+
if (auto txService = context->getModel().getTransactionService()) {
1182+
evse = txService->getEvse(connectorId);
1183+
}
1184+
if (!evse) {
1185+
MO_DBG_ERR("could not find EVSE");
1186+
return;
1187+
}
1188+
evse->setTxNotificationOutput(notificationOutput);
1189+
}
1190+
#endif //MO_ENABLE_V201
1191+
11621192
#if MO_ENABLE_CONNECTOR_LOCK
11631193
void setOnUnlockConnectorInOut(std::function<UnlockConnectorResult()> onUnlockConnectorInOut, unsigned int connectorId) {
11641194
if (!context) {

src/MicroOcpp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ void setStopTxReadyInput(std::function<bool()> stopTxReady, unsigned int connect
333333

334334
void setTxNotificationOutput(std::function<void(MicroOcpp::Transaction*,TxNotification)> notificationOutput, unsigned int connectorId = 1); //called when transaction state changes (see TxNotification for possible events). Transaction can be null
335335

336+
#if MO_ENABLE_V201
337+
void setTxNotificationOutputV201(std::function<void(MicroOcpp::Ocpp201::Transaction*,TxNotification)> notificationOutput, unsigned int connectorId = 1);
338+
#endif //MO_ENABLE_V201
339+
336340
#if MO_ENABLE_CONNECTOR_LOCK
337341
/*
338342
* Set an InputOutput (reads and sets information at the same time) for forcing to unlock the

src/MicroOcpp/Model/Transactions/TransactionService.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ void TransactionService::Evse::loop() {
261261

262262
MO_DBG_INFO("Session mngt: timeout");
263263
endTransaction(Ocpp201::Transaction::StoppedReason::Timeout, TransactionEventTriggerReason::EVConnectTimeout);
264+
265+
updateTxNotification(TxNotification_ConnectionTimeout);
264266
}
265267

266268
if (transaction->active &&
@@ -576,6 +578,14 @@ void TransactionService::Evse::loop() {
576578
txStore.commit(txEvent.get());
577579
}
578580

581+
if (txEvent) {
582+
if (txEvent->eventType == TransactionEventData::Type::Started) {
583+
updateTxNotification(TxNotification_StartTx);
584+
} else if (txEvent->eventType == TransactionEventData::Type::Ended) {
585+
updateTxNotification(TxNotification_StartTx);
586+
}
587+
}
588+
579589
//try to pass ownership to front txEvent immediatley
580590
if (txEvent && !txEventFront &&
581591
transaction->txNr == txNrFront &&
@@ -604,6 +614,16 @@ void TransactionService::Evse::setEvseReadyInput(std::function<bool()> connector
604614
this->evseReadyInput = connectorEnergized;
605615
}
606616

617+
void TransactionService::Evse::setTxNotificationOutput(std::function<void(Ocpp201::Transaction*, TxNotification)> txNotificationOutput) {
618+
this->txNotificationOutput = txNotificationOutput;
619+
}
620+
621+
void TransactionService::Evse::updateTxNotification(TxNotification event) {
622+
if (txNotificationOutput) {
623+
txNotificationOutput(transaction.get(), event);
624+
}
625+
}
626+
607627
bool TransactionService::Evse::beginAuthorization(IdToken idToken, bool validateIdToken) {
608628
MO_DBG_DEBUG("begin auth: %s", idToken.get());
609629

@@ -649,13 +669,17 @@ bool TransactionService::Evse::beginAuthorization(IdToken idToken, bool validate
649669
MO_DBG_DEBUG("Authorize rejected (%s), abort tx process", tx->idToken.get());
650670
tx->isDeauthorized = true;
651671
txStore.commit(tx);
672+
673+
updateTxNotification(TxNotification_AuthorizationRejected);
652674
return;
653675
}
654676

655677
MO_DBG_DEBUG("Authorized tx with validation (%s)", tx->idToken.get());
656678
tx->isAuthorized = true;
657679
tx->notifyIdToken = true;
658680
txStore.commit(tx);
681+
682+
updateTxNotification(TxNotification_Authorized);
659683
});
660684
authorize->setOnAbortListener([this, txId] () {
661685
auto tx = getTransaction();
@@ -667,13 +691,18 @@ bool TransactionService::Evse::beginAuthorization(IdToken idToken, bool validate
667691
MO_DBG_DEBUG("Authorize timeout (%s)", tx->idToken.get());
668692
tx->isDeauthorized = true;
669693
txStore.commit(tx);
694+
695+
updateTxNotification(TxNotification_AuthorizationTimeout);
670696
});
671697
authorize->setTimeout(20 * 1000);
672698
context.initiateRequest(std::move(authorize));
673699
} else {
674700
MO_DBG_DEBUG("Authorized tx directly (%s)", transaction->idToken.get());
675701
transaction->isAuthorized = true;
676702
transaction->notifyIdToken = true;
703+
txStore.commit(transaction.get());
704+
705+
updateTxNotification(TxNotification_Authorized);
677706
}
678707

679708
return true;
@@ -692,10 +721,16 @@ bool TransactionService::Evse::endAuthorization(IdToken idToken, bool validateId
692721
// use same idToken like tx start
693722
transaction->isAuthorizationActive = false;
694723
transaction->notifyIdToken = true;
724+
txStore.commit(transaction.get());
725+
726+
updateTxNotification(TxNotification_Authorized);
695727
} else if (!validateIdToken) {
696728
transaction->stopIdToken = std::unique_ptr<IdToken>(new IdToken(idToken, getMemoryTag()));
697729
transaction->isAuthorizationActive = false;
698730
transaction->notifyStopIdToken = true;
731+
txStore.commit(transaction.get());
732+
733+
updateTxNotification(TxNotification_Authorized);
699734
} else {
700735
// use a different idToken for stopping the tx
701736

@@ -718,6 +753,8 @@ bool TransactionService::Evse::endAuthorization(IdToken idToken, bool validateId
718753

719754
if (strcmp(response["idTokenInfo"]["status"] | "_Undefined", "Accepted")) {
720755
MO_DBG_DEBUG("Authorize rejected (%s), don't stop tx", idToken.get());
756+
757+
updateTxNotification(TxNotification_AuthorizationRejected);
721758
return;
722759
}
723760

@@ -735,6 +772,17 @@ bool TransactionService::Evse::endAuthorization(IdToken idToken, bool validateId
735772
tx->isAuthorizationActive = false;
736773
tx->notifyStopIdToken = true;
737774
txStore.commit(tx);
775+
776+
updateTxNotification(TxNotification_Authorized);
777+
});
778+
authorize->setOnTimeoutListener([this, txId] () {
779+
auto tx = getTransaction();
780+
if (!tx || strcmp(tx->transactionId, txId)) {
781+
MO_DBG_INFO("dangling Authorize -- discard");
782+
return;
783+
}
784+
785+
updateTxNotification(TxNotification_AuthorizationTimeout);
738786
});
739787
authorize->setTimeout(20 * 1000);
740788
context.initiateRequest(std::move(authorize));

src/MicroOcpp/Model/Transactions/TransactionService.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class TransactionService : public MemoryManaged {
5252
std::function<bool()> startTxReadyInput;
5353
std::function<bool()> stopTxReadyInput;
5454

55+
std::function<void(Ocpp201::Transaction*,TxNotification)> txNotificationOutput;
56+
5557
bool beginTransaction();
5658
bool endTransaction(Ocpp201::Transaction::StoppedReason stoppedReason, Ocpp201::TransactionEventTriggerReason stopTrigger);
5759

@@ -74,6 +76,9 @@ class TransactionService : public MemoryManaged {
7476
void setEvReadyInput(std::function<bool()> evRequestsEnergy);
7577
void setEvseReadyInput(std::function<bool()> connectorEnergized);
7678

79+
void setTxNotificationOutput(std::function<void(Ocpp201::Transaction*,TxNotification)> txNotificationOutput);
80+
void updateTxNotification(TxNotification event);
81+
7782
bool beginAuthorization(IdToken idToken, bool validateIdToken = true); // authorize by swipe RFID
7883
bool endAuthorization(IdToken idToken = IdToken(), bool validateIdToken = false); // stop authorization by swipe RFID
7984

0 commit comments

Comments
 (0)