organizing webserver template stuff, playing with templates

This commit is contained in:
Marcus 2024-10-20 04:48:05 +02:00
parent 2252fe0142
commit 187ade247f
10 changed files with 100 additions and 86 deletions

View file

@ -152,6 +152,9 @@ void loop() {
if((digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == false)) {
Serial.println(":: [LOOP] PinWIPE is triggered, saving config.json and set configSystem.httpLogSerial to true");
configSystem.httpLogSerial = true;
// save config to littlefs as json
SaveConfig();
// only print json to serial
SaveConfig(true);
alrdySaved = true;
} else if( (digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == true) ) {

View file

@ -226,23 +226,7 @@ bool LoadConfig() {
return true;
}
void SaveConfig(bool writeToSerial = false) {
#ifdef ESP8266
File file = LittleFS.open(CANGROW_CFG, "w");
#endif
#ifdef ESP32
fs::FS &fs = LittleFS;
File file = fs.open(CANGROW_CFG, FILE_WRITE);
#endif
if (!file) {
Serial.printf("!! [LittleFS:SaveConfig] FAILED to open configfile for writing: %s\n", CANGROW_CFG);
return;
} else {
Serial.printf(":: [LittleFS:SaveConfig] opened for writing %s\n", CANGROW_CFG);
}
void SaveConfig(bool writeToSerial = false) {
/*
* Building config.json here
*/
@ -291,22 +275,40 @@ void SaveConfig(bool writeToSerial = false) {
/*
* END Building config.json here
*/
// Serialize JSON to file
if (serializeJson(doc, file) == 0) {
Serial.printf("!! [LittleFS:SaveConfig] FAILED to write configfile: %s\n", CANGROW_CFG);
// if writeToSerial is true, output json to serial, but do not write to LittleFS
if(writeToSerial == false) {
#ifdef ESP8266
File file = LittleFS.open(CANGROW_CFG, "w");
#endif
#ifdef ESP32
fs::FS &fs = LittleFS;
File file = fs.open(CANGROW_CFG, FILE_WRITE);
#endif
if (!file) {
Serial.printf("!! [LittleFS:SaveConfig] FAILED to open configfile for writing: %s\n", CANGROW_CFG);
return;
} else {
Serial.printf(":: [LittleFS:SaveConfig] opened for writing %s\n", CANGROW_CFG);
}
// Serialize JSON to file
if (serializeJson(doc, file) == 0) {
Serial.printf("!! [LittleFS:SaveConfig] FAILED to write configfile: %s\n", CANGROW_CFG);
} else {
Serial.printf(":: [LittleFS:SaveConfig] successfully written %s\n", CANGROW_CFG);
}
file.close();
} else {
Serial.printf(":: [LittleFS:SaveConfig] successfully written %s\n", CANGROW_CFG);
}
file.close();
if(writeToSerial == true) {
Serial.printf(":: [LittleFS:SaveConfig] --- %s ---\n", CANGROW_CFG);
serializeJson(doc, Serial);
Serial.println("");
Serial.printf(":: [LittleFS:SaveConfig] --- %s ---\n", CANGROW_CFG);
}
}
///*

View file

@ -33,8 +33,8 @@
* include webpages header files
*/
#include "Webserver/header.h"
#include "Webserver/footer.h"
#include "Webserver/AddHeaderFooter.h"
#include "Webserver/Page_root.h"
@ -43,27 +43,26 @@
/*
* include static files files
*/
#include "Webserver/File_cangrow_JS.h"
#include "Webserver/File_cangrow_CSS.h"
AsyncWebServer webserver(80);
// log incoming requests
LoggingMiddleware requestLogger;
// https://github.com/mathieucarbou/ESPAsyncWebServer/blob/main/examples/SimpleServer/SimpleServer.ino
void WebserverNotFound(AsyncWebServerRequest* request) {
request->send(404, "text/plain", "Not found");
}
/*
* setup all the webhandlers
*/
void SetupWebserver() {
Serial.println(":: [Webserver] initializing");
webserver.on("/", HTTP_GET, WebPage_root);
webserver.on("/cangrow.js", HTTP_GET, WebFile_cangrow_JS);
webserver.on("/cangrow.css", HTTP_GET, WebFile_cangrow_CSS);
requestLogger.setOutput(Serial);
// this activates the middleware

View file

@ -1,6 +1,7 @@
/*
*
* include/Webserver/footer_HTML.h - footer page HTML header file
* include/Webserver/AddHeaderFooter.h - header file with functions to add
* HTML header or footer to a String()
*
*
* MIT License
@ -26,3 +27,36 @@
* THE SOFTWARE.
*
*/
bool TestHeaderFooter(const String& var) {
#ifdef DEBUG
Serial.print(":: [Webserver:Page:root:proc:hf] var: ");
Serial.println(var);
#endif
if(var == "HEADER") {
return true;
} else if(var == "FOOTER") {
return true;
} else if(var == "CGVER") {
return true;
} else if(var == "CGBUILD") {
return true;
} else {
return false;
}
}
String AddHeaderFooter(const String& var) {
if(var == "HEADER") {
return String(Header_HTML);
} else if(var == "FOOTER") {
return String(Footer_HTML);
} else if(var == "CGVER") {
return String(CANGROW_VER);
} else if(var == "CGBUILD") {
return String(CANGROW_BUILD);
} else {
return String();
}
}

View file

@ -1,6 +1,6 @@
/*
*
* include/Webserver/File_cangrow_JS.h - /cangrow.js header file
* include/Webserver/File_cangrow_CSS.h - /cangrow.css header file
*
*
* MIT License
@ -28,7 +28,7 @@
*/
const char* File_cangrow_JS PROGMEM = R"(body {
const char* File_cangrow_CSS PROGMEM = R"(body {
color: #cae0d0;
background-color: #1d211e;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
@ -190,6 +190,6 @@ input[type=text], input[type=date], input[type=number], input[type=password], se
}
)";
void WebFile_cangrow_JS(AsyncWebServerRequest *request) {
request->send_P(200, "text/javascript", File_cangrow_JS);
void WebFile_cangrow_CSS(AsyncWebServerRequest *request) {
request->send_P(200, "text/stylesheet", File_cangrow_CSS);
}

View file

@ -29,21 +29,23 @@
#include "Page_root_HTML.h"
// https://techtutorialsx.com/2018/07/23/esp32-arduino-http-server-template-processing-with-multiple-placeholders/
String Proc_WebPage_root(const String& var) {
Serial.println(":: [Webserver:Page:root:proc] start");
Serial.print(":: [Webserver:Page:root:proc] var: ");
Serial.println(var);
if(TestHeaderFooter(var)) {
return AddHeaderFooter(var);
}
if(var == "LOL") {
return String("omg das geht ja");
} else if(var == "ROFL") {
return String("rooooooofl geilooooomatic! omg %)");
return String("Nice");
} else if(var == "LOL") {
return String("Jojoojo :)");
} else {
return String();
}
}
void WebPage_root(AsyncWebServerRequest *request) {
void WebPage_root(AsyncWebServerRequest *request) {
request->send_P(200, "text/html", Page_root_HTML, Proc_WebPage_root);
}

View file

@ -28,14 +28,10 @@
*/
const char* Page_root_HTML PROGMEM = R"(
<html>
<body>
<h1>CanGrow</h1>
Hi<br>
%LOL%<br>
%ROFL% <br>
Bye<br>
</body>
</html>
)";
const char* Page_root_HTML PROGMEM = R"(%HEADER%
<h1>CanGrow</h1>
Hi<br>
%LOL%<br>
%ROFL% <br>
Bye<br>
%FOOTER%)";

View file

@ -1,6 +1,6 @@
/*
*
* include/Webserver/footer.h - footer page header file
* include/Webserver/footer_HTML.h - footer page HTML header file
*
*
* MIT License
@ -27,4 +27,6 @@
*
*/
const char* Footer_HTML PROGMEM = R"(<br><br><span>Bottom %CGVER%</span>
</body>
</html>)";

View file

@ -1,6 +1,6 @@
/*
*
* include/Webserver/header.h - header page header file
* include/Webserver/header_HTML.h - header page HTML header file
*
*
* MIT License
@ -27,4 +27,8 @@
*
*/
const char* Header_HTML PROGMEM = R"(<html>
<meta></meta>
<body>
%CGBUILD%
<br>)";

View file

@ -1,28 +0,0 @@
/*
*
* include/Webserver/header_HTML.h - header page HTML header file
*
*
* MIT License
*
* Copyright (c) 2024 DeltaLima
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/