Merge pull request #68 from rjwats/clang-format

reformat with .clang-format based on google's spec with some minor ch…
This commit is contained in:
rjwats 2019-12-03 23:28:11 +00:00 committed by GitHub
commit 368bfef29b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
40 changed files with 885 additions and 880 deletions

15
.clang-format Normal file
View File

@ -0,0 +1,15 @@
Language: Cpp
BasedOnStyle: Google
ColumnLimit: 120
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
BinPackArguments: false
BinPackParameters: false
BreakConstructorInitializers: AfterColon
AllowAllParametersOfDeclarationOnNextLine: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ExperimentalAutoDetectBinPacking: false
KeepEmptyLinesAtTheStartOfBlocks: false
DerivePointerAlignment: false

View File

@ -1,8 +1,11 @@
#include <APSettingsService.h> #include <APSettingsService.h>
APSettingsService::APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, AP_SETTINGS_SERVICE_PATH, AP_SETTINGS_FILE) {} APSettingsService::APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
AdminSettingsService(server, fs, securityManager, AP_SETTINGS_SERVICE_PATH, AP_SETTINGS_FILE) {
}
APSettingsService::~APSettingsService() {} APSettingsService::~APSettingsService() {
}
void APSettingsService::begin() { void APSettingsService::begin() {
SettingsService::begin(); SettingsService::begin();

View File

@ -20,9 +20,7 @@
#define AP_SETTINGS_SERVICE_PATH "/rest/apSettings" #define AP_SETTINGS_SERVICE_PATH "/rest/apSettings"
class APSettingsService : public AdminSettingsService { class APSettingsService : public AdminSettingsService {
public: public:
APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager); APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
~APSettingsService(); ~APSettingsService();
@ -30,13 +28,11 @@ class APSettingsService : public AdminSettingsService {
void loop(); void loop();
protected: protected:
void readFromJsonObject(JsonObject& root); void readFromJsonObject(JsonObject& root);
void writeToJsonObject(JsonObject& root); void writeToJsonObject(JsonObject& root);
void onConfigUpdated(); void onConfigUpdated();
private: private:
// access point settings // access point settings
uint8_t _provisionMode; uint8_t _provisionMode;
String _ssid; String _ssid;
@ -52,7 +48,6 @@ class APSettingsService : public AdminSettingsService {
void startAP(); void startAP();
void stopAP(); void stopAP();
void handleDNS(); void handleDNS();
}; };
#endif // end APSettingsConfig_h #endif // end APSettingsConfig_h

View File

@ -1,9 +1,10 @@
#include <APStatus.h> #include <APStatus.h>
APStatus::APStatus(AsyncWebServer* server, SecurityManager* securityManager) { APStatus::APStatus(AsyncWebServer* server, SecurityManager* securityManager) {
server->on(AP_STATUS_SERVICE_PATH, HTTP_GET, server->on(AP_STATUS_SERVICE_PATH,
securityManager->wrapRequest(std::bind(&APStatus::apStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED) HTTP_GET,
); securityManager->wrapRequest(std::bind(&APStatus::apStatus, this, std::placeholders::_1),
AuthenticationPredicates::IS_AUTHENTICATED));
} }
void APStatus::apStatus(AsyncWebServerRequest* request) { void APStatus::apStatus(AsyncWebServerRequest* request) {

View File

@ -5,13 +5,13 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM) #elif defined(ESP_PLATFORM)
#include <WiFi.h>
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <WiFi.h>
#endif #endif
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include <ESPAsyncWebServer.h>
#include <IPAddress.h> #include <IPAddress.h>
#include <SecurityManager.h> #include <SecurityManager.h>
@ -19,15 +19,11 @@
#define AP_STATUS_SERVICE_PATH "/rest/apStatus" #define AP_STATUS_SERVICE_PATH "/rest/apStatus"
class APStatus { class APStatus {
public: public:
APStatus(AsyncWebServer* server, SecurityManager* securityManager); APStatus(AsyncWebServer* server, SecurityManager* securityManager);
private: private:
void apStatus(AsyncWebServerRequest* request); void apStatus(AsyncWebServerRequest* request);
}; };
#endif // end APStatus_h #endif // end APStatus_h

View File

@ -4,10 +4,15 @@
#include <SettingsService.h> #include <SettingsService.h>
class AdminSettingsService : public SettingsService { class AdminSettingsService : public SettingsService {
public: public:
AdminSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager, char const* servicePath, char const* filePath): AdminSettingsService(AsyncWebServer* server,
SettingsService(server, fs, servicePath, filePath), _securityManager(securityManager) {} FS* fs,
SecurityManager* securityManager,
char const* servicePath,
char const* filePath) :
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
@ -39,7 +44,6 @@ class AdminSettingsService : public SettingsService {
AuthenticationPredicate getAuthenticationPredicate() { AuthenticationPredicate getAuthenticationPredicate() {
return AuthenticationPredicates::IS_ADMIN; return AuthenticationPredicates::IS_ADMIN;
} }
}; };
#endif // end AdminSettingsService #endif // end AdminSettingsService

View File

@ -1,6 +1,7 @@
#include "ArduinoJsonJWT.h" #include "ArduinoJsonJWT.h"
ArduinoJsonJWT::ArduinoJsonJWT(String secret) : _secret(secret) { } ArduinoJsonJWT::ArduinoJsonJWT(String secret) : _secret(secret) {
}
void ArduinoJsonJWT::setSecret(String secret) { void ArduinoJsonJWT::setSecret(String secret) {
_secret = secret; _secret = secret;

View File

@ -12,7 +12,6 @@
#endif #endif
class ArduinoJsonJWT { class ArduinoJsonJWT {
private: private:
String _secret; String _secret;
@ -34,5 +33,4 @@ public:
void parseJWT(String jwt, JsonDocument& jsonDocument); void parseJWT(String jwt, JsonDocument& jsonDocument);
}; };
#endif #endif

View File

@ -1,8 +1,8 @@
#ifndef _AsyncJsonCallbackResponse_H_ #ifndef _AsyncJsonCallbackResponse_H_
#define _AsyncJsonCallbackResponse_H_ #define _AsyncJsonCallbackResponse_H_
#include <ESPAsyncWebServer.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include <ESPAsyncWebServer.h>
/* /*
* Listens for a response being destroyed and calls a callback during said distruction. * Listens for a response being destroyed and calls a callback during said distruction.
@ -13,17 +13,19 @@
typedef std::function<void()> AsyncJsonCallback; typedef std::function<void()> AsyncJsonCallback;
class AsyncJsonCallbackResponse : public AsyncJsonResponse class AsyncJsonCallbackResponse : public AsyncJsonResponse {
{
private: private:
AsyncJsonCallback _callback; AsyncJsonCallback _callback;
public: public:
AsyncJsonCallbackResponse(AsyncJsonCallback callback, bool isArray = false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE) AsyncJsonCallbackResponse(AsyncJsonCallback callback,
: AsyncJsonResponse(isArray, maxJsonBufferSize), _callback{callback} {} bool isArray = false,
~AsyncJsonCallbackResponse() size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE) :
{ AsyncJsonResponse(isArray, maxJsonBufferSize),
_callback{callback} {
}
~AsyncJsonCallbackResponse() {
_callback(); _callback();
} }
}; };

View File

@ -1,8 +1,8 @@
#ifndef Async_Json_Request_Web_Handler_H_ #ifndef Async_Json_Request_Web_Handler_H_
#define Async_Json_Request_Web_Handler_H_ #define Async_Json_Request_Web_Handler_H_
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <ESPAsyncWebServer.h>
#define ASYNC_JSON_REQUEST_DEFAULT_MAX_SIZE 1024 #define ASYNC_JSON_REQUEST_DEFAULT_MAX_SIZE 1024
#define ASYNC_JSON_REQUEST_MIMETYPE "application/json" #define ASYNC_JSON_REQUEST_MIMETYPE "application/json"
@ -18,7 +18,6 @@
typedef std::function<void(AsyncWebServerRequest* request, JsonDocument& jsonDocument)> JsonRequestCallback; typedef std::function<void(AsyncWebServerRequest* request, JsonDocument& jsonDocument)> JsonRequestCallback;
class AsyncJsonWebHandler : public AsyncWebHandler { class AsyncJsonWebHandler : public AsyncWebHandler {
private: private:
WebRequestMethodComposite _method; WebRequestMethodComposite _method;
JsonRequestCallback _onRequest; JsonRequestCallback _onRequest;
@ -28,19 +27,28 @@ class AsyncJsonWebHandler: public AsyncWebHandler {
String _uri; String _uri;
public: public:
AsyncJsonWebHandler() : AsyncJsonWebHandler() :
_method(HTTP_POST | HTTP_PUT | HTTP_PATCH), _method(HTTP_POST | HTTP_PUT | HTTP_PATCH),
_onRequest(nullptr), _onRequest(nullptr),
_maxContentLength(ASYNC_JSON_REQUEST_DEFAULT_MAX_SIZE), _maxContentLength(ASYNC_JSON_REQUEST_DEFAULT_MAX_SIZE),
_uri() {} _uri() {
}
~AsyncJsonWebHandler() {} ~AsyncJsonWebHandler() {
}
void setUri(const String& uri) { _uri = uri; } void setUri(const String& uri) {
void setMethod(WebRequestMethodComposite method) { _method = method; } _uri = uri;
void setMaxContentLength(size_t maxContentLength) { _maxContentLength = maxContentLength; } }
void onRequest(JsonRequestCallback fn) { _onRequest = fn; } void setMethod(WebRequestMethodComposite method) {
_method = method;
}
void setMaxContentLength(size_t maxContentLength) {
_maxContentLength = maxContentLength;
}
void onRequest(JsonRequestCallback fn) {
_onRequest = fn;
}
virtual bool canHandle(AsyncWebServerRequest* request) override final { virtual bool canHandle(AsyncWebServerRequest* request) override final {
if (!_onRequest) if (!_onRequest)
@ -91,7 +99,11 @@ class AsyncJsonWebHandler: public AsyncWebHandler {
request->send(500); request->send(500);
} }
virtual void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) override final { virtual void handleBody(AsyncWebServerRequest* request,
uint8_t* data,
size_t len,
size_t index,
size_t total) override final {
if (_onRequest) { if (_onRequest) {
// don't allocate if data is too large // don't allocate if data is too large
if (total > _maxContentLength) { if (total > _maxContentLength) {
@ -114,7 +126,6 @@ class AsyncJsonWebHandler: public AsyncWebHandler {
virtual bool isRequestHandlerTrivial() override final { virtual bool isRequestHandlerTrivial() override final {
return _onRequest ? false : true; return _onRequest ? false : true;
} }
}; };
#endif // end Async_Json_Request_Web_Handler_H_ #endif // end Async_Json_Request_Web_Handler_H_

View File

@ -1,16 +1,20 @@
#include <AuthenticationService.h> #include <AuthenticationService.h>
AuthenticationService::AuthenticationService(AsyncWebServer* server, 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)); _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); server->addHandler(&_signInHandler);
} }
AuthenticationService::~AuthenticationService() {} AuthenticationService::~AuthenticationService() {
}
/** /**
* Verifys that the request supplied a valid JWT. * Verifys that the request supplied a valid JWT.
@ -21,7 +25,8 @@ void AuthenticationService::verifyAuthorization(AsyncWebServerRequest *request)
} }
/** /**
* Signs in a user if the username and password match. Provides a JWT to be used in the Authorization header in subsequent requests. * Signs in a user if the username and password match. Provides a JWT to be used in the Authorization header in
* subsequent requests.
*/ */
void AuthenticationService::signIn(AsyncWebServerRequest* request, JsonDocument& jsonDocument) { void AuthenticationService::signIn(AsyncWebServerRequest* request, JsonDocument& jsonDocument) {
if (jsonDocument.is<JsonObject>()) { if (jsonDocument.is<JsonObject>()) {
@ -41,4 +46,3 @@ void AuthenticationService::signIn(AsyncWebServerRequest *request, JsonDocument
AsyncWebServerResponse* response = request->beginResponse(401); AsyncWebServerResponse* response = request->beginResponse(401);
request->send(response); request->send(response);
} }

View File

@ -1,10 +1,10 @@
#ifndef AuthenticationService_H_ #ifndef AuthenticationService_H_
#define AuthenticationService_H_ #define AuthenticationService_H_
#include <SecurityManager.h>
#include <ESPAsyncWebServer.h>
#include <AsyncJsonWebHandler.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include <AsyncJsonWebHandler.h>
#include <ESPAsyncWebServer.h>
#include <SecurityManager.h>
#define VERIFY_AUTHORIZATION_PATH "/rest/verifyAuthorization" #define VERIFY_AUTHORIZATION_PATH "/rest/verifyAuthorization"
#define SIGN_IN_PATH "/rest/signIn" #define SIGN_IN_PATH "/rest/signIn"
@ -12,21 +12,17 @@
#define MAX_AUTHENTICATION_SIZE 256 #define MAX_AUTHENTICATION_SIZE 256
class AuthenticationService { class AuthenticationService {
public: public:
AuthenticationService(AsyncWebServer* server, SecurityManager* securityManager); AuthenticationService(AsyncWebServer* server, SecurityManager* securityManager);
~AuthenticationService(); ~AuthenticationService();
private: private:
SecurityManager* _securityManager; SecurityManager* _securityManager;
AsyncJsonWebHandler _signInHandler; AsyncJsonWebHandler _signInHandler;
// endpoint functions // endpoint functions
void signIn(AsyncWebServerRequest* request, JsonDocument& jsonDocument); void signIn(AsyncWebServerRequest* request, JsonDocument& jsonDocument);
void verifyAuthorization(AsyncWebServerRequest* request); void verifyAuthorization(AsyncWebServerRequest* request);
}; };
#endif // end SecurityManager_h #endif // end SecurityManager_h

View File

@ -7,29 +7,27 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM) #elif defined(ESP_PLATFORM)
#include <WiFi.h>
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <SPIFFS.h> #include <SPIFFS.h>
#include <WiFi.h>
#endif #endif
#include <FS.h>
#include <SecuritySettingsService.h>
#include <WiFiSettingsService.h>
#include <APSettingsService.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 <APStatus.h>
#include <SystemStatus.h> #include <AuthenticationService.h>
#include <FS.h>
#include <NTPSettingsService.h>
#include <NTPStatus.h>
#include <OTASettingsService.h>
#include <RestartService.h> #include <RestartService.h>
#include <SecuritySettingsService.h>
#include <SystemStatus.h>
#include <WiFiScanner.h>
#include <WiFiSettingsService.h>
#include <WiFiStatus.h>
class ESP8266React { class ESP8266React {
public: public:
ESP8266React(AsyncWebServer* server, FS* fs); ESP8266React(AsyncWebServer* server, FS* fs);
void begin(); void begin();
@ -40,7 +38,6 @@ class ESP8266React {
} }
private: private:
SecuritySettingsService _securitySettingsService; SecuritySettingsService _securitySettingsService;
WiFiSettingsService _wifiSettingsService; WiFiSettingsService _wifiSettingsService;
@ -50,13 +47,11 @@ class ESP8266React {
RestartService _restartService; RestartService _restartService;
AuthenticationService _authenticationService; AuthenticationService _authenticationService;
WiFiScanner _wifiScanner; WiFiScanner _wifiScanner;
WiFiStatus _wifiStatus; WiFiStatus _wifiStatus;
NTPStatus _ntpStatus; NTPStatus _ntpStatus;
APStatus _apStatus; APStatus _apStatus;
SystemStatus _systemStatus; SystemStatus _systemStatus;
}; };
#endif #endif

View File

@ -1,13 +1,18 @@
#include <NTPSettingsService.h> #include <NTPSettingsService.h>
NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE) { NTPSettingsService::NTPSettingsService(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(
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1)); std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
_onStationModeGotIPHandler =
WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1));
#elif defined(ESP_PLATFORM) #elif defined(ESP_PLATFORM)
WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED); WiFi.onEvent(
WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP); std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2),
WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
#endif #endif
NTP.onNTPSyncEvent([this](NTPSyncEvent_t ntpEvent) { NTP.onNTPSyncEvent([this](NTPSyncEvent_t ntpEvent) {
@ -16,7 +21,8 @@ NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityM
}); });
} }
NTPSettingsService::~NTPSettingsService() {} NTPSettingsService::~NTPSettingsService() {
}
void NTPSettingsService::loop() { void NTPSettingsService::loop() {
// detect when we need to re-configure NTP and do it in the main loop // detect when we need to re-configure NTP and do it in the main loop

View File

@ -3,8 +3,8 @@
#include <AdminSettingsService.h> #include <AdminSettingsService.h>
#include <TimeLib.h>
#include <NtpClientLib.h> #include <NtpClientLib.h>
#include <TimeLib.h>
// default time server // default time server
#define NTP_SETTINGS_SERVICE_DEFAULT_SERVER "pool.ntp.org" #define NTP_SETTINGS_SERVICE_DEFAULT_SERVER "pool.ntp.org"
@ -18,22 +18,18 @@
#define NTP_SETTINGS_SERVICE_PATH "/rest/ntpSettings" #define NTP_SETTINGS_SERVICE_PATH "/rest/ntpSettings"
class NTPSettingsService : public AdminSettingsService { class NTPSettingsService : public AdminSettingsService {
public: public:
NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager); NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
~NTPSettingsService(); ~NTPSettingsService();
void loop(); void loop();
protected: protected:
void readFromJsonObject(JsonObject& root); void readFromJsonObject(JsonObject& root);
void writeToJsonObject(JsonObject& root); void writeToJsonObject(JsonObject& root);
void onConfigUpdated(); void onConfigUpdated();
private: private:
String _server; String _server;
int _interval; int _interval;
@ -54,7 +50,6 @@ class NTPSettingsService : public AdminSettingsService {
void configureNTP(); void configureNTP();
void processSyncEvent(NTPSyncEvent_t ntpEvent); void processSyncEvent(NTPSyncEvent_t ntpEvent);
}; };
#endif // end NTPSettingsService_h #endif // end NTPSettingsService_h

View File

@ -1,9 +1,10 @@
#include <NTPStatus.h> #include <NTPStatus.h>
NTPStatus::NTPStatus(AsyncWebServer* server, SecurityManager* securityManager) { NTPStatus::NTPStatus(AsyncWebServer* server, SecurityManager* securityManager) {
server->on(NTP_STATUS_SERVICE_PATH, HTTP_GET, server->on(NTP_STATUS_SERVICE_PATH,
securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED) HTTP_GET,
); securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1),
AuthenticationPredicates::IS_AUTHENTICATED));
} }
void NTPStatus::ntpStatus(AsyncWebServerRequest* request) { void NTPStatus::ntpStatus(AsyncWebServerRequest* request) {

View File

@ -5,30 +5,26 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM) #elif defined(ESP_PLATFORM)
#include <WiFi.h>
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <WiFi.h>
#endif #endif
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include <TimeLib.h> #include <ESPAsyncWebServer.h>
#include <NtpClientLib.h> #include <NtpClientLib.h>
#include <SecurityManager.h> #include <SecurityManager.h>
#include <TimeLib.h>
#define MAX_NTP_STATUS_SIZE 1024 #define MAX_NTP_STATUS_SIZE 1024
#define NTP_STATUS_SERVICE_PATH "/rest/ntpStatus" #define NTP_STATUS_SERVICE_PATH "/rest/ntpStatus"
class NTPStatus { class NTPStatus {
public: public:
NTPStatus(AsyncWebServer* server, SecurityManager* securityManager); NTPStatus(AsyncWebServer* server, SecurityManager* securityManager);
private: private:
void ntpStatus(AsyncWebServerRequest* request); void ntpStatus(AsyncWebServerRequest* request);
}; };
#endif // end NTPStatus_h #endif // end NTPStatus_h

View File

@ -1,14 +1,18 @@
#include <OTASettingsService.h> #include <OTASettingsService.h>
OTASettingsService::OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, OTA_SETTINGS_SERVICE_PATH, OTA_SETTINGS_FILE) { OTASettingsService::OTASettingsService(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)
WiFi.onEvent(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP); WiFi.onEvent(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2),
WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
#endif #endif
} }
OTASettingsService::~OTASettingsService() {} OTASettingsService::~OTASettingsService() {
}
void OTASettingsService::loop() { void OTASettingsService::loop() {
if (_enabled && _arduinoOTA) { if (_enabled && _arduinoOTA) {
@ -50,22 +54,23 @@ void OTASettingsService::configureArduinoOTA() {
_arduinoOTA = new ArduinoOTAClass; _arduinoOTA = new ArduinoOTAClass;
_arduinoOTA->setPort(_port); _arduinoOTA->setPort(_port);
_arduinoOTA->setPassword(_password.c_str()); _arduinoOTA->setPassword(_password.c_str());
_arduinoOTA->onStart([]() { _arduinoOTA->onStart([]() { Serial.println("Starting"); });
Serial.println("Starting"); _arduinoOTA->onEnd([]() { Serial.println("\nEnd"); });
});
_arduinoOTA->onEnd([]() {
Serial.println("\nEnd");
});
_arduinoOTA->onProgress([](unsigned int progress, unsigned int total) { _arduinoOTA->onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100))); Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
}); });
_arduinoOTA->onError([](ota_error_t error) { _arduinoOTA->onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error); Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); if (error == OTA_AUTH_ERROR)
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); Serial.println("Auth Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_BEGIN_ERROR)
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); Serial.println("Begin Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed"); else if (error == OTA_CONNECT_ERROR)
Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR)
Serial.println("Receive Failed");
else if (error == OTA_END_ERROR)
Serial.println("End Failed");
}); });
_arduinoOTA->begin(); _arduinoOTA->begin();
} }

