From 28de5bb097aa11592fdd6c5e2c7bbca4fd6ee15b Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Mon, 26 Dec 2022 15:42:31 +0100 Subject: [PATCH 1/2] fix #841 --- src/command.cpp | 5 ++++- src/emsdevice.cpp | 2 +- src/emsdevice.h | 2 +- src/emsesp.cpp | 4 ++-- src/emsesp.h | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index 8f2b489ad..220b69006 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -247,10 +247,13 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char * // id may be used to represent a heating circuit for example // returns 0 if the command errored, 1 (TRUE) if ok, 2 if not found, 3 if error or 4 if not allowed uint8_t Command::call(const uint8_t device_type, const char * cmd, const char * value, const bool is_admin, const int8_t id, JsonObject & output) { + if (cmd == nullptr) { + return CommandRet::NOT_FOUND; + } uint8_t return_code = CommandRet::OK; auto dname = EMSdevice::device_type_2_device_name(device_type); - uint8_t device_id = EMSESP::device_id_from_cmd(device_type, id, cmd); + uint8_t device_id = EMSESP::device_id_from_cmd(device_type, cmd, id); // see if there is a command registered auto cf = find_command(device_type, device_id, cmd); diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index 6f9e280e1..cf73f54d2 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -274,7 +274,7 @@ bool EMSdevice::has_tag(const uint8_t tag) const { } // check if the device has a command on the with this tag. -bool EMSdevice::has_cmd(const int8_t id, const char * cmd) const { +bool EMSdevice::has_cmd(const char * cmd, const int8_t id) const { uint8_t tag = DeviceValueTAG::TAG_HC1 + id - 1; for (const auto & dv : devicevalues_) { if ((id < 1 || dv.tag == tag) && dv.has_cmd && strcmp(dv.short_name, cmd) == 0) { diff --git a/src/emsdevice.h b/src/emsdevice.h index a826e9383..2eff48154 100644 --- a/src/emsdevice.h +++ b/src/emsdevice.h @@ -53,7 +53,7 @@ class EMSdevice { static std::string tag_to_mqtt(uint8_t tag); bool has_tag(const uint8_t tag) const; - bool has_cmd(const int8_t id, const char * cmd) const; + bool has_cmd(const char * cmd, const int8_t id) const; inline uint8_t device_id() const { return device_id_; diff --git a/src/emsesp.cpp b/src/emsesp.cpp index d4cc7a02f..3d5e77c6c 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -114,9 +114,9 @@ bool EMSESP::cmd_is_readonly(const uint8_t device_type, const uint8_t device_id, return false; } -uint8_t EMSESP::device_id_from_cmd(const uint8_t device_type, const int8_t id, const char * cmd) { +uint8_t EMSESP::device_id_from_cmd(const uint8_t device_type, const char * cmd, const int8_t id) { for (const auto & emsdevice : emsdevices) { - if (emsdevice && emsdevice->device_type() == device_type && emsdevice->has_cmd(id, cmd)) { + if (emsdevice && emsdevice->device_type() == device_type && emsdevice->has_cmd(cmd, id)) { return emsdevice->device_id(); } } diff --git a/src/emsesp.h b/src/emsesp.h index 876bdd795..661918ef1 100644 --- a/src/emsesp.h +++ b/src/emsesp.h @@ -131,7 +131,7 @@ class EMSESP { static bool device_exists(const uint8_t device_id); static bool cmd_is_readonly(const uint8_t device_type, const uint8_t device_id, const char * cmd, const int8_t id); - static uint8_t device_id_from_cmd(const uint8_t device_type, const int8_t id, const char * cmd); + static uint8_t device_id_from_cmd(const uint8_t device_type, const char * cmd, const int8_t id); static uint8_t count_devices(const uint8_t device_type); static uint8_t count_devices(); static uint8_t device_index(const uint8_t device_type, const uint8_t unique_id); From a41de7ed1ca6d5257d6d62e306e0985a151bc9d4 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Tue, 27 Dec 2022 10:22:28 +0100 Subject: [PATCH 2/2] Avoid 507, reduce buffer stepwise until it fits --- src/web/WebAPIService.cpp | 12 +++++------- src/web/WebCustomizationService.cpp | 12 +++++------- src/web/WebDataService.cpp | 12 +++++------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/web/WebAPIService.cpp b/src/web/WebAPIService.cpp index 5fe1a40d2..1fc33e827 100644 --- a/src/web/WebAPIService.cpp +++ b/src/web/WebAPIService.cpp @@ -101,14 +101,12 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject & input) { } // output json buffer - auto * response = new PrettyAsyncJsonResponse(false, EMSESP_JSON_SIZE_XXLARGE_DYN); - if (!response->getSize()) { + size_t buffer = EMSESP_JSON_SIZE_XXLARGE_DYN; + auto * response = new PrettyAsyncJsonResponse(false, buffer); + while (!response->getSize()) { delete response; - response = new PrettyAsyncJsonResponse(false, 256); - response->setCode(507); // Insufficient Storage - response->setLength(); - request->send(response); - return; + buffer -= 1024; + response = new PrettyAsyncJsonResponse(false, buffer); } JsonObject output = response->getRoot(); diff --git a/src/web/WebCustomizationService.cpp b/src/web/WebCustomizationService.cpp index 5b2289d64..16c267585 100644 --- a/src/web/WebCustomizationService.cpp +++ b/src/web/WebCustomizationService.cpp @@ -200,14 +200,12 @@ void WebCustomizationService::devices(AsyncWebServerRequest * request) { // send back list of device entities void WebCustomizationService::device_entities(AsyncWebServerRequest * request, JsonVariant & json) { if (json.is()) { - auto * response = new MsgpackAsyncJsonResponse(true, EMSESP_JSON_SIZE_XXXLARGE_DYN); - if (!response->getSize()) { + size_t buffer = EMSESP_JSON_SIZE_XXXLARGE_DYN; + auto * response = new PrettyAsyncJsonResponse(true, buffer); + while (!response->getSize()) { delete response; - response = new MsgpackAsyncJsonResponse(true, 256); - response->setCode(507); // Insufficient Storage - response->setLength(); - request->send(response); - return; + buffer -= 1024; + response = new PrettyAsyncJsonResponse(true, buffer); } for (const auto & emsdevice : EMSESP::emsdevices) { if (emsdevice->unique_id() == json["id"]) { diff --git a/src/web/WebDataService.cpp b/src/web/WebDataService.cpp index e5187f69e..26ad1fa36 100644 --- a/src/web/WebDataService.cpp +++ b/src/web/WebDataService.cpp @@ -165,14 +165,12 @@ void WebDataService::sensor_data(AsyncWebServerRequest * request) { // Compresses the JSON using MsgPack https://msgpack.org/index.html void WebDataService::device_data(AsyncWebServerRequest * request, JsonVariant & json) { if (json.is()) { - auto * response = new MsgpackAsyncJsonResponse(false, EMSESP_JSON_SIZE_XXXLARGE_DYN); - if (!response->getSize()) { + size_t buffer = EMSESP_JSON_SIZE_XXXLARGE_DYN; + auto * response = new PrettyAsyncJsonResponse(false, buffer); + while (!response->getSize()) { delete response; - response = new MsgpackAsyncJsonResponse(false, 256); - response->setCode(507); // Insufficient Storage - response->setLength(); - request->send(response); - return; + buffer -= 1024; + response = new PrettyAsyncJsonResponse(false, buffer); } for (const auto & emsdevice : EMSESP::emsdevices) { if (emsdevice->unique_id() == json["id"]) {