memory optimizations

This commit is contained in:
Proddy
2024-02-05 09:38:11 +01:00
parent 738d9b1d94
commit 253adfeb45
7 changed files with 33 additions and 182 deletions

View File

@@ -1,23 +0,0 @@
{
"name": "AsyncTCP-esphome",
"description": "Asynchronous TCP Library for ESP32",
"keywords": "async,tcp",
"authors": {
"name": "Hristo Gochkov",
"maintainer": true
},
"repository": {
"type": "git",
"url": "https://github.com/esphome/AsyncTCP.git"
},
"version": "2.1.1",
"license": "LGPL-3.0",
"frameworks": "arduino",
"platforms": [
"espressif32",
"libretiny"
],
"build": {
"libCompatMode": 2
}
}

View File

@@ -1,43 +0,0 @@
{
"name": "ESP Async WebServer",
"version": "2.6.1",
"description": "Asynchronous HTTP and WebSocket Server Library for ESP32. Supports: WebSocket, SSE, Authentication, Arduino Json 7, File Upload, Static File serving, URL Rewrite, URL Redirect, etc.",
"keywords": "http,async,websocket,webserver",
"homepage": "https://github.com/mathieucarbou/ESPAsyncWebServer",
"repository": {
"type": "git",
"url": "https://github.com/mathieucarbou/ESPAsyncWebServer.git"
},
"authors": [
{
"name": "Hristo Gochkov"
},
{
"name": "Mathieu Carbou",
"maintainer": true
}
],
"license": "LGPL-3.0",
"frameworks": "arduino",
"platforms": [
"espressif32"
],
"dependencies": [
{
"owner": "esphome",
"name": "AsyncTCP-esphome",
"version": "^2.1.1",
"platforms": "espressif32"
}
],
"export": {
"include": [
"examples",
"src",
"library.json",
"library.properties",
"LICENSE",
"README.md"
]
}
}

View File