View File

@ -9,8 +9,8 @@
#include <ESPmDNS.h> #include <ESPmDNS.h>
#endif #endif
#include <WiFiUdp.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include <WiFiUdp.h>
// Emergency defaults // Emergency defaults
#define DEFAULT_OTA_PORT 8266 #define DEFAULT_OTA_PORT 8266
@ -20,22 +20,18 @@
#define OTA_SETTINGS_SERVICE_PATH "/rest/otaSettings" #define OTA_SETTINGS_SERVICE_PATH "/rest/otaSettings"
class OTASettingsService : public AdminSettingsService { class OTASettingsService : public AdminSettingsService {
public: public:
OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager); OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
~OTASettingsService(); ~OTASettingsService();
void loop(); void loop();
protected: protected:
void onConfigUpdated(); void onConfigUpdated();
void readFromJsonObject(JsonObject& root); void readFromJsonObject(JsonObject& root);
void writeToJsonObject(JsonObject& root); void writeToJsonObject(JsonObject& root);
private: private:
ArduinoOTAClass* _arduinoOTA; ArduinoOTAClass* _arduinoOTA;
bool _enabled; bool _enabled;
int _port; int _port;
@ -49,7 +45,6 @@ class OTASettingsService : public AdminSettingsService {
#elif defined(ESP_PLATFORM) #elif defined(ESP_PLATFORM)
void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info); void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
#endif #endif
}; };
#endif // end OTASettingsService_h #endif // end OTASettingsService_h

