Skip to content

Commit 2882f70

Browse files
author
Holger Pieta
committed
Made WebServer more reusable
Only non-general (non-reusable) part of WebServer was the device reboot after the submitted configuration had been stored. Removed this from the WebServer and added it (via a function reference call) to Basecamp. Added a reset function, which clears all interface elements such that new elemets can be added at will. The reset function can be used if the web server should be reconfigured during runtime. But it might not work yet.
1 parent 2fa6e78 commit 2882f70

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

Basecamp.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,11 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword)
192192
if (shouldEnableConfigWebserver())
193193
{
194194
// Start webserver and pass the configuration object to it
195-
web.begin(configuration);
195+
// Also pass a Lambda-function that restarts the device after the configuration has been saved.
196+
web.begin(configuration, [](){
197+
delay(2000);
198+
esp_restart();
199+
});
196200
// Add a webinterface element for the h1 that contains the device name. It is a child of the #wrapper-element.
197201
web.addInterfaceElement("heading", "h1", "","#wrapper");
198202
web.setInterfaceElementAttribute("heading", "class", "fat-border");

WebServer.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ WebServer::WebServer()
2626
#endif
2727
}
2828

29-
void WebServer::begin(Configuration &configuration) {
29+
void WebServer::begin(Configuration &configuration, std::function<void()> submitFunc) {
3030
SPIFFS.begin();
3131

3232
server.on("/" , HTTP_GET, [](AsyncWebServerRequest * request)
@@ -98,7 +98,7 @@ void WebServer::begin(Configuration &configuration) {
9898
request->send(response);
9999
});
100100

101-
server.on("/submitconfig", HTTP_POST, [&configuration, this](AsyncWebServerRequest *request)
101+
server.on("/submitconfig", HTTP_POST, [&configuration, submitFunc, this](AsyncWebServerRequest *request)
102102
{
103103
if (request->params() == 0) {
104104
DEBUG_PRINTLN("Refusing to take over an empty configuration submission.");
@@ -119,9 +119,8 @@ void WebServer::begin(Configuration &configuration) {
119119
configuration.save();
120120
request->send(201);
121121

122-
// Why? What is this magic value for?
123-
delay(2000);
124-
esp_restart();
122+
// Only call submitFunc when it has been set to something useful
123+
if( submitFunc ) submitFunc();
125124
});
126125

127126
server.onNotFound([this](AsyncWebServerRequest *request)
@@ -208,3 +207,16 @@ void WebServer::setInterfaceElementAttribute(const String &id, const String &key
208207
}
209208
}
210209
}
210+
211+
void WebServer::reset() {
212+
interfaceElements.clear();
213+
// We should also reset the server itself, according to documentation, but it will cause a crash.
214+
// It works without reset, if you only configure one server after a reboot. Not sure what happens if you want to reconfigure during runtime.
215+
//server.reset();
216+
//server.addHandler(&events);
217+
//#ifdef BASECAMP_USEDNS
218+
//#ifdef DNSServer_h
219+
//server.addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER);
220+
//#endif
221+
//#endif
222+
}

WebServer.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@ class WebServer {
3030
WebServer();
3131
~WebServer() = default;
3232

33-
void begin(Configuration &configuration);
33+
void begin(Configuration &configuration, std::function<void()> submitFunc = 0);
3434
bool addURL(const char* url, const char* content, const char* mimetype);
35+
36+
// Remark: The server should be stopped before any changes to the interface elements are done to avoid inconsistent results if a request comes in at that very moment.
37+
// However, ESPAsyncWebServer does not support any kind of end() function or something like that in the moment.
3538
void addInterfaceElement(const String &id, String element, String content, String parent = "#configform", String configvariable = "");
3639

3740
// Sets "key" to "value" in element with id "id" if exists.
3841
void setInterfaceElementAttribute(const String &id, const String &key, String value);
42+
43+
// Removes all interface elements
44+
void reset();
3945

4046
struct cmp_str
4147
{

0 commit comments

Comments
 (0)