diff --git a/examples/ESP/main.cpp b/examples/ESP/main.cpp index 306e700b..3f1611d9 100644 --- a/examples/ESP/main.cpp +++ b/examples/ESP/main.cpp @@ -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 diff --git a/src/MicroOcpp.cpp b/src/MicroOcpp.cpp index 5dbd3714..efce914e 100644 --- a/src/MicroOcpp.cpp +++ b/src/MicroOcpp.cpp @@ -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); } @@ -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)) { + //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); + return; + } + } + } + auto authorize = makeRequest( new Authorize(context->getModel(), idTag)); if (onConf)