From e0bb7d4a5d7cb9a18d788f292f324704661d172d Mon Sep 17 00:00:00 2001 From: matth-x <63792403+matth-x@users.noreply.github.com> Date: Sun, 8 Oct 2023 18:17:14 +0200 Subject: [PATCH 1/3] fix GetDiag double execution; cosmetic changes --- .../Model/Diagnostics/DiagnosticsService.cpp | 12 +++-- .../Model/Diagnostics/DiagnosticsService.h | 8 +-- src/MicroOcpp/Operations/GetDiagnostics.cpp | 49 ++++++++----------- src/MicroOcpp/Operations/GetDiagnostics.h | 20 +++----- 4 files changed, 38 insertions(+), 51 deletions(-) diff --git a/src/MicroOcpp/Model/Diagnostics/DiagnosticsService.cpp b/src/MicroOcpp/Model/Diagnostics/DiagnosticsService.cpp index e83c063f..7a83ffa8 100644 --- a/src/MicroOcpp/Model/Diagnostics/DiagnosticsService.cpp +++ b/src/MicroOcpp/Model/Diagnostics/DiagnosticsService.cpp @@ -16,8 +16,8 @@ using Ocpp16::DiagnosticsStatus; DiagnosticsService::DiagnosticsService(Context& context) : context(context) { - context.getOperationRegistry().registerOperation("GetDiagnostics", [&context] () { - return new Ocpp16::GetDiagnostics(context.getModel());}); + context.getOperationRegistry().registerOperation("GetDiagnostics", [this] () { + return new Ocpp16::GetDiagnostics(*this);}); //Register message handler for TriggerMessage operation context.getOperationRegistry().registerOperation("DiagnosticsStatusNotification", [this] () { @@ -36,7 +36,7 @@ void DiagnosticsService::loop() { if (!uploadIssued) { if (onUpload != nullptr) { MOCPP_DBG_DEBUG("Call onUpload"); - onUpload(location, startTime, stopTime); + onUpload(location.c_str(), startTime, stopTime); uploadIssued = true; } else { MOCPP_DBG_ERR("onUpload must be set! (see setOnUpload). Will abort"); @@ -82,7 +82,7 @@ void DiagnosticsService::loop() { } //timestamps before year 2021 will be treated as "undefined" -std::string DiagnosticsService::requestDiagnosticsUpload(const std::string &location, int retries, unsigned int retryInterval, Timestamp startTime, Timestamp stopTime) { +std::string DiagnosticsService::requestDiagnosticsUpload(const char *location, unsigned int retries, unsigned int retryInterval, Timestamp startTime, Timestamp stopTime) { if (onUpload == nullptr) //maybe add further plausibility checks return std::string{}; @@ -100,6 +100,7 @@ std::string DiagnosticsService::requestDiagnosticsUpload(const std::string &loca this->stopTime = newStop; } +#if MOCPP_DBG_LEVEL >= MOCPP_DL_INFO char dbuf [JSONDATE_LENGTH + 1] = {'\0'}; char dbuf2 [JSONDATE_LENGTH + 1] = {'\0'}; this->startTime.toJsonString(dbuf, JSONDATE_LENGTH + 1); @@ -116,6 +117,7 @@ std::string DiagnosticsService::requestDiagnosticsUpload(const std::string &loca this->retryInterval, dbuf, dbuf2); +#endif nextTry = context.getModel().getClock().now(); nextTry += 5; //wait for 5s before upload @@ -168,7 +170,7 @@ void DiagnosticsService::setRefreshFilename(std::function refresh this->refreshFilename = refreshFn; } -void DiagnosticsService::setOnUpload(std::function onUpload) { +void DiagnosticsService::setOnUpload(std::function onUpload) { this->onUpload = onUpload; } diff --git a/src/MicroOcpp/Model/Diagnostics/DiagnosticsService.h b/src/MicroOcpp/Model/Diagnostics/DiagnosticsService.h index 4edb160e..99368d93 100644 --- a/src/MicroOcpp/Model/Diagnostics/DiagnosticsService.h +++ b/src/MicroOcpp/Model/Diagnostics/DiagnosticsService.h @@ -27,7 +27,7 @@ class DiagnosticsService { Context& context; std::string location; - int retries = 0; + unsigned int retries = 0; unsigned int retryInterval = 0; Timestamp startTime; Timestamp stopTime; @@ -35,7 +35,7 @@ class DiagnosticsService { Timestamp nextTry; std::function refreshFilename; - std::function onUpload; + std::function onUpload; std::function uploadStatusInput; bool uploadIssued = false; @@ -51,13 +51,13 @@ class DiagnosticsService { //timestamps before year 2021 will be treated as "undefined" //returns empty std::string if onUpload is missing or upload cannot be scheduled for another reason //returns fileName of diagnostics file to be uploaded if upload has been scheduled - std::string requestDiagnosticsUpload(const std::string &location, int retries = 1, unsigned int retryInterval = 0, Timestamp startTime = Timestamp(), Timestamp stopTime = Timestamp()); + std::string requestDiagnosticsUpload(const char *location, unsigned int retries = 1, unsigned int retryInterval = 0, Timestamp startTime = Timestamp(), Timestamp stopTime = Timestamp()); Ocpp16::DiagnosticsStatus getDiagnosticsStatus(); void setRefreshFilename(std::function refreshFn); //refresh a new filename which will be used for the subsequent upload tries - void setOnUpload(std::function onUpload); + void setOnUpload(std::function onUpload); void setOnUploadStatusInput(std::function uploadStatusInput); }; diff --git a/src/MicroOcpp/Operations/GetDiagnostics.cpp b/src/MicroOcpp/Operations/GetDiagnostics.cpp index 3d317a7b..6e6d4fe2 100644 --- a/src/MicroOcpp/Operations/GetDiagnostics.cpp +++ b/src/MicroOcpp/Operations/GetDiagnostics.cpp @@ -9,57 +9,48 @@ using MicroOcpp::Ocpp16::GetDiagnostics; -GetDiagnostics::GetDiagnostics(Model& model) : model(model) { +GetDiagnostics::GetDiagnostics(DiagnosticsService& diagService) : diagService(diagService) { } void GetDiagnostics::processReq(JsonObject payload) { - /* - * Process the application data here. Note: you have to implement the FW update procedure in your client code. You have to set - * a onSendConfListener in which you initiate a FW update (e.g. calling ESPhttpUpdate.update(...) ) - */ - const char *loc = payload["location"] | ""; - location = loc; + const char *location = payload["location"] | ""; //check location URL. Maybe introduce Same-Origin-Policy? - if (location.empty()) { - formatError = true; - MOCPP_DBG_WARN("Could not read location. Abort"); + if (!*location) { + errorCode = "FormationViolation"; return; } - retries = payload["retries"] | 1; - retryInterval = payload["retryInterval"] | 180; + int retries = payload["retries"] | 1; + int retryInterval = payload["retryInterval"] | 180; + if (retries < 0 || retryInterval < 0) { + errorCode = "PropertyConstraintViolation"; + return; + } - //check the integrity of startTime + Timestamp startTime; if (payload.containsKey("startTime")) { - const char *startTimeRaw = payload["startTime"] | "Invalid"; - if (!startTime.setTime(startTimeRaw)) { - formatError = true; - MOCPP_DBG_WARN("Could not read startTime. Abort"); + if (!startTime.setTime(payload["startTime"] | "Invalid")) { + errorCode = "PropertyConstraintViolation"; + MOCPP_DBG_WARN("bad time format"); return; } } - //check the integrity of stopTime + Timestamp stopTime; if (payload.containsKey("startTime")) { - const char *stopTimeRaw = payload["stopTime"] | "Invalid"; - if (!stopTime.setTime(stopTimeRaw)) { - formatError = true; - MOCPP_DBG_WARN("Could not read stopTime. Abort"); + if (!stopTime.setTime(payload["stopTime"] | "Invalid")) { + errorCode = "PropertyConstraintViolation"; + MOCPP_DBG_WARN("bad time format"); return; } } + + fileName = diagService.requestDiagnosticsUpload(location, (unsigned int) retries, (unsigned int) retryInterval, startTime, stopTime); } std::unique_ptr GetDiagnostics::createConf(){ - if (auto diagService = model.getDiagnosticsService()) { - fileName = diagService->requestDiagnosticsUpload(location, retries, retryInterval, startTime, stopTime); - } else { - MOCPP_DBG_WARN("DiagnosticsService has not been initialized before! Please have a look at MicroOcpp.cpp for an example. Abort"); - return createEmptyDocument(); - } - if (fileName.empty()) { return createEmptyDocument(); } else { diff --git a/src/MicroOcpp/Operations/GetDiagnostics.h b/src/MicroOcpp/Operations/GetDiagnostics.h index 0c5cd16d..22c14983 100644 --- a/src/MicroOcpp/Operations/GetDiagnostics.h +++ b/src/MicroOcpp/Operations/GetDiagnostics.h @@ -10,24 +10,18 @@ namespace MicroOcpp { -class Model; +class DiagnosticsService; namespace Ocpp16 { class GetDiagnostics : public Operation { private: - Model& model; - std::string location {}; - int retries = 1; - unsigned long retryInterval = 180; - Timestamp startTime = Timestamp(); - Timestamp stopTime = Timestamp(); - - std::string fileName {}; - - bool formatError = false; + DiagnosticsService& diagService; + std::string fileName; + + const char *errorCode = nullptr; public: - GetDiagnostics(Model& model); + GetDiagnostics(DiagnosticsService& diagService); const char* getOperationType() override {return "GetDiagnostics";} @@ -35,7 +29,7 @@ class GetDiagnostics : public Operation { std::unique_ptr createConf() override; - const char *getErrorCode() override {return formatError ? "FormationViolation" : nullptr;} + const char *getErrorCode() override {return errorCode;} }; } //end namespace Ocpp16 From 565f6d4b0f306ce642db5d0b23c9184f9b262a74 Mon Sep 17 00:00:00 2001 From: matth-x <63792403+matth-x@users.noreply.github.com> Date: Sun, 8 Oct 2023 19:02:17 +0200 Subject: [PATCH 2/3] fix UpdateFw double execution; cosmetic changes --- .../FirmwareManagement/FirmwareService.cpp | 28 ++++++------ .../FirmwareManagement/FirmwareService.h | 16 +++---- src/MicroOcpp/Operations/UpdateFirmware.cpp | 43 +++++++++---------- src/MicroOcpp/Operations/UpdateFirmware.h | 15 +++---- 4 files changed, 50 insertions(+), 52 deletions(-) diff --git a/src/MicroOcpp/Model/FirmwareManagement/FirmwareService.cpp b/src/MicroOcpp/Model/FirmwareManagement/FirmwareService.cpp index 80ea675b..2c2519cc 100644 --- a/src/MicroOcpp/Model/FirmwareManagement/FirmwareService.cpp +++ b/src/MicroOcpp/Model/FirmwareManagement/FirmwareService.cpp @@ -27,8 +27,8 @@ using MicroOcpp::Ocpp16::FirmwareStatus; FirmwareService::FirmwareService(Context& context) : context(context) { - context.getOperationRegistry().registerOperation("UpdateFirmware", [&context] () { - return new Ocpp16::UpdateFirmware(context.getModel());}); + context.getOperationRegistry().registerOperation("UpdateFirmware", [this] () { + return new Ocpp16::UpdateFirmware(*this);}); //Register message handler for TriggerMessage operation context.getOperationRegistry().registerOperation("FirmwareStatusNotification", [this] () { @@ -78,7 +78,7 @@ void FirmwareService::loop() { MOCPP_DBG_INFO("Start download"); stage = UpdateStage::Downloading; if (onDownload != nullptr) { - onDownload(location); + onDownload(location.c_str()); timestampTransition = mocpp_tick_ms(); delayTransition = downloadStatusInput ? 1000 : 30000; //give the download at least 30s return; @@ -138,7 +138,7 @@ void FirmwareService::loop() { stage = UpdateStage::Installing; if (onInstall) { - onInstall(location); //should restart the device on success + onInstall(location.c_str()); //should restart the device on success } else { MOCPP_DBG_WARN("onInstall must be set! (see setOnInstall). Will abort"); } @@ -183,7 +183,7 @@ void FirmwareService::loop() { } } -void FirmwareService::scheduleFirmwareUpdate(const std::string &location, Timestamp retreiveDate, int retries, unsigned int retryInterval) { +void FirmwareService::scheduleFirmwareUpdate(const char *location, Timestamp retreiveDate, unsigned int retries, unsigned int retryInterval) { this->location = location; this->retreiveDate = retreiveDate; this->retries = retries; @@ -197,15 +197,17 @@ void FirmwareService::scheduleFirmwareUpdate(const std::string &location, Timest char dbuf [JSONDATE_LENGTH + 1] = {'\0'}; this->retreiveDate.toJsonString(dbuf, JSONDATE_LENGTH + 1); +#if MOCPP_DBG_LEVEL >= MOCPP_DL_INFO MOCPP_DBG_INFO("Scheduled FW update!\n" \ " location = %s\n" \ " retrieveDate = %s\n" \ - " retries = %i" \ + " retries = %u" \ ", retryInterval = %u", this->location.c_str(), dbuf, this->retries, this->retryInterval); +#endif timestampTransition = mocpp_tick_ms(); delayTransition = 1000; @@ -276,7 +278,7 @@ std::unique_ptr FirmwareService::getFirmwareStatusNotification() { return nullptr; } -void FirmwareService::setOnDownload(std::function onDownload) { +void FirmwareService::setOnDownload(std::function onDownload) { this->onDownload = onDownload; } @@ -284,7 +286,7 @@ void FirmwareService::setDownloadStatusInput(std::function dow this->downloadStatusInput = downloadStatusInput; } -void FirmwareService::setOnInstall(std::function onInstall) { +void FirmwareService::setOnInstall(std::function onInstall) { this->onInstall = onInstall; } @@ -310,7 +312,7 @@ FirmwareService *EspWiFi::makeFirmwareService(Context& context) { * example of how to integrate a separate download phase (optional) */ #if 0 //separate download phase - fwService->setOnDownload([] (const std::string &location) { + fwService->setOnDownload([] (const char *location) { //download the new binary //... return true; @@ -323,7 +325,7 @@ FirmwareService *EspWiFi::makeFirmwareService(Context& context) { }); #endif //separate download phase - fwService->setOnInstall([fwService] (const std::string &location) { + fwService->setOnInstall([fwService] (const char *location) { fwService->setInstallationStatusInput([](){return InstallationStatus::NotInstalled;}); @@ -333,7 +335,7 @@ FirmwareService *EspWiFi::makeFirmwareService(Context& context) { client.setTimeout(60); //in seconds // httpUpdate.setLedPin(LED_BUILTIN, HIGH); - t_httpUpdate_return ret = httpUpdate.update(client, location.c_str()); + t_httpUpdate_return ret = httpUpdate.update(client, location); switch (ret) { case HTTP_UPDATE_FAILED: @@ -368,7 +370,7 @@ FirmwareService *EspWiFi::makeFirmwareService(Context& context) { FirmwareService *EspWiFi::makeFirmwareService(Context& context) { FirmwareService *fwService = new FirmwareService(context); - fwService->setOnInstall([fwService] (const std::string &location) { + fwService->setOnInstall([fwService] (const char *location) { WiFiClient client; //WiFiClientSecure client; @@ -377,7 +379,7 @@ FirmwareService *EspWiFi::makeFirmwareService(Context& context) { //ESPhttpUpdate.setLedPin(downloadStatusLedPin); - HTTPUpdateResult ret = ESPhttpUpdate.update(client, location.c_str()); + HTTPUpdateResult ret = ESPhttpUpdate.update(client, location); switch (ret) { case HTTP_UPDATE_FAILED: diff --git a/src/MicroOcpp/Model/FirmwareManagement/FirmwareService.h b/src/MicroOcpp/Model/FirmwareManagement/FirmwareService.h index 45aefaad..43a27406 100644 --- a/src/MicroOcpp/Model/FirmwareManagement/FirmwareService.h +++ b/src/MicroOcpp/Model/FirmwareManagement/FirmwareService.h @@ -45,13 +45,13 @@ class FirmwareService { Ocpp16::FirmwareStatus lastReportedStatus = Ocpp16::FirmwareStatus::Idle; bool checkedSuccessfulFwUpdate = false; - std::string location {}; - Timestamp retreiveDate = Timestamp(); - int retries = 0; + std::string location; + Timestamp retreiveDate; + unsigned int retries = 0; unsigned int retryInterval = 0; - std::function onDownload; - std::function onInstall; + std::function onDownload; + std::function onInstall; unsigned long delayTransition = 0; unsigned long timestampTransition = 0; @@ -76,15 +76,15 @@ class FirmwareService { void loop(); - void scheduleFirmwareUpdate(const std::string &location, Timestamp retreiveDate, int retries = 1, unsigned int retryInterval = 0); + void scheduleFirmwareUpdate(const char *location, Timestamp retreiveDate, unsigned int retries = 1, unsigned int retryInterval = 0); Ocpp16::FirmwareStatus getFirmwareStatus(); - void setOnDownload(std::function onDownload); + void setOnDownload(std::function onDownload); void setDownloadStatusInput(std::function downloadStatusInput); - void setOnInstall(std::function onInstall); + void setOnInstall(std::function onInstall); void setInstallationStatusInput(std::function installationStatusInput); }; diff --git a/src/MicroOcpp/Operations/UpdateFirmware.cpp b/src/MicroOcpp/Operations/UpdateFirmware.cpp index cdfba5fd..b6300573 100644 --- a/src/MicroOcpp/Operations/UpdateFirmware.cpp +++ b/src/MicroOcpp/Operations/UpdateFirmware.cpp @@ -9,43 +9,42 @@ using MicroOcpp::Ocpp16::UpdateFirmware; -UpdateFirmware::UpdateFirmware(Model& model) : model(model) { +UpdateFirmware::UpdateFirmware(FirmwareService& fwService) : fwService(fwService) { } void UpdateFirmware::processReq(JsonObject payload) { - /* - * Process the application data here. Note: you have to implement the FW update procedure in your client code. You have to set - * a onSendConfListener in which you initiate a FW update (e.g. calling ESPhttpUpdate.update(...) ) - */ - const char *loc = payload["location"] | ""; - location = loc; + const char *location = payload["location"] | ""; //check location URL. Maybe introduce Same-Origin-Policy? - if (location.empty()) { - formatError = true; - MOCPP_DBG_WARN("Could not read location. Abort"); + if (!*location) { + errorCode = "FormationViolation"; + return; + } + + int retries = payload["retries"] | 1; + int retryInterval = payload["retryInterval"] | 180; + if (retries < 0 || retryInterval < 0) { + errorCode = "PropertyConstraintViolation"; return; } //check the integrity of retrieveDate - const char *retrieveDateRaw = payload["retrieveDate"] | "Invalid"; - if (!retreiveDate.setTime(retrieveDateRaw)) { - formatError = true; - MOCPP_DBG_WARN("Could not read retrieveDate. Abort"); + if (!payload.containsKey("retrieveDate")) { + errorCode = "FormationViolation"; + return; + } + + Timestamp retrieveDate; + if (!retrieveDate.setTime(payload["retrieveDate"] | "Invalid")) { + errorCode = "PropertyConstraintViolation"; + MOCPP_DBG_WARN("bad time format"); return; } - retries = payload["retries"] | 1; - retryInterval = payload["retryInterval"] | 180; + fwService.scheduleFirmwareUpdate(location, retrieveDate, (unsigned int) retries, (unsigned int) retryInterval); } std::unique_ptr UpdateFirmware::createConf(){ - if (auto fwService = model.getFirmwareService()) { - fwService->scheduleFirmwareUpdate(location, retreiveDate, retries, retryInterval); - } else { - MOCPP_DBG_ERR("FirmwareService has not been initialized before! Please have a look at MicroOcpp.cpp for an example. Abort"); - } - return createEmptyDocument(); } diff --git a/src/MicroOcpp/Operations/UpdateFirmware.h b/src/MicroOcpp/Operations/UpdateFirmware.h index 8c99a754..8efeba37 100644 --- a/src/MicroOcpp/Operations/UpdateFirmware.h +++ b/src/MicroOcpp/Operations/UpdateFirmware.h @@ -10,20 +10,17 @@ namespace MicroOcpp { -class Model; +class FirmwareService; namespace Ocpp16 { class UpdateFirmware : public Operation { private: - Model& model; - std::string location {}; - Timestamp retreiveDate = Timestamp(); - int retries = 1; - unsigned long retryInterval = 180; - bool formatError = false; + FirmwareService& fwService; + + const char *errorCode = nullptr; public: - UpdateFirmware(Model& model); + UpdateFirmware(FirmwareService& fwService); const char* getOperationType() override {return "UpdateFirmware";} @@ -31,7 +28,7 @@ class UpdateFirmware : public Operation { std::unique_ptr createConf() override; - const char *getErrorCode() override {if (formatError) return "FormationViolation"; else return NULL;} + const char *getErrorCode() override {return errorCode;} }; } //end namespace Ocpp16 From 7db7a53b0db01a42d8aa93617cece33e01170952 Mon Sep 17 00:00:00 2001 From: matth-x <63792403+matth-x@users.noreply.github.com> Date: Sun, 8 Oct 2023 19:03:09 +0200 Subject: [PATCH 3/3] fix double executions --- .../Operations/RemoteStartTransaction.cpp | 34 +++++++++---------- .../Operations/RemoteStartTransaction.h | 4 +-- .../Operations/RemoteStopTransaction.cpp | 20 +++++------ .../Operations/RemoteStopTransaction.h | 6 +++- src/MicroOcpp/Operations/TriggerMessage.cpp | 31 +++++------------ src/MicroOcpp/Operations/TriggerMessage.h | 4 +-- 6 files changed, 42 insertions(+), 57 deletions(-) diff --git a/src/MicroOcpp/Operations/RemoteStartTransaction.cpp b/src/MicroOcpp/Operations/RemoteStartTransaction.cpp index be294aaa..5c6d9d06 100644 --- a/src/MicroOcpp/Operations/RemoteStartTransaction.cpp +++ b/src/MicroOcpp/Operations/RemoteStartTransaction.cpp @@ -21,23 +21,23 @@ const char* RemoteStartTransaction::getOperationType() { } void RemoteStartTransaction::processReq(JsonObject payload) { - connectorId = payload["connectorId"] | -1; + int connectorId = payload["connectorId"] | -1; if (!payload.containsKey("idTag")) { errorCode = "FormationViolation"; return; } - const char *idTagIn = payload["idTag"] | ""; - size_t len = strnlen(idTagIn, IDTAG_LEN_MAX + 1); - if (len > 0 && len <= IDTAG_LEN_MAX) { - snprintf(idTag, IDTAG_LEN_MAX + 1, "%s", idTagIn); - } else { + const char *idTag = payload["idTag"] | ""; + size_t len = strnlen(idTag, IDTAG_LEN_MAX + 1); + if (len == 0 || len > IDTAG_LEN_MAX) { errorCode = "PropertyConstraintViolation"; errorDescription = "idTag empty or too long"; return; } + std::unique_ptr chargingProfile; + if (payload.containsKey("chargingProfile") && model.getSmartChargingService()) { MOCPP_DBG_INFO("Setting Charging profile via RemoteStartTransaction"); @@ -62,12 +62,7 @@ void RemoteStartTransaction::processReq(JsonObject payload) { return; } } -} -std::unique_ptr RemoteStartTransaction::createConf(){ - auto doc = std::unique_ptr(new DynamicJsonDocument(JSON_OBJECT_SIZE(1))); - JsonObject payload = doc->to(); - Connector *selectConnector = nullptr; if (connectorId >= 1) { //connectorId specified for given connector, try to start Transaction here @@ -113,16 +108,21 @@ std::unique_ptr RemoteStartTransaction::createConf(){ } } - if (success) { - payload["status"] = "Accepted"; - } else { - payload["status"] = "Rejected"; - } + accepted = success; } else { MOCPP_DBG_INFO("No connector to start transaction"); + accepted = false; + } +} + +std::unique_ptr RemoteStartTransaction::createConf(){ + auto doc = std::unique_ptr(new DynamicJsonDocument(JSON_OBJECT_SIZE(1))); + JsonObject payload = doc->to(); + if (accepted) { + payload["status"] = "Accepted"; + } else { payload["status"] = "Rejected"; } - return doc; } diff --git a/src/MicroOcpp/Operations/RemoteStartTransaction.h b/src/MicroOcpp/Operations/RemoteStartTransaction.h index ea3e64d8..90b78483 100644 --- a/src/MicroOcpp/Operations/RemoteStartTransaction.h +++ b/src/MicroOcpp/Operations/RemoteStartTransaction.h @@ -18,10 +18,8 @@ namespace Ocpp16 { class RemoteStartTransaction : public Operation { private: Model& model; - int connectorId; - char idTag [IDTAG_LEN_MAX + 1] = {'\0'}; - std::unique_ptr chargingProfile; + bool accepted = false; const char *errorCode {nullptr}; const char *errorDescription = ""; diff --git a/src/MicroOcpp/Operations/RemoteStopTransaction.cpp b/src/MicroOcpp/Operations/RemoteStopTransaction.cpp index f73a138d..f19dd969 100644 --- a/src/MicroOcpp/Operations/RemoteStopTransaction.cpp +++ b/src/MicroOcpp/Operations/RemoteStopTransaction.cpp @@ -18,29 +18,29 @@ const char* RemoteStopTransaction::getOperationType(){ } void RemoteStopTransaction::processReq(JsonObject payload) { - transactionId = payload["transactionId"] | -1; -} -std::unique_ptr RemoteStopTransaction::createConf(){ - auto doc = std::unique_ptr(new DynamicJsonDocument(JSON_OBJECT_SIZE(1))); - JsonObject payload = doc->to(); - - bool canStopTransaction = false; + if (!payload.containsKey("transactionId")) { + errorCode = "FormationViolation"; + } + int transactionId = payload["transactionId"]; for (unsigned int cId = 0; cId < model.getNumConnectors(); cId++) { auto connector = model.getConnector(cId); if (connector->getTransaction() && connector->getTransaction()->getTransactionId() == transactionId) { - canStopTransaction = true; connector->endTransaction(nullptr, "Remote"); + accepted = true; } } +} - if (canStopTransaction){ +std::unique_ptr RemoteStopTransaction::createConf(){ + auto doc = std::unique_ptr(new DynamicJsonDocument(JSON_OBJECT_SIZE(1))); + JsonObject payload = doc->to(); + if (accepted){ payload["status"] = "Accepted"; } else { payload["status"] = "Rejected"; } - return doc; } diff --git a/src/MicroOcpp/Operations/RemoteStopTransaction.h b/src/MicroOcpp/Operations/RemoteStopTransaction.h index 2f778957..984de7e5 100644 --- a/src/MicroOcpp/Operations/RemoteStopTransaction.h +++ b/src/MicroOcpp/Operations/RemoteStopTransaction.h @@ -16,7 +16,9 @@ namespace Ocpp16 { class RemoteStopTransaction : public Operation { private: Model& model; - int transactionId; + bool accepted = false; + + const char *errorCode = nullptr; public: RemoteStopTransaction(Model& model); @@ -25,6 +27,8 @@ class RemoteStopTransaction : public Operation { void processReq(JsonObject payload) override; std::unique_ptr createConf() override; + + const char *getErrorCode() override {return errorCode;} }; } //end namespace Ocpp16 diff --git a/src/MicroOcpp/Operations/TriggerMessage.cpp b/src/MicroOcpp/Operations/TriggerMessage.cpp index 99cf8ee9..7f0965e5 100644 --- a/src/MicroOcpp/Operations/TriggerMessage.cpp +++ b/src/MicroOcpp/Operations/TriggerMessage.cpp @@ -35,10 +35,12 @@ void TriggerMessage::processReq(JsonObject payload) { if (connectorId < 0) { auto nConnectors = mService->getNumConnectors(); for (decltype(nConnectors) cId = 0; cId < nConnectors; cId++) { - triggeredOperations.push_back(mService->takeTriggeredMeterValues(cId)); + context.initiatePreBootOperation(mService->takeTriggeredMeterValues(cId)); + statusMessage = "Accepted"; } } else if (connectorId < mService->getNumConnectors()) { - triggeredOperations.push_back(mService->takeTriggeredMeterValues(connectorId)); + context.initiatePreBootOperation(mService->takeTriggeredMeterValues(connectorId)); + statusMessage = "Accepted"; } else { errorCode = "PropertyConstraintViolation"; } @@ -64,40 +66,23 @@ void TriggerMessage::processReq(JsonObject payload) { statusNotification->setTimeout(60000); - triggeredOperations.push_back(std::move(statusNotification)); + context.initiatePreBootOperation(std::move(statusNotification)); + statusMessage = "Accepted"; } } else { auto msg = context.getOperationRegistry().deserializeOperation(requestedMessage); if (msg) { - triggeredOperations.push_back(std::move(msg)); + context.initiatePreBootOperation(std::move(msg)); + statusMessage = "Accepted"; } else { statusMessage = "NotImplemented"; } } - - if (!triggeredOperations.empty()) { - statusMessage = "Accepted"; - } else { - if (errorCode) { - MOCPP_DBG_ERR("errorCode: %s", errorCode); - } else { - MOCPP_DBG_WARN("TriggerMessage denied. statusMessage: %s", statusMessage); - } - } - } std::unique_ptr TriggerMessage::createConf(){ auto doc = std::unique_ptr(new DynamicJsonDocument(JSON_OBJECT_SIZE(1))); JsonObject payload = doc->to(); - payload["status"] = statusMessage; - - auto op = triggeredOperations.begin(); - while (op != triggeredOperations.end()) { - context.initiatePreBootOperation(std::move(triggeredOperations.front())); - op = triggeredOperations.erase(op); - } - return doc; } diff --git a/src/MicroOcpp/Operations/TriggerMessage.h b/src/MicroOcpp/Operations/TriggerMessage.h index df77d42b..b6f91025 100644 --- a/src/MicroOcpp/Operations/TriggerMessage.h +++ b/src/MicroOcpp/Operations/TriggerMessage.h @@ -12,15 +12,13 @@ namespace MicroOcpp { class Context; -class Request; namespace Ocpp16 { class TriggerMessage : public Operation { private: Context& context; - std::vector> triggeredOperations; - const char *statusMessage {nullptr}; + const char *statusMessage = nullptr; const char *errorCode = nullptr; public: