From f88520db441f7d6931d988adbfef9f409038afd9 Mon Sep 17 00:00:00 2001 From: Rick Watson Date: Sun, 14 Jul 2019 22:13:26 +0100 Subject: [PATCH] experimenting with some refactoring --- {src => lib/framework}/APSettingsService.cpp | 2 +- {src => lib/framework}/APSettingsService.h | 2 +- {src => lib/framework}/APStatus.cpp | 6 +- {src => lib/framework}/APStatus.h | 6 +- {src => lib/framework}/ArduinoJsonJWT.cpp | 0 {src => lib/framework}/ArduinoJsonJWT.h | 0 {src => lib/framework}/AsyncArduinoJson6.h | 0 {src => lib/framework}/AsyncJsonWebHandler.h | 0 .../framework}/AuthenticationService.cpp | 11 ++- .../framework}/AuthenticationService.h | 4 +- lib/framework/ESP8266React.cpp | 69 +++++++++++++ lib/framework/ESP8266React.h | 61 ++++++++++++ {src => lib/framework}/NTPSettingsService.cpp | 2 +- {src => lib/framework}/NTPSettingsService.h | 2 +- {src => lib/framework}/NTPStatus.cpp | 6 +- {src => lib/framework}/NTPStatus.h | 5 +- {src => lib/framework}/OTASettingsService.cpp | 2 +- {src => lib/framework}/OTASettingsService.h | 2 +- {src => lib/framework}/SecurityManager.cpp | 0 {src => lib/framework}/SecurityManager.h | 0 .../framework}/SecuritySettingsService.cpp | 6 +- .../framework}/SecuritySettingsService.h | 4 +- {src => lib/framework}/SettingsPersistence.h | 2 +- {src => lib/framework}/SettingsService.h | 26 +++-- {src => lib/framework}/SimpleService.h | 30 +++--- {src => lib/framework}/SystemStatus.cpp | 6 +- {src => lib/framework}/SystemStatus.h | 6 +- {src => lib/framework}/WiFiScanner.cpp | 12 ++- {src => lib/framework}/WiFiScanner.h | 5 +- .../framework}/WiFiSettingsService.cpp | 13 ++- {src => lib/framework}/WiFiSettingsService.h | 4 +- {src => lib/framework}/WiFiStatus.cpp | 11 ++- {src => lib/framework}/WiFiStatus.h | 5 +- platformio.ini | 9 +- src/DemoProject.cpp | 9 ++ src/DemoProject.h | 23 +++++ src/main.cpp | 98 +++---------------- 37 files changed, 276 insertions(+), 173 deletions(-) rename {src => lib/framework}/APSettingsService.cpp (91%) rename {src => lib/framework}/APSettingsService.h (92%) rename {src => lib/framework}/APStatus.cpp (78%) rename {src => lib/framework}/APStatus.h (84%) rename {src => lib/framework}/ArduinoJsonJWT.cpp (100%) rename {src => lib/framework}/ArduinoJsonJWT.h (100%) rename {src => lib/framework}/AsyncArduinoJson6.h (100%) rename {src => lib/framework}/AsyncJsonWebHandler.h (100%) rename {src => lib/framework}/AuthenticationService.cpp (90%) rename {src => lib/framework}/AuthenticationService.h (88%) create mode 100644 lib/framework/ESP8266React.cpp create mode 100644 lib/framework/ESP8266React.h rename {src => lib/framework}/NTPSettingsService.cpp (94%) rename {src => lib/framework}/NTPSettingsService.h (94%) rename {src => lib/framework}/NTPStatus.cpp (82%) rename {src => lib/framework}/NTPStatus.h (85%) rename {src => lib/framework}/OTASettingsService.cpp (92%) rename {src => lib/framework}/OTASettingsService.h (92%) rename {src => lib/framework}/SecurityManager.cpp (100%) rename {src => lib/framework}/SecurityManager.h (100%) rename {src => lib/framework}/SecuritySettingsService.cpp (77%) rename {src => lib/framework}/SecuritySettingsService.h (86%) rename {src => lib/framework}/SettingsPersistence.h (99%) rename {src => lib/framework}/SettingsService.h (83%) rename {src => lib/framework}/SimpleService.h (78%) rename {src => lib/framework}/SystemStatus.cpp (81%) rename {src => lib/framework}/SystemStatus.h (84%) rename {src => lib/framework}/WiFiScanner.cpp (77%) rename {src => lib/framework}/WiFiScanner.h (85%) rename {src => lib/framework}/WiFiSettingsService.cpp (89%) rename {src => lib/framework}/WiFiSettingsService.h (92%) rename {src => lib/framework}/WiFiStatus.cpp (93%) rename {src => lib/framework}/WiFiStatus.h (93%) create mode 100644 src/DemoProject.cpp create mode 100644 src/DemoProject.h diff --git a/src/APSettingsService.cpp b/lib/framework/APSettingsService.cpp similarity index 91% rename from src/APSettingsService.cpp rename to lib/framework/APSettingsService.cpp index 98ceb58..71f09a8 100644 --- a/src/APSettingsService.cpp +++ b/lib/framework/APSettingsService.cpp @@ -1,6 +1,6 @@ #include -APSettingsService::APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, AP_SETTINGS_SERVICE_PATH, AP_SETTINGS_FILE) { +APSettingsService::APSettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, AP_SETTINGS_SERVICE_PATH, AP_SETTINGS_FILE) { onConfigUpdated(); } diff --git a/src/APSettingsService.h b/lib/framework/APSettingsService.h similarity index 92% rename from src/APSettingsService.h rename to lib/framework/APSettingsService.h index 252df7f..d08f1ef 100644 --- a/src/APSettingsService.h +++ b/lib/framework/APSettingsService.h @@ -23,7 +23,7 @@ class APSettingsService : public AdminSettingsService { public: - APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager); + APSettingsService(FS* fs, SecurityManager* securityManager); ~APSettingsService(); void loop(); diff --git a/src/APStatus.cpp b/lib/framework/APStatus.cpp similarity index 78% rename from src/APStatus.cpp rename to lib/framework/APStatus.cpp index d11cb4d..e0c399b 100644 --- a/src/APStatus.cpp +++ b/lib/framework/APStatus.cpp @@ -1,7 +1,9 @@ #include -APStatus::APStatus(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) { - _server->on(AP_STATUS_SERVICE_PATH, HTTP_GET, +APStatus::APStatus(SecurityManager* securityManager) :_securityManager(securityManager) {} + +void APStatus::init(AsyncWebServer *server){ + server->on(AP_STATUS_SERVICE_PATH, HTTP_GET, _securityManager->wrapRequest(std::bind(&APStatus::apStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED) ); } diff --git a/src/APStatus.h b/lib/framework/APStatus.h similarity index 84% rename from src/APStatus.h rename to lib/framework/APStatus.h index 0b69a43..d389052 100644 --- a/src/APStatus.h +++ b/lib/framework/APStatus.h @@ -22,11 +22,11 @@ class APStatus { public: - APStatus(AsyncWebServer *server, SecurityManager* securityManager); + APStatus(SecurityManager* securityManager); + void init(AsyncWebServer *server); private: - - AsyncWebServer* _server; + SecurityManager* _securityManager; void apStatus(AsyncWebServerRequest *request); diff --git a/src/ArduinoJsonJWT.cpp b/lib/framework/ArduinoJsonJWT.cpp similarity index 100% rename from src/ArduinoJsonJWT.cpp rename to lib/framework/ArduinoJsonJWT.cpp diff --git a/src/ArduinoJsonJWT.h b/lib/framework/ArduinoJsonJWT.h similarity index 100% rename from src/ArduinoJsonJWT.h rename to lib/framework/ArduinoJsonJWT.h diff --git a/src/AsyncArduinoJson6.h b/lib/framework/AsyncArduinoJson6.h similarity index 100% rename from src/AsyncArduinoJson6.h rename to lib/framework/AsyncArduinoJson6.h diff --git a/src/AsyncJsonWebHandler.h b/lib/framework/AsyncJsonWebHandler.h similarity index 100% rename from src/AsyncJsonWebHandler.h rename to lib/framework/AsyncJsonWebHandler.h diff --git a/src/AuthenticationService.cpp b/lib/framework/AuthenticationService.cpp similarity index 90% rename from src/AuthenticationService.cpp rename to lib/framework/AuthenticationService.cpp index c67f091..e56dd5d 100644 --- a/src/AuthenticationService.cpp +++ b/lib/framework/AuthenticationService.cpp @@ -1,18 +1,19 @@ #include -AuthenticationService::AuthenticationService(AsyncWebServer* server, SecurityManager* securityManager): - _server(server), _securityManager(securityManager) { - server->on(VERIFY_AUTHORIZATION_PATH, HTTP_GET, std::bind(&AuthenticationService::verifyAuthorization, this, std::placeholders::_1)); - +AuthenticationService::AuthenticationService(SecurityManager* securityManager) : _securityManager(securityManager) { _signInHandler.setUri(SIGN_IN_PATH); _signInHandler.setMethod(HTTP_POST); _signInHandler.setMaxContentLength(MAX_AUTHENTICATION_SIZE); _signInHandler.onRequest(std::bind(&AuthenticationService::signIn, this, std::placeholders::_1, std::placeholders::_2)); - server->addHandler(&_signInHandler); } AuthenticationService::~AuthenticationService() {} +void AuthenticationService::init(AsyncWebServer* server) { + server->on(VERIFY_AUTHORIZATION_PATH, HTTP_GET, std::bind(&AuthenticationService::verifyAuthorization, this, std::placeholders::_1)); + server->addHandler(&_signInHandler); +} + /** * Verifys that the request supplied a valid JWT. */ diff --git a/src/AuthenticationService.h b/lib/framework/AuthenticationService.h similarity index 88% rename from src/AuthenticationService.h rename to lib/framework/AuthenticationService.h index 15d2941..06afd5f 100644 --- a/src/AuthenticationService.h +++ b/lib/framework/AuthenticationService.h @@ -15,9 +15,11 @@ class AuthenticationService { public: - AuthenticationService(AsyncWebServer* server, SecurityManager* securityManager) ; + AuthenticationService(SecurityManager* securityManager); ~AuthenticationService(); + void init(AsyncWebServer* server); + private: // server instance AsyncWebServer* _server; diff --git a/lib/framework/ESP8266React.cpp b/lib/framework/ESP8266React.cpp new file mode 100644 index 0000000..4ac706e --- /dev/null +++ b/lib/framework/ESP8266React.cpp @@ -0,0 +1,69 @@ +#include + +ESP8266React::ESP8266React(FS* fs): + _fs(fs), + _securitySettingsService(_fs), + _wifiSettingsService(_fs, &_securitySettingsService), + _apSettingsService(_fs, &_securitySettingsService), + _ntpSettingsService(_fs, &_securitySettingsService), + _otaSettingsService(_fs, &_securitySettingsService), + _authenticationService(&_securitySettingsService), + _wifiScanner(&_securitySettingsService), + _wifiStatus(&_securitySettingsService), + _ntpStatus(&_securitySettingsService), + _apStatus(&_securitySettingsService), + _systemStatus(&_securitySettingsService) { +} + +void ESP8266React::init(AsyncWebServer* server) { + // Start security settings service first + _securitySettingsService.init(server); + + // Core services + _wifiSettingsService.init(server); + _apSettingsService.init(server); + _ntpSettingsService.init(server); + _otaSettingsService.init(server); + _authenticationService.init(server); + + // Utility services + _wifiScanner.init(server); + _wifiStatus.init(server); + _ntpStatus.init(server); + _apStatus.init(server); + _systemStatus.init(server); + + + // Serving static resources from /www/ + server->serveStatic("/js/", SPIFFS, "/www/js/"); + server->serveStatic("/css/", SPIFFS, "/www/css/"); + server->serveStatic("/fonts/", SPIFFS, "/www/fonts/"); + server->serveStatic("/app/", SPIFFS, "/www/app/"); + server->serveStatic("/favicon.ico", SPIFFS, "/www/favicon.ico"); + + // Serving all other get requests with "/www/index.htm" + // OPTIONS get a straight up 200 response + server->onNotFound([](AsyncWebServerRequest *request) { + if (request->method() == HTTP_GET) { + request->send(SPIFFS, "/www/index.html"); + } else if (request->method() == HTTP_OPTIONS) { + request->send(200); + } else { + request->send(404); + } + }); + + // Disable CORS if required + #if defined(ENABLE_CORS) + DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", CORS_ORIGIN); + DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization"); + DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials", "true"); + #endif +} + +void ESP8266React::loop() { + _wifiSettingsService.loop(); + _apSettingsService.loop(); + _ntpSettingsService.loop(); + _otaSettingsService.loop(); +} diff --git a/lib/framework/ESP8266React.h b/lib/framework/ESP8266React.h new file mode 100644 index 0000000..dcd1e49 --- /dev/null +++ b/lib/framework/ESP8266React.h @@ -0,0 +1,61 @@ +#ifndef ESP8266React_h +#define ESP8266React_h + +#include + +#if defined(ESP8266) + #include + #include +#elif defined(ESP_PLATFORM) + #include + #include + #include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class ESP8266React { + + public: + + ESP8266React(FS* fs); + + void init(AsyncWebServer* server); + void loop(); + + SecurityManager* getSecurityManager(){ + return &_securitySettingsService; + } + + private: + + FS* _fs; + + SecuritySettingsService _securitySettingsService; + + WiFiSettingsService _wifiSettingsService; + APSettingsService _apSettingsService; + NTPSettingsService _ntpSettingsService; + OTASettingsService _otaSettingsService; + AuthenticationService _authenticationService; + + WiFiScanner _wifiScanner; + WiFiStatus _wifiStatus; + NTPStatus _ntpStatus; + APStatus _apStatus; + SystemStatus _systemStatus; + +}; + +#endif diff --git a/src/NTPSettingsService.cpp b/lib/framework/NTPSettingsService.cpp similarity index 94% rename from src/NTPSettingsService.cpp rename to lib/framework/NTPSettingsService.cpp index 7d8c571..5f9ae2d 100644 --- a/src/NTPSettingsService.cpp +++ b/lib/framework/NTPSettingsService.cpp @@ -1,6 +1,6 @@ #include -NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE) { +NTPSettingsService::NTPSettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE) { #if defined(ESP8266) _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1)); diff --git a/src/NTPSettingsService.h b/lib/framework/NTPSettingsService.h similarity index 94% rename from src/NTPSettingsService.h rename to lib/framework/NTPSettingsService.h index e24f237..0dc36e5 100644 --- a/src/NTPSettingsService.h +++ b/lib/framework/NTPSettingsService.h @@ -21,7 +21,7 @@ class NTPSettingsService : public AdminSettingsService { public: - NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager); + NTPSettingsService(FS* fs, SecurityManager* securityManager); ~NTPSettingsService(); void loop(); diff --git a/src/NTPStatus.cpp b/lib/framework/NTPStatus.cpp similarity index 82% rename from src/NTPStatus.cpp rename to lib/framework/NTPStatus.cpp index 21ac627..5b9df26 100644 --- a/src/NTPStatus.cpp +++ b/lib/framework/NTPStatus.cpp @@ -1,7 +1,9 @@ #include -NTPStatus::NTPStatus(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) { - _server->on(NTP_STATUS_SERVICE_PATH, HTTP_GET, +NTPStatus::NTPStatus(SecurityManager* securityManager) : _securityManager(securityManager) {} + +void NTPStatus::init(AsyncWebServer *server){ + server->on(NTP_STATUS_SERVICE_PATH, HTTP_GET, _securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED) ); } diff --git a/src/NTPStatus.h b/lib/framework/NTPStatus.h similarity index 85% rename from src/NTPStatus.h rename to lib/framework/NTPStatus.h index 82c00fa..a77b0f5 100644 --- a/src/NTPStatus.h +++ b/lib/framework/NTPStatus.h @@ -23,13 +23,12 @@ class NTPStatus { public: - NTPStatus(AsyncWebServer *server, SecurityManager* securityManager); + NTPStatus(SecurityManager* securityManager); + void init(AsyncWebServer *server); private: - AsyncWebServer* _server; SecurityManager* _securityManager; - void ntpStatus(AsyncWebServerRequest *request); }; diff --git a/src/OTASettingsService.cpp b/lib/framework/OTASettingsService.cpp similarity index 92% rename from src/OTASettingsService.cpp rename to lib/framework/OTASettingsService.cpp index 7de85b4..1fed36c 100644 --- a/src/OTASettingsService.cpp +++ b/lib/framework/OTASettingsService.cpp @@ -1,6 +1,6 @@ #include -OTASettingsService::OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, OTA_SETTINGS_SERVICE_PATH, OTA_SETTINGS_FILE) { +OTASettingsService::OTASettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, OTA_SETTINGS_SERVICE_PATH, OTA_SETTINGS_FILE) { #if defined(ESP8266) _onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1)); #elif defined(ESP_PLATFORM) diff --git a/src/OTASettingsService.h b/lib/framework/OTASettingsService.h similarity index 92% rename from src/OTASettingsService.h rename to lib/framework/OTASettingsService.h index 27993a8..7b65fb0 100644 --- a/src/OTASettingsService.h +++ b/lib/framework/OTASettingsService.h @@ -23,7 +23,7 @@ class OTASettingsService : public AdminSettingsService { public: - OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager); + OTASettingsService(FS* fs, SecurityManager* securityManager); ~OTASettingsService(); void loop(); diff --git a/src/SecurityManager.cpp b/lib/framework/SecurityManager.cpp similarity index 100% rename from src/SecurityManager.cpp rename to lib/framework/SecurityManager.cpp diff --git a/src/SecurityManager.h b/lib/framework/SecurityManager.h similarity index 100% rename from src/SecurityManager.h rename to lib/framework/SecurityManager.h diff --git a/src/SecuritySettingsService.cpp b/lib/framework/SecuritySettingsService.cpp similarity index 77% rename from src/SecuritySettingsService.cpp rename to lib/framework/SecuritySettingsService.cpp index 51c355d..b8f4392 100644 --- a/src/SecuritySettingsService.cpp +++ b/lib/framework/SecuritySettingsService.cpp @@ -1,6 +1,6 @@ #include -SecuritySettingsService::SecuritySettingsService(AsyncWebServer* server, FS* fs) : AdminSettingsService(server, fs, this, SECURITY_SETTINGS_PATH, SECURITY_SETTINGS_FILE), SecurityManager() {} +SecuritySettingsService::SecuritySettingsService(FS* fs) : AdminSettingsService(fs, this, SECURITY_SETTINGS_PATH, SECURITY_SETTINGS_FILE), SecurityManager() {} SecuritySettingsService::~SecuritySettingsService() {} void SecuritySettingsService::readFromJsonObject(JsonObject& root) { @@ -29,7 +29,3 @@ void SecuritySettingsService::writeToJsonObject(JsonObject& root) { user["admin"] = _user.isAdmin(); } } - -void SecuritySettingsService::begin() { - readFromFS(); -} diff --git a/src/SecuritySettingsService.h b/lib/framework/SecuritySettingsService.h similarity index 86% rename from src/SecuritySettingsService.h rename to lib/framework/SecuritySettingsService.h index 7e5b622..356ee67 100644 --- a/src/SecuritySettingsService.h +++ b/lib/framework/SecuritySettingsService.h @@ -11,11 +11,9 @@ class SecuritySettingsService : public AdminSettingsService, public SecurityMana public: - SecuritySettingsService(AsyncWebServer* server, FS* fs); + SecuritySettingsService(FS* fs); ~SecuritySettingsService(); - void begin(); - protected: void readFromJsonObject(JsonObject& root); diff --git a/src/SettingsPersistence.h b/lib/framework/SettingsPersistence.h similarity index 99% rename from src/SettingsPersistence.h rename to lib/framework/SettingsPersistence.h index 03ca73d..72a3edb 100644 --- a/src/SettingsPersistence.h +++ b/lib/framework/SettingsPersistence.h @@ -47,7 +47,7 @@ protected: return true; } - void readFromFS(){ + void readFromFS() { File configFile = _fs->open(_filePath, "r"); // use defaults if no config found diff --git a/src/SettingsService.h b/lib/framework/SettingsService.h similarity index 83% rename from src/SettingsService.h rename to lib/framework/SettingsService.h index ecf0d6a..f645852 100644 --- a/src/SettingsService.h +++ b/lib/framework/SettingsService.h @@ -16,7 +16,6 @@ #include #include - /* * Abstraction of a service which stores it's settings as JSON in a file system. */ @@ -24,30 +23,27 @@ class SettingsService : public SettingsPersistence { public: - SettingsService(AsyncWebServer* server, FS* fs, char const* servicePath, char const* filePath): - SettingsPersistence(fs, filePath), _server(server) { - - // configure fetch config handler - _server->on(servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1)); - - // configure update settings handler + SettingsService(FS* fs, char const* servicePath, char const* filePath): + SettingsPersistence(fs, filePath), _servicePath(servicePath) { _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 void begin() { + void init(AsyncWebServer* server) { + // configure fetch config handler + server->on(_servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1)); + server->addHandler(&_updateHandler); + + // read the initial data from the file system readFromFS(); } protected: - // will serve setting endpoints from here - AsyncWebServer* _server; - + char const* _servicePath; AsyncJsonWebHandler _updateHandler; virtual void fetchConfig(AsyncWebServerRequest *request) { @@ -84,8 +80,8 @@ protected: class AdminSettingsService : public SettingsService { public: - AdminSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager, char const* servicePath, char const* filePath): - SettingsService(server, fs, servicePath, filePath), _securityManager(securityManager) { + AdminSettingsService(FS* fs, SecurityManager* securityManager, char const* servicePath, char const* filePath): + SettingsService(fs, servicePath, filePath), _securityManager(securityManager) { } protected: diff --git a/src/SimpleService.h b/lib/framework/SimpleService.h similarity index 78% rename from src/SimpleService.h rename to lib/framework/SimpleService.h index 83534b1..95d434b 100644 --- a/src/SimpleService.h +++ b/lib/framework/SimpleService.h @@ -33,7 +33,7 @@ private: AsyncJsonWebHandler _updateHandler; - void fetchConfig(AsyncWebServerRequest *request){ + void fetchConfig(AsyncWebServerRequest *request) { AsyncJsonResponse * response = new AsyncJsonResponse(MAX_SETTINGS_SIZE); JsonObject jsonObject = response->getRoot(); writeToJsonObject(jsonObject); @@ -41,8 +41,8 @@ private: request->send(response); } - void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument){ - if (jsonDocument.is()){ + void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument) { + if (jsonDocument.is()) { JsonObject newConfig = jsonDocument.as(); readFromJsonObject(newConfig); @@ -60,33 +60,31 @@ private: protected: // will serve setting endpoints from here - AsyncWebServer* _server; + char const* _servicePath; // reads the local config from the - virtual void readFromJsonObject(JsonObject& root){} - virtual void writeToJsonObject(JsonObject& root){} + virtual void readFromJsonObject(JsonObject& root) {} + virtual void writeToJsonObject(JsonObject& root) {} // implement to perform action when config has been updated - virtual void onConfigUpdated(){} + virtual void onConfigUpdated() {} public: - SimpleService(AsyncWebServer* server, char const* servicePath): - _server(server) { - - // configure fetch config handler - _server->on(servicePath, HTTP_GET, std::bind(&SimpleService::fetchConfig, this, std::placeholders::_1)); - - // configure update settings handler + SimpleService(char const* servicePath): _servicePath(servicePath) { _updateHandler.setUri(servicePath); _updateHandler.setMethod(HTTP_POST); _updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE); _updateHandler.onRequest(std::bind(&SimpleService::updateConfig, this, std::placeholders::_1, std::placeholders::_2)); - _server->addHandler(&_updateHandler); } - + virtual ~SimpleService() {} + void init(AsyncWebServer* server) { + server->on(_servicePath, HTTP_GET, std::bind(&SimpleService::fetchConfig, this, std::placeholders::_1)); + server->addHandler(&_updateHandler); + } + }; #endif // end SimpleService diff --git a/src/SystemStatus.cpp b/lib/framework/SystemStatus.cpp similarity index 81% rename from src/SystemStatus.cpp rename to lib/framework/SystemStatus.cpp index a529c4a..4eb5bcc 100644 --- a/src/SystemStatus.cpp +++ b/lib/framework/SystemStatus.cpp @@ -1,7 +1,9 @@ #include - SystemStatus::SystemStatus(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) { - _server->on(SYSTEM_STATUS_SERVICE_PATH, HTTP_GET, +SystemStatus::SystemStatus(SecurityManager* securityManager) : _securityManager(securityManager) {} + +void SystemStatus::init(AsyncWebServer *server) { + server->on(SYSTEM_STATUS_SERVICE_PATH, HTTP_GET, _securityManager->wrapRequest(std::bind(&SystemStatus::systemStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED) ); } diff --git a/src/SystemStatus.h b/lib/framework/SystemStatus.h similarity index 84% rename from src/SystemStatus.h rename to lib/framework/SystemStatus.h index 4556ea8..ed60bcd 100644 --- a/src/SystemStatus.h +++ b/lib/framework/SystemStatus.h @@ -20,12 +20,12 @@ class SystemStatus { public: - - SystemStatus(AsyncWebServer *server, SecurityManager* securityManager); + + SystemStatus(SecurityManager* securityManager); + void init(AsyncWebServer* server); private: - AsyncWebServer* _server; SecurityManager* _securityManager; void systemStatus(AsyncWebServerRequest *request); diff --git a/src/WiFiScanner.cpp b/lib/framework/WiFiScanner.cpp similarity index 77% rename from src/WiFiScanner.cpp rename to lib/framework/WiFiScanner.cpp index e3277a5..da34e1a 100644 --- a/src/WiFiScanner.cpp +++ b/lib/framework/WiFiScanner.cpp @@ -1,11 +1,13 @@ #include -WiFiScanner::WiFiScanner(AsyncWebServer *server, SecurityManager* securityManager) : _server(server) { - _server->on(SCAN_NETWORKS_SERVICE_PATH, HTTP_GET, - securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN) +WiFiScanner::WiFiScanner(SecurityManager* securityManager) : _securityManager(securityManager) {}; + +void WiFiScanner::init(AsyncWebServer* server) { + server->on(SCAN_NETWORKS_SERVICE_PATH, HTTP_GET, + _securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN) ); - _server->on(LIST_NETWORKS_SERVICE_PATH, HTTP_GET, - securityManager->wrapRequest(std::bind(&WiFiScanner::listNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN) + server->on(LIST_NETWORKS_SERVICE_PATH, HTTP_GET, + _securityManager->wrapRequest(std::bind(&WiFiScanner::listNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN) ); } diff --git a/src/WiFiScanner.h b/lib/framework/WiFiScanner.h similarity index 85% rename from src/WiFiScanner.h rename to lib/framework/WiFiScanner.h index ca14db0..74ea61e 100644 --- a/src/WiFiScanner.h +++ b/lib/framework/WiFiScanner.h @@ -24,11 +24,12 @@ class WiFiScanner { public: - WiFiScanner(AsyncWebServer *server, SecurityManager* securityManager); + WiFiScanner(SecurityManager* securityManager); + void init(AsyncWebServer *server); private: - AsyncWebServer* _server; + SecurityManager* _securityManager; void scanNetworks(AsyncWebServerRequest *request); void listNetworks(AsyncWebServerRequest *request); diff --git a/src/WiFiSettingsService.cpp b/lib/framework/WiFiSettingsService.cpp similarity index 89% rename from src/WiFiSettingsService.cpp rename to lib/framework/WiFiSettingsService.cpp index 5483b46..d6fe4ca 100644 --- a/src/WiFiSettingsService.cpp +++ b/lib/framework/WiFiSettingsService.cpp @@ -1,17 +1,24 @@ #include -WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) { +WiFiSettingsService::WiFiSettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) { + // Disable wifi config persistance and auto reconnect + WiFi.persistent(false); + WiFi.setAutoReconnect(false); + #if defined(ESP8266) _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1)); #elif defined(ESP_PLATFORM) + // Init the wifi driver on ESP32 + WiFi.mode(WIFI_MODE_MAX); + WiFi.mode(WIFI_MODE_NULL); WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED); #endif } WiFiSettingsService::~WiFiSettingsService() {} -void WiFiSettingsService::begin() { - SettingsService::begin(); +void WiFiSettingsService::init(AsyncWebServer* server) { + SettingsService::init(server); reconfigureWiFiConnection(); } diff --git a/src/WiFiSettingsService.h b/lib/framework/WiFiSettingsService.h similarity index 92% rename from src/WiFiSettingsService.h rename to lib/framework/WiFiSettingsService.h index e846728..87ce575 100644 --- a/src/WiFiSettingsService.h +++ b/lib/framework/WiFiSettingsService.h @@ -12,10 +12,10 @@ class WiFiSettingsService : public AdminSettingsService { public: - WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager); + WiFiSettingsService(FS* fs, SecurityManager* securityManager); ~WiFiSettingsService(); - void begin(); + void init(AsyncWebServer* server); void loop(); protected: diff --git a/src/WiFiStatus.cpp b/lib/framework/WiFiStatus.cpp similarity index 93% rename from src/WiFiStatus.cpp rename to lib/framework/WiFiStatus.cpp index 3e6273c..09db75c 100644 --- a/src/WiFiStatus.cpp +++ b/lib/framework/WiFiStatus.cpp @@ -1,9 +1,6 @@ #include -WiFiStatus::WiFiStatus(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) { - _server->on(WIFI_STATUS_SERVICE_PATH, HTTP_GET, - _securityManager->wrapRequest(std::bind(&WiFiStatus::wifiStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED) - ); +WiFiStatus::WiFiStatus(SecurityManager* securityManager) : _securityManager(securityManager) { #if defined(ESP8266) _onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected); _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected); @@ -50,6 +47,12 @@ void WiFiStatus::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { } #endif +void WiFiStatus::init(AsyncWebServer* server) { + server->on(WIFI_STATUS_SERVICE_PATH, HTTP_GET, + _securityManager->wrapRequest(std::bind(&WiFiStatus::wifiStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED) + ); +} + void WiFiStatus::wifiStatus(AsyncWebServerRequest *request) { AsyncJsonResponse * response = new AsyncJsonResponse(MAX_WIFI_STATUS_SIZE); JsonObject root = response->getRoot(); diff --git a/src/WiFiStatus.h b/lib/framework/WiFiStatus.h similarity index 93% rename from src/WiFiStatus.h rename to lib/framework/WiFiStatus.h index d2372a6..05aa3a1 100644 --- a/src/WiFiStatus.h +++ b/lib/framework/WiFiStatus.h @@ -22,11 +22,12 @@ class WiFiStatus { public: - WiFiStatus(AsyncWebServer *server, SecurityManager* securityManager); + WiFiStatus(SecurityManager* securityManager); + + void init(AsyncWebServer* server); private: - AsyncWebServer* _server; SecurityManager* _securityManager; #if defined(ESP8266) diff --git a/platformio.ini b/platformio.ini index c06a090..44ee3cf 100644 --- a/platformio.ini +++ b/platformio.ini @@ -7,10 +7,9 @@ ; ; Please visit documentation for the other options and examples ; http://docs.platformio.org/page/projectconf.html -[env:esp12e] -platform = espressif8266 -board = esp12e -board_build.f_cpu = 160000000L +[env:node32s] +platform = espressif32 +board = node32s extra_scripts = pre:timelib_fix.py @@ -26,7 +25,7 @@ monitor_speed = 115200 build_flags= -D NO_GLOBAL_ARDUINOOTA ; Uncomment ENABLE_CORS to enable Cross-Origin Resource Sharing (required for local React development) - ;-D ENABLE_CORS + -D ENABLE_CORS -D CORS_ORIGIN=\"http://localhost:3000\" lib_deps = NtpClientLib@>=2.5.1,<3.0.0 diff --git a/src/DemoProject.cpp b/src/DemoProject.cpp new file mode 100644 index 0000000..e73a1fa --- /dev/null +++ b/src/DemoProject.cpp @@ -0,0 +1,9 @@ +#include + +void DemoProject::begin() { + +} + +void DemoProject::loop() { + +} diff --git a/src/DemoProject.h b/src/DemoProject.h new file mode 100644 index 0000000..81d0f47 --- /dev/null +++ b/src/DemoProject.h @@ -0,0 +1,23 @@ +#ifndef DemoProject_h +#define DemoProject_h + +#include +#include + +class DemoProject { + + public: + + DemoProject(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) {} + + void begin(); + void loop(); + + private: + + AsyncWebServer* _server; + SecurityManager* _securityManager; + +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index bdae650..ddddba7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,99 +1,31 @@ -#include - -#if defined(ESP8266) - #include - #include -#elif defined(ESP_PLATFORM) - #include - #include - #include -#endif - +#include +#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #define SERIAL_BAUD_RATE 115200 AsyncWebServer server(80); +ESP8266React espServer(&SPIFFS); -SecuritySettingsService securitySettingsService = SecuritySettingsService(&server, &SPIFFS); -WiFiSettingsService wifiSettingsService = WiFiSettingsService(&server, &SPIFFS, &securitySettingsService); -APSettingsService apSettingsService = APSettingsService(&server, &SPIFFS, &securitySettingsService); -NTPSettingsService ntpSettingsService = NTPSettingsService(&server, &SPIFFS, &securitySettingsService); -OTASettingsService otaSettingsService = OTASettingsService(&server, &SPIFFS, &securitySettingsService); -AuthenticationService authenticationService = AuthenticationService(&server, &securitySettingsService); - -WiFiScanner wifiScanner = WiFiScanner(&server, &securitySettingsService); -WiFiStatus wifiStatus = WiFiStatus(&server, &securitySettingsService); -NTPStatus ntpStatus = NTPStatus(&server, &securitySettingsService); -APStatus apStatus = APStatus(&server, &securitySettingsService); -SystemStatus systemStatus = SystemStatus(&server, &securitySettingsService);; +DemoProject demoProject = DemoProject(&server, espServer.getSecurityManager()); void setup() { - // Disable wifi config persistance and auto reconnect - WiFi.persistent(false); - WiFi.setAutoReconnect(false); - -#if defined(ESP_PLATFORM) - // Init the wifi driver on ESP32 - WiFi.mode(WIFI_MODE_MAX); - WiFi.mode(WIFI_MODE_NULL); -#endif - Serial.begin(SERIAL_BAUD_RATE); SPIFFS.begin(); + + // set up the framework + espServer.init(&server); + + // begin the demo project + demoProject.begin(); - // Start security settings service first - securitySettingsService.begin(); - - // Start services - ntpSettingsService.begin(); - otaSettingsService.begin(); - apSettingsService.begin(); - wifiSettingsService.begin(); - - // Serving static resources from /www/ - server.serveStatic("/js/", SPIFFS, "/www/js/"); - server.serveStatic("/css/", SPIFFS, "/www/css/"); - server.serveStatic("/fonts/", SPIFFS, "/www/fonts/"); - server.serveStatic("/app/", SPIFFS, "/www/app/"); - server.serveStatic("/favicon.ico", SPIFFS, "/www/favicon.ico"); - - // Serving all other get requests with "/www/index.htm" - // OPTIONS get a straight up 200 response - server.onNotFound([](AsyncWebServerRequest *request) { - if (request->method() == HTTP_GET) { - request->send(SPIFFS, "/www/index.html"); - } else if (request->method() == HTTP_OPTIONS) { - request->send(200); - } else { - request->send(404); - } - }); - - // Disable CORS if required - #if defined(ENABLE_CORS) - DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", CORS_ORIGIN); - DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization"); - DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials", "true"); - #endif - server.begin(); } void loop() { - wifiSettingsService.loop(); - apSettingsService.loop(); - ntpSettingsService.loop(); - otaSettingsService.loop(); + // run the framework loop + espServer.loop(); + + // run the demo project loop + demoProject.loop(); }