add abstraction layer for webserver
This commit is contained in:
@@ -28,6 +28,8 @@ file(GLOB SRCFILES
|
||||
PreferencesKeys.h
|
||||
SpiffsCookie.cpp
|
||||
Version.h
|
||||
webserver/AbstractWebServer.h
|
||||
webserver/EthWebServer.cpp
|
||||
lib/ESP32_BLE_Arduino-1.0.1/src/*.cpp
|
||||
lib/ESP32_BLE_Arduino-1.0.1/src/*.h
|
||||
lib/WiFiManager/WiFiManager.cpp
|
||||
@@ -52,7 +54,7 @@ add_executable(${PROJECT_NAME}
|
||||
main.cpp
|
||||
${SRCFILES}
|
||||
${SRCFILESREC}
|
||||
)
|
||||
webserver/EthWebServer.cpp webserver/EthWebServer.h)
|
||||
|
||||
# Arduino.h is included in hello_world.cpp, so link with Arduino core
|
||||
target_link_arduino_libraries(${PROJECT_NAME}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#include "WebCfgServer.h"
|
||||
#include "PreferencesKeys.h"
|
||||
#include "Version.h"
|
||||
#include "webserver/EthWebServer.h"
|
||||
|
||||
WebCfgServer::WebCfgServer(NukiWrapper* nuki, Network* network, Preferences* preferences)
|
||||
: server(80),
|
||||
: _server(new EthWebServer(80)),
|
||||
_nuki(nuki),
|
||||
_network(network),
|
||||
_preferences(preferences)
|
||||
@@ -25,43 +26,43 @@ WebCfgServer::WebCfgServer(NukiWrapper* nuki, Network* network, Preferences* pre
|
||||
|
||||
void WebCfgServer::initialize()
|
||||
{
|
||||
server.on("/", [&]() {
|
||||
if (_hasCredentials && !server.authenticate(_credUser, _credPassword)) {
|
||||
return server.requestAuthentication();
|
||||
_server->on("/", [&]() {
|
||||
if (_hasCredentials && !_server->authenticate(_credUser, _credPassword)) {
|
||||
return _server->requestAuthentication();
|
||||
}
|
||||
String response = "";
|
||||
buildHtml(response);
|
||||
server.send(200, "text/html", response);
|
||||
_server->send(200, "text/html", response);
|
||||
});
|
||||
server.on("/cred", [&]() {
|
||||
if (_hasCredentials && !server.authenticate(_credUser, _credPassword)) {
|
||||
return server.requestAuthentication();
|
||||
_server->on("/cred", [&]() {
|
||||
if (_hasCredentials && !_server->authenticate(_credUser, _credPassword)) {
|
||||
return _server->requestAuthentication();
|
||||
}
|
||||
String response = "";
|
||||
buildCredHtml(response);
|
||||
server.send(200, "text/html", response);
|
||||
_server->send(200, "text/html", response);
|
||||
});
|
||||
server.on("/wifi", [&]() {
|
||||
if (_hasCredentials && !server.authenticate(_credUser, _credPassword)) {
|
||||
return server.requestAuthentication();
|
||||
_server->on("/wifi", [&]() {
|
||||
if (_hasCredentials && !_server->authenticate(_credUser, _credPassword)) {
|
||||
return _server->requestAuthentication();
|
||||
}
|
||||
String response = "";
|
||||
buildConfigureWifiHtml(response);
|
||||
server.send(200, "text/html", response);
|
||||
_server->send(200, "text/html", response);
|
||||
});
|
||||
server.on("/wifimanager", [&]() {
|
||||
if (_hasCredentials && !server.authenticate(_credUser, _credPassword)) {
|
||||
return server.requestAuthentication();
|
||||
_server->on("/wifimanager", [&]() {
|
||||
if (_hasCredentials && !_server->authenticate(_credUser, _credPassword)) {
|
||||
return _server->requestAuthentication();
|
||||
}
|
||||
String response = "";
|
||||
buildConfirmHtml(response, "Restarting. Connect to ESP access point to reconfigure WiFi.", 0);
|
||||
server.send(200, "text/html", response);
|
||||
_server->send(200, "text/html", response);
|
||||
waitAndProcess(true, 2000);
|
||||
_network->restartAndConfigureWifi();
|
||||
});
|
||||
server.on("/method=get", [&]() {
|
||||
if (_hasCredentials && !server.authenticate(_credUser, _credPassword)) {
|
||||
return server.requestAuthentication();
|
||||
_server->on("/method=get", [&]() {
|
||||
if (_hasCredentials && !_server->authenticate(_credUser, _credPassword)) {
|
||||
return _server->requestAuthentication();
|
||||
}
|
||||
String message = "";
|
||||
bool restartEsp = processArgs(message);
|
||||
@@ -69,7 +70,7 @@ void WebCfgServer::initialize()
|
||||
{
|
||||
String response = "";
|
||||
buildConfirmHtml(response, message);
|
||||
server.send(200, "text/html", response);
|
||||
_server->send(200, "text/html", response);
|
||||
Serial.println(F("Restarting"));
|
||||
|
||||
waitAndProcess(true, 1000);
|
||||
@@ -79,12 +80,12 @@ void WebCfgServer::initialize()
|
||||
{
|
||||
String response = "";
|
||||
buildConfirmHtml(response, message, 3);
|
||||
server.send(200, "text/html", response);
|
||||
_server->send(200, "text/html", response);
|
||||
waitAndProcess(false, 1000);
|
||||
}
|
||||
});
|
||||
|
||||
server.begin();
|
||||
_server->begin();
|
||||
}
|
||||
|
||||
bool WebCfgServer::processArgs(String& message)
|
||||
@@ -93,11 +94,11 @@ bool WebCfgServer::processArgs(String& message)
|
||||
bool clearMqttCredentials = false;
|
||||
bool clearCredentials = false;
|
||||
|
||||
int count = server.args();
|
||||
int count = _server->args();
|
||||
for(int index = 0; index < count; index++)
|
||||
{
|
||||
String key = server.argName(index);
|
||||
String value = server.arg(index);
|
||||
String key = _server->argName(index);
|
||||
String value = _server->arg(index);
|
||||
|
||||
if(key == "MQTTSERVER")
|
||||
{
|
||||
@@ -214,7 +215,7 @@ void WebCfgServer::update()
|
||||
{
|
||||
if(!_enabled) return;
|
||||
|
||||
server.handleClient();
|
||||
_server->handleClient();
|
||||
}
|
||||
|
||||
void WebCfgServer::buildHtml(String& response)
|
||||
@@ -398,7 +399,7 @@ void WebCfgServer::waitAndProcess(const bool blocking, const uint32_t duration)
|
||||
unsigned long timeout = millis() + duration;
|
||||
while(millis() < timeout)
|
||||
{
|
||||
server.handleClient();
|
||||
_server->handleClient();
|
||||
if(blocking)
|
||||
{
|
||||
delay(10);
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <EthernetWebServer.h>
|
||||
#include "NukiWrapper.h"
|
||||
#include "Network.h"
|
||||
#include "webserver/AbstractWebServer.h"
|
||||
|
||||
enum class TokenType
|
||||
{
|
||||
@@ -42,7 +43,7 @@ private:
|
||||
|
||||
void waitAndProcess(const bool blocking, const uint32_t duration);
|
||||
|
||||
EthernetWebServer server;
|
||||
AbstractWebServer* _server;
|
||||
NukiWrapper* _nuki;
|
||||
Network* _network;
|
||||
Preferences* _preferences;
|
||||
|
||||
20
webserver/AbstractWebServer.h
Normal file
20
webserver/AbstractWebServer.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "EthernetWebServer.h"
|
||||
|
||||
class AbstractWebServer
|
||||
{
|
||||
public:
|
||||
explicit AbstractWebServer(int port) {};
|
||||
|
||||
virtual void begin() = 0;
|
||||
virtual bool authenticate(const char * username, const char * password) = 0;
|
||||
virtual void requestAuthentication(HTTPAuthMethod mode, const char* realm, const String& authFailMsg) = 0;
|
||||
virtual void requestAuthentication() = 0;
|
||||
virtual void send(int code, const char* content_type, const String& content) = 0;
|
||||
virtual void on(const Uri &uri, EthernetWebServer::THandlerFunction handler) = 0;
|
||||
virtual int args() = 0;
|
||||
virtual String arg(int i) = 0;
|
||||
virtual String argName(int i) = 0;
|
||||
virtual void handleClient() = 0;
|
||||
};
|
||||
58
webserver/EthWebServer.cpp
Normal file
58
webserver/EthWebServer.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "EthWebServer.h"
|
||||
|
||||
|
||||
EthWebServer::EthWebServer(int port)
|
||||
: AbstractWebServer(port),
|
||||
_server(port)
|
||||
{}
|
||||
|
||||
|
||||
void EthWebServer::begin()
|
||||
{
|
||||
_server.begin();
|
||||
}
|
||||
|
||||
bool EthWebServer::authenticate(const char *username, const char *password)
|
||||
{
|
||||
return _server.authenticate(username, password);
|
||||
}
|
||||
|
||||
void EthWebServer::requestAuthentication(HTTPAuthMethod mode, const char *realm, const String &authFailMsg)
|
||||
{
|
||||
return _server.requestAuthentication(mode, realm, authFailMsg);
|
||||
}
|
||||
|
||||
void EthWebServer::send(int code, const char *content_type, const String &content)
|
||||
{
|
||||
_server.send(code, content_type, content);
|
||||
}
|
||||
|
||||
void EthWebServer::on(const Uri &uri, EthernetWebServer::THandlerFunction handler)
|
||||
{
|
||||
_server.on(uri, handler);
|
||||
}
|
||||
|
||||
void EthWebServer::requestAuthentication()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int EthWebServer::args()
|
||||
{
|
||||
return _server.args();
|
||||
}
|
||||
|
||||
String EthWebServer::arg(int i)
|
||||
{
|
||||
return _server.arg(i);
|
||||
}
|
||||
|
||||
String EthWebServer::argName(int i)
|
||||
{
|
||||
return _server.argName(i);
|
||||
}
|
||||
|
||||
void EthWebServer::handleClient()
|
||||
{
|
||||
_server.handleClient();
|
||||
}
|
||||
27
webserver/EthWebServer.h
Normal file
27
webserver/EthWebServer.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "AbstractWebServer.h"
|
||||
|
||||
class EthWebServer : public AbstractWebServer
|
||||
{
|
||||
public:
|
||||
explicit EthWebServer(int port);
|
||||
|
||||
virtual void begin();
|
||||
virtual bool authenticate(const char *username, const char *password);
|
||||
virtual void requestAuthentication(HTTPAuthMethod mode, const char *realm, const String &authFailMsg);
|
||||
virtual void requestAuthentication();
|
||||
virtual void send(int code, const char *content_type, const String &content);
|
||||
virtual void on(const Uri &uri, EthernetWebServer::THandlerFunction handler);
|
||||
|
||||
virtual int args();
|
||||
|
||||
virtual String arg(int i);
|
||||
|
||||
virtual String argName(int i);
|
||||
|
||||
virtual void handleClient();
|
||||
|
||||
private:
|
||||
EthernetWebServer _server;
|
||||
};
|
||||
Reference in New Issue
Block a user