firmware wip - changed webhandlers - working now with redirects instead setting handlers

This commit is contained in:
Marcus 2024-04-17 02:03:45 +02:00
parent af9847ceb7
commit b3c0959521
1 changed files with 173 additions and 169 deletions

View File

@ -945,56 +945,34 @@ void WebHandler() {
*
* WebHandler_unconfigured() and WebHandler_configured()
*/
// generic handler
// style.css
webserver.on("/style.css", HTTP_GET, WEBstyleCSS);
// Web root
webserver.on("/", HTTP_GET, WEBroot);
// WiFi Stuff
webserver.on("/wifiSettings", HTTP_GET, WEBwifiSettings);
webserver.on("/wifiSettings/save", HTTP_POST, POSTwifiSettings);
webserver.on("/style.css", HTTP_GET, WEBstyleCSS);
// System stuff
webserver.on("/systemSettings", HTTP_GET, WEBsystemSettings);
webserver.on("/systemSettings/save", HTTP_POST, POSTsystemSettings);
// Grow stuff
webserver.on("/growSettings", HTTP_GET, WEBgrowSettings);
webserver.on("/growSettings/save", HTTP_POST, POSTgrowSettings);
// help
webserver.on("/help", HTTP_GET, WEBhelp);
// does not work atm TODO
webserver.on("/logout", [](){ webserver.send(401, "text/html", "logged out!"); });
//webserver.on("/logout", [](){ webserver.send(401, "text/html", "logged out!"); });
// 404 handling
// favicon.ico is a special one, because its requested everytime and i dont wont to deliver the
// failed whole page every call. we can save up this 0,5kb traffic :o)
webserver.on("/favicon.ico", [](){ webserver.send(404, "text/html", "404 - not found"); });
webserver.onNotFound(WEB404);
// when wifi is unconfigured (WIFIssid <1) then root are Wifi settings
if(strlen(WIFIssid) < 1) {
webserver.on("/", HTTP_GET, WEBwifiSettings);
webserver.on("/systemSettings", HTTP_GET, WEBwifiSettings);
webserver.on("/growSettings", HTTP_GET, WEBwifiSettings);
} else {
// now we need to receive systemSettings
webserver.on("/systemSettings/save", HTTP_POST, POSTsystemSettings);
// when system settings are unconfigured , then system settings are root
if(configured == false) {
webserver.on("/", HTTP_GET, WEBsystemSettings);
webserver.on("/systemSettings", HTTP_GET, WEBsystemSettings);
webserver.on("/growSettings", HTTP_GET, WEBsystemSettings);
} else {
webserver.on("/systemSettings", HTTP_GET, WEBsystemSettings);
}
webserver.on("/growSettings", HTTP_GET, WEBgrowSettings);
// now we need to receive growSettings
webserver.on("/growSettings/save", HTTP_POST, POSTgrowSettings);
// when grow is unconfigured (GrowStart <1) then Grow settings are root
if(GrowStart < 1) {
webserver.on("/", HTTP_GET, WEBgrowSettings);
} else {
webserver.on("/", HTTP_GET, WEBroot);
}
}
}
@ -1101,14 +1079,26 @@ void WEBhelp() {
*/
void WEBroot() {
String body = FPSTR(HTMLheader);
body += "<h1>configured!</h1>";
body += "<p>";
body += timeClient.getFormattedTime();
body += "</p>";
body += returnHTMLfooter();
webserver.send(200, "text/html", body);
if(strlen(WIFIssid) < 1) {
webserver.sendHeader("Location", String("/wifiSettings"), true);
webserver.send(302, "text/plain", "please configure wifiSettings first");
} else if(configured == false){
webserver.sendHeader("Location", String("/systemSettings"), true);
webserver.send(302, "text/plain", "please configure systemSettings first");
} else if(strlen(GrowName) < 1){
webserver.sendHeader("Location", String("/growSettings"), true);
webserver.send(302, "text/plain", "please configure growSettings first");
} else {
String body = FPSTR(HTMLheader);
body += "<h1>configured!</h1>";
body += "<p>";
body += timeClient.getFormattedTime();
body += "</p>";
body += returnHTMLfooter();
webserver.send(200, "text/html", body);
}
}
/*
@ -1163,143 +1153,157 @@ void WEBwifiSettings() {
}
void WEBsystemSettings() {
String body = FPSTR(HTMLheader);
if(configured == false) {
body += "<h1>Step 2: System settings</h1>";
body += "<p>Please configure all settings<br>";
body += "</p>";
}
body += "<h2>System settings</h2>";
if(webserver.hasArg("success")) {
body += FPSTR(HTMLsuccess);
}
body += "<p>here you can set which features and sensors you use<br>";
body += "</p>";
// form starts
body += "<form method='post' action='/systemSettings/save'>\n";
// UseFan bool
body += "Use fan: <select id='UseFan' name='UseFan' required>\n";
if(configured == false){body += "<option disabled value='' selected hidden>---</option>\n";}
body += "<option value='1'" + returnStrSelected(UseFan, 1) + ">Yes</option>\n";
body += "<option value='0'" + returnStrSelected(UseFan, 0) + ">No</option>\n";
body += "</select><br>\n";
// UsePump bool
body += "Use pump: <select id='UsePump' name='UsePump' required>\n";
if(configured == false){body += "<option disabled value='' selected hidden>---</option>\n";}
body += "<option value='1'" + returnStrSelected(UsePump, 1) + ">Yes</option>\n";
body += "<option value='0'" + returnStrSelected(UsePump, 0) + ">No</option>\n";
body += "</select><br>\n";
// UseLEDrelais bool
body += "Use relais for LED: <select id='UseLEDrelais' name='UseLEDrelais' required>\n";
if(configured == false){body += "<option disabled value='' selected hidden>---</option>\n";}
body += "<option value='1'" + returnStrSelected(UseLEDrelais, 1) + ">Yes</option>\n";
body += "<option value='0'" + returnStrSelected(UseLEDrelais, 0) + ">No</option>\n";
body += "</select><br>\n";
// TODO ugly. can this done be better?
// PumpOnTime int
body += "Pump on time: <input type='number' name='PumpOnTime' value='";
body += PumpOnTime;
body += "'required><br>\n";
// MoistureSensor_Type byte
body += "Moisture sensor type: <select id='MoistureSensor_Type' name='MoistureSensor_Type' required>\n";
if(configured == false) {
body += "<option disabled value='' selected hidden>---</option>\n";
// if wifi settings are unconfigured, we cannot proceed with systemSettings
if(strlen(WIFIssid) < 1) {
webserver.sendHeader("Location", String("/wifiSettings"), true);
webserver.send(302, "text/plain", "please configure wifiSettings first");
} else {
String body = FPSTR(HTMLheader);
if(configured == false) {
body += "<h1>Step 2: System settings</h1>";
body += "<p>Please configure all settings<br>";
body += "</p>";
}
body += "<option value='1'" + returnStrSelected(MoistureSensor_Type, 1) + ">Analog capacitive</option>\n";
body += "<option value='2'" + returnStrSelected(MoistureSensor_Type, 2) + ">I2C chirp</option>\n";
body += "</select><br>\n";
// SoilmoistureLow byte
body += "Soil moisture low: <input type='number' name='SoilmoistureLow' value='";
body += SoilmoistureLow;
body += "' required><br>\n";
body += "<h2>System settings</h2>";
if(webserver.hasArg("success")) {
body += FPSTR(HTMLsuccess);
}
body += "<p>here you can set which features and sensors you use<br>";
body += "</p>";
// form starts
body += "<form method='post' action='/systemSettings/save'>\n";
// UseFan bool
body += "Use fan: <select id='UseFan' name='UseFan' required>\n";
if(configured == false){body += "<option disabled value='' selected hidden>---</option>\n";}
body += "<option value='1'" + returnStrSelected(UseFan, 1) + ">Yes</option>\n";
body += "<option value='0'" + returnStrSelected(UseFan, 0) + ">No</option>\n";
body += "</select><br>\n";
// ntpOffset int
body += "NTP offset: <input type='number' name='ntpOffset' value='";
body += ntpOffset;
body+= "' required><br>\n";
body += "<input type='submit' value='Save'>\n";
body += "</form>\n";
// UsePump bool
body += "Use pump: <select id='UsePump' name='UsePump' required>\n";
if(configured == false){body += "<option disabled value='' selected hidden>---</option>\n";}
body += "<option value='1'" + returnStrSelected(UsePump, 1) + ">Yes</option>\n";
body += "<option value='0'" + returnStrSelected(UsePump, 0) + ">No</option>\n";
body += "</select><br>\n";
// UseLEDrelais bool
body += "Use relais for LED: <select id='UseLEDrelais' name='UseLEDrelais' required>\n";
if(configured == false){body += "<option disabled value='' selected hidden>---</option>\n";}
body += "<option value='1'" + returnStrSelected(UseLEDrelais, 1) + ">Yes</option>\n";
body += "<option value='0'" + returnStrSelected(UseLEDrelais, 0) + ">No</option>\n";
body += "</select><br>\n";
// TODO ugly. can this done be better?
// PumpOnTime int
body += "Pump on time: <input type='number' name='PumpOnTime' value='";
body += PumpOnTime;
body += "'required><br>\n";
body += returnHTMLfooter();
// MoistureSensor_Type byte
body += "Moisture sensor type: <select id='MoistureSensor_Type' name='MoistureSensor_Type' required>\n";
if(configured == false) {
body += "<option disabled value='' selected hidden>---</option>\n";
}
body += "<option value='1'" + returnStrSelected(MoistureSensor_Type, 1) + ">Analog capacitive</option>\n";
body += "<option value='2'" + returnStrSelected(MoistureSensor_Type, 2) + ">I2C chirp</option>\n";
body += "</select><br>\n";
webserver.send(200, "text/html", body);
// SoilmoistureLow byte
body += "Soil moisture low: <input type='number' name='SoilmoistureLow' value='";
body += SoilmoistureLow;
body += "' required><br>\n";
// ntpOffset int
body += "NTP offset: <input type='number' name='ntpOffset' value='";
body += ntpOffset;
body+= "' required><br>\n";
body += "<input type='submit' value='Save'>\n";
body += "</form>\n";
body += returnHTMLfooter();
webserver.send(200, "text/html", body);
}
}
/*
* Grow pages
*/
void WEBgrowSettings() {
String body = FPSTR(HTMLheader);
// if system settings are unconfigured, we cannot proceed with growSettings
if(configured == false) {
webserver.sendHeader("Location", String("/systemSettings"), true);
webserver.send(302, "text/plain", "please configure systemSettings first");
} else {
if(strlen(GrowName) < 1) {
body += "<h1>Step 3: Grow settings</h1>";
body += "<p>Please configure all settings<br>";
String body = FPSTR(HTMLheader);
if(strlen(GrowName) < 1) {
body += "<h1>Step 3: Grow settings</h1>";
body += "<p>Please configure all settings<br>";
body += "</p>";
}
body += "<h2>Grow Settings</h2>";
if(webserver.hasArg("success")) {
body += FPSTR(HTMLsuccess);
}
body += "<p>Here you can set everything grow related, like light hours, how much water, LED brightness<br>";
body += "</p>";
body += "<form method='post' action='/growSettings/save'>\n";
body += "Grow name: <input type='text' name='GrowName' value='";
body += GrowName;
body+= "' required><br>\n";
body += "Grow start date: <input type='text' name='GrowStart' value='";
body += GrowStart;
body+= "' required><br>\n";
body += "Days of vegetation: <input type='number' name='DaysVeg' value='";
body += DaysVeg;
body+= "' required><br>\n";
body += "Days of bloom: <input type='number' name='DaysBloom' value='";
body += DaysBloom;
body+= "' required><br>\n";
body += "Hours light on vegetation: <input type='number' name='LighthoursVeg' value='";
body += LighthoursVeg;
body+= "' required><br>\n";
body += "Hours light on bloom: <input type='number' name='LighthoursBloom' value='";
body += LighthoursBloom;
body+= "' required><br>\n";
body += "Sunrise: <input type='number' name='SunriseHour' value='";
body += SunriseHour;
body+= "' required>\n";
body += " : <input type='number' name='SunriseMinute' value='";
body += SunriseMinute;
body+= "' required><br>\n";
if(UseLEDrelais == false) {
body += "Brightness LED: <input type='range' id='PINled_PWM' name='PINled_PWM' min='1' max='255' value='";
body += PINled_PWM;
body += "'/><br>\n";
}
body += "<input type='submit' value='Save'>\n";
body += "</form>\n";
body += returnHTMLfooter();
webserver.send(200, "text/html", body);
}
body += "<h2>Grow Settings</h2>";
if(webserver.hasArg("success")) {
body += FPSTR(HTMLsuccess);
}
body += "<p>Here you can set everything grow related, like light hours, how much water, LED brightness<br>";
body += "</p>";
body += "<form method='post' action='/growSettings/save'>\n";
body += "Grow name: <input type='text' name='GrowName' value='";
body += GrowName;
body+= "' required><br>\n";
body += "Grow start date: <input type='text' name='GrowStart' value='";
body += GrowStart;
body+= "' required><br>\n";
body += "Days of vegetation: <input type='number' name='DaysVeg' value='";
body += DaysVeg;
body+= "' required><br>\n";
body += "Days of bloom: <input type='number' name='DaysBloom' value='";
body += DaysBloom;
body+= "' required><br>\n";
body += "Hours light on vegetation: <input type='number' name='LighthoursVeg' value='";
body += LighthoursVeg;
body+= "' required><br>\n";
body += "Hours light on bloom: <input type='number' name='LighthoursBloom' value='";
body += LighthoursBloom;
body+= "' required><br>\n";
body += "Sunrise: <input type='number' name='SunriseHour' value='";
body += SunriseHour;
body+= "' required>\n";
body += " : <input type='number' name='SunriseMinute' value='";
body += SunriseMinute;
body+= "' required><br>\n";
if(UseLEDrelais == false) {
body += "Brightness LED: <input type='range' id='PINled_PWM' name='PINled_PWM' min='1' max='255' value='";
body += PINled_PWM;
body += "'/><br>\n";
}
body += "<input type='submit' value='Save'>\n";
body += "</form>\n";
body += returnHTMLfooter();
webserver.send(200, "text/html", body);
}