Update xdrv_01_webserver.ino

Prep for chunks
This commit is contained in:
Theo Arends 2019-03-01 18:25:46 +01:00
parent 9e8ed1ae79
commit d70ed77f1a
1 changed files with 84 additions and 71 deletions

View File

@ -76,7 +76,7 @@ const char HTTP_SCRIPT_ROOT[] PROGMEM =
"a=p;"
"clearTimeout(lt);"
"}"
"if(x!=null){x.abort();}" // Abort if no response within 2 seconds (happens on restart 1)
"if(x!=null){x.abort();}" // Abort if no response within Settings.web_refresh milliseconds (happens on restart 1)
"x=new XMLHttpRequest();"
"x.onreadystatechange=function(){"
"if(x.readyState==4&&x.status==200){"
@ -84,15 +84,15 @@ const char HTTP_SCRIPT_ROOT[] PROGMEM =
"eb('l1').innerHTML=s;"
"}"
"};"
"x.open('GET','ay'+a,true);"
"x.open('GET','?m=1'+a,true);" // ?m related to WebServer->hasArg("m")
"x.send();"
"lt=setTimeout(la,{a});" // Settings.web_refresh
"}"
"function lb(p){"
"la('?d='+p);" // ?d related to WebGetArg("d", tmp, sizeof(tmp));
"la('&d='+p);" // &d related to WebGetArg("d", tmp, sizeof(tmp));
"}"
"function lc(p){"
"la('?t='+p);" // ?t related to WebGetArg("t", tmp, sizeof(tmp));
"la('&t='+p);" // &t related to WebGetArg("t", tmp, sizeof(tmp));
"}"
"window.onload=la();";
@ -140,7 +140,7 @@ const char HTTP_SCRIPT_CONSOL[] PROGMEM =
"sn=t.scrollTop;"
"}"
"};"
"x.open('GET','ax?c2='+id+o,true);"
"x.open('GET','cs?c2='+id+o,true);" // Related to WebServer->hasArg("c2") and WebGetArg("c2", stmp, sizeof(stmp))
"x.send();"
"}"
"lt=setTimeout(l,{a});"
@ -431,7 +431,7 @@ const char HTTP_END[] PROGMEM =
"</body>"
"</html>";
const char HTTP_DEVICE_CONTROL[] PROGMEM = "<td style='width:%d%%'><button onclick='la(\"?o=%d\");'>%s%s</button></td>"; // ?o is related to WebGetArg("o", tmp, sizeof(tmp));
const char HTTP_DEVICE_CONTROL[] PROGMEM = "<td style='width:%d%%'><button onclick='la(\"&o=%d\");'>%s%s</button></td>"; // ?o is related to WebGetArg("o", tmp, sizeof(tmp));
const char HTTP_DEVICE_STATE[] PROGMEM = "%s<td style='width:%d{c}%s;font-size:%dpx'>%s</div></td>"; // {c} = %'><div style='text-align:center;font-weight:
enum CTypes { CT_HTML, CT_PLAIN, CT_XML, CT_JSON, CT_STREAM };
@ -504,8 +504,6 @@ void StartWebserver(int type, IPAddress ipweb)
WebServer->on("/u2", HTTP_POST, HandleUploadDone, HandleUploadLoop);
WebServer->on("/u2", HTTP_OPTIONS, HandlePreflightRequest);
WebServer->on("/cs", HandleConsole);
WebServer->on("/ax", HandleAjaxConsoleRefresh);
WebServer->on("/ay", HandleAjaxStatusRefresh);
WebServer->on("/cm", HandleHttpCommand);
#ifndef FIRMWARE_MINIMAL
WebServer->on("/cn", HandleConfiguration);
@ -601,9 +599,8 @@ bool WebAuthenticate(void)
{
if (Settings.web_password[0] != 0 && HTTP_MANAGER_RESET_ONLY != webserver_state) {
return WebServer->authenticate(WEB_USERNAME, Settings.web_password);
} else {
return true;
}
return true;
}
void ShowPage(String &page, bool auth)
@ -706,8 +703,6 @@ void HandleWifiLogin(void)
void HandleRoot(void)
{
AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_MAIN_MENU);
if (CaptivePortal()) { return; } // If captive portal redirect instead of displaying the page.
if (WebServer->hasArg("rstrt")) {
@ -728,7 +723,15 @@ void HandleRoot(void)
}
}
#endif // Not FIRMWARE_MINIMAL
} else {
return;
}
if (HandleRootStatusRefresh()) {
return;
}
AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_MAIN_MENU);
char stemp[5];
String page = FPSTR(HTTP_HEAD);
@ -774,7 +777,7 @@ void HandleRoot(void)
if (idx > 0) { page += F("</tr><tr>"); }
for (uint8_t j = 0; j < 4; j++) {
idx++;
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("<td style='width:25%%'><button onclick='la(\"?k=%d\");'>%d</button></td>"), idx, idx); // ?k is related to WebGetArg("k", tmp, sizeof(tmp));
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("<td style='width:25%%'><button onclick='la(\"&k=%d\");'>%d</button></td>"), idx, idx); // ?k is related to WebGetArg("k", tmp, sizeof(tmp));
page += mqtt_data;
}
}
@ -794,11 +797,17 @@ void HandleRoot(void)
}
ShowPage(page);
}
bool HandleRootStatusRefresh(void)
{
if (!WebAuthenticate()) {
WebServer->requestAuthentication();
return true;
}
void HandleAjaxStatusRefresh(void)
{
if (!WebAuthenticate()) { return WebServer->requestAuthentication(); }
if (!WebServer->hasArg("m")) { // Status refresh requested
return false;
}
char tmp[8]; // WebGetArg numbers only
char svalue[32]; // Command and number parameter
@ -864,6 +873,7 @@ void HandleAjaxStatusRefresh(void)
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s</tr></table>"), mqtt_data);
}
WSSend(200, CT_HTML, mqtt_data);
return true;
}
bool HttpCheckPriviledgedAccess(bool autorequestauth = true)
@ -2016,6 +2026,11 @@ void HandleConsole(void)
{
if (!HttpCheckPriviledgedAccess()) { return; }
if (WebServer->hasArg("c2")) { // Console refresh requested
HandleConsoleRefresh();
return;
}
AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONSOLE);
String page = FPSTR(HTTP_HEAD);
@ -2027,10 +2042,8 @@ void HandleConsole(void)
ShowPage(page);
}
void HandleAjaxConsoleRefresh(void)
void HandleConsoleRefresh(void)
{
if (!HttpCheckPriviledgedAccess()) { return; }
bool cflg = true;
uint8_t counter = 0; // Initial start, should never be 0 again