diff --git a/src/WebApi_ws_Huawei.cpp b/src/WebApi_ws_Huawei.cpp index d7d77a0a..7e15db8d 100644 --- a/src/WebApi_ws_Huawei.cpp +++ b/src/WebApi_ws_Huawei.cpp @@ -33,8 +33,6 @@ void WebApiWsHuaweiLiveClass::init(AsyncWebServer* server) void WebApiWsHuaweiLiveClass::loop() { - - // see: https://github.com/me-no-dev/ESPAsyncWebServer#limiting-the-number-of-web-socket-clients if (millis() - _lastWsCleanup > 1000) { _ws.cleanupClients(); @@ -51,24 +49,28 @@ void WebApiWsHuaweiLiveClass::loop() } _lastUpdateCheck = millis(); - DynamicJsonDocument root(1024); - JsonVariant var = root; - generateJsonResponse(var); - - String buffer; - if (buffer) { - serializeJson(root, buffer); - - if (Configuration.get().Security_AllowReadonly) { - _ws.setAuthentication("", ""); - } else { - _ws.setAuthentication(AUTH_USERNAME, Configuration.get().Security_Password); + try { + String buffer; + // free JsonDocument as soon as possible + { + DynamicJsonDocument root(1024); + JsonVariant var = root; + generateJsonResponse(var); + serializeJson(root, buffer); } - _ws.textAll(buffer); - } + if (buffer) { + if (Configuration.get().Security_AllowReadonly) { + _ws.setAuthentication("", ""); + } else { + _ws.setAuthentication(AUTH_USERNAME, Configuration.get().Security_Password); + } - + _ws.textAll(buffer); + } + } catch (std::bad_alloc& bad_alloc) { + MessageOutput.printf("Call to /api/livedata/status temporarely out of resources. Reason: \"%s\".\r\n", bad_alloc.what()); + } } void WebApiWsHuaweiLiveClass::generateJsonResponse(JsonVariant& root) @@ -119,10 +121,16 @@ void WebApiWsHuaweiLiveClass::onLivedataStatus(AsyncWebServerRequest* request) if (!WebApi.checkCredentialsReadonly(request)) { return; } - AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U); - JsonVariant root = response->getRoot().as(); - generateJsonResponse(root); + try { + AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U); + JsonVariant root = response->getRoot().as(); + generateJsonResponse(root); - response->setLength(); - request->send(response); + response->setLength(); + request->send(response); + } catch (std::bad_alloc& bad_alloc) { + MessageOutput.printf("Call to /api/livedata/status temporarely out of resources. Reason: \"%s\".\r\n", bad_alloc.what()); + + WebApi.sendTooManyRequests(request); + } } \ No newline at end of file diff --git a/src/WebApi_ws_Pylontech.cpp b/src/WebApi_ws_Pylontech.cpp index 30c3a646..60cf0e5f 100644 --- a/src/WebApi_ws_Pylontech.cpp +++ b/src/WebApi_ws_Pylontech.cpp @@ -49,24 +49,28 @@ void WebApiWsPylontechLiveClass::loop() } _lastUpdateCheck = millis(); - DynamicJsonDocument root(1024); - JsonVariant var = root; - generateJsonResponse(var); - - String buffer; - if (buffer) { - serializeJson(root, buffer); - - if (Configuration.get().Security_AllowReadonly) { - _ws.setAuthentication("", ""); - } else { - _ws.setAuthentication(AUTH_USERNAME, Configuration.get().Security_Password); + try { + String buffer; + // free JsonDocument as soon as possible + { + DynamicJsonDocument root(1024); + JsonVariant var = root; + generateJsonResponse(var); + serializeJson(root, buffer); } - _ws.textAll(buffer); - } + if (buffer) { + if (Configuration.get().Security_AllowReadonly) { + _ws.setAuthentication("", ""); + } else { + _ws.setAuthentication(AUTH_USERNAME, Configuration.get().Security_Password); + } - + _ws.textAll(buffer); + } + } catch (std::bad_alloc& bad_alloc) { + MessageOutput.printf("Call to /api/livedata/status temporarely out of resources. Reason: \"%s\".\r\n", bad_alloc.what()); + } } void WebApiWsPylontechLiveClass::generateJsonResponse(JsonVariant& root) @@ -137,10 +141,16 @@ void WebApiWsPylontechLiveClass::onLivedataStatus(AsyncWebServerRequest* request if (!WebApi.checkCredentialsReadonly(request)) { return; } - AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U); - JsonVariant root = response->getRoot().as(); - generateJsonResponse(root); + try { + AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U); + JsonVariant root = response->getRoot().as(); + generateJsonResponse(root); - response->setLength(); - request->send(response); + response->setLength(); + request->send(response); + } catch (std::bad_alloc& bad_alloc) { + MessageOutput.printf("Call to /api/livedata/status temporarely out of resources. Reason: \"%s\".\r\n", bad_alloc.what()); + + WebApi.sendTooManyRequests(request); + } } \ No newline at end of file