add output add dialog, try to use chunked response, but not working atm

This commit is contained in:
Marcus 2024-10-26 03:39:36 +02:00
parent 9c51386be9
commit f7c4739f0d
4 changed files with 136 additions and 2 deletions

View file

@ -68,6 +68,7 @@ const byte BOOTFAILS_HIGH = 2;
const byte FLASHMODE_LOW = 3;
const byte INPUT_ONLY = 4;
const byte NO_PWM = 5;
const byte HIGH_BOOT = 6;
struct GPIO_Index {
const byte gpio;

View file

@ -204,6 +204,10 @@ input[type=text], input[type=date], input[type=number], input[type=password], se
border-radius: 3px;
}
.hidden {
display: none;
}
@media only screen and (min-width: 1820px) {
/*.center, .nav {
width: 60; min-width: 420px;

View file

@ -359,6 +359,31 @@ String Proc_WebPage_system_output_add(const String& var) {
return AddHeaderFooter(var, 2);
} else if(Test_WebPage_system_SUBNAV(var)) {
return Proc_WebPage_system_SUBNAV(var, 1);
} else if(var == "GPIO_INDEX") {
String gpioIndex_html;
for(byte i = 0; i < GPIOindex_length; i++) {
gpioIndex_html += "<option value='";
gpioIndex_html += i;
gpioIndex_html += "'>GPIO ";
gpioIndex_html += GPIOindex[i].gpio;
//add gpio note if there is some
if(GPIOindex[i].note > 0) {
switch(GPIOindex[i].note) {
case BOOTFAILS_LOW: gpioIndex_html += " BFL"; break;
case BOOTFAILS_HIGH: gpioIndex_html += " BFH"; break;
case FLASHMODE_LOW: gpioIndex_html += " FML"; break;
case INPUT_ONLY: gpioIndex_html += " INO"; break;
case NO_PWM: gpioIndex_html += " NPWM"; break;
case HIGH_BOOT: gpioIndex_html += " HB"; break;
default: break;
}
}
gpioIndex_html += "</option>";
}
return String(Common_HTML_SAVE_MSG);
} else{
return String();
}
@ -372,14 +397,41 @@ String Proc_WebPage_system_output_add_POST(const String& var) {
}
}
// https://github.com/mathieucarbou/ESPAsyncWebServer?tab=readme-ov-file#chunked-response-containing-templates
auto Chunk_system_output_add_HTML(uint8_t *buffer, size_t maxLen, size_t alreadySent) -> size_t {
//Write up to "maxLen" bytes into "buffer" and return the amount written.
//index equals the amount of bytes that have been already sent
//You will be asked for more data until 0 is returned
//Keep in mind that you can not delay or yield waiting for more data!
// https://forum.arduino.cc/t/espasyncwebserver-replay-page-size/1049106/19
//const int len_html = sizeof(Page_system_output_add_HTML)/sizeof(Page_system_output_add_HTML[0]);
//size_t len = min(maxLen, String(Page_system_output_add_HTML).length() - index);
//Serial.printf("Sending %u bytes\n", len);
//memcpy(buffer, String(Page_system_output_add_HTML).c_str() + index, len);
//return len;
const size_t size = sizeof Page_system_output_add_HTML;
if (size - alreadySent >= maxLen) {
memcpy(buffer, Page_system_output_add_HTML + alreadySent, maxLen);
return maxLen;
} else { // last chunk and then 0
memcpy(buffer, Page_system_output_add_HTML + alreadySent, size - alreadySent);
return size - alreadySent;
}
}
void WebPage_system_output_add(AsyncWebServerRequest *request) {
if(request->method() == HTTP_POST) {
request->send_P(200, "text/html", Page_system_output_add_HTML, Proc_WebPage_system_output_add_POST);
AsyncWebServerResponse *response = request->beginChunkedResponse("text/plain", Chunk_system_output_add_HTML);
request->send(response);
Serial.println(":: [Webserver:system:output:add] [POST] hello");
} else {
request->send_P(200, "text/html", Page_system_output_add_HTML, Proc_WebPage_system_output_add);
request->beginChunkedResponse("text/html", Chunk_system_output_add_HTML);
}
}

View file

@ -156,4 +156,81 @@ const char* Page_system_output_HTML PROGMEM = R"(%HEADER%
const char* Page_system_output_add_HTML PROGMEM = R"(%HEADER%
%SUBNAV%
<h3>&#10133; Add output</h3>
<p>Add a new output to CanGrow.</p>
<form method='post' action='/system/output/add'>
<u>Type</u>:<br>
<select id='type_sel' name='type' onchange="showSelect('type_sel', 'type_', 'hidden');" required>
<option disabled value='' selected hidden>---</option>
<option value='1'>GPIO</option>
<option value='2'>I2C</option>
<option value='3'>URL</option>
</select><br>
<u>Device</u>:<br>
<select name='device' required>
<option disabled value='' selected hidden>---</option>
<option value='1'>Light</option>
<option value='2'>Fan</option>
<option value='3'>Pump</option>
<option value='3'>Humidifier</option>
<option value='3'>Dehumidifier</option>
<option value='3'>Heating</option>
</select><br>
<u>Name</u>:<br>
<input type='text' name='name' maxlength='16' value='' ><br>
<u>Enable</u>:<br>
<select name='enable' required>
<option disabled value='' selected hidden>---</option>
<option value='1'>Yes</option>
<option value='0'>No</option>
</select><br>
<div class='hidden' id='type_1'>
<u>GPIO</u>:<br>
<select name='gpio' required>
<option disabled value='' selected hidden>---</option>
%GPIO_INDEX%
<option value='0'>GPIO 0 !BFL!</option>
<option value='1'>GPIO 12</option>
<option value='2'>GPIO 13</option>
<option value='2'>GPIO 14</option>
<option value='2'>GPIO 15 !BFH!</option>
<option value='2'>GPIO 16 !NOPWM!</option>
</select><br>
<u>GPIO PWM</u>:<br>
<select name='gpio_pwm' required>
<option disabled value='' selected hidden>---</option>
<option value='1'>Enable</option>
<option value='2'>Disable</option>
</select><br>
</div>
<div class='hidden' id='type_2'>
<u>I2C</u>:<br>
<input type='text' name='i2c' maxlength='16' value='' ><br>
</div>
<div class='hidden' id='type_3'>
<u>Host</u>:<br>
<input type='text' name='host' maxlength='16' value='' ><br>
<u>URI on</u>:<br>
<input type='text' name='req_on' maxlength='16' value='' ><br>
<u>URI off</u>:<br>
<input type='text' name='req_off' maxlength='16' value='' ><br>
</div>
<br>
<input type='submit' value='&#x1F4BE; Save settings'>
</form>
%FOOTER%)";