experimenting with some refactoring
This commit is contained in:
parent
a0d6524180
commit
f88520db44
@ -1,6 +1,6 @@
|
||||
#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();
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ class APSettingsService : public AdminSettingsService {
|
||||
|
||||
public:
|
||||
|
||||
APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
||||
APSettingsService(FS* fs, SecurityManager* securityManager);
|
||||
~APSettingsService();
|
||||
|
||||
void loop();
|
@ -1,7 +1,9 @@
|
||||
#include <APStatus.h>
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
@ -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);
|
@ -1,18 +1,19 @@
|
||||
#include <AuthenticationService.h>
|
||||
|
||||
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.
|
||||
*/
|
@ -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;
|
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>
|
||||
|
||||
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));
|
@ -21,7 +21,7 @@ class NTPSettingsService : public AdminSettingsService {
|
||||
|
||||
public:
|
||||
|
||||
NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
||||
NTPSettingsService(FS* fs, SecurityManager* securityManager);
|
||||
~NTPSettingsService();
|
||||
|
||||
void loop();
|
@ -1,7 +1,9 @@
|
||||
#include <NTPStatus.h>
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
@ -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);
|
||||
|
||||
};
|
@ -1,6 +1,6 @@
|
||||
#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)
|
||||
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1));
|
||||
#elif defined(ESP_PLATFORM)
|
@ -23,7 +23,7 @@ class OTASettingsService : public AdminSettingsService {
|
||||
|
||||
public:
|
||||
|
||||
OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
||||
OTASettingsService(FS* fs, SecurityManager* securityManager);
|
||||
~OTASettingsService();
|
||||
|
||||
void loop();
|
@ -1,6 +1,6 @@
|
||||
#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() {}
|
||||
|
||||
void SecuritySettingsService::readFromJsonObject(JsonObject& root) {
|
||||
@ -29,7 +29,3 @@ void SecuritySettingsService::writeToJsonObject(JsonObject& root) {
|
||||
user["admin"] = _user.isAdmin();
|
||||
}
|
||||
}
|
||||
|
||||
void SecuritySettingsService::begin() {
|
||||
readFromFS();
|
||||
}
|
@ -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);
|
@ -47,7 +47,7 @@ protected:
|
||||
return true;
|
||||
}
|
||||
|
||||
void readFromFS(){
|
||||
void readFromFS() {
|
||||
File configFile = _fs->open(_filePath, "r");
|
||||
|
||||
// use defaults if no config found
|
@ -16,7 +16,6 @@
|
||||
#include <AsyncJsonWebHandler.h>
|
||||
#include <AsyncArduinoJson6.h>
|
||||
|
||||
|
||||
/*
|
||||
* 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:
|
@ -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<JsonObject>()){
|
||||
void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument) {
|
||||
if (jsonDocument.is<JsonObject>()) {
|
||||
JsonObject newConfig = jsonDocument.as<JsonObject>();
|
||||
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
|
@ -1,7 +1,9 @@
|
||||
#include <SystemStatus.h>
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
@ -21,11 +21,11 @@ class SystemStatus {
|
||||
|
||||
public:
|
||||
|
||||
SystemStatus(AsyncWebServer *server, SecurityManager* securityManager);
|
||||
SystemStatus(SecurityManager* securityManager);
|
||||
void init(AsyncWebServer* server);
|
||||
|
||||
private:
|
||||
|
||||
AsyncWebServer* _server;
|
||||
SecurityManager* _securityManager;
|
||||
|
||||
void systemStatus(AsyncWebServerRequest *request);
|
@ -1,11 +1,13 @@
|
||||
#include <WiFiScanner.h>
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
@ -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);
|
@ -1,17 +1,24 @@
|
||||
#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)
|
||||
_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();
|
||||
}
|
||||
|
@ -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:
|
@ -1,9 +1,6 @@
|
||||
#include <WiFiStatus.h>
|
||||
|
||||
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();
|
@ -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)
|
@ -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
|
||||
|
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>
|
||||
|
||||
#if defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#elif defined(ESP_PLATFORM)
|
||||
#include <WiFi.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <SPIFFS.h>
|
||||
#endif
|
||||
|
||||
#include <ESP8266React.h>
|
||||
#include <DemoProject.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
|
||||
|
||||
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();
|
||||
|
||||
// Start security settings service first
|
||||
securitySettingsService.begin();
|
||||
// set up the framework
|
||||
espServer.init(&server);
|
||||
|
||||
// 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
|
||||
// begin the demo project
|
||||
demoProject.begin();
|
||||
|
||||
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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user