mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
memory optimizations
This commit is contained in:
@@ -34,21 +34,19 @@ class HttpEndpoint {
|
||||
, _stateUpdater(stateUpdater)
|
||||
, _statefulService(statefulService) {
|
||||
// Create the GET and POST endpoints
|
||||
// We can't use HTTP_ANY and process one a single endpoint due to the way the ESPAsyncWebServer library works
|
||||
// Could also use server->on() but this is more efficient
|
||||
|
||||
// create the GET
|
||||
GEThandler = new AsyncCallbackWebHandler();
|
||||
GEThandler->setUri(servicePath);
|
||||
GEThandler->setMethod(HTTP_GET);
|
||||
GEThandler->onRequest(securityManager->wrapRequest(std::bind(&HttpEndpoint::fetchSettings, this, _1), authenticationPredicate));
|
||||
server->addHandler(GEThandler);
|
||||
|
||||
// create the POST
|
||||
POSThandler =
|
||||
new AsyncCallbackJsonWebHandler(servicePath,
|
||||
securityManager->wrapCallback(std::bind(&HttpEndpoint::updateSettings, this, _1, _2), authenticationPredicate));
|
||||
POSThandler->setMethod(HTTP_POST);
|
||||
POSThandler = new AsyncCallbackJsonWebHandler(servicePath,
|
||||
securityManager->wrapCallback(
|
||||
[this](AsyncWebServerRequest * request, JsonVariant json) {
|
||||
//
|
||||
if (request->method() == HTTP_GET) {
|
||||
fetchSettings(request);
|
||||
} else if (request->method() == HTTP_POST) {
|
||||
updateSettings(request, json);
|
||||
} else {
|
||||
request->send(405, "application/json", "{\"message\":\"Method Not Allowed\"}");
|
||||
}
|
||||
},
|
||||
authenticationPredicate));
|
||||
server->addHandler(POSThandler);
|
||||
}
|
||||
|
||||
@@ -59,20 +57,25 @@ class HttpEndpoint {
|
||||
request->send(400);
|
||||
return;
|
||||
}
|
||||
|
||||
JsonObject jsonObject = json.as<JsonObject>();
|
||||
StateUpdateResult outcome = _statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
||||
|
||||
if (outcome == StateUpdateResult::ERROR) {
|
||||
request->send(400);
|
||||
return;
|
||||
} else if ((outcome == StateUpdateResult::CHANGED) || (outcome == StateUpdateResult::CHANGED_RESTART)) {
|
||||
request->onDisconnect([this]() { _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); });
|
||||
}
|
||||
|
||||
AsyncJsonResponse * response = new AsyncJsonResponse(false);
|
||||
jsonObject = response->getRoot().to<JsonObject>();
|
||||
_statefulService->read(jsonObject, _stateReader);
|
||||
|
||||
if (outcome == StateUpdateResult::CHANGED_RESTART) {
|
||||
response->setCode(205); // reboot required
|
||||
}
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user