move initialization code to constructors as a simplfication

This commit is contained in:
Rick Watson 2019-09-28 21:29:46 +01:00
parent 29906a1d97
commit f77428e4dc
30 changed files with 76 additions and 151 deletions

View File

@ -1,6 +1,6 @@
#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();
}

View File

@ -23,7 +23,7 @@ class APSettingsService : public AdminSettingsService {
public:
APSettingsService(FS* fs, SecurityManager* securityManager);
APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
~APSettingsService();
void loop();

View File

@ -1,10 +1,8 @@
#include <APStatus.h>
APStatus::APStatus(SecurityManager* securityManager) :_securityManager(securityManager) {}
void APStatus::init(AsyncWebServer *server){
APStatus::APStatus(AsyncWebServer* server, SecurityManager* securityManager) {
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)
);
}

View File

@ -22,13 +22,10 @@ class APStatus {
public:
APStatus(SecurityManager* securityManager);
void init(AsyncWebServer *server);
APStatus(AsyncWebServer* server, SecurityManager* securityManager);
private:
SecurityManager* _securityManager;
void apStatus(AsyncWebServerRequest *request);
};

View File

@ -6,9 +6,8 @@
class AdminSettingsService : public SettingsService {
public:
AdminSettingsService(FS* fs, SecurityManager* securityManager, char const* servicePath, char const* filePath):
SettingsService(fs, servicePath, filePath), _securityManager(securityManager) {
}
AdminSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager, char const* servicePath, char const* filePath):
SettingsService(server, fs, servicePath, filePath), _securityManager(securityManager) {}
protected:
// will validate the requests with the security manager

View File

@ -1,19 +1,17 @@
#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.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.
*/

View File

