Skip to content

Commit f77428e

Browse files
committed
move initialization code to constructors as a simplfication
1 parent 29906a1 commit f77428e

30 files changed

+76
-151
lines changed

lib/framework/APSettingsService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <APSettingsService.h>
22

3-
APSettingsService::APSettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, AP_SETTINGS_SERVICE_PATH, AP_SETTINGS_FILE) {
3+
APSettingsService::APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, AP_SETTINGS_SERVICE_PATH, AP_SETTINGS_FILE) {
44
onConfigUpdated();
55
}
66

lib/framework/APSettingsService.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class APSettingsService : public AdminSettingsService {
2323

2424
public:
2525

26-
APSettingsService(FS* fs, SecurityManager* securityManager);
26+
APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
2727
~APSettingsService();
2828

2929
void loop();

lib/framework/APStatus.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#include <APStatus.h>
22

3-
APStatus::APStatus(SecurityManager* securityManager) :_securityManager(securityManager) {}
4-
5-
void APStatus::init(AsyncWebServer *server){
6-
server->on(AP_STATUS_SERVICE_PATH, HTTP_GET,
7-
_securityManager->wrapRequest(std::bind(&APStatus::apStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
3+
APStatus::APStatus(AsyncWebServer* server, SecurityManager* securityManager) {
4+
server->on(AP_STATUS_SERVICE_PATH, HTTP_GET,
5+
securityManager->wrapRequest(std::bind(&APStatus::apStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
86
);
97
}
108

lib/framework/APStatus.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ class APStatus {
2222

2323
public:
2424

25-
APStatus(SecurityManager* securityManager);
26-
void init(AsyncWebServer *server);
25+
APStatus(AsyncWebServer* server, SecurityManager* securityManager);
2726

2827
private:
2928

30-
SecurityManager* _securityManager;
31-
3229
void apStatus(AsyncWebServerRequest *request);
3330

3431
};

lib/framework/AdminSettingsService.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
class AdminSettingsService : public SettingsService {
77

88
public:
9-
AdminSettingsService(FS* fs, SecurityManager* securityManager, char const* servicePath, char const* filePath):
10-
SettingsService(fs, servicePath, filePath), _securityManager(securityManager) {
11-
}
9+
AdminSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager, char const* servicePath, char const* filePath):
10+
SettingsService(server, fs, servicePath, filePath), _securityManager(securityManager) {}
1211

1312
protected:
1413
// will validate the requests with the security manager

lib/framework/AuthenticationService.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
#include <AuthenticationService.h>
22

3-
AuthenticationService::AuthenticationService(SecurityManager* securityManager) : _securityManager(securityManager) {
3+
AuthenticationService::AuthenticationService(AsyncWebServer* server, SecurityManager* securityManager) {
4+
server->on(VERIFY_AUTHORIZATION_PATH, HTTP_GET, std::bind(&AuthenticationService::verifyAuthorization, this, std::placeholders::_1));
5+
46
_signInHandler.setUri(SIGN_IN_PATH);
57
_signInHandler.setMethod(HTTP_POST);
68
_signInHandler.setMaxContentLength(MAX_AUTHENTICATION_SIZE);
79
_signInHandler.onRequest(std::bind(&AuthenticationService::signIn, this, std::placeholders::_1, std::placeholders::_2));
10+
server->addHandler(&_signInHandler);
811
}
912

1013
AuthenticationService::~AuthenticationService() {}
1114

12-
void AuthenticationService::init(AsyncWebServer* server) {
13-
server->on(VERIFY_AUTHORIZATION_PATH, HTTP_GET, std::bind(&AuthenticationService::verifyAuthorization, this, std::placeholders::_1));
14-
server->addHandler(&_signInHandler);
15-
}
16-
1715
/**
1816
* Verifys that the request supplied a valid JWT.
1917
*/

lib/framework/AuthenticationService.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ class AuthenticationService {
1515

1616
public:
1717

18-
AuthenticationService(SecurityManager* securityManager);
18+
AuthenticationService(AsyncWebServer* server, SecurityManager* securityManager);
1919
~AuthenticationService();
2020

21-
void init(AsyncWebServer* server);
22-
2321
private:
24-
// server instance
25-
AsyncWebServer* _server;
22+
2623
SecurityManager* _securityManager;
2724
AsyncJsonWebHandler _signInHandler;
2825

lib/framework/ESP8266React.cpp

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,18 @@
11
#include <ESP8266React.h>
22

3-
ESP8266React::ESP8266React(FS* fs):
4-
_fs(fs),
5-
_securitySettingsService(_fs),
6-
_wifiSettingsService(_fs, &_securitySettingsService),
7-
_apSettingsService(_fs, &_securitySettingsService),
8-
_ntpSettingsService(_fs, &_securitySettingsService),
9-
_otaSettingsService(_fs, &_securitySettingsService),
10-
_authenticationService(&_securitySettingsService),
11-
_wifiScanner(&_securitySettingsService),
12-
_wifiStatus(&_securitySettingsService),
13-
_ntpStatus(&_securitySettingsService),
14-
_apStatus(&_securitySettingsService),
15-
_systemStatus(&_securitySettingsService) {
16-
}
17-
18-
void ESP8266React::init(AsyncWebServer* server) {
19-
// Start security settings service first
20-
_securitySettingsService.init(server);
21-
22-
// Core services
23-
_wifiSettingsService.init(server);
24-
_apSettingsService.init(server);
25-
_ntpSettingsService.init(server);
26-
_otaSettingsService.init(server);
27-
_authenticationService.init(server);
28-
29-
// Utility services
30-
_wifiScanner.init(server);
31-
_wifiStatus.init(server);
32-
_ntpStatus.init(server);
33-
_apStatus.init(server);
34-
_systemStatus.init(server);
35-
36-
37-
// Serving static resources from /www/
3+
ESP8266React::ESP8266React(AsyncWebServer* server, FS* fs):
4+
_securitySettingsService(server, fs),
5+
_wifiSettingsService(server, fs, &_securitySettingsService),
6+
_apSettingsService(server, fs, &_securitySettingsService),
7+
_ntpSettingsService(server, fs, &_securitySettingsService),
8+
_otaSettingsService(server, fs, &_securitySettingsService),
9+
_authenticationService(server, &_securitySettingsService),
10+
_wifiScanner(server, &_securitySettingsService),
11+
_wifiStatus(server, &_securitySettingsService),
12+
_ntpStatus(server, &_securitySettingsService),
13+
_apStatus(server, &_securitySettingsService),
14+
_systemStatus(server, &_securitySettingsService) {
15+
// Serve static resources from /www/
3816
server->serveStatic("/js/", SPIFFS, "/www/js/");
3917
server->serveStatic("/css/", SPIFFS, "/www/css/");
4018
server->serveStatic("/fonts/", SPIFFS, "/www/fonts/");

lib/framework/ESP8266React.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ class ESP8266React {
2929

3030
public:
3131

32-
ESP8266React(FS* fs);
33-
34-
void init(AsyncWebServer* server);
32+
ESP8266React(AsyncWebServer* server, FS* fs);
33+
3534
void loop();
3635

3736
SecurityManager* getSecurityManager(){
@@ -40,8 +39,6 @@ class ESP8266React {
4039

4140
private:
4241

43-
FS* _fs;
44-
4542
SecuritySettingsService _securitySettingsService;
4643

4744
WiFiSettingsService _wifiSettingsService;

lib/framework/NTPSettingsService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <NTPSettingsService.h>
22

3-
NTPSettingsService::NTPSettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE) {
3+
NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE) {
44

55
#if defined(ESP8266)
66
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));

lib/framework/NTPSettingsService.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class NTPSettingsService : public AdminSettingsService {
2121

2222
public:
2323

24-
NTPSettingsService(FS* fs, SecurityManager* securityManager);
24+
NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
2525
~NTPSettingsService();
2626

2727
void loop();

lib/framework/NTPStatus.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#include <NTPStatus.h>
22

3-
NTPStatus::NTPStatus(SecurityManager* securityManager) : _securityManager(securityManager) {}
4-
5-
void NTPStatus::init(AsyncWebServer *server){
3+
NTPStatus::NTPStatus(AsyncWebServer* server, SecurityManager* securityManager) {
64
server->on(NTP_STATUS_SERVICE_PATH, HTTP_GET,
7-
_securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
5+
securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
86
);
97
}
108

lib/framework/NTPStatus.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ class NTPStatus {
2323

2424
public:
2525

26-
NTPStatus(SecurityManager* securityManager);
27-
void init(AsyncWebServer *server);
26+
NTPStatus(AsyncWebServer* server, SecurityManager* securityManager);
2827

2928
private:
3029

31-
SecurityManager* _securityManager;
3230
void ntpStatus(AsyncWebServerRequest *request);
3331

3432
};

lib/framework/OTASettingsService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <OTASettingsService.h>
22

3-
OTASettingsService::OTASettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, OTA_SETTINGS_SERVICE_PATH, OTA_SETTINGS_FILE) {
3+
OTASettingsService::OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, OTA_SETTINGS_SERVICE_PATH, OTA_SETTINGS_FILE) {
44
#if defined(ESP8266)
55
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1));
66
#elif defined(ESP_PLATFORM)

lib/framework/OTASettingsService.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class OTASettingsService : public AdminSettingsService {
2323

2424
public:
2525

26-
OTASettingsService(FS* fs, SecurityManager* securityManager);
26+
OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
2727
~OTASettingsService();
2828

2929
void loop();
@@ -52,4 +52,4 @@ class OTASettingsService : public AdminSettingsService {
5252

5353
};
5454

55-
#endif // end NTPSettingsService_h
55+
#endif // end OTASettingsService_h

lib/framework/SecuritySettingsService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <SecuritySettingsService.h>
22

3-
SecuritySettingsService::SecuritySettingsService(FS* fs) : AdminSettingsService(fs, this, SECURITY_SETTINGS_PATH, SECURITY_SETTINGS_FILE), SecurityManager() {}
3+
SecuritySettingsService::SecuritySettingsService(AsyncWebServer* server, FS* fs) : AdminSettingsService(server, fs, this, SECURITY_SETTINGS_PATH, SECURITY_SETTINGS_FILE), SecurityManager() {}
44
SecuritySettingsService::~SecuritySettingsService() {}
55

66
void SecuritySettingsService::readFromJsonObject(JsonObject& root) {

lib/framework/SecuritySettingsService.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class SecuritySettingsService : public AdminSettingsService, public SecurityMana
1111

1212
public:
1313

14-
SecuritySettingsService(FS* fs);
14+
SecuritySettingsService(AsyncWebServer* server, FS* fs);
1515
~SecuritySettingsService();
1616

1717
protected:

lib/framework/SettingsService.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,21 @@ class SettingsService : public SettingsPersistence {
2323

2424
public:
2525

26-
SettingsService(FS* fs, char const* servicePath, char const* filePath):
27-
SettingsPersistence(fs, filePath), _servicePath(servicePath) {
26+
SettingsService(AsyncWebServer* server, FS* fs, char const* servicePath, char const* filePath): SettingsPersistence(fs, filePath), _servicePath(servicePath) {
27+
server->on(_servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1));
28+
2829
_updateHandler.setUri(servicePath);
2930
_updateHandler.setMethod(HTTP_POST);
3031
_updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE);
3132
_updateHandler.onRequest(std::bind(&SettingsService::updateConfig, this, std::placeholders::_1, std::placeholders::_2));
32-
}
33-
34-
virtual ~SettingsService() {}
35-
36-
void init(AsyncWebServer* server) {
37-
// configure fetch config handler
38-
server->on(_servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1));
39-
server->addHandler(&_updateHandler);
33+
server->addHandler(&_updateHandler);
4034

4135
// read the initial data from the file system
4236
readFromFS();
4337
}
4438

39+
virtual ~SettingsService() {}
40+
4541
protected:
4642
char const* _servicePath;
4743
AsyncJsonWebHandler _updateHandler;

lib/framework/SimpleService.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ class SimpleService {
5959

6060
protected:
6161

62-
// will serve setting endpoints from here
63-
char const* _servicePath;
64-
6562
// reads the local config from the
6663
virtual void readFromJsonObject(JsonObject& root) {}
6764
virtual void writeToJsonObject(JsonObject& root) {}
@@ -71,20 +68,18 @@ class SimpleService {
7168

7269
public:
7370

74-
SimpleService(char const* servicePath): _servicePath(servicePath) {
71+
SimpleService(AsyncWebServer* server, char const* servicePath) {
72+
server->on(servicePath, HTTP_GET, std::bind(&SimpleService::fetchConfig, this, std::placeholders::_1));
73+
7574
_updateHandler.setUri(servicePath);
7675
_updateHandler.setMethod(HTTP_POST);
7776
_updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE);
7877
_updateHandler.onRequest(std::bind(&SimpleService::updateConfig, this, std::placeholders::_1, std::placeholders::_2));
78+
server->addHandler(&_updateHandler);
7979
}
8080

8181
virtual ~SimpleService() {}
8282

83-
void init(AsyncWebServer* server) {
84-
server->on(_servicePath, HTTP_GET, std::bind(&SimpleService::fetchConfig, this, std::placeholders::_1));
85-
server->addHandler(&_updateHandler);
86-
}
87-
8883
};
8984

9085
#endif // end SimpleService

lib/framework/SystemStatus.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#include <SystemStatus.h>
22

3-
SystemStatus::SystemStatus(SecurityManager* securityManager) : _securityManager(securityManager) {}
4-
5-
void SystemStatus::init(AsyncWebServer *server) {
3+
SystemStatus::SystemStatus(AsyncWebServer* server, SecurityManager* securityManager) {
64
server->on(SYSTEM_STATUS_SERVICE_PATH, HTTP_GET,
7-
_securityManager->wrapRequest(std::bind(&SystemStatus::systemStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
5+
securityManager->wrapRequest(std::bind(&SystemStatus::systemStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
86
);
97
}
108

11-
void SystemStatus::systemStatus(AsyncWebServerRequest *request) {
9+
void SystemStatus::systemStatus(AsyncWebServerRequest *request) {
1210
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_ESP_STATUS_SIZE);
1311
JsonObject root = response->getRoot();
1412
#if defined(ESP8266)

lib/framework/SystemStatus.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ class SystemStatus {
2121

2222
public:
2323

24-
SystemStatus(SecurityManager* securityManager);
25-
void init(AsyncWebServer* server);
24+
SystemStatus(AsyncWebServer* server, SecurityManager* securityManager);
2625

2726
private:
2827

29-
SecurityManager* _securityManager;
30-
3128
void systemStatus(AsyncWebServerRequest *request);
3229

3330
};

lib/framework/WiFiScanner.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#include <WiFiScanner.h>
22

3-
WiFiScanner::WiFiScanner(SecurityManager* securityManager) : _securityManager(securityManager) {};
4-
5-
void WiFiScanner::init(AsyncWebServer* server) {
3+
WiFiScanner::WiFiScanner(AsyncWebServer *server, SecurityManager* securityManager) {
64
server->on(SCAN_NETWORKS_SERVICE_PATH, HTTP_GET,
7-
_securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN)
5+
securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN)
86
);
97
server->on(LIST_NETWORKS_SERVICE_PATH, HTTP_GET,
10-
_securityManager->wrapRequest(std::bind(&WiFiScanner::listNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN)
8+
securityManager->wrapRequest(std::bind(&WiFiScanner::listNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN)
119
);
12-
}
10+
};
1311

1412
void WiFiScanner::scanNetworks(AsyncWebServerRequest *request) {
1513
if (WiFi.scanComplete() != -1){

lib/framework/WiFiScanner.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@ class WiFiScanner {
2424

2525
public:
2626

27-
WiFiScanner(SecurityManager* securityManager);
28-
void init(AsyncWebServer *server);
27+
WiFiScanner(AsyncWebServer *server, SecurityManager* securityManager);
2928

3029
private:
3130

32-
SecurityManager* _securityManager;
33-
3431
void scanNetworks(AsyncWebServerRequest *request);
3532
void listNetworks(AsyncWebServerRequest *request);
3633

0 commit comments

Comments
 (0)