From d482648ecfe233a485ac9e8b92c9bf6add6c7132 Mon Sep 17 00:00:00 2001 From: fvanroie <15969459+fvanroie@users.noreply.github.com> Date: Tue, 9 Nov 2021 07:29:56 +0100 Subject: [PATCH] Convert HTTP Settings and HASP Design forms to CSS #246 --- data/style.css | 2 +- data/style.css.gz | Bin 1090 -> 1101 bytes src/sys/svc/hasp_http.cpp | 621 ++++++++++++++++++++------------------ 3 files changed, 327 insertions(+), 296 deletions(-) diff --git a/data/style.css b/data/style.css index c694b57d..d679ee4f 100644 --- a/data/style.css +++ b/data/style.css @@ -1 +1 @@ -:root{--star:url('data:image/svg+xml,')}.c,body{text-align:center}.clear{clear:both}.foot{background-color:transparent!important;all:revert;float:right;margin:0;font-size:.6em}a.foot:link,a.foot:visited{color:var(--footfg)}input:not([type=file]){background-color:var(--fldbg);color:var(--fldfg)}input[type=checkbox],input[type=radio]{width:1em}select{background-color:var(--fldbg);color:var(--fldfg)}input:invalid{border:1px solid var(--redbg)}body{font-family:verdana;width:60%;margin:auto;background:var(--bg);color:var(--txt)}.red{background-color:var(--btnred)}.red:focus,.red:hover{background-color:var(--btnredhi)}#doc{text-align:left;display:inline-block;color:var(--txt);width:100%;min-width:320px;max-width:700px;display:table}td{font-size:.87rem;padding-bottom:0;padding-top:0}th{padding-top:.5em}*{box-sizing:border-box}a,input,select,textarea{width:100%;padding:12px;border:1px solid #ccc;border-radius:.6rem;resize:vertical}label{padding:12px 18px 12px 0;display:inline-block}.required{background-image:var(--star);background-size:1.5em 1.5em;background-position:right;background-repeat:no-repeat}a,button,input[type=submit]{padding:12px;border:1px solid var(--btnbrd);border-radius:.6rem;background-color:var(--btnbg);color:var(--btnfg);font-size:1rem;width:100%;margin:12px 0;display:flex;align-items:center;justify-content:center;text-align:center;text-decoration:none}a:hover,button:hover,input[type=submit]:hover{background-color:var(--btnbghi)}.container{background-color:var(--grpbg);color:var(--grpfg);border-radius:.6rem;padding:10px;margin:20px 0}.dist{display:flex;justify-content:space-between;width:100%}.dist>a{flex-grow:1;margin:0 6px}.dist>a:first-child{margin-left:0}.dist>a:last-child{margin-right:0}.col-25{float:left;width:25%;text-align:right;margin:0}.col-75{margin:0;float:left;width:75%}.row:after{content:"";display:table;clear:both}.row{font-size:.833rem;margin:5px}.gap,.row:last-of-type{margin:5px 5px 20px}.error,.info,.success,.validation,.warning{border:1px solid;margin:10px auto;padding:15px 10px 15px 50px;background-repeat:no-repeat;background-position:10px center;max-width:460px}.info{color:#00529b;background-color:#bde5f8}.success{color:#4f8a10;background-color:#dff2bf}.warning{color:#9f6000;background-color:#feefb3}.error{color:#d8000c;background-color:#ffd2d2}@media screen and (max-width:800px){.col-25,.col-75,input[type=submit]{width:100%;margin-top:0}.col-25{text-align:left}label{padding:5px 18px 5px 0}} \ No newline at end of file +:root{--star:url('data:image/svg+xml,')}a.foot:link,a.foot:visited{color:var(--footfg)}input:not([type=file]){background-color:var(--fldbg);color:var(--fldfg)}input[type=checkbox],input[type=radio]{width:1em}select{background-color:var(--fldbg);color:var(--fldfg)}input:invalid{border:1px solid var(--fldred)}body{font-family:verdana;margin:auto;background:var(--bg);color:var(--txt)}.c,body{text-align:center}.clear{clear:both}.foot{background-color:transparent!important;all:revert;float:right;margin:0;font-size:.6em}.red{background-color:var(--btnred)}.red:focus,.red:hover{background-color:var(--btnredhi)}#doc{text-align:left;margin:0 auto;display:inline-block;color:var(--txt);width:80%;min-width:300px;max-width:600px;display:table}td{font-size:.87rem;padding-bottom:0;padding-top:0}th{padding-top:.5em}*{box-sizing:border-box}a,input,select,textarea{width:100%;padding:12px;border:1px solid #ccc;border-radius:.6rem;resize:vertical}label{padding:12px 18px 12px 0;display:inline-block}.required{background-image:var(--star);background-size:1.5em 1.5em;background-position:right;background-repeat:no-repeat}a,button,input[type=submit]{padding:12px;border:1px solid var(--btnbrd);border-radius:.6rem;background-color:var(--btnbg);color:var(--btnfg);font-size:1rem;width:100%;margin:12px 0;display:flex;align-items:center;justify-content:center;text-align:center;text-decoration:none}a:hover,button:hover,input[type=submit]:hover{background-color:var(--btnbghi)}.container{background-color:var(--grpbg);color:var(--grpfg);border-radius:.6rem;padding:10px;margin:20px 0}.dist{display:flex;justify-content:space-between;width:100%}.dist > a{flex-grow:1;margin:0 6px}.dist>a:first-child{margin-left:0}.dist>a:last-child{margin-right:0}.col-25{float:left;width:25%;text-align:right;margin:0}.col-75{margin:0;float:left;width:75%}.row:after{content:"";display:table;clear:both}.row{font-size:.833rem;margin:5px}.gap,.row:last-of-type{margin:5px 5px 20px}.error,.info,.success,.validation,.warning{border:1px solid;margin:10px auto;padding:15px 10px 15px 50px;background-repeat:no-repeat;background-position:10px center;max-width:500px}.info{color:#00529B;background-color:#BDE5F8}.success{color:#4F8A10;background-color:#DFF2BF}.warning{color:#9F6000;background-color:#FEEFB3}.error{color:#D8000C;background-color:#FFD2D2}@media screen and (max-width:800px){.col-25,.col-75,input[type=submit]{width:100%;margin-top:0}.col-25{text-align:left}label{padding:5px 18px 5px 0}} \ No newline at end of file diff --git a/data/style.css.gz b/data/style.css.gz index e481c60ab785fb73caad5759cc2e3243995eecf3..9a9fe282e860f6cf7e46be5be68509cf20a359bb 100644 GIT binary patch literal 1101 zcmV-T1hV@diwFpN`iWrz0CRMCY-KKEb8`TtR&A4$FcAJLSjX|IzyTGl!MoA(?4NM% zLx4bILxLgnMTYOV1Pcomyt-R#l`7Hg?cs264=Y#5{5J4BPpdAhJtc<+alBhNzGpe_Enj~Y_n#Q$ z#5(yPUoEUtaJPV;N=dOJcMD0DpbQ%UYJ%E;5QiE*;a zrKox8{9(n?IA2C*qR~(1BH5B;7mMSQJrhctQt{LrXsWizCq-KkMiTXp2O-M$n9;O} zg-nS=etEPi0Rn5(1<;mlEYeex39g(B7nGgQp2!q)92HpRlp|a#5ltcVC!L?Hj%wMu ziEX?oa#RkSopY2Bu84#;j9}TACyIsIwg&dg)JzFWEWzvrElMF3<|@LBAxR)G6=h6d zg(S_lYKYK_O!yW3MUeX*aC0HCzo8Z@uG2O;C=*Ft*=D&F;Qo1|EnT)RQ<2Q`VLqO-@n)U(VxOGBdtoCS+?sB zk`z&i)0Fbu0brFVkT(jHD3RByZ8I0RAwcw|0rqJpAVnQR9FVlQBc$C?&(@hjG4Mc2 z58~~8AwPiNF39yVNs?abXhPQ&)IY5>EpzY@0a>mvUCSRv)ndkgnDWK=6;2oRhZzL%R)-B(!0G7B0Z27!ht2!>IdTO4> zV4)8$oQ{?56v$tDrB_l)hnwx3Ef5b+GcjFDqx=;}~KimGqQ=to^Cnw{WOu0S$~ zXY8I!Qj!RX4PIPu(&DaVdSLp^B|OhJmPu0gm8*lml>c2dm*t4ZB+BxupiU%(4jNzy zJPk|h0xqhVgL8odOrQifN7R83J_ES3weBt4Xgdf%9FRZt|NHXTsoZ0fQCTS`*;1A^ zor0r>1@cA>hR+o=5se09=>%cZ4Lfbt2N8s?=JlD6!NKf9*o<@W!u~@DIq0BqrU2?8 z-^Jq3DHBbD+Nl3Aa#kwhjpPPDX3$PG*6xkitU_2l*UVQ(fHCjsJ`Lc|(?OeP`+o*6 zJueJCK3q2K%ZJBr;bz?qNe%k%HtR3GcSZkkvk4wH?U-*b{-wyHz|kuA(q>t@Pst+QJ;J=yG@J=aFu#fa%|mfrOILOh%8 T;V{}m(|_%6)k^jFtqK4De6k@V literal 1090 zcmV-I1ikwoiwFoDExd_zS8+)6a;})T{%ZiPoLHKdF6$H<87VL{;fWK5L{B{ z5`uiQaxT&H3Vtf1)qy^*G$kv?Xe=wHbOC#1u%QoOyavNZYxz(1PycD}y<1*j!~6Jp z-TIlEDspKIJsS@To=cQbX{d%1L9uSSFG`i!w?4DTrZ_v~TGf(xnG#B)?x_-MFnhs^ zQfY&wNw5$|(<9X;VM1YpG|%@YDX`ADL_xxoG+xDjP~>lE(cxXF1>!ciGuPBA)bf*QvmzvLW5I5TSJ-!gLY4v z7DTSql6Z()p%=jN3~TgG`i$lT^Y_U@TxQ*8N~}i zFC~O#O5+YMsU&T2UnK)D!{!#Azt&Qk+fwqa6PU~2tLD0#@R&s`zYFS2QmCL2mME~W zv_9Zsnk6_lNI+N5fOFKGD3uFmyRkv*~YU4afdRfmNkiEA9GRGUfVJon^Fwj@ylf zj#;jM!dgP!ZU)6fZJR^e{pSqZ>pO9`iedGqQoh>&I`e_-bNhX_wN7{54zu`W5X8~@ z^tNPQri8|9*N#Dr`tR5dhru0v!dR5Dc8Yfpzh~PZxTnu3W$9)>aMUI{P|ogYGZK-g z{ZvrGu~TIlXzXA~oQFBOogFUgW+0n8u;*TbyJ;}PTp3KSC*W0QkH^Lych%Sa1O}{< IWTy%M02Ce?x&QzG diff --git a/src/sys/svc/hasp_http.cpp b/src/sys/svc/hasp_http.cpp index 90ea27ee..d255652b 100644 --- a/src/sys/svc/hasp_http.cpp +++ b/src/sys/svc/hasp_http.cpp @@ -197,7 +197,7 @@ static String getContentType(const String& path) //////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleHaspConfig(); +static void webHandleHaspConfig(); //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -227,7 +227,7 @@ bool httpIsAuthenticated(const __FlashStringHelper* notused) return true; } -void webSendFooter() +static void webSendFooter() { #if defined(STM32F4xx) webServer.sendContent(HTTP_FOOTER); @@ -240,7 +240,7 @@ void webSendFooter() #endif } -void webSendCacheHeader(int size, int age) +static void webSendCacheHeader(int size, int age) { webServer.sendHeader(F("Content-Length"), (String)(size)); webServer.sendHeader(F("Cache-Control"), F("max-age=3600, public")); @@ -257,7 +257,7 @@ static int webSendCached(int statuscode, const char* contenttype, const char* da return statuscode; } -void webSendPage(const char* nodename, uint32_t httpdatalength, bool gohome = false) +static void webSendHeader(const char* nodename, uint32_t httpdatalength, bool gohome = false) { { char buffer[64]; @@ -342,7 +342,7 @@ void saveConfig() } } -void webHandleRoot() +static void webHandleRoot() { if(!httpIsAuthenticated(F("root"))) return; @@ -373,18 +373,18 @@ void webHandleRoot() httpMessage += F("" D_HTTP_REBOOT ""); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } webSendFooter(); } //////////////////////////////////////////////////////////////////////////////////////////////////// -void httpHandleReboot() +static void httpHandleReboot() { // http://plate01/reboot if(!httpIsAuthenticated(F("reboot"))) return; - { + { // Send Content String httpMessage((char*)0); httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage += F("