View File

@ -1,9 +1,10 @@
#include <RestartService.h> #include <RestartService.h>
RestartService::RestartService(AsyncWebServer* server, SecurityManager* securityManager) { RestartService::RestartService(AsyncWebServer* server, SecurityManager* securityManager) {
server->on(RESTART_SERVICE_PATH, HTTP_POST, securityManager->wrapRequest( server->on(RESTART_SERVICE_PATH,
std::bind(&RestartService::restart, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN HTTP_POST,
)); securityManager->wrapRequest(std::bind(&RestartService::restart, this, std::placeholders::_1),
AuthenticationPredicates::IS_ADMIN));
} }
void RestartService::restart(AsyncWebServerRequest* request) { void RestartService::restart(AsyncWebServerRequest* request) {

View File

@ -5,8 +5,8 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM) #elif defined(ESP_PLATFORM)
#include <WiFi.h>
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <WiFi.h>
#endif #endif
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
@ -15,15 +15,11 @@
#define RESTART_SERVICE_PATH "/rest/restart" #define RESTART_SERVICE_PATH "/rest/restart"
class RestartService { class RestartService {
public: public:
RestartService(AsyncWebServer* server, SecurityManager* securityManager); RestartService(AsyncWebServer* server, SecurityManager* securityManager);
private: private:
void restart(AsyncWebServerRequest* request); void restart(AsyncWebServerRequest* request);
}; };
#endif // end RestartService_h #endif // end RestartService_h

View File

@ -55,7 +55,8 @@ String SecurityManager::generateJWT(User *user) {
return _jwtHandler.buildJWT(payload); return _jwtHandler.buildJWT(payload);
} }
ArRequestHandlerFunction SecurityManager::wrapRequest(ArRequestHandlerFunction onRequest, AuthenticationPredicate predicate) { ArRequestHandlerFunction SecurityManager::wrapRequest(ArRequestHandlerFunction onRequest,
AuthenticationPredicate predicate) {
return [this, onRequest, predicate](AsyncWebServerRequest *request) { return [this, onRequest, predicate](AsyncWebServerRequest *request) {
Authentication authentication = authenticateRequest(request); Authentication authentication = authenticateRequest(request);
if (!predicate(authentication)) { if (!predicate(authentication)) {
@ -65,4 +66,3 @@ ArRequestHandlerFunction SecurityManager::wrapRequest(ArRequestHandlerFunction o
onRequest(request); onRequest(request);
}; };
} }

View File

@ -1,9 +1,9 @@
#ifndef SecurityManager_h #ifndef SecurityManager_h
#define SecurityManager_h #define SecurityManager_h
#include <list>
#include <ArduinoJsonJWT.h> #include <ArduinoJsonJWT.h>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <list>
#define DEFAULT_JWT_SECRET "esp8266-react" #define DEFAULT_JWT_SECRET "esp8266-react"
@ -18,8 +18,10 @@ class User {
String _username; String _username;
String _password; String _password;
bool _admin; bool _admin;
public: public:
User(String username, String password, bool admin): _username(username), _password(password), _admin(admin) {} User(String username, String password, bool admin) : _username(username), _password(password), _admin(admin) {
}
String getUsername() { String getUsername() {
return _username; return _username;
} }
@ -35,9 +37,12 @@ class Authentication {
private: private:
User* _user; User* _user;
boolean _authenticated; boolean _authenticated;
public: public:
Authentication(User& user): _user(new User(user)), _authenticated(true) {} Authentication(User& user) : _user(new User(user)), _authenticated(true) {
Authentication() : _user(nullptr), _authenticated(false) {} }
Authentication() : _user(nullptr), _authenticated(false) {
}
~Authentication() { ~Authentication() {
delete (_user); delete (_user);
} }
@ -65,9 +70,7 @@ class AuthenticationPredicates {
}; };
class SecurityManager { class SecurityManager {
public: public:
/* /*
* Authenticate, returning the user if found * Authenticate, returning the user if found
*/ */
@ -89,12 +92,10 @@ class SecurityManager {
ArRequestHandlerFunction wrapRequest(ArRequestHandlerFunction onRequest, AuthenticationPredicate predicate); ArRequestHandlerFunction wrapRequest(ArRequestHandlerFunction onRequest, AuthenticationPredicate predicate);
protected: protected:
ArduinoJsonJWT _jwtHandler = ArduinoJsonJWT(DEFAULT_JWT_SECRET); ArduinoJsonJWT _jwtHandler = ArduinoJsonJWT(DEFAULT_JWT_SECRET);
std::list<User> _users; std::list<User> _users;
private: private:
/* /*
* Lookup the user by JWT * Lookup the user by JWT
*/ */
@ -104,7 +105,6 @@ class SecurityManager {
* Verify the payload is correct * Verify the payload is correct
*/ */
boolean validatePayload(JsonObject& parsedPayload, User* user); boolean validatePayload(JsonObject& parsedPayload, User* user);
}; };
#endif // end SecurityManager_h #endif // end SecurityManager_h

View File

@ -1,7 +1,11 @@
#include <SecuritySettingsService.h> #include <SecuritySettingsService.h>
SecuritySettingsService::SecuritySettingsService(AsyncWebServer* server, FS* fs) : AdminSettingsService(server, fs, this, SECURITY_SETTINGS_PATH, SECURITY_SETTINGS_FILE), SecurityManager() {} SecuritySettingsService::SecuritySettingsService(AsyncWebServer* server, FS* fs) :
SecuritySettingsService::~SecuritySettingsService() {} AdminSettingsService(server, fs, this, SECURITY_SETTINGS_PATH, SECURITY_SETTINGS_FILE),
SecurityManager() {
}
SecuritySettingsService::~SecuritySettingsService() {
}
void SecuritySettingsService::readFromJsonObject(JsonObject& root) { void SecuritySettingsService::readFromJsonObject(JsonObject& root) {
// secret // secret

View File

@ -8,17 +8,13 @@
#define SECURITY_SETTINGS_PATH "/rest/securitySettings" #define SECURITY_SETTINGS_PATH "/rest/securitySettings"
class SecuritySettingsService : public AdminSettingsService, public SecurityManager { class SecuritySettingsService : public AdminSettingsService, public SecurityManager {
public: public:
SecuritySettingsService(AsyncWebServer* server, FS* fs); SecuritySettingsService(AsyncWebServer* server, FS* fs);
~SecuritySettingsService(); ~SecuritySettingsService();
protected: protected:
void readFromJsonObject(JsonObject& root); void readFromJsonObject(JsonObject& root);
void writeToJsonObject(JsonObject& root); void writeToJsonObject(JsonObject& root);
}; };
#endif // end SecuritySettingsService_h #endif // end SecuritySettingsService_h

View File

@ -1,11 +1,11 @@
#ifndef SettingsPersistence_h #ifndef SettingsPersistence_h
#define SettingsPersistence_h #define SettingsPersistence_h
#include <ArduinoJson.h>
#include <AsyncJson.h>
#include <AsyncJsonWebHandler.h>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <FS.h> #include <FS.h>
#include <ArduinoJson.h>
#include <AsyncJsonWebHandler.h>
#include <AsyncJson.h>
/** /**
* At the moment, not expecting settings service to have to deal with large JSON * At the moment, not expecting settings service to have to deal with large JSON
@ -18,9 +18,7 @@
* Mixin for classes which need to save settings to/from a file on the the file system as JSON. * Mixin for classes which need to save settings to/from a file on the the file system as JSON.
*/ */
class SettingsPersistence { class SettingsPersistence {
protected: protected:
// will store and retrieve config from the file system // will store and retrieve config from the file system
FS* _fs; FS* _fs;
@ -73,10 +71,11 @@ protected:
applyDefaultConfig(); applyDefaultConfig();
} }
// serialization routene, from local config to JsonObject // serialization routene, from local config to JsonObject
virtual void readFromJsonObject(JsonObject& root){} virtual void readFromJsonObject(JsonObject& root) {
virtual void writeToJsonObject(JsonObject& root){} }
virtual void writeToJsonObject(JsonObject& root) {
}
// We assume the readFromJsonObject supplies sensible defaults if an empty object // We assume the readFromJsonObject supplies sensible defaults if an empty object
// is supplied, this virtual function allows that to be changed. // is supplied, this virtual function allows that to be changed.
@ -87,12 +86,11 @@ protected:
} }
public: public:
SettingsPersistence(FS* fs, char const* filePath) : _fs(fs), _filePath(filePath) {
}
SettingsPersistence(FS* fs, char const* filePath): virtual ~SettingsPersistence() {
_fs(fs), _filePath(filePath) {} }
virtual ~SettingsPersistence() {}
}; };
#endif // end SettingsPersistence #endif // end SettingsPersistence

View File

@ -5,36 +5,37 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM) #elif defined(ESP_PLATFORM)
#include <WiFi.h>
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <WiFi.h>
#endif #endif
#include <SecurityManager.h>
#include <SettingsPersistence.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <AsyncJsonWebHandler.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include <AsyncJsonCallbackResponse.h> #include <AsyncJsonCallbackResponse.h>
#include <AsyncJsonWebHandler.h>
#include <ESPAsyncWebServer.h>
#include <SecurityManager.h>
#include <SettingsPersistence.h>
/* /*
* Abstraction of a service which stores it's settings as JSON in a file system. * Abstraction of a service which stores it's settings as JSON in a file system.
*/ */
class SettingsService : public SettingsPersistence { class SettingsService : public SettingsPersistence {
public: public:
SettingsService(AsyncWebServer* server, 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)); 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); server->addHandler(&_updateHandler);
} }
virtual ~SettingsService() {} virtual ~SettingsService() {
}
void begin() { void begin() {
// read the initial data from the file system // read the initial data from the file system
@ -62,7 +63,8 @@ protected:
writeToFS(); writeToFS();
// write settings back with a callback to reconfigure the wifi // write settings back with a callback to reconfigure the wifi
AsyncJsonCallbackResponse * response = new AsyncJsonCallbackResponse([this] () {onConfigUpdated();}, false, MAX_SETTINGS_SIZE); AsyncJsonCallbackResponse* response =
new AsyncJsonCallbackResponse([this]() { onConfigUpdated(); }, false, MAX_SETTINGS_SIZE);
JsonObject jsonObject = response->getRoot(); JsonObject jsonObject = response->getRoot();
writeToJsonObject(jsonObject); writeToJsonObject(jsonObject);
response->setLength(); response->setLength();
@ -73,8 +75,8 @@ protected:
} }
// implement to perform action when config has been updated // implement to perform action when config has been updated
virtual void onConfigUpdated(){} virtual void onConfigUpdated() {
}
}; };
#endif // end SettingsService #endif // end SettingsService

