basic work done for editing output

i had to use a global variable to put the outputId to edit in I receive
from the GET param edit. I havent found another solution. I wish I could
just pass the GET param to the template processor, but it seems this is
not possible at the moment or I havent seen the correct solution yet.
This commit is contained in:
Marcus 2024-10-29 00:15:28 +01:00
parent 790b9bb9c9
commit b28c71c9a4
2 changed files with 116 additions and 9 deletions

View file

@ -30,6 +30,23 @@
#include "Page_system_HTML.h"
/* global runtime variables */
/* VERY VERY DIRTY WORKAROUND
* I have the problem, that I cannot pass a parameter I receive from a http
* request to it's template processor. In my case i want to edit an output,
* the user should click an edit button on the system/output overview page.
* I am lazy so i want to reuse the output_add page, because it is quite
* kinda exactly the same. so i want to call GET /system/output/add?edit=ID
* I have searched and came to the conclusion, that at this point i see no
* other way then giving the parameter I need, the outputId, to an global
* variable, so the template processor can read it.
*/
byte tmpParam_editOutputId = 255;
/* subnav processor */
bool Test_WebPage_system_SUBNAV(const String& var) {
@ -390,9 +407,14 @@ void WebPage_system_output(AsyncWebServerRequest *request) {
Serial.println(":: [Webserver:system:output] [POST] hello");
} else {
if(request->hasParam("success")) {
// when GET param success is present, we use the _POST processor for the save message
request->send_P(200, "text/html", Page_system_output_HTML, Proc_WebPage_system_output_POST);
} else {
request->send_P(200, "text/html", Page_system_output_HTML, Proc_WebPage_system_output);
}
}
}
@ -401,7 +423,7 @@ void WebPage_system_output(AsyncWebServerRequest *request) {
*/
/* returns select <option> list of available output types */
String Html_SelOpt_type_WebPage_system_output_add(byte selectId = 0) {
String Html_SelOpt_type_WebPage_system_output_add(byte selectId = 255) {
String outputType_html;
// go through all available Output Devices, skip 0 because it means unconfigured
for(byte i = 1; i < Output_Type_total; i++) {
@ -418,7 +440,7 @@ String Html_SelOpt_type_WebPage_system_output_add(byte selectId = 0) {
return outputType_html;
}
String Html_SelOpt_device_WebPage_system_output_add(byte selectId = 0) {
String Html_SelOpt_device_WebPage_system_output_add(byte selectId = 255) {
String outputDevice_html;
// go through all available Output Devices, skip 0 because it means unconfigured
for(byte i = 1; i < Output_Device_total; i++) {
@ -435,7 +457,8 @@ String Html_SelOpt_device_WebPage_system_output_add(byte selectId = 0) {
return outputDevice_html;
}
String Proc_WebPage_system_output_add(const String& var) {
String Proc_WebPage_system_output_addCommon(const String& var) {
#ifndef DEBUG
Serial.print("DB [Webserver:system:output:add(Proc)] var: ");
Serial.println(var);
@ -465,6 +488,74 @@ String Proc_WebPage_system_output_add(const String& var) {
}
}
String Proc_WebPage_system_output_add(const String& var) {
#ifndef DEBUG
Serial.print("DB [Webserver:system:output:add(Proc)] var: ");
Serial.println(var);
#endif
if(TestHeaderFooter(var)) {
return AddHeaderFooter(var, 2);
} else if(Test_WebPage_system_SUBNAV(var)) {
return Proc_WebPage_system_SUBNAV(var, 1);
} else if(var == "ACTION") {
return String("Add");
} else if(var == "OUTPUT_ID") {
// we check which id is free. A free ID as type == 0
return String(Give_Free_OutputId());
/* OUTPUT_TYPE */
} else if(var == "OUTPUT_TYPE") {
return Html_SelOpt_type_WebPage_system_output_add();
/* OUTPUT_DEVICE */
} else if(var == "OUTPUT_DEVICE") {
return Html_SelOpt_device_WebPage_system_output_add();
/* GPIO_INDEX */
} else if(var == "GPIO_INDEX") {
return Html_SelectOpt_GPIOindex();
} else {
return String();
}
}
String Proc_WebPage_system_output_addEdit(const String& var) {
#ifndef DEBUG
Serial.print("DB [Webserver:system:output:addEdit(Proc)] var: ");
Serial.println(var);
#endif
if(TestHeaderFooter(var)) {
return AddHeaderFooter(var, 2);
} else if(Test_WebPage_system_SUBNAV(var)) {
return Proc_WebPage_system_SUBNAV(var, 1);
} else if(var == "ACTION") {
return String("Edit");
} else if(var == "OUTPUT_ID") {
// return the outputId we got from GET .../add?edit=ID
// dirty workaround to put this in a global variable
return String(tmpParam_editOutputId);
/* OUTPUT_TYPE */
} else if(var == "OUTPUT_TYPE") {
return Html_SelOpt_type_WebPage_system_output_add(config.system.output.type[tmpParam_editOutputId]);
/* OUTPUT_DEVICE */
} else if(var == "OUTPUT_DEVICE") {
return Html_SelOpt_device_WebPage_system_output_add(config.system.output.device[tmpParam_editOutputId]);
/* GPIO_INDEX */
} else if(var == "GPIO_INDEX") {
return Html_SelectOpt_GPIOindex(config.system.output.gpio[tmpParam_editOutputId]);
} else {
return String();
}
}
String Proc_WebPage_system_output_add_POST(const String& var) {
if(var == "SAVE_MSG") {
return String(Common_HTML_SAVE_MSG);
@ -571,8 +662,24 @@ void WebPage_system_output_add(AsyncWebServerRequest *request) {
SaveConfig();
request->send_P(200, "text/html", Page_system_output_add_HTML, Proc_WebPage_system_output_add_POST);
// request->send_P(200, "text/html", Page_system_output_add_HTML, Proc_WebPage_system_output_add_POST);
// I like it more when user gets redirected to the output overview after saving
request->redirect("/system/output/?success");
} else {
if(request->hasParam("edit")) {
const AsyncWebParameter* p_edit = request->getParam("edit");
Serial.println("DB [Webserver:system:output:add?edit] ");
//Serial.printf(":: [Webserver:system] POST[%s]: %s\n", p_edit->name().c_str(), p_edit->value().c_str());
tmpParam_editOutputId = p_edit->value().toInt();
request->send_P(200, "text/html", Page_system_output_add_HTML, Proc_WebPage_system_output_addEdit);
} else {
request->send_P(200, "text/html", Page_system_output_add_HTML, Proc_WebPage_system_output_add);
}
}
}

View file

@ -30,8 +30,8 @@
const char* Page_system_HTML PROGMEM = R"(%HEADER%
%SAVE_MSG%
%SUBNAV%
%SAVE_MSG%
<p>here you can set which features and sensors you use<br></p><form method='post' action='/system/'>
<u>NTP offset</u>:<br>
@ -157,10 +157,10 @@ const char* Page_system_output_HTML PROGMEM = R"(%HEADER%
*/
const char* Page_system_output_add_HTML PROGMEM = R"(%HEADER%
%SUBNAV%
<h3>&#10133; Add output ID %OUTPUT_ID%</h3>
<h3>&#10133; %ACTION% output ID %OUTPUT_ID%</h3>
%SAVE_MSG%
<p>Add a new output to CanGrow.</p>
<p>%ACTION% CanGrow output.</p>
<form method='post' action='/system/output/add'>
<input type='hidden' name='outputId' value='%OUTPUT_ID%' />
<u>Type</u>:<br>