"); @@ -392,41 +392,44 @@ void httpHandleReboot() httpMessage += F("


"); httpMessage = F(D_DISPATCH_REBOOT); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), true); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), true); webServer.sendContent(httpMessage); } - // httpMessage.clear(); webSendFooter(); - delay(200); - dispatch_reboot(true); + { // Execute Actions + // delay(200); + dispatch_reboot(true); + } } //////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleScreenshot() +static void webHandleScreenshot() { // http://plate01/screenshot if(!httpIsAuthenticated(F("screenshot"))) return; - if(webServer.hasArg(F("a"))) { - if(webServer.arg(F("a")) == F("next")) { - dispatch_page_next(LV_SCR_LOAD_ANIM_NONE); - } else if(webServer.arg(F("a")) == F("prev")) { - dispatch_page_prev(LV_SCR_LOAD_ANIM_NONE); - } else if(webServer.arg(F("a")) == F("back")) { - dispatch_page_back(LV_SCR_LOAD_ANIM_NONE); + { // Execute actions + if(webServer.hasArg(F("a"))) { + if(webServer.arg(F("a")) == F("next")) { + dispatch_page_next(LV_SCR_LOAD_ANIM_NONE); + } else if(webServer.arg(F("a")) == F("prev")) { + dispatch_page_prev(LV_SCR_LOAD_ANIM_NONE); + } else if(webServer.arg(F("a")) == F("back")) { + dispatch_page_back(LV_SCR_LOAD_ANIM_NONE); + } + } + + if(webServer.hasArg(F("q"))) { + lv_disp_t* disp = lv_disp_get_default(); + webServer.setContentLength(122 + disp->driver.hor_res * disp->driver.ver_res * sizeof(lv_color_t)); + webServer.send_P(200, PSTR("image/bmp"), ""); + guiTakeScreenshot(); + webServer.client().stop(); + return; } } - if(webServer.hasArg(F("q"))) { - lv_disp_t* disp = lv_disp_get_default(); - webServer.setContentLength(122 + disp->driver.hor_res * disp->driver.ver_res * sizeof(lv_color_t)); - webServer.send_P(200, PSTR("image/bmp"), ""); - guiTakeScreenshot(); - webServer.client().stop(); - return; - } - - { + { // Send Content String httpMessage((char*)0); httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage += F("

