2018-09-30 20:24:57 +02:00
|
|
|
/*
|
|
|
|
* Server page definitions
|
|
|
|
*/
|
|
|
|
|
|
|
|
void initServer()
|
|
|
|
{
|
2019-02-14 17:25:41 +01:00
|
|
|
//CORS compatiblity
|
|
|
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
|
2019-03-06 21:31:12 +01:00
|
|
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Methods", "*");
|
|
|
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "*");
|
2019-02-14 17:25:41 +01:00
|
|
|
|
2018-09-30 20:24:57 +02:00
|
|
|
//settings page
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/settings", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveSettings(request);
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request){
|
2018-09-30 20:24:57 +02:00
|
|
|
if(!handleFileRead("/favicon.ico"))
|
|
|
|
{
|
2019-02-16 00:21:22 +01:00
|
|
|
request->send_P(200, "image/x-icon", favicon, 156);
|
2018-09-30 20:24:57 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/sliders", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveIndex(request);
|
|
|
|
});
|
2018-09-30 20:24:57 +02:00
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/welcome", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveSettings(request);
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/reset", HTTP_GET, [](AsyncWebServerRequest *request){
|
2019-02-20 23:44:34 +01:00
|
|
|
serveMessage(request, 200,"Rebooting now...","Please wait ~10 seconds...",129);
|
2019-02-17 17:11:10 +01:00
|
|
|
doReboot = true;
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/settings/wifi", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
|
|
if (!(wifiLock && otaLock)) handleSettingsSet(request, 1);
|
|
|
|
serveMessage(request, 200,"WiFi settings saved.","Rebooting now...",255);
|
2019-02-17 17:11:10 +01:00
|
|
|
doReboot = true;
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/settings/leds", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
|
|
handleSettingsSet(request, 2);
|
|
|
|
serveMessage(request, 200,"LED settings saved.","Redirecting...",1);
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/settings/ui", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
|
|
handleSettingsSet(request, 3);
|
|
|
|
serveMessage(request, 200,"UI settings saved.","Reloading to apply theme...",122);
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/settings/sync", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
|
|
handleSettingsSet(request, 4);
|
2019-02-18 22:34:21 +01:00
|
|
|
serveMessage(request, 200,"Sync settings saved.","Redirecting...",1);
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/settings/time", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
|
|
handleSettingsSet(request, 5);
|
|
|
|
serveMessage(request, 200,"Time settings saved.","Redirecting...",1);
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/settings/sec", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
|
|
handleSettingsSet(request, 6);
|
2019-03-07 23:22:52 +01:00
|
|
|
if (!doReboot) serveMessage(request, 200,"Security settings saved.","Rebooting now, please wait ~10 seconds...",129);
|
2019-02-17 17:11:10 +01:00
|
|
|
doReboot = true;
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
2019-02-10 23:05:06 +01:00
|
|
|
|
2019-03-06 01:20:38 +01:00
|
|
|
server.on("/json", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveJson(request);
|
|
|
|
});
|
2019-02-20 23:44:34 +01:00
|
|
|
|
2019-03-06 01:20:38 +01:00
|
|
|
AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/json", [](AsyncWebServerRequest *request, JsonVariant &json) {
|
|
|
|
JsonObject& root = json.as<JsonObject>();
|
|
|
|
if (!root.success()){request->send(500, "application/json", "{\"error\":\"Parsing failed\"}"); return;}
|
|
|
|
deserializeState(root);
|
|
|
|
request->send(200, "application/json", "{\"success\":true}");
|
|
|
|
});
|
|
|
|
server.addHandler(handler);
|
2019-02-21 16:32:15 +01:00
|
|
|
|
|
|
|
//*******DEPRECATED*******
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/version", HTTP_GET, [](AsyncWebServerRequest *request){
|
2019-02-14 17:25:41 +01:00
|
|
|
request->send(200, "text/plain", (String)VERSION);
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/uptime", HTTP_GET, [](AsyncWebServerRequest *request){
|
2019-02-14 17:25:41 +01:00
|
|
|
request->send(200, "text/plain", (String)millis());
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/freeheap", HTTP_GET, [](AsyncWebServerRequest *request){
|
2019-02-14 17:25:41 +01:00
|
|
|
request->send(200, "text/plain", (String)ESP.getFreeHeap());
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
2019-02-21 16:32:15 +01:00
|
|
|
//*******END*******/
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/u", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
request->send_P(200, "text/html", PAGE_usermod);
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/teapot", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveMessage(request, 418, "418. I'm a teapot.", "(Tangible Embedded Advanced Project Of Twinkling)", 254);
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
//if OTA is allowed
|
|
|
|
if (!otaLock){
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/edit", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
request->send(200, "text/html", PAGE_edit);
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
#ifdef USEFS
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/edit", HTTP_PUT, handleFileCreate);
|
|
|
|
server.on("/edit", HTTP_DELETE, handleFileDelete);
|
|
|
|
server.on("/edit", HTTP_POST, [](){ server->send(200, "text/plain", ""); }, handleFileUpload);
|
|
|
|
server.on("/list", HTTP_GET, handleFileList);
|
2018-09-30 20:24:57 +02:00
|
|
|
#endif
|
|
|
|
//init ota page
|
2018-11-01 15:36:13 +01:00
|
|
|
#ifndef WLED_DISABLE_OTA
|
2019-02-17 17:11:10 +01:00
|
|
|
server.on("/update", HTTP_GET, [](AsyncWebServerRequest *request){
|
2019-03-09 21:41:23 +01:00
|
|
|
olen = 0;
|
|
|
|
getCSSColors();
|
|
|
|
request->send_P(200, "text/html", PAGE_update, msgProcessor);
|
2019-02-17 17:11:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
server.on("/update", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
|
|
if (Update.hasError())
|
|
|
|
{
|
|
|
|
serveMessage(request, 500, "Failed updating firmware!", "Please check your file and retry!", 254); return;
|
|
|
|
}
|
2019-02-20 23:44:34 +01:00
|
|
|
serveMessage(request, 200, "Successfully updated firmware!", "Please wait while the module reboots...", 131);
|
2019-02-17 17:11:10 +01:00
|
|
|
doReboot = true;
|
|
|
|
},[](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
|
|
|
|
if(!index){
|
|
|
|
DEBUG_PRINTLN("OTA Update Start");
|
|
|
|
#ifndef ARDUINO_ARCH_ESP32
|
|
|
|
Update.runAsync(true);
|
|
|
|
#endif
|
|
|
|
Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000);
|
|
|
|
}
|
|
|
|
if(!Update.hasError()) Update.write(data, len);
|
|
|
|
if(final){
|
|
|
|
if(Update.end(true)){
|
|
|
|
DEBUG_PRINTLN("Update Success");
|
|
|
|
} else {
|
|
|
|
DEBUG_PRINTLN("Update Failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-11-01 15:36:13 +01:00
|
|
|
#else
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/update", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveMessage(request, 500, "Not implemented", "OTA updates are unsupported in this build.", 254);
|
2018-11-01 15:36:13 +01:00
|
|
|
});
|
|
|
|
#endif
|
2018-09-30 20:24:57 +02:00
|
|
|
} else
|
|
|
|
{
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/edit", HTTP_GET, [](AsyncWebServerRequest *request){
|
2019-03-05 10:59:15 +01:00
|
|
|
serveMessage(request, 500, "Access Denied", "Please unlock OTA in security settings!", 254);
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/update", HTTP_GET, [](AsyncWebServerRequest *request){
|
2019-03-05 10:59:15 +01:00
|
|
|
serveMessage(request, 500, "Access Denied", "Please unlock OTA in security settings!", 254);
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/list", HTTP_GET, [](AsyncWebServerRequest *request){
|
2019-03-05 10:59:15 +01:00
|
|
|
serveMessage(request, 500, "Access Denied", "Please unlock OTA in security settings!", 254);
|
2018-09-30 20:24:57 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveIndexOrWelcome(request);
|
|
|
|
});
|
2018-09-30 20:24:57 +02:00
|
|
|
|
|
|
|
//called when the url is not defined here, ajax-in; get-settings
|
2019-02-16 00:21:22 +01:00
|
|
|
server.onNotFound([](AsyncWebServerRequest *request){
|
2018-09-30 20:24:57 +02:00
|
|
|
DEBUG_PRINTLN("Not-Found HTTP call:");
|
2019-02-18 22:34:21 +01:00
|
|
|
DEBUG_PRINTLN("URI: " + request->url());
|
2018-09-30 20:24:57 +02:00
|
|
|
|
2019-01-18 01:20:36 +01:00
|
|
|
//make API CORS compatible
|
2019-02-16 00:21:22 +01:00
|
|
|
if (request->method() == HTTP_OPTIONS)
|
2019-01-18 01:20:36 +01:00
|
|
|
{
|
2019-02-16 00:21:22 +01:00
|
|
|
request->send(200); return;
|
2019-01-18 01:20:36 +01:00
|
|
|
}
|
2018-09-30 20:24:57 +02:00
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
if(!handleSet(request, request->url())){
|
2019-01-09 22:52:42 +01:00
|
|
|
#ifndef WLED_DISABLE_ALEXA
|
2019-02-16 00:21:22 +01:00
|
|
|
if(!espalexa.handleAlexaApiCall(request))
|
2019-01-09 22:52:42 +01:00
|
|
|
#endif
|
2019-02-16 00:21:22 +01:00
|
|
|
request->send(404, "text/plain", "Not Found");
|
2018-09-30 20:24:57 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
void serveIndexOrWelcome(AsyncWebServerRequest *request)
|
2018-09-30 20:24:57 +02:00
|
|
|
{
|
|
|
|
if (!showWelcomePage){
|
2019-02-16 00:21:22 +01:00
|
|
|
serveIndex(request);
|
|
|
|
} else {
|
|
|
|
serveSettings(request);
|
2018-09-30 20:24:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 19:59:00 +01:00
|
|
|
|
|
|
|
void getCSSColors()
|
|
|
|
{
|
|
|
|
char cs[6][9];
|
|
|
|
getThemeColors(cs);
|
|
|
|
oappend("<style>:root{--aCol:#"); oappend(cs[0]);
|
|
|
|
oappend(";--bCol:#"); oappend(cs[1]);
|
|
|
|
oappend(";--cCol:#"); oappend(cs[2]);
|
|
|
|
oappend(";--dCol:#"); oappend(cs[3]);
|
|
|
|
oappend(";--sCol:#"); oappend(cs[4]);
|
|
|
|
oappend(";--tCol:#"); oappend(cs[5]);
|
|
|
|
oappend(";--cFn:"); oappend(cssFont);
|
|
|
|
oappend(";}");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
void serveIndex(AsyncWebServerRequest* request)
|
2018-09-30 20:24:57 +02:00
|
|
|
{
|
2019-02-16 00:21:22 +01:00
|
|
|
bool serveMobile = false;
|
|
|
|
if (uiConfiguration == 0 && request->hasHeader("User-Agent")) serveMobile = checkClientIsMobile(request->getHeader("User-Agent")->value());
|
|
|
|
else if (uiConfiguration == 2) serveMobile = true;
|
|
|
|
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html",
|
2019-03-05 10:59:15 +01:00
|
|
|
(serveMobile) ? (uint8_t*)PAGE_indexM : PAGE_index,
|
2019-02-16 00:21:22 +01:00
|
|
|
(serveMobile) ? PAGE_indexM_L : PAGE_index_L);
|
2018-11-16 19:59:00 +01:00
|
|
|
|
|
|
|
//error message is not gzipped
|
|
|
|
#ifdef WLED_DISABLE_MOBILE_UI
|
2019-02-16 00:21:22 +01:00
|
|
|
if (!serveMobile) response->addHeader("Content-Encoding","gzip");
|
2018-11-16 19:59:00 +01:00
|
|
|
#else
|
2019-02-16 00:21:22 +01:00
|
|
|
response->addHeader("Content-Encoding","gzip");
|
2018-11-16 19:59:00 +01:00
|
|
|
#endif
|
2018-11-09 17:00:36 +01:00
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
request->send(response);
|
2018-09-30 20:24:57 +02:00
|
|
|
}
|
|
|
|
|
2018-11-16 19:59:00 +01:00
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
String msgProcessor(const String& var)
|
|
|
|
{
|
|
|
|
if (var == "CSS") return String(obuf);
|
|
|
|
if (var == "MSG") {
|
2019-03-09 21:41:23 +01:00
|
|
|
String messageBody = messageHead;
|
2019-02-16 00:21:22 +01:00
|
|
|
messageBody += "</h2>";
|
|
|
|
messageBody += messageSub;
|
2019-02-20 23:44:34 +01:00
|
|
|
uint32_t optt = optionType;
|
2019-02-16 00:21:22 +01:00
|
|
|
|
2019-02-20 23:44:34 +01:00
|
|
|
if (optt < 60) //redirect to settings after optionType seconds
|
2019-02-16 00:21:22 +01:00
|
|
|
{
|
2019-02-20 23:44:34 +01:00
|
|
|
messageBody += "<script>setTimeout(RS," + String(optt*1000) + ")</script>";
|
|
|
|
} else if (optt < 120) //redirect back after optionType-60 seconds, unused
|
2019-02-16 00:21:22 +01:00
|
|
|
{
|
2019-02-20 23:44:34 +01:00
|
|
|
//messageBody += "<script>setTimeout(B," + String((optt-60)*1000) + ")</script>";
|
|
|
|
} else if (optt < 180) //reload parent after optionType-120 seconds
|
2019-02-16 00:21:22 +01:00
|
|
|
{
|
2019-02-20 23:44:34 +01:00
|
|
|
messageBody += "<script>setTimeout(RP," + String((optt-120)*1000) + ")</script>";
|
|
|
|
} else if (optt == 253)
|
2019-02-16 00:21:22 +01:00
|
|
|
{
|
2019-02-17 17:11:10 +01:00
|
|
|
messageBody += "<br><br><form action=/settings><button class=\"bt\" type=submit>Back</button></form>"; //button to settings
|
2019-02-20 23:44:34 +01:00
|
|
|
} else if (optt == 254)
|
2019-02-16 00:21:22 +01:00
|
|
|
{
|
2019-02-17 17:11:10 +01:00
|
|
|
messageBody += "<br><br><button type=\"button\" class=\"bt\" onclick=\"B()\">Back</button>";
|
2019-02-16 00:21:22 +01:00
|
|
|
}
|
|
|
|
return messageBody;
|
|
|
|
}
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-20 23:44:34 +01:00
|
|
|
void serveMessage(AsyncWebServerRequest* request, uint16_t code, String headl, String subl="", byte optionT=255)
|
2018-09-30 20:24:57 +02:00
|
|
|
{
|
2019-03-11 19:30:49 +01:00
|
|
|
char buf[512];
|
|
|
|
obuf = buf;
|
2018-11-16 19:59:00 +01:00
|
|
|
olen = 0;
|
|
|
|
getCSSColors();
|
2019-02-16 00:21:22 +01:00
|
|
|
messageHead = headl;
|
|
|
|
messageSub = subl;
|
|
|
|
optionType = optionT;
|
2018-11-16 19:59:00 +01:00
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
request->send_P(code, "text/html", PAGE_msg, msgProcessor);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String settingsProcessor(const String& var)
|
|
|
|
{
|
2019-03-11 19:30:49 +01:00
|
|
|
if (var == "CSS") {
|
|
|
|
char* buf = getSettingsJS(optionType);
|
|
|
|
getCSSColors();
|
|
|
|
return buf;
|
|
|
|
}
|
2019-02-16 00:21:22 +01:00
|
|
|
if (var == "SCSS") return String(PAGE_settingsCss);
|
|
|
|
return String();
|
2018-09-30 20:24:57 +02:00
|
|
|
}
|
|
|
|
|
2018-11-16 19:59:00 +01:00
|
|
|
|
2019-02-16 00:21:22 +01:00
|
|
|
void serveSettings(AsyncWebServerRequest* request)
|
2018-09-30 20:24:57 +02:00
|
|
|
{
|
2019-02-16 00:21:22 +01:00
|
|
|
byte subPage = 0;
|
2019-03-06 01:20:38 +01:00
|
|
|
const String& url = request->url();
|
2019-02-16 00:21:22 +01:00
|
|
|
if (url.indexOf("sett") >= 0)
|
|
|
|
{
|
|
|
|
if (url.indexOf("wifi") > 0) subPage = 1;
|
|
|
|
else if (url.indexOf("leds") > 0) subPage = 2;
|
|
|
|
else if (url.indexOf("ui") > 0) subPage = 3;
|
|
|
|
else if (url.indexOf("sync") > 0) subPage = 4;
|
|
|
|
else if (url.indexOf("time") > 0) subPage = 5;
|
|
|
|
else if (url.indexOf("sec") > 0) subPage = 6;
|
|
|
|
} else subPage = 255; //welcome page
|
|
|
|
|
|
|
|
if (subPage == 1 && wifiLock && otaLock)
|
|
|
|
{
|
2019-03-05 10:59:15 +01:00
|
|
|
serveMessage(request, 500, "Access Denied", "Please unlock OTA in security settings!", 254); return;
|
2019-02-16 00:21:22 +01:00
|
|
|
}
|
2018-11-09 17:00:36 +01:00
|
|
|
|
|
|
|
#ifdef WLED_DISABLE_MOBILE_UI //disable welcome page if not enough storage
|
2019-02-16 00:21:22 +01:00
|
|
|
if (subPage == 255) {serveIndex(request); return;}
|
2018-11-09 17:00:36 +01:00
|
|
|
#endif
|
2018-11-16 19:59:00 +01:00
|
|
|
|
2019-03-11 19:30:49 +01:00
|
|
|
optionType = subPage;
|
2018-11-09 17:00:36 +01:00
|
|
|
|
|
|
|
switch (subPage)
|
|
|
|
{
|
2019-02-16 00:21:22 +01:00
|
|
|
case 1: request->send_P(200, "text/html", PAGE_settings_wifi, settingsProcessor); break;
|
|
|
|
case 2: request->send_P(200, "text/html", PAGE_settings_leds, settingsProcessor); break;
|
|
|
|
case 3: request->send_P(200, "text/html", PAGE_settings_ui , settingsProcessor); break;
|
|
|
|
case 4: request->send_P(200, "text/html", PAGE_settings_sync, settingsProcessor); break;
|
|
|
|
case 5: request->send_P(200, "text/html", PAGE_settings_time, settingsProcessor); break;
|
|
|
|
case 6: request->send_P(200, "text/html", PAGE_settings_sec , settingsProcessor); break;
|
|
|
|
case 255: request->send_P(200, "text/html", PAGE_welcome , settingsProcessor); break;
|
|
|
|
default: request->send_P(200, "text/html", PAGE_settings , settingsProcessor);
|
2018-11-09 17:00:36 +01:00
|
|
|
}
|
2018-09-30 20:24:57 +02:00
|
|
|
}
|