View File

@ -5,15 +5,15 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM) #elif defined(ESP_PLATFORM)
#include <WiFi.h>
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <WiFi.h>
#endif #endif
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include <AsyncJsonWebHandler.h>
#include <AsyncJsonCallbackResponse.h> #include <AsyncJsonCallbackResponse.h>
#include <AsyncJsonWebHandler.h>
#include <ESPAsyncWebServer.h>
/** /**
* At the moment, not expecting services to have to deal with large JSON * At the moment, not expecting services to have to deal with large JSON
@ -29,9 +29,7 @@
* require setting persistance. * require setting persistance.
*/ */
class SimpleService { class SimpleService {
private: private:
AsyncJsonWebHandler _updateHandler; AsyncJsonWebHandler _updateHandler;
void fetchConfig(AsyncWebServerRequest* request) { void fetchConfig(AsyncWebServerRequest* request) {
@ -48,7 +46,8 @@ private:
readFromJsonObject(newConfig); readFromJsonObject(newConfig);
// write settings back with a callback to reconfigure the wifi // write settings back with a callback to reconfigure the wifi
AsyncJsonCallbackResponse * response = new AsyncJsonCallbackResponse([this] () {onConfigUpdated();}, false, MAX_SETTINGS_SIZE); AsyncJsonCallbackResponse* response =
new AsyncJsonCallbackResponse([this]() { onConfigUpdated(); }, false, MAX_SETTINGS_SIZE);
JsonObject jsonObject = response->getRoot(); JsonObject jsonObject = response->getRoot();
writeToJsonObject(jsonObject); writeToJsonObject(jsonObject);
response->setLength(); response->setLength();
@ -59,28 +58,30 @@ private:
} }
protected: protected:
// reads the local config from the // reads the local config from the
virtual void readFromJsonObject(JsonObject& root) {} virtual void readFromJsonObject(JsonObject& root) {
virtual void writeToJsonObject(JsonObject& root) {} }
virtual void writeToJsonObject(JsonObject& root) {
}
// implement to perform action when config has been updated // implement to perform action when config has been updated
virtual void onConfigUpdated() {} virtual void onConfigUpdated() {
}
public: public:
SimpleService(AsyncWebServer* server, char const* servicePath) { SimpleService(AsyncWebServer* server, char const* servicePath) {
server->on(servicePath, HTTP_GET, std::bind(&SimpleService::fetchConfig, this, std::placeholders::_1)); 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); server->addHandler(&_updateHandler);
} }
virtual ~SimpleService() {} virtual ~SimpleService() {
}
}; };
#endif // end SimpleService #endif // end SimpleService