@@ -38,18 +38,8 @@
#include <ESPAsyncWebServer.h>
#include <Print.h>
#if ARDUINOJSON_VERSION_MAJOR == 6
#ifndef DYNAMIC_JSON_DOCUMENT_SIZE
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
#endif
#endif
constexpr const char * JSON_MIMETYPE = "application/json";
/*
* Json Response
* */
class ChunkPrint : public Print {
private:
uint8_t * _destination;
@@ -129,40 +119,11 @@ class MsgpackAsyncJsonResponse : public AsyncAbstractResponse {
class AsyncJsonResponse : public AsyncAbstractResponse {
protected:
#if ARDUINOJSON_VERSION_MAJOR == 5
DynamicJsonBuffer _jsonBuffer;
#elif ARDUINOJSON_VERSION_MAJOR == 6
DynamicJsonDocument _jsonBuffer;
#else
JsonDocument _jsonBuffer;
#endif
JsonVariant _root;
bool _isValid;
JsonVariant _root;
bool _isValid;
public:
#if ARDUINOJSON_VERSION_MAJOR == 5
AsyncJsonResponse(bool isArray = false)
: _isValid{false} {
_code = 200;
_contentType = JSON_MIMETYPE;
if (isArray)
_root = _jsonBuffer.createArray();
else
_root = _jsonBuffer.createObject();
}
#elif ARDUINOJSON_VERSION_MAJOR == 6
AsyncJsonResponse(bool isArray = false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE)
: _jsonBuffer(maxJsonBufferSize)
, _isValid{false} {
_code = 200;
_contentType = JSON_MIMETYPE;
if (isArray)
_root = _jsonBuffer.createNestedArray();
else
_root = _jsonBuffer.createNestedObject();
}
#else
AsyncJsonResponse(bool isArray = false)
: _isValid{false} {
_code = 200;
@@ -172,7 +133,6 @@ class AsyncJsonResponse : public AsyncAbstractResponse {
else
_root = _jsonBuffer.add<JsonObject>();
}
#endif
~AsyncJsonResponse() {
}
@@ -183,11 +143,7 @@ class AsyncJsonResponse : public AsyncAbstractResponse {
return _isValid;
}
size_t setLength() {
#if ARDUINOJSON_VERSION_MAJOR == 5
_contentLength = _root.measureLength();
#else
_contentLength = measureJson(_root);
#endif
if (_contentLength) {
_isValid = true;
@@ -201,33 +157,18 @@ class AsyncJsonResponse : public AsyncAbstractResponse {
size_t _fillBuffer(uint8_t * data, size_t len) {
ChunkPrint dest(data, _sentLength, len);
#if ARDUINOJSON_VERSION_MAJOR == 5
_root.printTo(dest);
#else
serializeJson(_root, dest);
#endif
return len;
}
};
class PrettyAsyncJsonResponse : public AsyncJsonResponse {
public:
#if ARDUINOJSON_VERSION_MAJOR == 6
PrettyAsyncJsonResponse(bool isArray = false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE)
: AsyncJsonResponse{isArray, maxJsonBufferSize} {
}
#else
PrettyAsyncJsonResponse(bool isArray = false)
: AsyncJsonResponse{isArray} {
}
#endif
size_t setLength() {
#if ARDUINOJSON_VERSION_MAJOR == 5
_contentLength = _root.measurePrettyLength();
#else
_contentLength = measureJsonPretty(_root);
#endif
if (_contentLength) {
_isValid = true;
}
@@ -235,11 +176,7 @@ class PrettyAsyncJsonResponse : public AsyncJsonResponse {
}
size_t _fillBuffer(uint8_t * data, size_t len) {
ChunkPrint dest(data, _sentLength, len);
#if ARDUINOJSON_VERSION_MAJOR == 5
_root.prettyPrintTo(dest);
#else
serializeJsonPretty(_root, dest);
#endif
return len;
}
};
@@ -253,28 +190,15 @@ class AsyncCallbackJsonWebHandler : public AsyncWebHandler {
WebRequestMethodComposite _method;
ArJsonRequestHandlerFunction _onRequest;
size_t _contentLength;
#if ARDUINOJSON_VERSION_MAJOR == 6
const size_t maxJsonBufferSize;
#endif
size_t _maxContentLength;
size_t _maxContentLength;
public:
#if ARDUINOJSON_VERSION_MAJOR == 6
AsyncCallbackJsonWebHandler(const String & uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE)
: _uri(uri)
, _method(HTTP_POST | HTTP_PUT | HTTP_PATCH)
, _onRequest(onRequest)
, maxJsonBufferSize(maxJsonBufferSize)
, _maxContentLength(16384) {
}
#else
AsyncCallbackJsonWebHandler(const String & uri, ArJsonRequestHandlerFunction onRequest)
: _uri(uri)
, _method(HTTP_POST | HTTP_PUT | HTTP_PATCH)
, _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH)
, _onRequest(onRequest)
, _maxContentLength(16384) {
}
#endif
void setMethod(WebRequestMethodComposite method) {
_method = method;
@@ -286,6 +210,7 @@ class AsyncCallbackJsonWebHandler : public AsyncWebHandler {
_onRequest = fn;
}
virtual bool canHandle(AsyncWebServerRequest * request) override final {
if (!_onRequest)
return false;
@@ -305,28 +230,17 @@ class AsyncCallbackJsonWebHandler : public AsyncWebHandler {
virtual void handleRequest(AsyncWebServerRequest * request) override final {
if (_onRequest) {
JsonVariant json; // empty variant
if (request->_tempObject != NULL) {
#if ARDUINOJSON_VERSION_MAJOR == 5
DynamicJsonBuffer jsonBuffer;
JsonVariant json = jsonBuffer.parse((uint8_t *)(request->_tempObject));
if (json.success()) {
#elif ARDUINOJSON_VERSION_MAJOR == 6
DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize);
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t *)(request->_tempObject));
if (!error) {
JsonVariant json = jsonBuffer.as<JsonVariant>();
#else
JsonDocument jsonBuffer;
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t *)(request->_tempObject));
if (!error) {
JsonVariant json = jsonBuffer.as<JsonVariant>();
#endif
json = jsonBuffer.as<JsonVariant>();
_onRequest(request, json);
return;
}
}
request->send(_contentLength > _maxContentLength ? 413 : 400);
_onRequest(request, json);
} else {
request->send(500);
}

View File

@@ -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);
}