experimenting with some refactoring
This commit is contained in:
parent
a0d6524180
commit
f88520db44
@ -1,6 +1,6 @@
|
|||||||
#include <APSettingsService.h>
|
#include <APSettingsService.h>
|
||||||
|
|
||||||
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();
|
onConfigUpdated();
|
||||||
}
|
}
|
||||||
|
|
@ -23,7 +23,7 @@ class APSettingsService : public AdminSettingsService {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
APSettingsService(FS* fs, SecurityManager* securityManager);
|
||||||
~APSettingsService();
|
~APSettingsService();
|
||||||
|
|
||||||
void loop();
|
void loop();
|
@ -1,7 +1,9 @@
|
|||||||
#include <APStatus.h>
|
#include <APStatus.h>
|
||||||
|
|
||||||
APStatus::APStatus(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) {
|
APStatus::APStatus(SecurityManager* securityManager) :_securityManager(securityManager) {}
|
||||||
_server->on(AP_STATUS_SERVICE_PATH, HTTP_GET,
|
|
||||||
|
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)
|
_securityManager->wrapRequest(std::bind(&APStatus::apStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
|
||||||
);
|
);
|
||||||
}
|
}
|
@ -22,11 +22,11 @@ class APStatus {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
APStatus(AsyncWebServer *server, SecurityManager* securityManager);
|
APStatus(SecurityManager* securityManager);
|
||||||
|
void init(AsyncWebServer *server);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
AsyncWebServer* _server;
|
|
||||||
SecurityManager* _securityManager;
|
SecurityManager* _securityManager;
|
||||||
|
|
||||||
void apStatus(AsyncWebServerRequest *request);
|
void apStatus(AsyncWebServerRequest *request);
|
@ -1,18 +1,19 @@
|
|||||||
#include <AuthenticationService.h>
|
#include <AuthenticationService.h>
|
||||||
|
|
||||||
AuthenticationService::AuthenticationService(AsyncWebServer* server, SecurityManager* securityManager):
|
AuthenticationService::AuthenticationService(SecurityManager* securityManager) : _securityManager(securityManager) {
|
||||||
_server(server), _securityManager(securityManager) {
|
|
||||||
server->on(VERIFY_AUTHORIZATION_PATH, HTTP_GET, std::bind(&AuthenticationService::verifyAuthorization, this, std::placeholders::_1));
|
|
||||||
|
|
||||||
_signInHandler.setUri(SIGN_IN_PATH);
|
_signInHandler.setUri(SIGN_IN_PATH);
|
||||||
_signInHandler.setMethod(HTTP_POST);
|
_signInHandler.setMethod(HTTP_POST);
|
||||||
_signInHandler.setMaxContentLength(MAX_AUTHENTICATION_SIZE);
|
_signInHandler.setMaxContentLength(MAX_AUTHENTICATION_SIZE);
|
||||||
_signInHandler.onRequest(std::bind(&AuthenticationService::signIn, this, std::placeholders::_1, std::placeholders::_2));
|
_signInHandler.onRequest(std::bind(&AuthenticationService::signIn, this, std::placeholders::_1, std::placeholders::_2));
|
||||||
server->addHandler(&_signInHandler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AuthenticationService::~AuthenticationService() {}
|
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.
|
* Verifys that the request supplied a valid JWT.
|
||||||
*/
|
*/
|
@ -15,9 +15,11 @@ class AuthenticationService {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
AuthenticationService(AsyncWebServer* server, SecurityManager* securityManager) ;
|
AuthenticationService(SecurityManager* securityManager);
|
||||||
~AuthenticationService();
|
~AuthenticationService();
|
||||||
|
|
||||||
|
void init(AsyncWebServer* server);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// server instance
|
// server instance
|
||||||
AsyncWebServer* _server;
|
AsyncWebServer* _server;
|
69
lib/framework/ESP8266React.cpp
Normal file
69
lib/framework/ESP8266React.cpp
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
#include <ESP8266React.h>
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
61
lib/framework/ESP8266React.h
Normal file
61
lib/framework/ESP8266React.h
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#ifndef ESP8266React_h
|
||||||
|
#define ESP8266React_h
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#if defined(ESP8266)
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ESPAsyncTCP.h>
|
||||||
|
#elif defined(ESP_PLATFORM)
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <AsyncTCP.h>
|
||||||
|
#include <SPIFFS.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <FS.h>
|
||||||
|
#include <SecuritySettingsService.h>
|
||||||
|
#include <WiFiSettingsService.h>
|
||||||
|
#include <APSettingsService.h>
|
||||||
|
#include <NTPSettingsService.h>
|
||||||
|
#include <OTASettingsService.h>
|
||||||
|
#include <AuthenticationService.h>
|
||||||
|
#include <WiFiScanner.h>
|
||||||
|
#include <WiFiStatus.h>
|
||||||
|
#include <NTPStatus.h>
|
||||||
|
#include <APStatus.h>
|
||||||
|
#include <SystemStatus.h>
|
||||||
|
|
||||||
|
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
|
@ -1,6 +1,6 @@
|
|||||||
#include <NTPSettingsService.h>
|
#include <NTPSettingsService.h>
|
||||||
|
|
||||||
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)
|
#if defined(ESP8266)
|
||||||
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
|
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
|
@ -21,7 +21,7 @@ class NTPSettingsService : public AdminSettingsService {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
NTPSettingsService(FS* fs, SecurityManager* securityManager);
|
||||||
~NTPSettingsService();
|
~NTPSettingsService();
|
||||||
|
|
||||||
void loop();
|
void loop();
|
@ -1,7 +1,9 @@
|
|||||||
#include <NTPStatus.h>
|
#include <NTPStatus.h>
|
||||||
|
|
||||||
NTPStatus::NTPStatus(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) {
|
NTPStatus::NTPStatus(SecurityManager* securityManager) : _securityManager(securityManager) {}
|
||||||
_server->on(NTP_STATUS_SERVICE_PATH, HTTP_GET,
|
|
||||||
|
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)
|
_securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
|
||||||
);
|
);
|
||||||
}
|
}
|
@ -23,13 +23,12 @@ class NTPStatus {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
NTPStatus(AsyncWebServer *server, SecurityManager* securityManager);
|
NTPStatus(SecurityManager* securityManager);
|
||||||
|
void init(AsyncWebServer *server);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
AsyncWebServer* _server;
|
|
||||||
SecurityManager* _securityManager;
|
SecurityManager* _securityManager;
|
||||||
|
|
||||||
void ntpStatus(AsyncWebServerRequest *request);
|
void ntpStatus(AsyncWebServerRequest *request);
|
||||||
|
|
||||||
};
|
};
|
@ -1,6 +1,6 @@
|
|||||||
#include <OTASettingsService.h>
|
#include <OTASettingsService.h>
|
||||||
|
|
||||||
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)
|
#if defined(ESP8266)
|
||||||
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1));
|
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1));
|
||||||
#elif defined(ESP_PLATFORM)
|
#elif defined(ESP_PLATFORM)
|
@ -23,7 +23,7 @@ class OTASettingsService : public AdminSettingsService {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
OTASettingsService(FS* fs, SecurityManager* securityManager);
|
||||||
~OTASettingsService();
|
~OTASettingsService();
|
||||||
|
|
||||||
void loop();
|
void loop();
|
@ -1,6 +1,6 @@
|
|||||||
#include <SecuritySettingsService.h>
|
#include <SecuritySettingsService.h>
|
||||||
|
|
||||||
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() {}
|
SecuritySettingsService::~SecuritySettingsService() {}
|
||||||
|
|
||||||
void SecuritySettingsService::readFromJsonObject(JsonObject& root) {
|
void SecuritySettingsService::readFromJsonObject(JsonObject& root) {
|
||||||
@ -29,7 +29,3 @@ void SecuritySettingsService::writeToJsonObject(JsonObject& root) {
|
|||||||
user["admin"] = _user.isAdmin();
|
user["admin"] = _user.isAdmin();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SecuritySettingsService::begin() {
|
|
||||||
readFromFS();
|
|
||||||
}
|
|
@ -11,11 +11,9 @@ class SecuritySettingsService : public AdminSettingsService, public SecurityMana
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SecuritySettingsService(AsyncWebServer* server, FS* fs);
|
SecuritySettingsService(FS* fs);
|
||||||
~SecuritySettingsService();
|
~SecuritySettingsService();
|
||||||
|
|
||||||
void begin();
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void readFromJsonObject(JsonObject& root);
|
void readFromJsonObject(JsonObject& root);
|
@ -47,7 +47,7 @@ protected:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void readFromFS(){
|
void readFromFS() {
|
||||||
File configFile = _fs->open(_filePath, "r");
|
File configFile = _fs->open(_filePath, "r");
|
||||||
|
|
||||||
// use defaults if no config found
|
// use defaults if no config found
|
@ -16,7 +16,6 @@
|
|||||||
#include <AsyncJsonWebHandler.h>
|
#include <AsyncJsonWebHandler.h>
|
||||||
#include <AsyncArduinoJson6.h>
|
#include <AsyncArduinoJson6.h>
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Abstraction of a service which stores it's settings as JSON in a file system.
|
* Abstraction of a service which stores it's settings as JSON in a file system.
|
||||||
*/
|
*/
|
||||||
@ -24,30 +23,27 @@ class SettingsService : public SettingsPersistence {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SettingsService(AsyncWebServer* server, FS* fs, char const* servicePath, char const* filePath):
|
SettingsService(FS* fs, char const* servicePath, char const* filePath):
|
||||||
SettingsPersistence(fs, filePath), _server(server) {
|
SettingsPersistence(fs, filePath), _servicePath(servicePath) {
|
||||||
|
|
||||||
// configure fetch config handler
|
|
||||||
_server->on(servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1));
|
|
||||||
|
|
||||||
// configure update settings handler
|
|
||||||
_updateHandler.setUri(servicePath);
|
_updateHandler.setUri(servicePath);
|
||||||
_updateHandler.setMethod(HTTP_POST);
|
_updateHandler.setMethod(HTTP_POST);
|
||||||
_updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE);
|
_updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE);
|
||||||
_updateHandler.onRequest(std::bind(&SettingsService::updateConfig, this, std::placeholders::_1, std::placeholders::_2));
|
_updateHandler.onRequest(std::bind(&SettingsService::updateConfig, this, std::placeholders::_1, std::placeholders::_2));
|
||||||
_server->addHandler(&_updateHandler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~SettingsService() {}
|
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();
|
readFromFS();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// will serve setting endpoints from here
|
char const* _servicePath;
|
||||||
AsyncWebServer* _server;
|
|
||||||
|
|
||||||
AsyncJsonWebHandler _updateHandler;
|
AsyncJsonWebHandler _updateHandler;
|
||||||
|
|
||||||
virtual void fetchConfig(AsyncWebServerRequest *request) {
|
virtual void fetchConfig(AsyncWebServerRequest *request) {
|
||||||
@ -84,8 +80,8 @@ protected:
|
|||||||
|
|
||||||
class AdminSettingsService : public SettingsService {
|
class AdminSettingsService : public SettingsService {
|
||||||
public:
|
public:
|
||||||
AdminSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager, char const* servicePath, char const* filePath):
|
AdminSettingsService(FS* fs, SecurityManager* securityManager, char const* servicePath, char const* filePath):
|
||||||
SettingsService(server, fs, servicePath, filePath), _securityManager(securityManager) {
|
SettingsService(fs, servicePath, filePath), _securityManager(securityManager) {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
@ -33,7 +33,7 @@ private:
|
|||||||
|
|
||||||
AsyncJsonWebHandler _updateHandler;
|
AsyncJsonWebHandler _updateHandler;
|
||||||
|
|
||||||
void fetchConfig(AsyncWebServerRequest *request){
|
void fetchConfig(AsyncWebServerRequest *request) {
|
||||||
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_SETTINGS_SIZE);
|
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_SETTINGS_SIZE);
|
||||||
JsonObject jsonObject = response->getRoot();
|
JsonObject jsonObject = response->getRoot();
|
||||||
writeToJsonObject(jsonObject);
|
writeToJsonObject(jsonObject);
|
||||||
@ -41,8 +41,8 @@ private:
|
|||||||
request->send(response);
|
request->send(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument){
|
void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument) {
|
||||||
if (jsonDocument.is<JsonObject>()){
|
if (jsonDocument.is<JsonObject>()) {
|
||||||
JsonObject newConfig = jsonDocument.as<JsonObject>();
|
JsonObject newConfig = jsonDocument.as<JsonObject>();
|
||||||
readFromJsonObject(newConfig);
|
readFromJsonObject(newConfig);
|
||||||
|
|
||||||
@ -60,33 +60,31 @@ private:
|
|||||||
protected:
|
protected:
|
||||||
|
|
||||||
// will serve setting endpoints from here
|
// will serve setting endpoints from here
|
||||||
AsyncWebServer* _server;
|
char const* _servicePath;
|
||||||
|
|
||||||
// reads the local config from the
|
// reads the local config from the
|
||||||
virtual void readFromJsonObject(JsonObject& root){}
|
virtual void readFromJsonObject(JsonObject& root) {}
|
||||||
virtual void writeToJsonObject(JsonObject& root){}
|
virtual void writeToJsonObject(JsonObject& root) {}
|
||||||
|
|
||||||
// implement to perform action when config has been updated
|
// implement to perform action when config has been updated
|
||||||
virtual void onConfigUpdated(){}
|
virtual void onConfigUpdated() {}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SimpleService(AsyncWebServer* server, char const* servicePath):
|
SimpleService(char const* servicePath): _servicePath(servicePath) {
|
||||||
_server(server) {
|
|
||||||
|
|
||||||
// configure fetch config handler
|
|
||||||
_server->on(servicePath, HTTP_GET, std::bind(&SimpleService::fetchConfig, this, std::placeholders::_1));
|
|
||||||
|
|
||||||
// configure update settings handler
|
|
||||||
_updateHandler.setUri(servicePath);
|
_updateHandler.setUri(servicePath);
|
||||||
_updateHandler.setMethod(HTTP_POST);
|
_updateHandler.setMethod(HTTP_POST);
|
||||||
_updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE);
|
_updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE);
|
||||||
_updateHandler.onRequest(std::bind(&SimpleService::updateConfig, this, std::placeholders::_1, std::placeholders::_2));
|
_updateHandler.onRequest(std::bind(&SimpleService::updateConfig, this, std::placeholders::_1, std::placeholders::_2));
|
||||||
_server->addHandler(&_updateHandler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~SimpleService() {}
|
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
|
#endif // end SimpleService
|
@ -1,7 +1,9 @@
|
|||||||
#include <SystemStatus.h>
|
#include <SystemStatus.h>
|
||||||
|
|
||||||
SystemStatus::SystemStatus(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) {
|
SystemStatus::SystemStatus(SecurityManager* securityManager) : _securityManager(securityManager) {}
|
||||||
_server->on(SYSTEM_STATUS_SERVICE_PATH, HTTP_GET,
|
|
||||||
|
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)
|
_securityManager->wrapRequest(std::bind(&SystemStatus::systemStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
|
||||||
);
|
);
|
||||||
}
|
}
|
@ -21,11 +21,11 @@ class SystemStatus {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SystemStatus(AsyncWebServer *server, SecurityManager* securityManager);
|
SystemStatus(SecurityManager* securityManager);
|
||||||
|
void init(AsyncWebServer* server);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
AsyncWebServer* _server;
|
|
||||||
SecurityManager* _securityManager;
|
SecurityManager* _securityManager;
|
||||||
|
|
||||||
void systemStatus(AsyncWebServerRequest *request);
|
void systemStatus(AsyncWebServerRequest *request);
|
@ -1,11 +1,13 @@
|
|||||||
#include <WiFiScanner.h>
|
#include <WiFiScanner.h>
|
||||||
|
|
||||||
WiFiScanner::WiFiScanner(AsyncWebServer *server, SecurityManager* securityManager) : _server(server) {
|
WiFiScanner::WiFiScanner(SecurityManager* securityManager) : _securityManager(securityManager) {};
|
||||||
_server->on(SCAN_NETWORKS_SERVICE_PATH, HTTP_GET,
|
|
||||||
securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN)
|
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,
|
server->on(LIST_NETWORKS_SERVICE_PATH, HTTP_GET,
|
||||||
securityManager->wrapRequest(std::bind(&WiFiScanner::listNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN)
|
_securityManager->wrapRequest(std::bind(&WiFiScanner::listNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -24,11 +24,12 @@ class WiFiScanner {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
WiFiScanner(AsyncWebServer *server, SecurityManager* securityManager);
|
WiFiScanner(SecurityManager* securityManager);
|
||||||
|
void init(AsyncWebServer *server);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
AsyncWebServer* _server;
|
SecurityManager* _securityManager;
|
||||||
|
|
||||||
void scanNetworks(AsyncWebServerRequest *request);
|
void scanNetworks(AsyncWebServerRequest *request);
|
||||||
void listNetworks(AsyncWebServerRequest *request);
|
void listNetworks(AsyncWebServerRequest *request);
|
@ -1,17 +1,24 @@
|
|||||||
#include <WiFiSettingsService.h>
|
#include <WiFiSettingsService.h>
|
||||||
|
|
||||||
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)
|
#if defined(ESP8266)
|
||||||
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
|
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
|
||||||
#elif defined(ESP_PLATFORM)
|
#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);
|
WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
WiFiSettingsService::~WiFiSettingsService() {}
|
WiFiSettingsService::~WiFiSettingsService() {}
|
||||||
|
|
||||||
void WiFiSettingsService::begin() {
|
void WiFiSettingsService::init(AsyncWebServer* server) {
|
||||||
SettingsService::begin();
|
SettingsService::init(server);
|
||||||
reconfigureWiFiConnection();
|
reconfigureWiFiConnection();
|
||||||
}
|
}
|
||||||
|
|
@ -12,10 +12,10 @@ class WiFiSettingsService : public AdminSettingsService {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
WiFiSettingsService(FS* fs, SecurityManager* securityManager);
|
||||||
~WiFiSettingsService();
|
~WiFiSettingsService();
|
||||||
|
|
||||||
void begin();
|
void init(AsyncWebServer* server);
|
||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
protected:
|
protected:
|
@ -1,9 +1,6 @@
|
|||||||
#include <WiFiStatus.h>
|
#include <WiFiStatus.h>
|
||||||
|
|
||||||
WiFiStatus::WiFiStatus(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) {
|
WiFiStatus::WiFiStatus(SecurityManager* securityManager) : _securityManager(securityManager) {
|
||||||
_server->on(WIFI_STATUS_SERVICE_PATH, HTTP_GET,
|
|
||||||
_securityManager->wrapRequest(std::bind(&WiFiStatus::wifiStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
|
|
||||||
);
|
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
_onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected);
|
_onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected);
|
||||||
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected);
|
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected);
|
||||||
@ -50,6 +47,12 @@ void WiFiStatus::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|||||||
}
|
}
|
||||||
#endif
|
#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) {
|
void WiFiStatus::wifiStatus(AsyncWebServerRequest *request) {
|
||||||
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_WIFI_STATUS_SIZE);
|
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_WIFI_STATUS_SIZE);
|
||||||
JsonObject root = response->getRoot();
|
JsonObject root = response->getRoot();
|
@ -22,11 +22,12 @@ class WiFiStatus {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
WiFiStatus(AsyncWebServer *server, SecurityManager* securityManager);
|
WiFiStatus(SecurityManager* securityManager);
|
||||||
|
|
||||||
|
void init(AsyncWebServer* server);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
AsyncWebServer* _server;
|
|
||||||
SecurityManager* _securityManager;
|
SecurityManager* _securityManager;
|
||||||
|
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
@ -7,10 +7,9 @@
|
|||||||
;
|
;
|
||||||
; Please visit documentation for the other options and examples
|
; Please visit documentation for the other options and examples
|
||||||
; http://docs.platformio.org/page/projectconf.html
|
; http://docs.platformio.org/page/projectconf.html
|
||||||
[env:esp12e]
|
[env:node32s]
|
||||||
platform = espressif8266
|
platform = espressif32
|
||||||
board = esp12e
|
board = node32s
|
||||||
board_build.f_cpu = 160000000L
|
|
||||||
|
|
||||||
extra_scripts = pre:timelib_fix.py
|
extra_scripts = pre:timelib_fix.py
|
||||||
|
|
||||||
@ -26,7 +25,7 @@ monitor_speed = 115200
|
|||||||
build_flags=
|
build_flags=
|
||||||
-D NO_GLOBAL_ARDUINOOTA
|
-D NO_GLOBAL_ARDUINOOTA
|
||||||
; Uncomment ENABLE_CORS to enable Cross-Origin Resource Sharing (required for local React development)
|
; 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\"
|
-D CORS_ORIGIN=\"http://localhost:3000\"
|
||||||
lib_deps =
|
lib_deps =
|
||||||
NtpClientLib@>=2.5.1,<3.0.0
|
NtpClientLib@>=2.5.1,<3.0.0
|
||||||
|
9
src/DemoProject.cpp
Normal file
9
src/DemoProject.cpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include <DemoProject.h>
|
||||||
|
|
||||||
|
void DemoProject::begin() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void DemoProject::loop() {
|
||||||
|
|
||||||
|
}
|
23
src/DemoProject.h
Normal file
23
src/DemoProject.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef DemoProject_h
|
||||||
|
#define DemoProject_h
|
||||||
|
|
||||||
|
#include <ESPAsyncWebServer.h>
|
||||||
|
#include <SecurityManager.h>
|
||||||
|
|
||||||
|
class DemoProject {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
DemoProject(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) {}
|
||||||
|
|
||||||
|
void begin();
|
||||||
|
void loop();
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
AsyncWebServer* _server;
|
||||||
|
SecurityManager* _securityManager;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
94
src/main.cpp
94
src/main.cpp
@ -1,99 +1,31 @@
|
|||||||
#include <Arduino.h>
|
#include <ESP8266React.h>
|
||||||
|
#include <DemoProject.h>
|
||||||
#if defined(ESP8266)
|
|
||||||
#include <ESP8266WiFi.h>
|
|
||||||
#include <ESPAsyncTCP.h>
|
|
||||||
#elif defined(ESP_PLATFORM)
|
|
||||||
#include <WiFi.h>
|
|
||||||
#include <AsyncTCP.h>
|
|
||||||
#include <SPIFFS.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <FS.h>
|
#include <FS.h>
|
||||||
#include <SecuritySettingsService.h>
|
|
||||||
#include <WiFiSettingsService.h>
|
|
||||||
#include <APSettingsService.h>
|
|
||||||
#include <NTPSettingsService.h>
|
|
||||||
#include <OTASettingsService.h>
|
|
||||||
#include <AuthenticationService.h>
|
|
||||||
#include <WiFiScanner.h>
|
|
||||||
#include <WiFiStatus.h>
|
|
||||||
#include <NTPStatus.h>
|
|
||||||
#include <APStatus.h>
|
|
||||||
#include <SystemStatus.h>
|
|
||||||
|
|
||||||
#define SERIAL_BAUD_RATE 115200
|
#define SERIAL_BAUD_RATE 115200
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
|
ESP8266React espServer(&SPIFFS);
|
||||||
|
|
||||||
SecuritySettingsService securitySettingsService = SecuritySettingsService(&server, &SPIFFS);
|
DemoProject demoProject = DemoProject(&server, espServer.getSecurityManager());
|
||||||
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);;
|
|
||||||
|
|
||||||
void setup() {
|
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);
|
Serial.begin(SERIAL_BAUD_RATE);
|
||||||
SPIFFS.begin();
|
SPIFFS.begin();
|
||||||
|
|
||||||
// Start security settings service first
|
// set up the framework
|
||||||
securitySettingsService.begin();
|
espServer.init(&server);
|
||||||
|
|
||||||
// Start services
|
// begin the demo project
|
||||||
ntpSettingsService.begin();
|
demoProject.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();
|
server.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
wifiSettingsService.loop();
|
// run the framework loop
|
||||||
apSettingsService.loop();
|
espServer.loop();
|
||||||
ntpSettingsService.loop();
|
|
||||||
otaSettingsService.loop();
|
// run the demo project loop
|
||||||
|
demoProject.loop();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user