View File

@ -1,9 +1,10 @@
#include <SystemStatus.h> #include <SystemStatus.h>
SystemStatus::SystemStatus(AsyncWebServer* server, SecurityManager* securityManager) { SystemStatus::SystemStatus(AsyncWebServer* server, SecurityManager* securityManager) {
server->on(SYSTEM_STATUS_SERVICE_PATH, HTTP_GET, server->on(SYSTEM_STATUS_SERVICE_PATH,
securityManager->wrapRequest(std::bind(&SystemStatus::systemStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED) HTTP_GET,
); securityManager->wrapRequest(std::bind(&SystemStatus::systemStatus, this, std::placeholders::_1),
AuthenticationPredicates::IS_AUTHENTICATED));
} }
void SystemStatus::systemStatus(AsyncWebServerRequest* request) { void SystemStatus::systemStatus(AsyncWebServerRequest* request) {

View File

@ -5,28 +5,24 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM) #elif defined(ESP_PLATFORM)
#include <WiFi.h>
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <WiFi.h>
#endif #endif
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include <ESPAsyncWebServer.h>
#include <SecurityManager.h> #include <SecurityManager.h>
#define MAX_ESP_STATUS_SIZE 1024 #define MAX_ESP_STATUS_SIZE 1024
#define SYSTEM_STATUS_SERVICE_PATH "/rest/systemStatus" #define SYSTEM_STATUS_SERVICE_PATH "/rest/systemStatus"
class SystemStatus { class SystemStatus {
public: public:
SystemStatus(AsyncWebServer* server, SecurityManager* securityManager); SystemStatus(AsyncWebServer* server, SecurityManager* securityManager);
private: private:
void systemStatus(AsyncWebServerRequest* request); void systemStatus(AsyncWebServerRequest* request);
}; };
#endif // end SystemStatus_h #endif // end SystemStatus_h

View File

@ -1,12 +1,14 @@
#include <WiFiScanner.h> #include <WiFiScanner.h>
WiFiScanner::WiFiScanner(AsyncWebServer* server, SecurityManager* securityManager) { WiFiScanner::WiFiScanner(AsyncWebServer* server, SecurityManager* securityManager) {
server->on(SCAN_NETWORKS_SERVICE_PATH, HTTP_GET, server->on(SCAN_NETWORKS_SERVICE_PATH,
securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN) HTTP_GET,
); securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1),
server->on(LIST_NETWORKS_SERVICE_PATH, HTTP_GET, AuthenticationPredicates::IS_ADMIN));
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));
}; };
void WiFiScanner::scanNetworks(AsyncWebServerRequest* request) { void WiFiScanner::scanNetworks(AsyncWebServerRequest* request) {

View File

@ -5,15 +5,15 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM) #elif defined(ESP_PLATFORM)
#include <WiFi.h>
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <WiFi.h>
#endif #endif
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include <TimeLib.h> #include <ESPAsyncWebServer.h>
#include <SecurityManager.h> #include <SecurityManager.h>
#include <TimeLib.h>
#define SCAN_NETWORKS_SERVICE_PATH "/rest/scanNetworks" #define SCAN_NETWORKS_SERVICE_PATH "/rest/scanNetworks"
#define LIST_NETWORKS_SERVICE_PATH "/rest/listNetworks" #define LIST_NETWORKS_SERVICE_PATH "/rest/listNetworks"
@ -21,20 +21,16 @@
#define MAX_WIFI_SCANNER_SIZE 1024 #define MAX_WIFI_SCANNER_SIZE 1024
class WiFiScanner { class WiFiScanner {
public: public:
WiFiScanner(AsyncWebServer* server, SecurityManager* securityManager); WiFiScanner(AsyncWebServer* server, SecurityManager* securityManager);
private: private:
void scanNetworks(AsyncWebServerRequest* request); void scanNetworks(AsyncWebServerRequest* request);
void listNetworks(AsyncWebServerRequest* request); void listNetworks(AsyncWebServerRequest* request);
#if defined(ESP8266) #if defined(ESP8266)
uint8_t convertEncryptionType(uint8_t encryptionType); uint8_t convertEncryptionType(uint8_t encryptionType);
#endif #endif
}; };
#endif // end WiFiScanner_h #endif // end WiFiScanner_h

View File

@ -1,6 +1,7 @@
#include <WiFiSettingsService.h> #include <WiFiSettingsService.h>
WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) { WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
AdminSettingsService(server, fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) {
// We want the device to come up in opmode=0 (WIFI_OFF), when erasing the flash this is not the default. // We want the device to come up in opmode=0 (WIFI_OFF), when erasing the flash this is not the default.
// If needed, we save opmode=0 before disabling persistence so the device boots with WiFi disabled in the future. // If needed, we save opmode=0 before disabling persistence so the device boots with WiFi disabled in the future.
if (WiFi.getMode() != WIFI_OFF) { if (WiFi.getMode() != WIFI_OFF) {
@ -12,16 +13,20 @@ WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, Securit
WiFi.setAutoReconnect(false); WiFi.setAutoReconnect(false);
#if defined(ESP8266) #if defined(ESP8266)
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1)); _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(
std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
#elif defined(ESP_PLATFORM) #elif defined(ESP_PLATFORM)
// Init the wifi driver on ESP32 // Init the wifi driver on ESP32
WiFi.mode(WIFI_MODE_MAX); WiFi.mode(WIFI_MODE_MAX);
WiFi.mode(WIFI_MODE_NULL); WiFi.mode(WIFI_MODE_NULL);
WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED); WiFi.onEvent(
std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
#endif #endif
} }
WiFiSettingsService::~WiFiSettingsService() {} WiFiSettingsService::~WiFiSettingsService() {
}
void WiFiSettingsService::begin() { void WiFiSettingsService::begin() {
SettingsService::begin(); SettingsService::begin();
@ -137,4 +142,3 @@ void WiFiSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEvent
WiFi.disconnect(true); WiFi.disconnect(true);
} }
#endif #endif

View File

@ -9,9 +9,7 @@
#define WIFI_RECONNECTION_DELAY 1000 * 60 #define WIFI_RECONNECTION_DELAY 1000 * 60
class WiFiSettingsService : public AdminSettingsService { class WiFiSettingsService : public AdminSettingsService {
public: public:
WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager); WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
~WiFiSettingsService(); ~WiFiSettingsService();
@ -19,7 +17,6 @@ class WiFiSettingsService : public AdminSettingsService {
void loop(); void loop();
protected: protected:
void readFromJsonObject(JsonObject& root); void readFromJsonObject(JsonObject& root);
void writeToJsonObject(JsonObject& root); void writeToJsonObject(JsonObject& root);
void onConfigUpdated(); void onConfigUpdated();
@ -52,7 +49,6 @@ class WiFiSettingsService : public AdminSettingsService {
void writeIP(JsonObject& root, String key, IPAddress& _ip); void writeIP(JsonObject& root, String key, IPAddress& _ip);
void reconfigureWiFiConnection(); void reconfigureWiFiConnection();
void manageSTA(); void manageSTA();
}; };
#endif // end WiFiSettingsService_h #endif // end WiFiSettingsService_h

View File

@ -1,9 +1,10 @@
#include <WiFiStatus.h> #include <WiFiStatus.h>
WiFiStatus::WiFiStatus(AsyncWebServer* server, SecurityManager* securityManager) { WiFiStatus::WiFiStatus(AsyncWebServer* server, SecurityManager* securityManager) {
server->on(WIFI_STATUS_SERVICE_PATH, HTTP_GET, server->on(WIFI_STATUS_SERVICE_PATH,
securityManager->wrapRequest(std::bind(&WiFiStatus::wifiStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED) 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);

View File

@ -5,13 +5,13 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM) #elif defined(ESP_PLATFORM)
#include <WiFi.h>
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <WiFi.h>
#endif #endif
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include <ESPAsyncWebServer.h>
#include <IPAddress.h> #include <IPAddress.h>
#include <SecurityManager.h> #include <SecurityManager.h>
@ -19,13 +19,10 @@
#define WIFI_STATUS_SERVICE_PATH "/rest/wifiStatus" #define WIFI_STATUS_SERVICE_PATH "/rest/wifiStatus"
class WiFiStatus { class WiFiStatus {
public: public:
WiFiStatus(AsyncWebServer* server, SecurityManager* securityManager); WiFiStatus(AsyncWebServer* server, SecurityManager* securityManager);
private: private:
#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;
@ -43,7 +40,6 @@ class WiFiStatus {
#endif #endif
void wifiStatus(AsyncWebServerRequest* request); void wifiStatus(AsyncWebServerRequest* request);
}; };
#endif // end WiFiStatus_h #endif // end WiFiStatus_h

View File

@ -1,10 +1,12 @@
#include <DemoProject.h> #include <DemoProject.h>
DemoProject::DemoProject(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, DEMO_SETTINGS_PATH, DEMO_SETTINGS_FILE) { DemoProject::DemoProject(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
AdminSettingsService(server, fs, securityManager, DEMO_SETTINGS_PATH, DEMO_SETTINGS_FILE) {
pinMode(BLINK_LED, OUTPUT); pinMode(BLINK_LED, OUTPUT);
} }
DemoProject::~DemoProject() {} DemoProject::~DemoProject() {
}
void DemoProject::loop() { void DemoProject::loop() {
unsigned delay = MAX_DELAY / 255 * (255 - _blinkSpeed); unsigned delay = MAX_DELAY / 255 * (255 - _blinkSpeed);
@ -23,4 +25,3 @@ void DemoProject::writeToJsonObject(JsonObject& root) {
// connection settings // connection settings
root["blink_speed"] = _blinkSpeed; root["blink_speed"] = _blinkSpeed;
} }

View File

@ -11,24 +11,19 @@
#define DEMO_SETTINGS_PATH "/rest/demoSettings" #define DEMO_SETTINGS_PATH "/rest/demoSettings"
class DemoProject : public AdminSettingsService { class DemoProject : public AdminSettingsService {
public: public:
DemoProject(AsyncWebServer* server, FS* fs, SecurityManager* securityManager); DemoProject(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
~DemoProject(); ~DemoProject();
void loop(); void loop();
private: private:
unsigned long _lastBlink = 0; unsigned long _lastBlink = 0;
uint8_t _blinkSpeed = 255; uint8_t _blinkSpeed = 255;
protected: protected:
void readFromJsonObject(JsonObject& root); void readFromJsonObject(JsonObject& root);
void writeToJsonObject(JsonObject& root); void writeToJsonObject(JsonObject& root);
}; };
#endif #endif

View File

@ -1,5 +1,5 @@
#include <ESP8266React.h>
#include <DemoProject.h> #include <DemoProject.h>
#include <ESP8266React.h>
#include <FS.h> #include <FS.h>
#define SERIAL_BAUD_RATE 115200 #define SERIAL_BAUD_RATE 115200