add abstraction layer for webserver

This commit is contained in:
technyon
2022-04-28 23:08:58 +02:00
parent 47c3729800
commit 46e49f2118
6 changed files with 139 additions and 30 deletions

View File

@@ -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);