@ -15,14 +15,11 @@ class AuthenticationService {
public:
AuthenticationService(SecurityManager* securityManager);
AuthenticationService(AsyncWebServer* server, SecurityManager* securityManager);
~AuthenticationService();
void init(AsyncWebServer* server);
private:
// server instance
AsyncWebServer* _server;
SecurityManager* _securityManager;
AsyncJsonWebHandler _signInHandler;

View File

@ -1,40 +1,18 @@
#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/
ESP8266React::ESP8266React(AsyncWebServer* server, FS* fs):
_securitySettingsService(server, fs),
_wifiSettingsService(server, fs, &_securitySettingsService),
_apSettingsService(server, fs, &_securitySettingsService),
_ntpSettingsService(server, fs, &_securitySettingsService),
_otaSettingsService(server, fs, &_securitySettingsService),
_authenticationService(server, &_securitySettingsService),
_wifiScanner(server, &_securitySettingsService),
_wifiStatus(server, &_securitySettingsService),
_ntpStatus(server, &_securitySettingsService),
_apStatus(server, &_securitySettingsService),
_systemStatus(server, &_securitySettingsService) {
// Serve static resources from /www/
server->serveStatic("/js/", SPIFFS, "/www/js/");
server->serveStatic("/css/", SPIFFS, "/www/css/");
server->serveStatic("/fonts/", SPIFFS, "/www/fonts/");

View File

@ -29,9 +29,8 @@ class ESP8266React {
public:
ESP8266React(FS* fs);
ESP8266React(AsyncWebServer* server, FS* fs);
void init(AsyncWebServer* server);
void loop();
SecurityManager* getSecurityManager(){
@ -40,8 +39,6 @@ class ESP8266React {
private:
FS* _fs;
SecuritySettingsService _securitySettingsService;
WiFiSettingsService _wifiSettingsService;

View File

@ -1,6 +1,6 @@
#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)
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));

View File

@ -21,7 +21,7 @@ class NTPSettingsService : public AdminSettingsService {
public:
NTPSettingsService(FS* fs, SecurityManager* securityManager);
NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
~NTPSettingsService();
void loop();

View File

@ -1,10 +1,8 @@
#include <NTPStatus.h>
NTPStatus::NTPStatus(SecurityManager* securityManager) : _securityManager(securityManager) {}
void NTPStatus::init(AsyncWebServer *server){
NTPStatus::NTPStatus(AsyncWebServer* server, SecurityManager* securityManager) {
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)
);
}

View File

@ -23,12 +23,10 @@ class NTPStatus {
public:
NTPStatus(SecurityManager* securityManager);
void init(AsyncWebServer *server);
NTPStatus(AsyncWebServer* server, SecurityManager* securityManager);
private:
SecurityManager* _securityManager;
void ntpStatus(AsyncWebServerRequest *request);
};

View File

@ -1,6 +1,6 @@
#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)
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1));
#elif defined(ESP_PLATFORM)

View File

@ -23,7 +23,7 @@ class OTASettingsService : public AdminSettingsService {
public:
OTASettingsService(FS* fs, SecurityManager* securityManager);
OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
~OTASettingsService();
void loop();
@ -52,4 +52,4 @@ class OTASettingsService : public AdminSettingsService {
};
#endif // end NTPSettingsService_h
#endif // end OTASettingsService_h

View File

@ -1,6 +1,6 @@
#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() {}
void SecuritySettingsService::readFromJsonObject(JsonObject& root) {

View File

@ -11,7 +11,7 @@ class SecuritySettingsService : public AdminSettingsService, public SecurityMana
public:
SecuritySettingsService(FS* fs);
SecuritySettingsService(AsyncWebServer* server, FS* fs);
~SecuritySettingsService();
protected:

View File

@ -23,25 +23,21 @@ class SettingsService : public SettingsPersistence {
public:
SettingsService(FS* fs, char const* servicePath, char const* filePath):
SettingsPersistence(fs, filePath), _servicePath(servicePath) {
SettingsService(AsyncWebServer* server, FS* fs, char const* servicePath, char const* filePath): SettingsPersistence(fs, filePath), _servicePath(servicePath) {
server->on(_servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1));
_updateHandler.setUri(servicePath);
_updateHandler.setMethod(HTTP_POST);
_updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE);
_updateHandler.onRequest(std::bind(&SettingsService::updateConfig, this, std::placeholders::_1, std::placeholders::_2));
}
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
readFromFS();
}
virtual ~SettingsService() {}
protected:
char const* _servicePath;
AsyncJsonWebHandler _updateHandler;

View File

@ -59,9 +59,6 @@ private:
protected:
// will serve setting endpoints from here
char const* _servicePath;
// reads the local config from the
virtual void readFromJsonObject(JsonObject& root) {}
virtual void writeToJsonObject(JsonObject& root) {}
@ -71,20 +68,18 @@ private:
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.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

View File

@ -1,14 +1,12 @@
#include <SystemStatus.h>
SystemStatus::SystemStatus(SecurityManager* securityManager) : _securityManager(securityManager) {}
void SystemStatus::init(AsyncWebServer *server) {
SystemStatus::SystemStatus(AsyncWebServer* server, SecurityManager* securityManager) {
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);
JsonObject root = response->getRoot();
#if defined(ESP8266)

View File

@ -21,13 +21,10 @@ class SystemStatus {
public:
SystemStatus(SecurityManager* securityManager);
void init(AsyncWebServer* server);
SystemStatus(AsyncWebServer* server, SecurityManager* securityManager);
private:
SecurityManager* _securityManager;
void systemStatus(AsyncWebServerRequest *request);
};

View File

@ -1,15 +1,13 @@
#include <WiFiScanner.h>
WiFiScanner::WiFiScanner(SecurityManager* securityManager) : _securityManager(securityManager) {};
void WiFiScanner::init(AsyncWebServer* server) {
WiFiScanner::WiFiScanner(AsyncWebServer *server, SecurityManager* securityManager) {
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,
_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) {
if (WiFi.scanComplete() != -1){

View File

@ -24,13 +24,10 @@ class WiFiScanner {
public:
WiFiScanner(SecurityManager* securityManager);
void init(AsyncWebServer *server);
WiFiScanner(AsyncWebServer *server, SecurityManager* securityManager);
private:
SecurityManager* _securityManager;
void scanNetworks(AsyncWebServerRequest *request);
void listNetworks(AsyncWebServerRequest *request);

View File

@ -1,10 +1,13 @@
#include <WiFiSettingsService.h>
WiFiSettingsService::WiFiSettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) {
// Disable wifi config persistance and auto reconnect
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
WiFi.persistent(false);
WiFi.setAutoReconnect(false);
// Force WiFi config to be applied when loop() called
reconfigureWiFiConnection();
#if defined(ESP8266)
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
#elif defined(ESP_PLATFORM)
@ -17,11 +20,6 @@ WiFiSettingsService::WiFiSettingsService(FS* fs, SecurityManager* securityManage
WiFiSettingsService::~WiFiSettingsService() {}
void WiFiSettingsService::init(AsyncWebServer* server) {
SettingsService::init(server);
reconfigureWiFiConnection();
}
void WiFiSettingsService::readFromJsonObject(JsonObject& root){
_ssid = root["ssid"] | "";
_password = root["password"] | "";

View File

@ -12,10 +12,9 @@ class WiFiSettingsService : public AdminSettingsService {
public:
WiFiSettingsService(FS* fs, SecurityManager* securityManager);
WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
~WiFiSettingsService();
void init(AsyncWebServer* server);
void loop();
protected:

View File

@ -1,6 +1,9 @@
#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)
_onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected);
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected);
@ -47,12 +50,6 @@ 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();

View File

@ -22,14 +22,10 @@ class WiFiStatus {
public:
WiFiStatus(SecurityManager* securityManager);
void init(AsyncWebServer* server);
WiFiStatus(AsyncWebServer* server, SecurityManager* securityManager);
private:
SecurityManager* _securityManager;
#if defined(ESP8266)
// handler refrences for logging important WiFi events over serial
WiFiEventHandler _onStationModeConnectedHandler;

View File

@ -1,10 +1,11 @@
#include <DemoProject.h>
void DemoProject::init(AsyncWebServer* server) {
AdminSettingsService::init(server);
DemoProject::DemoProject(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, DEMO_SETTINGS_PATH, DEMO_SETTINGS_FILE) {
pinMode(BLINK_LED, OUTPUT);
}
DemoProject::~DemoProject() {}
void DemoProject::loop() {
unsigned delay = MAX_DELAY / 255 * (255 - _blinkSpeed);
unsigned long currentMillis = millis();

View File

@ -14,10 +14,9 @@ class DemoProject : public AdminSettingsService {
public:
DemoProject(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, DEMO_SETTINGS_PATH, DEMO_SETTINGS_FILE) {}
~DemoProject() {}
DemoProject(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
~DemoProject();
void init(AsyncWebServer* server);
void loop();
private:

View File

@ -5,20 +5,14 @@
#define SERIAL_BAUD_RATE 115200
AsyncWebServer server(80);
ESP8266React framework(&SPIFFS);
DemoProject demoProject = DemoProject(&SPIFFS, framework.getSecurityManager());
ESP8266React framework(&server, &SPIFFS);
DemoProject demoProject = DemoProject(&server, &SPIFFS, framework.getSecurityManager());
void setup() {
// start serial and filesystem
Serial.begin(SERIAL_BAUD_RATE);
SPIFFS.begin();
// set up the framework
framework.init(&server);
// begin the demo project
demoProject.init(&server);
// start the server
server.begin();
}