"); @@ -441,19 +444,18 @@ void webHandleScreenshot() httpMessage += F("" D_HTTP_NEXT_PAGE ""); httpMessage += FPSTR(MAIN_MENU_BUTTON); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } webSendFooter(); } //////////////////////////////////////////////////////////////////////////////////////////////////// - -void webHandleAbout() +static void webHandleAbout() { // http://plate01/about if(!httpIsAuthenticated(F("about"))) return; - { + { // Send Content String mitLicense((char*)0); mitLicense = FPSTR(MIT_LICENSE); @@ -496,16 +498,14 @@ void webHandleAbout() httpMessage += FPSTR(MAIN_MENU_BUTTON); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } - // httpMessage.clear(); webSendFooter(); } //////////////////////////////////////////////////////////////////////////////////////////////////// - -void add_json(String& data, JsonDocument& doc) +static void add_json(String& data, JsonDocument& doc) { char buffer[512]; size_t len = serializeJson(doc, buffer, sizeof(buffer)); @@ -517,62 +517,59 @@ void add_json(String& data, JsonDocument& doc) doc.clear(); } -void webHandleInfoJson() +static void webHandleInfoJson() { // http://plate01/ if(!httpIsAuthenticated(F("infojson"))) return; - String htmldata((char*)0); - htmldata.reserve(HTTP_PAGE_SIZE); - DynamicJsonDocument doc(512); + { // Send Content + String htmldata((char*)0); + htmldata.reserve(HTTP_PAGE_SIZE); + DynamicJsonDocument doc(512); - htmldata += F("

