Rick Watson
5 years ago
37 changed files with 275 additions and 172 deletions
-
2lib/framework/APSettingsService.cpp
-
2lib/framework/APSettingsService.h
-
6lib/framework/APStatus.cpp
-
6lib/framework/APStatus.h
-
0lib/framework/ArduinoJsonJWT.cpp
-
0lib/framework/ArduinoJsonJWT.h
-
0lib/framework/AsyncArduinoJson6.h
-
0lib/framework/AsyncJsonWebHandler.h
-
11lib/framework/AuthenticationService.cpp
-
4lib/framework/AuthenticationService.h
-
69lib/framework/ESP8266React.cpp
-
61lib/framework/ESP8266React.h
-
2lib/framework/NTPSettingsService.cpp
-
2lib/framework/NTPSettingsService.h
-
6lib/framework/NTPStatus.cpp
-
5lib/framework/NTPStatus.h
-
2lib/framework/OTASettingsService.cpp
-
2lib/framework/OTASettingsService.h
-
0lib/framework/SecurityManager.cpp
-
0lib/framework/SecurityManager.h
-
6lib/framework/SecuritySettingsService.cpp
-
4lib/framework/SecuritySettingsService.h
-
2lib/framework/SettingsPersistence.h
-
26lib/framework/SettingsService.h
-
30lib/framework/SimpleService.h
-
6lib/framework/SystemStatus.cpp
-
6lib/framework/SystemStatus.h
-
12lib/framework/WiFiScanner.cpp
-
5lib/framework/WiFiScanner.h
-
13lib/framework/WiFiSettingsService.cpp
-
4lib/framework/WiFiSettingsService.h
-
11lib/framework/WiFiStatus.cpp
-
5lib/framework/WiFiStatus.h
-
9platformio.ini
-
9src/DemoProject.cpp
-
23src/DemoProject.h
-
96src/main.cpp
@ -1,6 +1,6 @@ |
|||
#include <APSettingsService.h>
|
|||
|
|||
APSettingsService::APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, AP_SETTINGS_SERVICE_PATH, AP_SETTINGS_FILE) { |
|||
APSettingsService::APSettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, AP_SETTINGS_SERVICE_PATH, AP_SETTINGS_FILE) { |
|||
onConfigUpdated(); |
|||
} |
|||
|
@ -1,7 +1,9 @@ |
|||
#include <APStatus.h>
|
|||
|
|||
APStatus::APStatus(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) { |
|||
_server->on(AP_STATUS_SERVICE_PATH, HTTP_GET, |
|||
APStatus::APStatus(SecurityManager* securityManager) :_securityManager(securityManager) {} |
|||
|
|||
void APStatus::init(AsyncWebServer *server){ |
|||
server->on(AP_STATUS_SERVICE_PATH, HTTP_GET, |
|||
_securityManager->wrapRequest(std::bind(&APStatus::apStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED) |
|||
); |
|||
} |
@ -1,18 +1,19 @@ |
|||
#include <AuthenticationService.h>
|
|||
|
|||
AuthenticationService::AuthenticationService(AsyncWebServer* server, SecurityManager* securityManager): |
|||
_server(server), _securityManager(securityManager) { |
|||
server->on(VERIFY_AUTHORIZATION_PATH, HTTP_GET, std::bind(&AuthenticationService::verifyAuthorization, this, std::placeholders::_1)); |
|||
|
|||
AuthenticationService::AuthenticationService(SecurityManager* securityManager) : _securityManager(securityManager) { |
|||
_signInHandler.setUri(SIGN_IN_PATH); |
|||
_signInHandler.setMethod(HTTP_POST); |
|||
_signInHandler.setMaxContentLength(MAX_AUTHENTICATION_SIZE); |
|||
_signInHandler.onRequest(std::bind(&AuthenticationService::signIn, this, std::placeholders::_1, std::placeholders::_2)); |
|||
server->addHandler(&_signInHandler); |
|||
} |
|||
|
|||
AuthenticationService::~AuthenticationService() {} |
|||
|
|||
void AuthenticationService::init(AsyncWebServer* server) { |
|||
server->on(VERIFY_AUTHORIZATION_PATH, HTTP_GET, std::bind(&AuthenticationService::verifyAuthorization, this, std::placeholders::_1)); |
|||
server->addHandler(&_signInHandler); |
|||
} |
|||
|
|||
/**
|
|||
* Verifys that the request supplied a valid JWT. |
|||
*/ |
@ -0,0 +1,69 @@ |
|||
#include <ESP8266React.h>
|
|||
|
|||
ESP8266React::ESP8266React(FS* fs): |
|||
_fs(fs), |
|||
_securitySettingsService(_fs), |
|||
_wifiSettingsService(_fs, &_securitySettingsService), |
|||
_apSettingsService(_fs, &_securitySettingsService), |
|||
_ntpSettingsService(_fs, &_securitySettingsService), |
|||
_otaSettingsService(_fs, &_securitySettingsService), |
|||
_authenticationService(&_securitySettingsService), |
|||
_wifiScanner(&_securitySettingsService), |
|||
_wifiStatus(&_securitySettingsService), |
|||
_ntpStatus(&_securitySettingsService), |
|||
_apStatus(&_securitySettingsService), |
|||
_systemStatus(&_securitySettingsService) { |
|||
} |
|||
|
|||
void ESP8266React::init(AsyncWebServer* server) { |
|||
// Start security settings service first
|
|||
_securitySettingsService.init(server); |
|||
|
|||
// Core services
|
|||
_wifiSettingsService.init(server); |
|||
_apSettingsService.init(server); |
|||
_ntpSettingsService.init(server); |
|||
_otaSettingsService.init(server); |
|||
_authenticationService.init(server); |
|||
|
|||
// Utility services
|
|||
_wifiScanner.init(server); |
|||
_wifiStatus.init(server); |
|||
_ntpStatus.init(server); |
|||
_apStatus.init(server); |
|||
_systemStatus.init(server); |
|||
|
|||
|
|||
// Serving static resources from /www/
|
|||
server->serveStatic("/js/", SPIFFS, "/www/js/"); |
|||
server->serveStatic("/css/", SPIFFS, "/www/css/"); |
|||
server->serveStatic("/fonts/", SPIFFS, "/www/fonts/"); |
|||
server->serveStatic("/app/", SPIFFS, "/www/app/"); |
|||
server->serveStatic("/favicon.ico", SPIFFS, "/www/favicon.ico"); |
|||
|
|||
// Serving all other get requests with "/www/index.htm"
|
|||
// OPTIONS get a straight up 200 response
|
|||
server->onNotFound([](AsyncWebServerRequest *request) { |
|||
if (request->method() == HTTP_GET) { |
|||
request->send(SPIFFS, "/www/index.html"); |
|||
} else if (request->method() == HTTP_OPTIONS) { |
|||
request->send(200); |
|||
} else { |
|||
request->send(404); |
|||
} |
|||
}); |
|||
|
|||
// Disable CORS if required
|
|||
#if defined(ENABLE_CORS)
|
|||
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", CORS_ORIGIN); |
|||
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization"); |
|||
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials", "true"); |
|||
#endif
|
|||
} |
|||
|
|||
void ESP8266React::loop() { |
|||
_wifiSettingsService.loop(); |
|||
_apSettingsService.loop(); |
|||
_ntpSettingsService.loop(); |
|||
_otaSettingsService.loop(); |
|||
} |
@ -0,0 +1,61 @@ |
|||
#ifndef ESP8266React_h |
|||
#define ESP8266React_h |
|||
|
|||
#include <Arduino.h> |
|||
|
|||
#if defined(ESP8266) |
|||
#include <ESP8266WiFi.h> |
|||
#include <ESPAsyncTCP.h> |
|||
#elif defined(ESP_PLATFORM) |
|||
#include <WiFi.h> |
|||
#include <AsyncTCP.h> |
|||
#include <SPIFFS.h> |
|||
#endif |
|||
|
|||
#include <FS.h> |
|||
#include <SecuritySettingsService.h> |
|||
#include <WiFiSettingsService.h> |
|||
#include <APSettingsService.h> |
|||
#include <NTPSettingsService.h> |
|||
#include <OTASettingsService.h> |
|||
#include <AuthenticationService.h> |
|||
#include <WiFiScanner.h> |
|||
#include <WiFiStatus.h> |
|||
#include <NTPStatus.h> |
|||
#include <APStatus.h> |
|||
#include <SystemStatus.h> |
|||
|
|||
class ESP8266React { |
|||
|
|||
public: |
|||
|
|||
ESP8266React(FS* fs); |
|||
|
|||
void init(AsyncWebServer* server); |
|||
void loop(); |
|||
|
|||
SecurityManager* getSecurityManager(){ |
|||
return &_securitySettingsService; |
|||
} |
|||
|
|||
private: |
|||
|
|||
FS* _fs; |
|||
|
|||
SecuritySettingsService _securitySettingsService; |
|||
|
|||
WiFiSettingsService _wifiSettingsService; |
|||
APSettingsService _apSettingsService; |
|||
NTPSettingsService _ntpSettingsService; |
|||
OTASettingsService _otaSettingsService; |
|||
AuthenticationService _authenticationService; |
|||
|
|||
WiFiScanner _wifiScanner; |
|||
WiFiStatus _wifiStatus; |
|||
NTPStatus _ntpStatus; |
|||
APStatus _apStatus; |
|||
SystemStatus _systemStatus; |
|||
|
|||
}; |
|||
|
|||
#endif |
@ -1,6 +1,6 @@ |
|||
#include <NTPSettingsService.h>
|
|||
|
|||
NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE) { |
|||
NTPSettingsService::NTPSettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE) { |
|||
|
|||
#if defined(ESP8266)
|
|||
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1)); |
@ -1,7 +1,9 @@ |
|||
#include <NTPStatus.h>
|
|||
|
|||
NTPStatus::NTPStatus(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) { |
|||
_server->on(NTP_STATUS_SERVICE_PATH, HTTP_GET, |
|||
NTPStatus::NTPStatus(SecurityManager* securityManager) : _securityManager(securityManager) {} |
|||
|
|||
void NTPStatus::init(AsyncWebServer *server){ |
|||
server->on(NTP_STATUS_SERVICE_PATH, HTTP_GET, |
|||
_securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED) |
|||
); |
|||
} |
@ -1,6 +1,6 @@ |
|||
#include <OTASettingsService.h>
|
|||
|
|||
OTASettingsService::OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, OTA_SETTINGS_SERVICE_PATH, OTA_SETTINGS_FILE) { |
|||
OTASettingsService::OTASettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, OTA_SETTINGS_SERVICE_PATH, OTA_SETTINGS_FILE) { |
|||
#if defined(ESP8266)
|
|||
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1)); |
|||
#elif defined(ESP_PLATFORM)
|
@ -1,7 +1,9 @@ |
|||
#include <SystemStatus.h>
|
|||
|
|||
SystemStatus::SystemStatus(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) { |
|||
_server->on(SYSTEM_STATUS_SERVICE_PATH, HTTP_GET, |
|||
SystemStatus::SystemStatus(SecurityManager* securityManager) : _securityManager(securityManager) {} |
|||
|
|||
void SystemStatus::init(AsyncWebServer *server) { |
|||
server->on(SYSTEM_STATUS_SERVICE_PATH, HTTP_GET, |
|||
_securityManager->wrapRequest(std::bind(&SystemStatus::systemStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED) |
|||
); |
|||
} |
@ -1,11 +1,13 @@ |
|||
#include <WiFiScanner.h>
|
|||
|
|||
WiFiScanner::WiFiScanner(AsyncWebServer *server, SecurityManager* securityManager) : _server(server) { |
|||
_server->on(SCAN_NETWORKS_SERVICE_PATH, HTTP_GET, |
|||
securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN) |
|||
WiFiScanner::WiFiScanner(SecurityManager* securityManager) : _securityManager(securityManager) {}; |
|||
|
|||
void WiFiScanner::init(AsyncWebServer* server) { |
|||
server->on(SCAN_NETWORKS_SERVICE_PATH, HTTP_GET, |
|||
_securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN) |
|||
); |
|||
_server->on(LIST_NETWORKS_SERVICE_PATH, HTTP_GET, |
|||
securityManager->wrapRequest(std::bind(&WiFiScanner::listNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN) |
|||
server->on(LIST_NETWORKS_SERVICE_PATH, HTTP_GET, |
|||
_securityManager->wrapRequest(std::bind(&WiFiScanner::listNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN) |
|||
); |
|||
} |
|||
|
@ -1,17 +1,24 @@ |
|||
#include <WiFiSettingsService.h>
|
|||
|
|||
WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) { |
|||
WiFiSettingsService::WiFiSettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) { |
|||
// Disable wifi config persistance and auto reconnect
|
|||
WiFi.persistent(false); |
|||
WiFi.setAutoReconnect(false); |
|||
|
|||
#if defined(ESP8266)
|
|||
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1)); |
|||
#elif defined(ESP_PLATFORM)
|
|||
// Init the wifi driver on ESP32
|
|||
WiFi.mode(WIFI_MODE_MAX); |
|||
WiFi.mode(WIFI_MODE_NULL); |
|||
WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED); |
|||
#endif
|
|||
} |
|||
|
|||
WiFiSettingsService::~WiFiSettingsService() {} |
|||
|
|||
void WiFiSettingsService::begin() { |
|||
SettingsService::begin(); |
|||
void WiFiSettingsService::init(AsyncWebServer* server) { |
|||
SettingsService::init(server); |
|||
reconfigureWiFiConnection(); |
|||
} |
|||
|
@ -0,0 +1,9 @@ |
|||
#include <DemoProject.h>
|
|||
|
|||
void DemoProject::begin() { |
|||
|
|||
} |
|||
|
|||
void DemoProject::loop() { |
|||
|
|||
} |
@ -0,0 +1,23 @@ |
|||
#ifndef DemoProject_h |
|||
#define DemoProject_h |
|||
|
|||
#include <ESPAsyncWebServer.h> |
|||
#include <SecurityManager.h> |
|||
|
|||
class DemoProject { |
|||
|
|||
public: |
|||
|
|||
DemoProject(AsyncWebServer *server, SecurityManager* securityManager) : _server(server), _securityManager(securityManager) {} |
|||
|
|||
void begin(); |
|||
void loop(); |
|||
|
|||
private: |
|||
|
|||
AsyncWebServer* _server; |
|||
SecurityManager* _securityManager; |
|||
|
|||
}; |
|||
|
|||
#endif |
@ -1,99 +1,31 @@ |
|||
#include <Arduino.h>
|
|||
|
|||
#if defined(ESP8266)
|
|||
#include <ESP8266WiFi.h>
|
|||
#include <ESPAsyncTCP.h>
|
|||
#elif defined(ESP_PLATFORM)
|
|||
#include <WiFi.h>
|
|||
#include <AsyncTCP.h>
|
|||
#include <SPIFFS.h>
|
|||
#endif
|
|||
|
|||
#include <ESP8266React.h>
|
|||
#include <DemoProject.h>
|
|||
#include <FS.h>
|
|||
#include <SecuritySettingsService.h>
|
|||
#include <WiFiSettingsService.h>
|
|||
#include <APSettingsService.h>
|
|||
#include <NTPSettingsService.h>
|
|||
#include <OTASettingsService.h>
|
|||
#include <AuthenticationService.h>
|
|||
#include <WiFiScanner.h>
|
|||
#include <WiFiStatus.h>
|
|||
#include <NTPStatus.h>
|
|||
#include <APStatus.h>
|
|||
#include <SystemStatus.h>
|
|||
|
|||
#define SERIAL_BAUD_RATE 115200
|
|||
|
|||
AsyncWebServer server(80); |
|||
ESP8266React espServer(&SPIFFS); |
|||
|
|||
SecuritySettingsService securitySettingsService = SecuritySettingsService(&server, &SPIFFS); |
|||
WiFiSettingsService wifiSettingsService = WiFiSettingsService(&server, &SPIFFS, &securitySettingsService); |
|||
APSettingsService apSettingsService = APSettingsService(&server, &SPIFFS, &securitySettingsService); |
|||
NTPSettingsService ntpSettingsService = NTPSettingsService(&server, &SPIFFS, &securitySettingsService); |
|||
OTASettingsService otaSettingsService = OTASettingsService(&server, &SPIFFS, &securitySettingsService); |
|||
AuthenticationService authenticationService = AuthenticationService(&server, &securitySettingsService); |
|||
|
|||
WiFiScanner wifiScanner = WiFiScanner(&server, &securitySettingsService); |
|||
WiFiStatus wifiStatus = WiFiStatus(&server, &securitySettingsService); |
|||
NTPStatus ntpStatus = NTPStatus(&server, &securitySettingsService); |
|||
APStatus apStatus = APStatus(&server, &securitySettingsService); |
|||
SystemStatus systemStatus = SystemStatus(&server, &securitySettingsService);; |
|||
DemoProject demoProject = DemoProject(&server, espServer.getSecurityManager()); |
|||
|
|||
void setup() { |
|||
// Disable wifi config persistance and auto reconnect
|
|||
WiFi.persistent(false); |
|||
WiFi.setAutoReconnect(false); |
|||
|
|||
#if defined(ESP_PLATFORM)
|
|||
// Init the wifi driver on ESP32
|
|||
WiFi.mode(WIFI_MODE_MAX); |
|||
WiFi.mode(WIFI_MODE_NULL); |
|||
#endif
|
|||
|
|||
Serial.begin(SERIAL_BAUD_RATE); |
|||
SPIFFS.begin(); |
|||
|
|||
// Start security settings service first
|
|||
securitySettingsService.begin(); |
|||
|
|||
// Start services
|
|||
ntpSettingsService.begin(); |
|||
otaSettingsService.begin(); |
|||
apSettingsService.begin(); |
|||
wifiSettingsService.begin(); |
|||
|
|||
// Serving static resources from /www/
|
|||
server.serveStatic("/js/", SPIFFS, "/www/js/"); |
|||
server.serveStatic("/css/", SPIFFS, "/www/css/"); |
|||
server.serveStatic("/fonts/", SPIFFS, "/www/fonts/"); |
|||
server.serveStatic("/app/", SPIFFS, "/www/app/"); |
|||
server.serveStatic("/favicon.ico", SPIFFS, "/www/favicon.ico"); |
|||
|
|||
// Serving all other get requests with "/www/index.htm"
|
|||
// OPTIONS get a straight up 200 response
|
|||
server.onNotFound([](AsyncWebServerRequest *request) { |
|||
if (request->method() == HTTP_GET) { |
|||
request->send(SPIFFS, "/www/index.html"); |
|||
} else if (request->method() == HTTP_OPTIONS) { |
|||
request->send(200); |
|||
} else { |
|||
request->send(404); |
|||
} |
|||
}); |
|||
|
|||
// Disable CORS if required
|
|||
#if defined(ENABLE_CORS)
|
|||
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", CORS_ORIGIN); |
|||
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization"); |
|||
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials", "true"); |
|||
#endif
|
|||
// set up the framework
|
|||
espServer.init(&server); |
|||
|
|||
// begin the demo project
|
|||
demoProject.begin(); |
|||
|
|||
server.begin(); |
|||
} |
|||
|
|||
void loop() { |
|||
wifiSettingsService.loop(); |
|||
apSettingsService.loop(); |
|||
ntpSettingsService.loop(); |
|||
otaSettingsService.loop(); |
|||
// run the framework loop
|
|||
espServer.loop(); |
|||
|
|||
// run the demo project loop
|
|||
demoProject.loop(); |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue