move initialization code to constructors as a simplfication
This commit is contained in:
parent
29906a1d97
commit
f77428e4dc
@ -1,6 +1,6 @@
|
|||||||
#include <APSettingsService.h>
|
#include <APSettingsService.h>
|
||||||
|
|
||||||
APSettingsService::APSettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, AP_SETTINGS_SERVICE_PATH, AP_SETTINGS_FILE) {
|
APSettingsService::APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, AP_SETTINGS_SERVICE_PATH, AP_SETTINGS_FILE) {
|
||||||
onConfigUpdated();
|
onConfigUpdated();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ class APSettingsService : public AdminSettingsService {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
APSettingsService(FS* fs, SecurityManager* securityManager);
|
APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
||||||
~APSettingsService();
|
~APSettingsService();
|
||||||
|
|
||||||
void loop();
|
void loop();
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
#include <APStatus.h>
|
#include <APStatus.h>
|
||||||
|
|
||||||
APStatus::APStatus(SecurityManager* securityManager) :_securityManager(securityManager) {}
|
APStatus::APStatus(AsyncWebServer* server, SecurityManager* securityManager) {
|
||||||
|
server->on(AP_STATUS_SERVICE_PATH, HTTP_GET,
|
||||||
void APStatus::init(AsyncWebServer *server){
|
securityManager->wrapRequest(std::bind(&APStatus::apStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
|
||||||
server->on(AP_STATUS_SERVICE_PATH, HTTP_GET,
|
|
||||||
_securityManager->wrapRequest(std::bind(&APStatus::apStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,13 +22,10 @@ class APStatus {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
APStatus(SecurityManager* securityManager);
|
APStatus(AsyncWebServer* server, SecurityManager* securityManager);
|
||||||
void init(AsyncWebServer *server);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
SecurityManager* _securityManager;
|
|
||||||
|
|
||||||
void apStatus(AsyncWebServerRequest *request);
|
void apStatus(AsyncWebServerRequest *request);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -6,9 +6,8 @@
|
|||||||
class AdminSettingsService : public SettingsService {
|
class AdminSettingsService : public SettingsService {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AdminSettingsService(FS* fs, SecurityManager* securityManager, char const* servicePath, char const* filePath):
|
AdminSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager, char const* servicePath, char const* filePath):
|
||||||
SettingsService(fs, servicePath, filePath), _securityManager(securityManager) {
|
SettingsService(server, fs, servicePath, filePath), _securityManager(securityManager) {}
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// will validate the requests with the security manager
|
// will validate the requests with the security manager
|
||||||
|
@ -1,19 +1,17 @@
|
|||||||
#include <AuthenticationService.h>
|
#include <AuthenticationService.h>
|
||||||
|
|
||||||
AuthenticationService::AuthenticationService(SecurityManager* securityManager) : _securityManager(securityManager) {
|
AuthenticationService::AuthenticationService(AsyncWebServer* 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,14 +15,11 @@ class AuthenticationService {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
AuthenticationService(SecurityManager* securityManager);
|
AuthenticationService(AsyncWebServer* server, SecurityManager* securityManager);
|
||||||
~AuthenticationService();
|
~AuthenticationService();
|
||||||
|
|
||||||
void init(AsyncWebServer* server);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// server instance
|
|
||||||
AsyncWebServer* _server;
|
|
||||||
SecurityManager* _securityManager;
|
SecurityManager* _securityManager;
|
||||||
AsyncJsonWebHandler _signInHandler;
|
AsyncJsonWebHandler _signInHandler;
|
||||||
|
|
||||||
|
@ -1,40 +1,18 @@
|
|||||||
#include <ESP8266React.h>
|
#include <ESP8266React.h>
|
||||||
|
|
||||||
ESP8266React::ESP8266React(FS* fs):
|
ESP8266React::ESP8266React(AsyncWebServer* server, FS* fs):
|
||||||
_fs(fs),
|
_securitySettingsService(server, fs),
|
||||||
_securitySettingsService(_fs),
|
_wifiSettingsService(server, fs, &_securitySettingsService),
|
||||||
_wifiSettingsService(_fs, &_securitySettingsService),
|
_apSettingsService(server, fs, &_securitySettingsService),
|
||||||
_apSettingsService(_fs, &_securitySettingsService),
|
_ntpSettingsService(server, fs, &_securitySettingsService),
|
||||||
_ntpSettingsService(_fs, &_securitySettingsService),
|
_otaSettingsService(server, fs, &_securitySettingsService),
|
||||||
_otaSettingsService(_fs, &_securitySettingsService),
|
_authenticationService(server, &_securitySettingsService),
|
||||||
_authenticationService(&_securitySettingsService),
|
_wifiScanner(server, &_securitySettingsService),
|
||||||
_wifiScanner(&_securitySettingsService),
|
_wifiStatus(server, &_securitySettingsService),
|
||||||
_wifiStatus(&_securitySettingsService),
|
_ntpStatus(server, &_securitySettingsService),
|
||||||
_ntpStatus(&_securitySettingsService),
|
_apStatus(server, &_securitySettingsService),
|
||||||
_apStatus(&_securitySettingsService),
|
_systemStatus(server, &_securitySettingsService) {
|
||||||
_systemStatus(&_securitySettingsService) {
|
// Serve static resources from /www/
|
||||||
}
|
|
||||||
|
|
||||||
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("/js/", SPIFFS, "/www/js/");
|
||||||
server->serveStatic("/css/", SPIFFS, "/www/css/");
|
server->serveStatic("/css/", SPIFFS, "/www/css/");
|
||||||
server->serveStatic("/fonts/", SPIFFS, "/www/fonts/");
|
server->serveStatic("/fonts/", SPIFFS, "/www/fonts/");
|
||||||
|
@ -29,9 +29,8 @@ class ESP8266React {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
ESP8266React(FS* fs);
|
ESP8266React(AsyncWebServer* server, FS* fs);
|
||||||
|
|
||||||
void init(AsyncWebServer* server);
|
|
||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
SecurityManager* getSecurityManager(){
|
SecurityManager* getSecurityManager(){
|
||||||
@ -40,8 +39,6 @@ class ESP8266React {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
FS* _fs;
|
|
||||||
|
|
||||||
SecuritySettingsService _securitySettingsService;
|
SecuritySettingsService _securitySettingsService;
|
||||||
|
|
||||||
WiFiSettingsService _wifiSettingsService;
|
WiFiSettingsService _wifiSettingsService;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include <NTPSettingsService.h>
|
#include <NTPSettingsService.h>
|
||||||
|
|
||||||
NTPSettingsService::NTPSettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE) {
|
NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, 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(FS* fs, SecurityManager* securityManager);
|
NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
||||||
~NTPSettingsService();
|
~NTPSettingsService();
|
||||||
|
|
||||||
void loop();
|
void loop();
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
#include <NTPStatus.h>
|
#include <NTPStatus.h>
|
||||||
|
|
||||||
NTPStatus::NTPStatus(SecurityManager* securityManager) : _securityManager(securityManager) {}
|
NTPStatus::NTPStatus(AsyncWebServer* server, SecurityManager* securityManager) {
|
||||||
|
|
||||||
void NTPStatus::init(AsyncWebServer *server){
|
|
||||||
server->on(NTP_STATUS_SERVICE_PATH, HTTP_GET,
|
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,12 +23,10 @@ class NTPStatus {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
NTPStatus(SecurityManager* securityManager);
|
NTPStatus(AsyncWebServer* server, SecurityManager* securityManager);
|
||||||
void init(AsyncWebServer *server);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
SecurityManager* _securityManager;
|
|
||||||
void ntpStatus(AsyncWebServerRequest *request);
|
void ntpStatus(AsyncWebServerRequest *request);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include <OTASettingsService.h>
|
#include <OTASettingsService.h>
|
||||||
|
|
||||||
OTASettingsService::OTASettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, OTA_SETTINGS_SERVICE_PATH, OTA_SETTINGS_FILE) {
|
OTASettingsService::OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, 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(FS* fs, SecurityManager* securityManager);
|
OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
||||||
~OTASettingsService();
|
~OTASettingsService();
|
||||||
|
|
||||||
void loop();
|
void loop();
|
||||||
@ -52,4 +52,4 @@ class OTASettingsService : public AdminSettingsService {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // end NTPSettingsService_h
|
#endif // end OTASettingsService_h
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include <SecuritySettingsService.h>
|
#include <SecuritySettingsService.h>
|
||||||
|
|
||||||
SecuritySettingsService::SecuritySettingsService(FS* fs) : AdminSettingsService(fs, this, SECURITY_SETTINGS_PATH, SECURITY_SETTINGS_FILE), SecurityManager() {}
|
SecuritySettingsService::SecuritySettingsService(AsyncWebServer* server, FS* fs) : AdminSettingsService(server, fs, this, SECURITY_SETTINGS_PATH, SECURITY_SETTINGS_FILE), SecurityManager() {}
|
||||||
SecuritySettingsService::~SecuritySettingsService() {}
|
SecuritySettingsService::~SecuritySettingsService() {}
|
||||||
|
|
||||||
void SecuritySettingsService::readFromJsonObject(JsonObject& root) {
|
void SecuritySettingsService::readFromJsonObject(JsonObject& root) {
|
||||||
|
@ -11,7 +11,7 @@ class SecuritySettingsService : public AdminSettingsService, public SecurityMana
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SecuritySettingsService(FS* fs);
|
SecuritySettingsService(AsyncWebServer* server, FS* fs);
|
||||||
~SecuritySettingsService();
|
~SecuritySettingsService();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -23,25 +23,21 @@ class SettingsService : public SettingsPersistence {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SettingsService(FS* fs, char const* servicePath, char const* filePath):
|
SettingsService(AsyncWebServer* server, FS* fs, char const* servicePath, char const* filePath): SettingsPersistence(fs, filePath), _servicePath(servicePath) {
|
||||||
SettingsPersistence(fs, filePath), _servicePath(servicePath) {
|
server->on(_servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1));
|
||||||
|
|
||||||
_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() {}
|
|
||||||
|
|
||||||
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
|
// read the initial data from the file system
|
||||||
readFromFS();
|
readFromFS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual ~SettingsService() {}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
char const* _servicePath;
|
char const* _servicePath;
|
||||||
AsyncJsonWebHandler _updateHandler;
|
AsyncJsonWebHandler _updateHandler;
|
||||||
|
@ -59,9 +59,6 @@ private:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
// will serve setting endpoints from here
|
|
||||||
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) {}
|
||||||
@ -71,20 +68,18 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SimpleService(char const* servicePath): _servicePath(servicePath) {
|
SimpleService(AsyncWebServer* server, char const* servicePath) {
|
||||||
|
server->on(servicePath, HTTP_GET, std::bind(&SimpleService::fetchConfig, this, std::placeholders::_1));
|
||||||
|
|
||||||
_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,14 +1,12 @@
|
|||||||
#include <SystemStatus.h>
|
#include <SystemStatus.h>
|
||||||
|
|
||||||
SystemStatus::SystemStatus(SecurityManager* securityManager) : _securityManager(securityManager) {}
|
SystemStatus::SystemStatus(AsyncWebServer* server, SecurityManager* securityManager) {
|
||||||
|
|
||||||
void SystemStatus::init(AsyncWebServer *server) {
|
|
||||||
server->on(SYSTEM_STATUS_SERVICE_PATH, HTTP_GET,
|
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)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SystemStatus::systemStatus(AsyncWebServerRequest *request) {
|
void SystemStatus::systemStatus(AsyncWebServerRequest *request) {
|
||||||
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_ESP_STATUS_SIZE);
|
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_ESP_STATUS_SIZE);
|
||||||
JsonObject root = response->getRoot();
|
JsonObject root = response->getRoot();
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
|
@ -21,13 +21,10 @@ class SystemStatus {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SystemStatus(SecurityManager* securityManager);
|
SystemStatus(AsyncWebServer* server, SecurityManager* securityManager);
|
||||||
void init(AsyncWebServer* server);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
SecurityManager* _securityManager;
|
|
||||||
|
|
||||||
void systemStatus(AsyncWebServerRequest *request);
|
void systemStatus(AsyncWebServerRequest *request);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -1,15 +1,13 @@
|
|||||||
#include <WiFiScanner.h>
|
#include <WiFiScanner.h>
|
||||||
|
|
||||||
WiFiScanner::WiFiScanner(SecurityManager* securityManager) : _securityManager(securityManager) {};
|
WiFiScanner::WiFiScanner(AsyncWebServer *server, SecurityManager* securityManager) {
|
||||||
|
|
||||||
void WiFiScanner::init(AsyncWebServer* server) {
|
|
||||||
server->on(SCAN_NETWORKS_SERVICE_PATH, HTTP_GET,
|
server->on(SCAN_NETWORKS_SERVICE_PATH, HTTP_GET,
|
||||||
_securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN)
|
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)
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
void WiFiScanner::scanNetworks(AsyncWebServerRequest *request) {
|
void WiFiScanner::scanNetworks(AsyncWebServerRequest *request) {
|
||||||
if (WiFi.scanComplete() != -1){
|
if (WiFi.scanComplete() != -1){
|
||||||
|
@ -24,13 +24,10 @@ class WiFiScanner {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
WiFiScanner(SecurityManager* securityManager);
|
WiFiScanner(AsyncWebServer *server, SecurityManager* securityManager);
|
||||||
void init(AsyncWebServer *server);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
SecurityManager* _securityManager;
|
|
||||||
|
|
||||||
void scanNetworks(AsyncWebServerRequest *request);
|
void scanNetworks(AsyncWebServerRequest *request);
|
||||||
void listNetworks(AsyncWebServerRequest *request);
|
void listNetworks(AsyncWebServerRequest *request);
|
||||||
|
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
#include <WiFiSettingsService.h>
|
#include <WiFiSettingsService.h>
|
||||||
|
|
||||||
WiFiSettingsService::WiFiSettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) {
|
WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) {
|
||||||
// Disable wifi config persistance and auto reconnect
|
// Disable WiFi config persistance and auto reconnect
|
||||||
WiFi.persistent(false);
|
WiFi.persistent(false);
|
||||||
WiFi.setAutoReconnect(false);
|
WiFi.setAutoReconnect(false);
|
||||||
|
|
||||||
|
// Force WiFi config to be applied when loop() called
|
||||||
|
reconfigureWiFiConnection();
|
||||||
|
|
||||||
#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)
|
||||||
@ -17,11 +20,6 @@ WiFiSettingsService::WiFiSettingsService(FS* fs, SecurityManager* securityManage
|
|||||||
|
|
||||||
WiFiSettingsService::~WiFiSettingsService() {}
|
WiFiSettingsService::~WiFiSettingsService() {}
|
||||||
|
|
||||||
void WiFiSettingsService::init(AsyncWebServer* server) {
|
|
||||||
SettingsService::init(server);
|
|
||||||
reconfigureWiFiConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
void WiFiSettingsService::readFromJsonObject(JsonObject& root){
|
void WiFiSettingsService::readFromJsonObject(JsonObject& root){
|
||||||
_ssid = root["ssid"] | "";
|
_ssid = root["ssid"] | "";
|
||||||
_password = root["password"] | "";
|
_password = root["password"] | "";
|
||||||
|
@ -12,10 +12,9 @@ class WiFiSettingsService : public AdminSettingsService {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
WiFiSettingsService(FS* fs, SecurityManager* securityManager);
|
WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
||||||
~WiFiSettingsService();
|
~WiFiSettingsService();
|
||||||
|
|
||||||
void init(AsyncWebServer* server);
|
|
||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
#include <WiFiStatus.h>
|
#include <WiFiStatus.h>
|
||||||
|
|
||||||
WiFiStatus::WiFiStatus(SecurityManager* securityManager) : _securityManager(securityManager) {
|
WiFiStatus::WiFiStatus(AsyncWebServer* server, 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);
|
||||||
@ -47,12 +50,6 @@ 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,14 +22,10 @@ class WiFiStatus {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
WiFiStatus(SecurityManager* securityManager);
|
WiFiStatus(AsyncWebServer* server, SecurityManager* securityManager);
|
||||||
|
|
||||||
void init(AsyncWebServer* server);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
SecurityManager* _securityManager;
|
|
||||||
|
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
// handler refrences for logging important WiFi events over serial
|
// handler refrences for logging important WiFi events over serial
|
||||||
WiFiEventHandler _onStationModeConnectedHandler;
|
WiFiEventHandler _onStationModeConnectedHandler;
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
#include <DemoProject.h>
|
#include <DemoProject.h>
|
||||||
|
|
||||||
void DemoProject::init(AsyncWebServer* server) {
|
DemoProject::DemoProject(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, DEMO_SETTINGS_PATH, DEMO_SETTINGS_FILE) {
|
||||||
AdminSettingsService::init(server);
|
|
||||||
pinMode(BLINK_LED, OUTPUT);
|
pinMode(BLINK_LED, OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DemoProject::~DemoProject() {}
|
||||||
|
|
||||||
void DemoProject::loop() {
|
void DemoProject::loop() {
|
||||||
unsigned delay = MAX_DELAY / 255 * (255 - _blinkSpeed);
|
unsigned delay = MAX_DELAY / 255 * (255 - _blinkSpeed);
|
||||||
unsigned long currentMillis = millis();
|
unsigned long currentMillis = millis();
|
||||||
|
@ -14,10 +14,9 @@ class DemoProject : public AdminSettingsService {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
DemoProject(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, DEMO_SETTINGS_PATH, DEMO_SETTINGS_FILE) {}
|
DemoProject(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
||||||
~DemoProject() {}
|
~DemoProject();
|
||||||
|
|
||||||
void init(AsyncWebServer* server);
|
|
||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
10
src/main.cpp
10
src/main.cpp
@ -5,19 +5,13 @@
|
|||||||
#define SERIAL_BAUD_RATE 115200
|
#define SERIAL_BAUD_RATE 115200
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
ESP8266React framework(&SPIFFS);
|
ESP8266React framework(&server, &SPIFFS);
|
||||||
DemoProject demoProject = DemoProject(&SPIFFS, framework.getSecurityManager());
|
DemoProject demoProject = DemoProject(&server, &SPIFFS, framework.getSecurityManager());
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// start serial and filesystem
|
// start serial and filesystem
|
||||||
Serial.begin(SERIAL_BAUD_RATE);
|
Serial.begin(SERIAL_BAUD_RATE);
|
||||||
SPIFFS.begin();
|
SPIFFS.begin();
|
||||||
|
|
||||||
// set up the framework
|
|
||||||
framework.init(&server);
|
|
||||||
|
|
||||||
// begin the demo project
|
|
||||||
demoProject.init(&server);
|
|
||||||
|
|
||||||
// start the server
|
// start the server
|
||||||
server.begin();
|
server.begin();
|
||||||
|
Loading…
Reference in New Issue
Block a user