From d650280a870c7c8aa1e804e9340596e485a3b91f Mon Sep 17 00:00:00 2001 From: Rick Watson Date: Sat, 30 Nov 2019 09:58:28 +0000 Subject: [PATCH] remove custom work-around for missing arduinojson6 support - it has since been added to async esp core --- lib/framework/APStatus.cpp | 2 +- lib/framework/APStatus.h | 2 +- lib/framework/AsyncArduinoJson6.h | 167 ---------------------- lib/framework/AsyncJsonCallbackResponse.h | 31 ++++ lib/framework/AuthenticationService.cpp | 2 +- lib/framework/AuthenticationService.h | 2 +- lib/framework/NTPStatus.cpp | 2 +- lib/framework/NTPStatus.h | 2 +- lib/framework/SettingsPersistence.h | 2 +- lib/framework/SettingsService.h | 71 ++++----- lib/framework/SimpleService.h | 7 +- lib/framework/SystemStatus.cpp | 2 +- lib/framework/SystemStatus.h | 2 +- lib/framework/WiFiScanner.cpp | 2 +- lib/framework/WiFiScanner.h | 2 +- lib/framework/WiFiStatus.cpp | 2 +- lib/framework/WiFiStatus.h | 2 +- 17 files changed, 87 insertions(+), 215 deletions(-) delete mode 100644 lib/framework/AsyncArduinoJson6.h create mode 100644 lib/framework/AsyncJsonCallbackResponse.h diff --git a/lib/framework/APStatus.cpp b/lib/framework/APStatus.cpp index f8d1660..819a1e2 100644 --- a/lib/framework/APStatus.cpp +++ b/lib/framework/APStatus.cpp @@ -7,7 +7,7 @@ APStatus::APStatus(AsyncWebServer* server, SecurityManager* securityManager) { } void APStatus::apStatus(AsyncWebServerRequest *request) { - AsyncJsonResponse * response = new AsyncJsonResponse(MAX_AP_STATUS_SIZE); + AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_AP_STATUS_SIZE); JsonObject root = response->getRoot(); WiFiMode_t currentWiFiMode = WiFi.getMode(); diff --git a/lib/framework/APStatus.h b/lib/framework/APStatus.h index 773f743..2f4e9c2 100644 --- a/lib/framework/APStatus.h +++ b/lib/framework/APStatus.h @@ -11,7 +11,7 @@ #include #include -#include +#include #include #include diff --git a/lib/framework/AsyncArduinoJson6.h b/lib/framework/AsyncArduinoJson6.h deleted file mode 100644 index b95ea6c..0000000 --- a/lib/framework/AsyncArduinoJson6.h +++ /dev/null @@ -1,167 +0,0 @@ - -/** -* A copy of AsyncJson.h from ESPAsyncWebServer, updated for ArduinoJson6. -*/ - -#ifndef ASYNC_ARDUINO_JSON_6_H -#define ASYNC_ARDUINO_JSON_6_H - -#include -#include -#include - -constexpr const char* JSON_MIMETYPE = "application/json"; - -class ChunkPrint : public Print { - private: - uint8_t* _destination; - size_t _to_skip; - size_t _to_write; - size_t _pos; - public: - ChunkPrint(uint8_t* destination, size_t from, size_t len) - : _destination(destination), _to_skip(from), _to_write(len), _pos{0} {} - virtual ~ChunkPrint(){} - size_t write(uint8_t c){ - if (_to_skip > 0) { - _to_skip--; - return 1; - } else if (_to_write > 0) { - _to_write--; - _destination[_pos++] = c; - return 1; - } - return 0; - } - size_t write(const uint8_t *buffer, size_t size) { - size_t written = 0; - while (written < size && write(buffer[written])) { - written++; - } - return written; - } -}; - -class AsyncJsonResponse: public AsyncAbstractResponse { - private: - DynamicJsonDocument _jsonDocument; - bool _isValid; - JsonObject _root; - - public: - AsyncJsonResponse(int maxSize): _jsonDocument(maxSize), _isValid{false} { - _code = 200; - _contentType = JSON_MIMETYPE; - _root = _jsonDocument.to(); - } - ~AsyncJsonResponse() {} - JsonObject getRoot() { - return _root; - } - bool _sourceValid() const { - return _isValid; - } - size_t setLength() { - _contentLength = measureJson(_jsonDocument); - if (_contentLength) { _isValid = true; } - return _contentLength; - } - size_t getSize() { - return _jsonDocument.size(); - } - size_t _fillBuffer(uint8_t *data, size_t len){ - ChunkPrint dest(data, _sentLength, len); - serializeJson(_jsonDocument, dest); - return len; - } -}; - -typedef std::function ArJsonRequestHandlerFunction; - -class AsyncCallbackJsonWebHandler: public AsyncWebHandler { -private: -protected: - const String _uri; - WebRequestMethodComposite _method; - ArJsonRequestHandlerFunction _onRequest; - size_t _contentLength; - size_t _maxContentLength; -public: - AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest) : _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {} - void setMethod(WebRequestMethodComposite method){ _method = method; } - void setMaxContentLength(int maxContentLength){ _maxContentLength = maxContentLength; } - void onRequest(ArJsonRequestHandlerFunction fn){ _onRequest = fn; } - virtual bool canHandle(AsyncWebServerRequest *request) override final{ - if(!_onRequest) - return false; - - if(!(_method & request->method())) - return false; - - if(_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri+"/"))) - return false; - - if (!request->contentType().equalsIgnoreCase(JSON_MIMETYPE)) - return false; - - request->addInterestingHeader("ANY"); - return true; - } - virtual void handleRequest(AsyncWebServerRequest *request) override final { - if(_onRequest) { - if (request->_tempObject != nullptr) { - DynamicJsonDocument _jsonDocument(_maxContentLength); - DeserializationError err = deserializeJson(_jsonDocument, (uint8_t*)(request->_tempObject)); - if (err == DeserializationError::Ok) { - _onRequest(request, _jsonDocument.as()); - return; - } - } - request->send(_contentLength > _maxContentLength ? 413 : 400); - } else { - request->send(500); - } - } - virtual void handleUpload(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) override final { - } - virtual void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) override final { - if (_onRequest) { - _contentLength = total; - if (total > 0 && request->_tempObject == nullptr && total < _maxContentLength) { - request->_tempObject = malloc(total); - } - if (request->_tempObject != nullptr) { - memcpy((uint8_t*)(request->_tempObject) + index, data, len); - } - } - } - virtual bool isRequestHandlerTrivial() override final {return _onRequest ? false : true;} -}; - - -/* -* Listens for a response being destroyed and calls a callback during said distruction. -* -* Used so we can take action after the response has been rendered to the client. -* -* Avoids having to fork ESPAsyncWebServer with a callback feature - still not a nice use of a destructor! -*/ - -typedef std::function AsyncJsonCallback; - -class AsyncJsonCallbackResponse: public AsyncJsonResponse { - - private: - - AsyncJsonCallback _callback; - - public: - - AsyncJsonCallbackResponse(AsyncJsonCallback callback, int maxSize) : AsyncJsonResponse(maxSize), _callback{callback} {} - ~AsyncJsonCallbackResponse() { - _callback(); - } - -}; - -#endif diff --git a/lib/framework/AsyncJsonCallbackResponse.h b/lib/framework/AsyncJsonCallbackResponse.h new file mode 100644 index 0000000..a67b4a0 --- /dev/null +++ b/lib/framework/AsyncJsonCallbackResponse.h @@ -0,0 +1,31 @@ +#ifndef _AsyncJsonCallbackResponse_H_ +#define _AsyncJsonCallbackResponse_H_ + +#include +#include + +/* +* Listens for a response being destroyed and calls a callback during said distruction. +* used so we can take action after the response has been rendered to the client. +* +* Avoids having to fork ESPAsyncWebServer with a callback feature, but not nice! +*/ + +typedef std::function AsyncJsonCallback; + +class AsyncJsonCallbackResponse : public AsyncJsonResponse +{ + +private: + AsyncJsonCallback _callback; + +public: + AsyncJsonCallbackResponse(AsyncJsonCallback callback, bool isArray = false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE) + : AsyncJsonResponse(isArray, maxJsonBufferSize), _callback{callback} {} + ~AsyncJsonCallbackResponse() + { + _callback(); + } +}; + +#endif // end _AsyncJsonCallbackResponse_H_ diff --git a/lib/framework/AuthenticationService.cpp b/lib/framework/AuthenticationService.cpp index 95f5323..8a1debf 100644 --- a/lib/framework/AuthenticationService.cpp +++ b/lib/framework/AuthenticationService.cpp @@ -30,7 +30,7 @@ void AuthenticationService::signIn(AsyncWebServerRequest *request, JsonDocument Authentication authentication = _securityManager->authenticate(username, password); if (authentication.isAuthenticated()) { User* user = authentication.getUser(); - AsyncJsonResponse * response = new AsyncJsonResponse(MAX_AUTHENTICATION_SIZE); + AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_AUTHENTICATION_SIZE); JsonObject jsonObject = response->getRoot(); jsonObject["access_token"] = _securityManager->generateJWT(user); response->setLength(); diff --git a/lib/framework/AuthenticationService.h b/lib/framework/AuthenticationService.h index 472c4ef..ae5bbc0 100644 --- a/lib/framework/AuthenticationService.h +++ b/lib/framework/AuthenticationService.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include #define VERIFY_AUTHORIZATION_PATH "/rest/verifyAuthorization" #define SIGN_IN_PATH "/rest/signIn" diff --git a/lib/framework/NTPStatus.cpp b/lib/framework/NTPStatus.cpp index 7f70d0c..87d8a90 100644 --- a/lib/framework/NTPStatus.cpp +++ b/lib/framework/NTPStatus.cpp @@ -7,7 +7,7 @@ NTPStatus::NTPStatus(AsyncWebServer* server, SecurityManager* securityManager) { } void NTPStatus::ntpStatus(AsyncWebServerRequest *request) { - AsyncJsonResponse * response = new AsyncJsonResponse(MAX_NTP_STATUS_SIZE); + AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_NTP_STATUS_SIZE); JsonObject root = response->getRoot(); // request time now first, this can sometimes force a sync diff --git a/lib/framework/NTPStatus.h b/lib/framework/NTPStatus.h index 976ca42..b6dc7fd 100644 --- a/lib/framework/NTPStatus.h +++ b/lib/framework/NTPStatus.h @@ -11,7 +11,7 @@ #include #include -#include +#include #include #include #include diff --git a/lib/framework/SettingsPersistence.h b/lib/framework/SettingsPersistence.h index 72a3edb..ad4e2d6 100644 --- a/lib/framework/SettingsPersistence.h +++ b/lib/framework/SettingsPersistence.h @@ -5,7 +5,7 @@ #include #include #include -#include +#include /** * At the moment, not expecting settings service to have to deal with large JSON diff --git a/lib/framework/SettingsService.h b/lib/framework/SettingsService.h index ab16e10..7f40505 100644 --- a/lib/framework/SettingsService.h +++ b/lib/framework/SettingsService.h @@ -2,11 +2,11 @@ #define SettingsService_h #if defined(ESP8266) - #include - #include +#include +#include #elif defined(ESP_PLATFORM) - #include - #include +#include +#include #endif #include @@ -14,66 +14,73 @@ #include #include #include -#include +#include +#include /* * Abstraction of a service which stores it's settings as JSON in a file system. */ -class SettingsService : public SettingsPersistence { +class SettingsService : public SettingsPersistence +{ - public: +public: + SettingsService(AsyncWebServer *server, FS *fs, char const *servicePath, char const *filePath) : SettingsPersistence(fs, filePath), _servicePath(servicePath) + { + server->on(_servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1)); - SettingsService(AsyncWebServer* server, FS* fs, char const* servicePath, char const* filePath): SettingsPersistence(fs, filePath), _servicePath(servicePath) { - server->on(_servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1)); - - _updateHandler.setUri(servicePath); - _updateHandler.setMethod(HTTP_POST); - _updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE); - _updateHandler.onRequest(std::bind(&SettingsService::updateConfig, this, std::placeholders::_1, std::placeholders::_2)); - server->addHandler(&_updateHandler); - } + _updateHandler.setUri(servicePath); + _updateHandler.setMethod(HTTP_POST); + _updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE); + _updateHandler.onRequest(std::bind(&SettingsService::updateConfig, this, std::placeholders::_1, std::placeholders::_2)); + server->addHandler(&_updateHandler); + } - virtual ~SettingsService() {} + virtual ~SettingsService() {} - void begin() { - // read the initial data from the file system - readFromFS(); - } + void begin() + { + // read the initial data from the file system + readFromFS(); + } protected: - char const* _servicePath; + char const *_servicePath; AsyncJsonWebHandler _updateHandler; - virtual void fetchConfig(AsyncWebServerRequest *request) { + virtual void fetchConfig(AsyncWebServerRequest *request) + { // handle the request - AsyncJsonResponse * response = new AsyncJsonResponse(MAX_SETTINGS_SIZE); - JsonObject jsonObject = response->getRoot(); + AsyncJsonResponse *response = new AsyncJsonResponse(false, MAX_SETTINGS_SIZE); + JsonObject jsonObject = response->getRoot(); writeToJsonObject(jsonObject); response->setLength(); request->send(response); } - virtual void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument) { + virtual void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument) + { // handle the request - if (jsonDocument.is()){ + if (jsonDocument.is()) + { JsonObject newConfig = jsonDocument.as(); readFromJsonObject(newConfig); writeToFS(); // write settings back with a callback to reconfigure the wifi - AsyncJsonCallbackResponse * response = new AsyncJsonCallbackResponse([this] () {onConfigUpdated();}, MAX_SETTINGS_SIZE); - JsonObject jsonObject = response->getRoot(); + AsyncJsonCallbackResponse *response = new AsyncJsonCallbackResponse([this]() { onConfigUpdated(); }, false, MAX_SETTINGS_SIZE); + JsonObject jsonObject = response->getRoot(); writeToJsonObject(jsonObject); response->setLength(); request->send(response); - } else { + } + else + { request->send(400); } } // implement to perform action when config has been updated - virtual void onConfigUpdated(){} - + virtual void onConfigUpdated() {} }; #endif // end SettingsService diff --git a/lib/framework/SimpleService.h b/lib/framework/SimpleService.h index 0541a7c..64b250c 100644 --- a/lib/framework/SimpleService.h +++ b/lib/framework/SimpleService.h @@ -11,8 +11,9 @@ #include #include -#include +#include #include +#include /** * At the moment, not expecting services to have to deal with large JSON @@ -34,7 +35,7 @@ private: AsyncJsonWebHandler _updateHandler; void fetchConfig(AsyncWebServerRequest *request) { - AsyncJsonResponse * response = new AsyncJsonResponse(MAX_SETTINGS_SIZE); + AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_SETTINGS_SIZE); JsonObject jsonObject = response->getRoot(); writeToJsonObject(jsonObject); response->setLength(); @@ -47,7 +48,7 @@ private: readFromJsonObject(newConfig); // write settings back with a callback to reconfigure the wifi - AsyncJsonCallbackResponse * response = new AsyncJsonCallbackResponse([this] () {onConfigUpdated();}, MAX_SETTINGS_SIZE); + AsyncJsonCallbackResponse * response = new AsyncJsonCallbackResponse([this] () {onConfigUpdated();}, false, MAX_SETTINGS_SIZE); JsonObject jsonObject = response->getRoot(); writeToJsonObject(jsonObject); response->setLength(); diff --git a/lib/framework/SystemStatus.cpp b/lib/framework/SystemStatus.cpp index bcdd212..325cf74 100644 --- a/lib/framework/SystemStatus.cpp +++ b/lib/framework/SystemStatus.cpp @@ -7,7 +7,7 @@ SystemStatus::SystemStatus(AsyncWebServer* server, SecurityManager* securityMana } void SystemStatus::systemStatus(AsyncWebServerRequest *request) { - AsyncJsonResponse * response = new AsyncJsonResponse(MAX_ESP_STATUS_SIZE); + AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_ESP_STATUS_SIZE); JsonObject root = response->getRoot(); #if defined(ESP8266) root["esp_platform"] = "esp8266"; diff --git a/lib/framework/SystemStatus.h b/lib/framework/SystemStatus.h index 7858bee..c93b81f 100644 --- a/lib/framework/SystemStatus.h +++ b/lib/framework/SystemStatus.h @@ -11,7 +11,7 @@ #include #include -#include +#include #include #define MAX_ESP_STATUS_SIZE 1024 diff --git a/lib/framework/WiFiScanner.cpp b/lib/framework/WiFiScanner.cpp index 309cdd6..70a3539 100644 --- a/lib/framework/WiFiScanner.cpp +++ b/lib/framework/WiFiScanner.cpp @@ -20,7 +20,7 @@ void WiFiScanner::scanNetworks(AsyncWebServerRequest *request) { void WiFiScanner::listNetworks(AsyncWebServerRequest *request) { int numNetworks = WiFi.scanComplete(); if (numNetworks > -1){ - AsyncJsonResponse * response = new AsyncJsonResponse(MAX_WIFI_SCANNER_SIZE); + AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_WIFI_SCANNER_SIZE); JsonObject root = response->getRoot(); JsonArray networks = root.createNestedArray("networks"); for (int i=0; i #include -#include +#include #include #include diff --git a/lib/framework/WiFiStatus.cpp b/lib/framework/WiFiStatus.cpp index 6c4f586..466b18e 100644 --- a/lib/framework/WiFiStatus.cpp +++ b/lib/framework/WiFiStatus.cpp @@ -51,7 +51,7 @@ void WiFiStatus::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { #endif void WiFiStatus::wifiStatus(AsyncWebServerRequest *request) { - AsyncJsonResponse * response = new AsyncJsonResponse(MAX_WIFI_STATUS_SIZE); + AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_WIFI_STATUS_SIZE); JsonObject root = response->getRoot(); wl_status_t status = WiFi.status(); root["status"] = (uint8_t) status; diff --git a/lib/framework/WiFiStatus.h b/lib/framework/WiFiStatus.h index 7132238..de7a24b 100644 --- a/lib/framework/WiFiStatus.h +++ b/lib/framework/WiFiStatus.h @@ -11,7 +11,7 @@ #include #include -#include +#include #include #include