catch bad_alloc for Huawei and Pylontech WebApi_ws
This commit is contained in:
parent
0c34554b9c
commit
c0dff1e7df
@ -33,8 +33,6 @@ void WebApiWsHuaweiLiveClass::init(AsyncWebServer* server)
|
|||||||
|
|
||||||
void WebApiWsHuaweiLiveClass::loop()
|
void WebApiWsHuaweiLiveClass::loop()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
// see: https://github.com/me-no-dev/ESPAsyncWebServer#limiting-the-number-of-web-socket-clients
|
// see: https://github.com/me-no-dev/ESPAsyncWebServer#limiting-the-number-of-web-socket-clients
|
||||||
if (millis() - _lastWsCleanup > 1000) {
|
if (millis() - _lastWsCleanup > 1000) {
|
||||||
_ws.cleanupClients();
|
_ws.cleanupClients();
|
||||||
@ -51,24 +49,28 @@ void WebApiWsHuaweiLiveClass::loop()
|
|||||||
}
|
}
|
||||||
_lastUpdateCheck = millis();
|
_lastUpdateCheck = millis();
|
||||||
|
|
||||||
DynamicJsonDocument root(1024);
|
try {
|
||||||
JsonVariant var = root;
|
String buffer;
|
||||||
generateJsonResponse(var);
|
// free JsonDocument as soon as possible
|
||||||
|
{
|
||||||
String buffer;
|
DynamicJsonDocument root(1024);
|
||||||
if (buffer) {
|
JsonVariant var = root;
|
||||||
serializeJson(root, buffer);
|
generateJsonResponse(var);
|
||||||
|
serializeJson(root, buffer);
|
||||||
if (Configuration.get().Security_AllowReadonly) {
|
|
||||||
_ws.setAuthentication("", "");
|
|
||||||
} else {
|
|
||||||
_ws.setAuthentication(AUTH_USERNAME, Configuration.get().Security_Password);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_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)
|
void WebApiWsHuaweiLiveClass::generateJsonResponse(JsonVariant& root)
|
||||||
@ -119,10 +121,16 @@ void WebApiWsHuaweiLiveClass::onLivedataStatus(AsyncWebServerRequest* request)
|
|||||||
if (!WebApi.checkCredentialsReadonly(request)) {
|
if (!WebApi.checkCredentialsReadonly(request)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U);
|
try {
|
||||||
JsonVariant root = response->getRoot().as<JsonVariant>();
|
AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U);
|
||||||
generateJsonResponse(root);
|
JsonVariant root = response->getRoot().as<JsonVariant>();
|
||||||
|
generateJsonResponse(root);
|
||||||
|
|
||||||
response->setLength();
|
response->setLength();
|
||||||
request->send(response);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -49,24 +49,28 @@ void WebApiWsPylontechLiveClass::loop()
|
|||||||
}
|
}
|
||||||
_lastUpdateCheck = millis();
|
_lastUpdateCheck = millis();
|
||||||
|
|
||||||
DynamicJsonDocument root(1024);
|
try {
|
||||||
JsonVariant var = root;
|
String buffer;
|
||||||
generateJsonResponse(var);
|
// free JsonDocument as soon as possible
|
||||||
|
{
|
||||||
String buffer;
|
DynamicJsonDocument root(1024);
|
||||||
if (buffer) {
|
JsonVariant var = root;
|
||||||
serializeJson(root, buffer);
|
generateJsonResponse(var);
|
||||||
|
serializeJson(root, buffer);
|
||||||
if (Configuration.get().Security_AllowReadonly) {
|
|
||||||
_ws.setAuthentication("", "");
|
|
||||||
} else {
|
|
||||||
_ws.setAuthentication(AUTH_USERNAME, Configuration.get().Security_Password);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_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)
|
void WebApiWsPylontechLiveClass::generateJsonResponse(JsonVariant& root)
|
||||||
@ -137,10 +141,16 @@ void WebApiWsPylontechLiveClass::onLivedataStatus(AsyncWebServerRequest* request
|
|||||||
if (!WebApi.checkCredentialsReadonly(request)) {
|
if (!WebApi.checkCredentialsReadonly(request)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U);
|
try {
|
||||||
JsonVariant root = response->getRoot().as<JsonVariant>();
|
AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U);
|
||||||
generateJsonResponse(root);
|
JsonVariant root = response->getRoot().as<JsonVariant>();
|
||||||
|
generateJsonResponse(root);
|
||||||
|
|
||||||
response->setLength();
|
response->setLength();
|
||||||
request->send(response);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user