"); - htmldata += haspDevice.get_hostname(); - htmldata += F("


"); + htmldata += F("

"); + htmldata += haspDevice.get_hostname(); + htmldata += F("


"); - htmldata += "
"; + htmldata += "'; data = JSON.parse(data); var table = \"\"; for(let header in data) { " + "table += `
${header}`;" + "for(let key in data[header]) { " + "table += `
${key}: ${data[header][key]}`;" + "}} table += \"
\"; " + "document.getElementById(\"info\").innerHTML = table;});"; - // String path = F(".html"); - // webServer.send(200, getContentType(path), htmldata); + htmldata += FPSTR(MAIN_MENU_BUTTON); - htmldata += FPSTR(MAIN_MENU_BUTTON); - - webSendPage(haspDevice.get_hostname(), htmldata.length(), false); - webServer.sendContent(htmldata); - - // htmldata.clear(); + webSendHeader(haspDevice.get_hostname(), htmldata.length(), false); + webServer.sendContent(htmldata); + } webSendFooter(); } //////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleInfo() +static void webHandleInfo() { // http://plate01/ if(!httpIsAuthenticated(F("info"))) return; - { + { // Send Content char size_buf[32]; String httpMessage((char*)0); httpMessage.reserve(HTTP_PAGE_SIZE); @@ -764,10 +761,9 @@ void webHandleInfo() httpMessage += FPSTR(MAIN_MENU_BUTTON); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } - // httpMessage.clear(); webSendFooter(); } @@ -798,7 +794,7 @@ void webHandleInfo() } */ static unsigned long htppLastLoopTime = 0; -void webUploadProgress() +static void webUploadProgress() { long t = webServer.header("Content-Length").toInt(); if(millis() - htppLastLoopTime >= 1250) { @@ -825,11 +821,11 @@ static inline void webUpdatePrintError() #endif } -void webUpdateReboot() +static void webUpdateReboot() { LOG_INFO(TAG_HTTP, F("Update Success: %u bytes received. Rebooting..."), upload->totalSize); - { + { // Send Content String httpMessage((char*)0); httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage += F("

"); @@ -837,17 +833,19 @@ void webUpdateReboot() httpMessage += F("


"); httpMessage += F("Upload complete. Rebooting device, please wait..."); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), true); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), true); webServer.sendContent(httpMessage); } - // httpMessage.clear(); webSendFooter(); - delay(250); - dispatch_reboot(true); // Save the current config + // Exectute Actions + { + // delay(250); + dispatch_reboot(true); // Save the current config + } } -void webHandleFirmwareUpload() +static void webHandleFirmwareUpload() { upload = &webServer.upload(); @@ -903,7 +901,7 @@ void webHandleFirmwareUpload() #endif #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 -int handleFileRead(String path) +static int handleFileRead(String path) { // if(!httpIsAuthenticated(F("fileread"))) return false; if(!httpIsAuthenticated()) return false; @@ -974,7 +972,7 @@ int handleFileRead(String path) return 404; // Not found } -void handleFileUpload() +static void handleFileUpload() { if(webServer.uri() != "/edit") { return; @@ -1020,7 +1018,7 @@ void handleFileUpload() } } -void handleFileDelete() +static void handleFileDelete() { if(!httpIsAuthenticated(F("filedelete"))) return; @@ -1043,7 +1041,7 @@ void handleFileDelete() // path.clear(); } -void handleFileCreate() +static void handleFileCreate() { if(!httpIsAuthenticated(F("filecreate"))) return; @@ -1083,7 +1081,7 @@ void handleFileCreate() webServer.send(200, PSTR("text/plain"), ""); } -void handleFileList() +static void handleFileList() { if(!httpIsAuthenticated(F("filelist"))) return; @@ -1154,7 +1152,7 @@ void handleFileList() //////////////////////////////////////////////////////////////////////////////////////////////////// #if HASP_USE_CONFIG > 0 -void webHandleConfig() +static void webHandleConfig() { // http://plate01/config if(!httpIsAuthenticated(F("config"))) return; @@ -1191,23 +1189,22 @@ void webHandleConfig() httpMessage += F("" D_HTTP_FACTORY_RESET ""); httpMessage += FPSTR(MAIN_MENU_BUTTON); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } - // httpMessage.clear(); webSendFooter(); } //////////////////////////////////////////////////////////////////////////////////////////////////// #if HASP_USE_MQTT > 0 -void webHandleMqttConfig() +static void webHandleMqttConfig() { // http://plate01/config/mqtt if(!httpIsAuthenticated(F("config/mqtt"))) return; - StaticJsonDocument<256> settings; - mqttGetConfig(settings.to()); + { // Send Content + StaticJsonDocument<256> settings; + mqttGetConfig(settings.to()); - { String httpMessage((char*)0); httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage += F("

"); @@ -1268,20 +1265,19 @@ void webHandleMqttConfig() httpMessage += F(""); add_form_button(httpMessage, F(D_BACK_ICON D_HTTP_CONFIGURATION), F("/config")); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } - // httpMessage.clear(); webSendFooter(); } #endif //////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleGuiConfig() +static void webHandleGuiConfig() { // http://plate01/config/wifi if(!httpIsAuthenticated(F("config/gui"))) return; - { + { // Send Content StaticJsonDocument<256> settings; guiGetConfig(settings.to()); @@ -1365,111 +1361,106 @@ void webHandleGuiConfig() add_form_button(httpMessage, F(D_HTTP_ANTIBURN), F("/config/gui?brn=1")); add_form_button(httpMessage, F(D_BACK_ICON D_HTTP_CONFIGURATION), F("/config")); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } webSendFooter(); - if(webServer.hasArg(F("cal"))) dispatch_calibrate(NULL, NULL, TAG_HTTP); - if(webServer.hasArg(F("brn"))) dispatch_antiburn(NULL, "on", TAG_HTTP); + { // Execute Actions + if(webServer.hasArg(F("cal"))) dispatch_calibrate(NULL, NULL, TAG_HTTP); + if(webServer.hasArg(F("brn"))) dispatch_antiburn(NULL, "on", TAG_HTTP); + } } //////////////////////////////////////////////////////////////////////////////////////////////////// #if HASP_USE_WIFI > 0 -void webHandleWifiConfig() +static void webHandleWifiConfig() { // http://plate01/config/wifi if(!httpIsAuthenticated(F("config/wifi"))) return; - StaticJsonDocument<256> settings; - wifiGetConfig(settings.to()); + { // Send Content + StaticJsonDocument<256> settings; + wifiGetConfig(settings.to()); - String httpMessage((char*)0); - httpMessage.reserve(HTTP_PAGE_SIZE); - httpMessage += F("

"); - httpMessage += haspDevice.get_hostname(); - httpMessage += F("


"); - httpMessage += F("

" D_HTTP_WIFI_SETTINGS "

"); + String httpMessage((char*)0); + httpMessage.reserve(HTTP_PAGE_SIZE); + httpMessage += F("

"); + httpMessage += haspDevice.get_hostname(); + httpMessage += F("


"); + httpMessage += F("

" D_HTTP_WIFI_SETTINGS "

"); - // Form - httpMessage += F("
"); + // Form + httpMessage += F("
"); - // Wifi SSID - httpMessage += F("
"); - httpMessage += F("
"); + // Wifi SSID + httpMessage += F("
"); + httpMessage += F("
"); - // Wifi Password - httpMessage += F("
"); - httpMessage += F("
"); + // Wifi Password + httpMessage += F("
"); + httpMessage += F("
"); - // Submit & End Form - httpMessage += F(""); - httpMessage += F("
"); + // Submit & End Form + httpMessage += F(""); + httpMessage += F("
"); #if HASP_USE_WIFI > 0 && !defined(STM32F4xx) - if(WiFi.getMode() == WIFI_STA) { - add_form_button(httpMessage, F(D_BACK_ICON D_HTTP_CONFIGURATION), F("/config")); - } + if(WiFi.getMode() == WIFI_STA) { + add_form_button(httpMessage, F(D_BACK_ICON D_HTTP_CONFIGURATION), F("/config")); + } #endif - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); - webServer.sendContent(httpMessage); -#if defined(STM32F4xx) - httpMessage = ""; -#else - httpMessage.clear(); -#endif + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); + webServer.sendContent(httpMessage); + } webSendFooter(); } #if HASP_USE_CAPTIVE_PORTAL > 0 // I'm not an experienced programmer, this was the only way I managed to get it to work.. -void webHandleCaptivePortalWifiConfig() +static void webHandleCaptivePortalWifiConfig() { // http://plate01/config/wifi if(!httpIsAuthenticated(F("config/wifi"))) return; - StaticJsonDocument<256> settings; - wifiGetConfig(settings.to()); + { // Send Content + StaticJsonDocument<256> settings; + wifiGetConfig(settings.to()); - String httpMessage((char*)0); - httpMessage.reserve(HTTP_PAGE_SIZE); - httpMessage += F("

"); - httpMessage += haspDevice.get_hostname(); - httpMessage += F("


"); + String httpMessage((char*)0); + httpMessage.reserve(HTTP_PAGE_SIZE); + httpMessage += F("

"); + httpMessage += haspDevice.get_hostname(); + httpMessage += F("


"); - httpMessage += F("
"); - httpMessage += F("WiFi SSID (required)
WiFi Password (required)

"); + httpMessage += F("
"); + httpMessage += F("WiFi SSID (required)
WiFi Password (required)

"); #if HASP_USE_WIFI > 0 && !defined(STM32F4xx) - if(WiFi.getMode() == WIFI_STA) { - add_form_button(httpMessage, F(D_BACK_ICON D_HTTP_CONFIGURATION), F("/config")); - } + if(WiFi.getMode() == WIFI_STA) { + add_form_button(httpMessage, F(D_BACK_ICON D_HTTP_CONFIGURATION), F("/config")); + } #endif - // webServer.send(200, "text/html", httpMessage); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); - webServer.sendContent(httpMessage); -#if defined(STM32F4xx) - httpMessage = ""; -#else - httpMessage.clear(); -#endif + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); + webServer.sendContent(httpMessage); + } webSendFooter(); } #endif // HASP_USE_CAPTIVE_PORTAL @@ -1477,35 +1468,47 @@ void webHandleCaptivePortalWifiConfig() #endif // HASP_USE_WIFI //////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleHttpConfig() +static void webHandleHttpConfig() { // http://plate01/config/http if(!httpIsAuthenticated(F("config/http"))) return; - { + { // Send Content StaticJsonDocument<256> settings; httpGetConfig(settings.to()); - char httpMessage[HTTP_PAGE_SIZE]; + String httpMessage((char*)0); + httpMessage.reserve(HTTP_PAGE_SIZE); + httpMessage += F("

"); + httpMessage += haspDevice.get_hostname(); + httpMessage += F("


"); + httpMessage += F("

" D_HTTP_HTTP_SETTINGS "

"); - size_t len = snprintf_P( - httpMessage, sizeof(httpMessage), - PSTR("

%s


" - "
" - "Web Username (optional)" - "
" - "Web Password (optional)" - "" - "

" - "

"), - haspDevice.get_hostname(), settings[FPSTR(FP_CONFIG_USER)].as().c_str(), - settings[FPSTR(FP_CONFIG_PASS)].as().c_str()); + // Form + httpMessage += F("
"); - // if(settings[FPSTR(FP_CONFIG_PASS)].as() != "") { - // httpMessage += F(D_PASSWORD_MASK); - // } + // Username + httpMessage += F("
"); + httpMessage += F("
"); - webSendPage(haspDevice.get_hostname(), len, false); + // Password + httpMessage += F("
"); + httpMessage += F("
"); + + // Submit & End Form + httpMessage += F(""); + httpMessage += F("
"); + + httpMessage += F("" D_HTTP_CONFIGURATION ""); + + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } webSendFooter(); @@ -1513,30 +1516,29 @@ void webHandleHttpConfig() //////////////////////////////////////////////////////////////////////////////////////////////////// #if HASP_USE_GPIO > 0 -void webHandleGpioConfig() +static void webHandleGpioConfig() { // http://plate01/config/gpio if(!httpIsAuthenticated(F("config/gpio"))) return; uint8_t configCount = 0; - // StaticJsonDocument<256> settings; - // gpioGetConfig(settings.to()); - - if(webServer.hasArg(PSTR("save"))) { - uint8_t id = webServer.arg(F("id")).toInt(); - uint8_t pin = webServer.arg(F("pin")).toInt(); - uint8_t type = webServer.arg(F("type")).toInt(); - uint8_t group = webServer.arg(F("group")).toInt(); - uint8_t pinfunc = webServer.arg(F("func")).toInt(); - bool inverted = webServer.arg(F("state")).toInt(); - gpioSavePinConfig(id, pin, type, group, pinfunc, inverted); - } - if(webServer.hasArg(PSTR("del"))) { + { // Execute Actions uint8_t id = webServer.arg(F("id")).toInt(); uint8_t pin = webServer.arg(F("pin")).toInt(); - gpioSavePinConfig(id, pin, hasp_gpio_type_t::FREE, 0, 0, false); + + if(webServer.hasArg(PSTR("save"))) { + uint8_t type = webServer.arg(F("type")).toInt(); + uint8_t group = webServer.arg(F("group")).toInt(); + uint8_t pinfunc = webServer.arg(F("func")).toInt(); + bool inverted = webServer.arg(F("state")).toInt(); + gpioSavePinConfig(id, pin, type, group, pinfunc, inverted); + } + + if(webServer.hasArg(PSTR("del"))) { + gpioSavePinConfig(id, pin, hasp_gpio_type_t::FREE, 0, 0, false); + } } - { + { // Send Content String httpMessage((char*)0); httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage += F("

"); @@ -1693,19 +1695,18 @@ void webHandleGpioConfig() add_form_button(httpMessage, F(D_BACK_ICON D_HTTP_CONFIGURATION), F("/config")); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } - // httpMessage.clear(); webSendFooter(); } //////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleGpioOutput() +static void webHandleGpioOutput() { // http://plate01/config/gpio/options if(!httpIsAuthenticated(F("config/gpio/options"))) return; - { + { // Send Content StaticJsonDocument<256> settings; guiGetConfig(settings.to()); @@ -1775,7 +1776,7 @@ void webHandleGpioOutput() httpMessage += PSTR("

"); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } webSendFooter(); @@ -1784,10 +1785,11 @@ void webHandleGpioOutput() } //////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleGpioInput() +static void webHandleGpioInput() { // http://plate01/config/gpio/options if(!httpIsAuthenticated(F("config/gpio/input"))) return; - { + + { // Send Content StaticJsonDocument<256> settings; guiGetConfig(settings.to()); @@ -1865,7 +1867,7 @@ void webHandleGpioInput() httpMessage += PSTR("

"); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } webSendFooter(); @@ -1875,14 +1877,14 @@ void webHandleGpioInput() #endif // HASP_USE_GPIO //////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleDebugConfig() +static void webHandleDebugConfig() { // http://plate01/config/debug if(!httpIsAuthenticated(F("config/debug"))) return; - StaticJsonDocument<256> settings; - debugGetConfig(settings.to()); + { // Send Content + StaticJsonDocument<256> settings; + debugGetConfig(settings.to()); - { String httpMessage((char*)0); httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage += F("

"); @@ -1956,22 +1958,21 @@ void webHandleDebugConfig() add_form_button(httpMessage, F(D_BACK_ICON D_HTTP_CONFIGURATION), F("/config")); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } - // httpMessage.clear(); webSendFooter(); } //////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleHaspConfig() +static void webHandleHaspConfig() { // http://plate01/config/http if(!httpIsAuthenticated(F("config/hasp"))) return; - StaticJsonDocument<256> settings; - haspGetConfig(settings.to()); + { // Send Content + StaticJsonDocument<256> settings; + haspGetConfig(settings.to()); - { String httpMessage((char*)0); httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage += F("

"); @@ -1979,16 +1980,35 @@ void webHandleHaspConfig() httpMessage += F("


"); httpMessage += F("

" D_HTTP_HASP_DESIGN "

"); - httpMessage += F("

"); - httpMessage += F("


"); +#if defined(ARDUINO_ARCH_ESP8266) + // Form + httpMessage += F("
"); - // httpMessage += F(""); - httpMessage += F(""); - httpMessage += F("

UI Theme (required)

"); + // Destination + httpMessage += F("
"); + httpMessage += + F("
Firmware   " + "Filesystem
"); + + // Submit & End Form + httpMessage += F(""); + httpMessage += F(""); +#endif + + // Form + // httpMessage += F("
"); + httpMessage += F("
"); + + // Theme + httpMessage += F("
"); + httpMessage += F("

"); - httpMessage += F("Hue
"); + + // Hue + httpMessage += F("
"); + httpMessage += F("

"); - httpMessage += F("

Default Font"); #if defined(ARDUINO_ARCH_ESP32) File root = HASP_FS.open("/"); File file = root.openNextFile(); @@ -2036,39 +2061,44 @@ void webHandleHaspConfig() file.close(); } #endif - httpMessage += F("

"); - - httpMessage += F("

Startup Layout (optional)

"); + httpMessage += + F("

Startup Page (required)
"); + + // Startup Page + httpMessage += F("
"); + httpMessage += F("

Startup Brightness (required)

"); + + // Startup Brightness + httpMessage += F("
"); + httpMessage += F("

"); + httpMessage += F("'>
"); - httpMessage += - F("

"); + // Submit & End Form + httpMessage += F(""); + httpMessage += F("
"); - // httpMessage += - // F("

"); httpMessage += FPSTR(MAIN_MENU_BUTTON); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } - // httpMessage.clear(); webSendFooter(); } #endif // HASP_USE_CONFIG //////////////////////////////////////////////////////////////////////////////////////////////////// -void httpHandleNotFound() +static void httpHandleNotFound() { // webServer 404 #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 int statuscode = handleFileRead(webServer.uri()); @@ -2108,11 +2138,11 @@ void httpHandleNotFound() } //////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleFirmware() +static void webHandleFirmware() { if(!httpIsAuthenticated(F("firmware"))) return; - { + { // Send Content String httpMessage((char*)0); httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage += F("

"); @@ -2152,21 +2182,19 @@ void webHandleFirmware() httpMessage += FPSTR(MAIN_MENU_BUTTON); - webSendPage(haspDevice.get_hostname(), httpMessage.length(), false); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), false); webServer.sendContent(httpMessage); } - // httpMessage.clear(); webSendFooter(); } //////////////////////////////////////////////////////////////////////////////////////////////////// -void httpHandleEspFirmware() +static void httpHandleEspFirmware() { // http://plate01/espfirmware - char url[4]; - memcpy_P(url, PSTR("url"), 4); - if(!httpIsAuthenticated(F("espfirmware"))) return; + if(!webServer.hasArg(F("url"))) return; + const char* url = webServer.arg(F("url")).c_str(); { String httpMessage((char*)0); httpMessage.reserve(HTTP_PAGE_SIZE); @@ -2175,34 +2203,34 @@ void httpHandleEspFirmware() httpMessage += F("


"); httpMessage += F("

ESP update

Updating ESP firmware from: "); - httpMessage += webServer.arg(url); + httpMessage += url; - webSendPage(haspDevice.get_hostname(), httpMessage.length(), true); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), true); webServer.sendContent(httpMessage); - // httpMessage.clear(); } webSendFooter(); - LOG_TRACE(TAG_HTTP, F("Updating ESP firmware from: %s"), webServer.arg(url).c_str()); - dispatch_web_update(NULL, webServer.arg(url).c_str(), TAG_HTTP); + LOG_TRACE(TAG_HTTP, F("Updating ESP firmware from: %s"), url); + dispatch_web_update(NULL, url, TAG_HTTP); } //////////////////////////////////////////////////////////////////////////////////////////////////// #if HASP_USE_CONFIG > 0 -void webHandleSaveConfig() +static void webHandleSaveConfig() { if(!httpIsAuthenticated(F("saveConfig"))) return; + configWrite(); } //////////////////////////////////////////////////////////////////////////////////////////////////// -void httpHandleResetConfig() +static void httpHandleResetConfig() { // http://plate01/resetConfig if(!httpIsAuthenticated(F("resetConfig"))) return; bool resetConfirmed = webServer.arg(F("confirm")) == F("yes"); - { + { // Send Content String httpMessage((char*)0); httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage += F("

"); @@ -2233,14 +2261,16 @@ void httpHandleResetConfig() add_form_button(httpMessage, F(D_BACK_ICON D_HTTP_CONFIGURATION), F("/config")); } - webSendPage(haspDevice.get_hostname(), httpMessage.length(), resetConfirmed); + webSendHeader(haspDevice.get_hostname(), httpMessage.length(), resetConfirmed); webServer.sendContent(httpMessage); } webSendFooter(); - if(resetConfirmed) { - delay(250); - dispatch_reboot(false); // Do NOT save the current config + { // Execute Actions + if(resetConfirmed) { + // delay(250); + dispatch_reboot(false); // Do NOT save the current config + } } } #endif // HASP_USE_CONFIG @@ -2277,7 +2307,7 @@ void httpStop() } // Do not keep CSS in memory because it is cached in the browser -void webSendCss() +static void webSendCssVars() { char filename[32]; strncpy(filename, webServer.uri().c_str(), sizeof(filename)); @@ -2312,7 +2342,7 @@ void webSendCss() } // Do not keep JS in memory because it is cached in the browser -void webSendJavascript() +static void webSendJavascript() { String javascript = F("function aref(t){setTimeout(function() {ref('');}, t*1000)}" "function ref(a){ var t=new " @@ -2321,27 +2351,40 @@ void webSendJavascript() } //////////////////////////////////////////////////////////////////////////////////////////////////// +static inline void webStartConfigPortal() +{ +#if HASP_USE_CAPTIVE_PORTAL > 0 + // if DNSServer is started with "*" for domain name, it will reply with + // provided IP to all DNS request + dnsServer.start(DNS_PORT, "*", apIP); + // replay to all requests with same HTML + webServer.onNotFound([]() { + webHandleCaptivePortalWifiConfig(); + // webServer.send(200, "text/html", responseHTML); + // webServer.on(F("/"), webHandleWifiConfig); + }); +#endif // HASP_USE_CAPTIVE_PORTAL + + webServer.on(F("/"), webHandleWifiConfig); + LOG_TRACE(TAG_HTTP, F("Wifi access point")); +} + void httpSetup() { // httpSetConfig(settings); // ask server to track these headers const char* headerkeys[] = {"Content-Length"}; // "Authentication" - - // const char HTTP_SCRIPT[] PROGMEM = ""; - size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*); + size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*); webServer.collectHeaders(headerkeys, headerkeyssize); - // Shared pages + // Shared pages between STA and AP webServer.on(F("/about"), webHandleAbout); - webServer.on(F("/vars.css"), webSendCss); + webServer.on(F("/vars.css"), webSendCssVars); webServer.on(F("/js"), webSendJavascript); webServer.onNotFound(httpHandleNotFound); #if HASP_USE_WIFI > 0 - // These two endpoints are needed in STA and AP mode webServer.on(F("/config"), webHandleConfig); @@ -2349,19 +2392,7 @@ void httpSetup() #if HASP_USE_CONFIG > 0 if(WiFi.getMode() != WIFI_STA) { -#if HASP_USE_CAPTIVE_PORTAL > 0 - // if DNSServer is started with "*" for domain name, it will reply with - // provided IP to all DNS request - dnsServer.start(DNS_PORT, "*", apIP); - // replay to all requests with same HTML - webServer.onNotFound([]() { - webHandleCaptivePortalWifiConfig(); - // webServer.send(200, "text/html", responseHTML); - // webServer.on(F("/"), webHandleWifiConfig); - }); -#endif - webServer.on(F("/"), webHandleWifiConfig); - LOG_TRACE(TAG_HTTP, F("Wifi access point")); + webStartConfigPortal(); return; }