Skip to content

Commit

Permalink
Improved admin stability
Browse files Browse the repository at this point in the history
  • Loading branch information
birdoffire1549 committed Apr 17, 2024
1 parent 2ecd2ac commit a232f1c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 51 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added lib/.DS_Store
Binary file not shown.
84 changes: 33 additions & 51 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ void resetOrLoadSettings(void);
void doStartNetwork(void);
void checkIpDisplayRequest(void);

String htmlPageTemplate(
void sendHtmlPageUsingTemplate(
int code,
String title,
String heading,
String &content,
Expand Down Expand Up @@ -356,8 +357,7 @@ void initWebServer() {
*/
void endpointHandlerRoot() {
String content = "";
Serial.println(F("Client requested endpoint: '/'; Generating response content..."));


// Handle incoming parameters...
if (webServer.arg("source").equalsIgnoreCase("manualcontrols") && !settings.getIsAutoControl()) { // <----------------------- AutoControl is OFF...
String autoControl = webServer.arg("autocontrol");
Expand Down Expand Up @@ -430,12 +430,7 @@ void endpointHandlerRoot() {
content = content + temp;
}

Serial.println(F("Sending html to client for endpoint: '/'"));
webServer.send(
200,
"text/html",
htmlPageTemplate(String(settings.getTitle()), String(settings.getHeading()), content).c_str()
);
sendHtmlPageUsingTemplate(200, settings.getTitle(), settings.getHeading(), content);
}

bool adminPageSettingsUpdater() {
Expand Down Expand Up @@ -557,7 +552,7 @@ void endpointHandlerAdmin() {

return webServer.requestAuthentication(DIGEST_AUTH, "AdminRealm", "Authentication failed!");
}
Serial.println(F("Client has been Authenticated; Generating web content..."));
Serial.println(F("Client has been Authenticated."));

String content = ADMIN_SETTINGS_PAGE;
bool changeRequiresReboot = false;
Expand Down Expand Up @@ -593,56 +588,43 @@ void endpointHandlerAdmin() {
if (changeRequiresReboot) { // Needs to reboot...
content = F("<div id=\"successful\">Settings update Successful!</div><h4>Device will reboot now...</h4>");

Serial.println("Sending '/admin' content to client, then rebooting to apply changes.");
webServer.send(200, "text/html", htmlPageTemplate(settings.getTitle(), F("Device Settings"), content));
sendHtmlPageUsingTemplate(200, settings.getTitle(), F("Device Settings"), content);
yield();
delay(1000);
ESP.restart();
} else { // No reboot needed; Send to home page...
Serial.println("Sending '/admin' content to client.");
content = F("<div id=\"success\">Settings update Successful!</div><a href='/'><h4>Home</h4></a>");

webServer.send(
200,
"text/html",
htmlPageTemplate(
settings.getTitle(),
F("Device Settings"),
content,
"/",
5
)
sendHtmlPageUsingTemplate(
200,
settings.getTitle(),
F("Device Settings"),
content,
"/",
5
);
yield();

return;
}
} else { // Error...
Serial.println("Sending '/admin' content to client; An error prevented saving settings!");
content = F("<div id=\"failed\">Error Saving Settings!!!</div>");

webServer.send(
500,
"text/html",
htmlPageTemplate(
F("500 - Server Error"),
F("Server Error!"),
content,
"/",
5
)
sendHtmlPageUsingTemplate(
500,
F("500 - Server Error"),
F("Server Error!"),
content,
"/",
5
);
yield();

return;
}
}

Serial.println(F("Seinding '/admin' content to client."));
String htmlPage = htmlPageTemplate(settings.getTitle(), F("Device Settings"), content);

webServer.send(200, "text/html", htmlPage);
yield();
sendHtmlPageUsingTemplate(200, settings.getTitle(), F("Device Settings"), content);
}

/**
Expand All @@ -651,19 +633,17 @@ void endpointHandlerAdmin() {
*
*/
void notFoundHandler() {
Serial.printf("Client requested '%s'; 404 - Not Found send to client!\n", webServer.uri().c_str());
String content = F("Just kidding...<br>But seriously what you were looking for doesn't exist.");
webServer.send(404, "text/html", htmlPageTemplate(F("404 Not Found"), F("OOPS! You broke it!!!"), content));
sendHtmlPageUsingTemplate(404, F("404 Not Found"), F("OOPS! You broke it!!!"), content);
}

/**
* #### HANDLER - File Upload ####
* This function handles file upload requests.
*/
void fileUploadHandler() {
Serial.println(F("Client attempted to upload a file and I was like Wuuuut!?"));
String content = F("Um, I don't want your nasty files, go peddle that junk elsewhere!");
webServer.send(400, "text/html", htmlPageTemplate(F("400 Bad Request"), F("Uhhh, Wuuuuut!?"), content));
sendHtmlPageUsingTemplate(400, F("400 Bad Request"), F("Uhhh, Wuuuuut!?"), content);
}

/**
Expand All @@ -672,17 +652,18 @@ void fileUploadHandler() {
* This function is used to Generate the HTML for a web page where the
* title, heading and content is provided to the function as parameters.
*
* @param title - The page's title as String.
* @param heading - The heading that appears on the info page as String.
* @param content - The main content of the web page as String.
* @param redirectUrl - OPTIONAL PARAM, used to specify a page that this page should
* @param code The HTTP Code as int.
* @param title The page's title as String.
* @param heading The heading that appears on the info page as String.
* @param content The main content of the web page as String.
* @param redirectUrl OPTIONAL PARAM, used to specify a page that this page should
* redirect to after a specified amount of time.
* @param delaySeconds - OPTIONAL PARAM, the number of seconds to delay before sending
* @param delaySeconds OPTIONAL PARAM, the number of seconds to delay before sending
* the client to the redirectUrl, as int.
*/
String htmlPageTemplate(String title, String heading, String &content, String redirectUrl, int delaySeconds) {
void sendHtmlPageUsingTemplate(int code, String title, String heading, String &content, String redirectUrl, int delaySeconds) {
String result = HTML_PAGE_TEMPLATE;
if (!result.reserve(8000U)) {
if (!result.reserve(6000U)) {
Serial.println(F("WARNING!!! htmlPageTemplate() failed to reserve desired memory!"));
}

Expand All @@ -703,5 +684,6 @@ String htmlPageTemplate(String title, String heading, String &content, String r
result.replace("${metainsert}", temp);
}

return result;
webServer.send(code, "text/html", result);
yield();
}

0 comments on commit a232f1c

Please sign in to comment.