Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 8 additions & 29 deletions examples/ESP/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,35 +103,14 @@ void loop() {
if (/* RFID chip detected? */ false) {
String idTag = "0123456789ABCD"; //e.g. idTag = RFID.readIdTag();

if (!getTransaction()) {
//no transaction running or preparing. Begin a new transaction
Serial.printf("[main] Begin Transaction with idTag %s\n", idTag.c_str());

/*
* Begin Transaction. The OCPP lib will prepare transaction by checking the Authorization
* and listen to the ConnectorPlugged Input. When the Authorization succeeds and an EV
* is plugged, the OCPP lib will send the StartTransaction
*/
auto ret = beginTransaction(idTag.c_str());

if (ret) {
Serial.println(F("[main] Transaction initiated. OCPP lib will send a StartTransaction when" \
"ConnectorPlugged Input becomes true and if the Authorization succeeds"));
} else {
Serial.println(F("[main] No transaction initiated"));
}

} else {
//Transaction already initiated. Check if to stop current Tx by RFID card
if (idTag.equals(getTransactionIdTag())) {
//card matches -> user can stop Tx
Serial.println(F("[main] End transaction by RFID card"));

endTransaction(idTag.c_str());
} else {
Serial.println(F("[main] Cannot end transaction by RFID card (different card?)"));
}
}
/*
* Begin Transaction. The OCPP lib will prepare transaction by checking the Authorization
* and listen to the ConnectorPlugged Input. When the Authorization succeeds and an EV
* is plugged, the OCPP lib will send the StartTransaction
*
* If transaction already exists, stop by RFID card (if idTag or parentIdTag match)
*/
authorize(idTag.c_str());
}

//... see MicroOcpp.h for more possibilities
Expand Down
14 changes: 13 additions & 1 deletion src/MicroOcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ bool endTransaction(const char *idTag, const char *reason, unsigned int connecto
}
return;
}
if (idTagInfo.containsKey("parentIdTag") && !strcmp(idTagInfo["parenIdTag"], tx->getParentIdTag()))
if (idTagInfo.containsKey("parentIdTag") && !strcmp(idTagInfo["parentIdTag"], tx->getParentIdTag()))
{
endTransaction_authorized(idTag_capture.c_str(), reason_capture.empty() ? (const char*)nullptr : reason_capture.c_str(), connectorId);
}
Expand Down Expand Up @@ -1397,6 +1397,18 @@ void authorize(const char *idTag, OnReceiveConfListener onConf, OnAbortListener
MO_DBG_ERR("idTag format violation. Expect c-style string with at most %u characters", IDTAG_LEN_MAX);
return;
}

//check for active transactions
for (unsigned int cId = 1; cId < context->getModel().getNumConnectors(); cId++) {
if (isTransactionActive(cId)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be honest, this is not really needed, as it's checked again in endTransaction()...

//this will check both parentIdTag and idTag
if (endTransaction(idTag, "Local", cId)) {
MO_DBG_INFO("ended active transaction on connector %d for idTag '%s'", cId, idTag);
Copy link
Contributor Author

@razvanphp razvanphp Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this log is also duplicated in endTransaction(), so I could slim this down a lot. What do you think?

return;
}
}
}

auto authorize = makeRequest(
new Authorize(context->getModel(), idTag));
if (onConf)
Expand Down
Loading