#include "webinterface.h" #include #include #include #include #include #include #include "parameters.h" #include "romfs.h" static WebServer server(80); /* serve files from ROMFS */ class ROMFS_Handler : public RequestHandler { bool canHandle(HTTPMethod method, String uri) { if (uri == "/") { uri = "/index.html"; } Serial.printf("canHandle: %s\n", uri.c_str()); uri = "web" + uri; if (ROMFS::exists(uri.c_str())) { return true; } return false; } bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) { if (requestUri == "/") { requestUri = "/index.html"; } String uri = "web" + requestUri; Serial.printf("handle: '%s'\n", requestUri.c_str()); const char *content_type = "text/html"; if (uri.endsWith(".js")) { content_type = "text/javascript"; } auto *f = ROMFS::find_stream(uri.c_str()); if (f != nullptr) { server.sendHeader("Content-Encoding", "gzip"); server.streamFile(*f, content_type); delete f; return true; } return false; } } ROMFS_Handler; /* init web server */ void WebInterface::init(void) { Serial.printf("WAP start %s %s\n", g.wifi_ssid, g.wifi_password); WiFi.softAP(g.wifi_ssid, g.wifi_password); IPAddress myIP = WiFi.softAPIP(); server.addHandler( &ROMFS_Handler ); #if 0 /*return index page which is stored in serverIndex */ server.on("/", HTTP_GET, []() { server.sendHeader("Connection", "close"); server.send(200, "text/html", ROMFS::find_string("web/login.html")); }); server.on("/serverIndex", HTTP_GET, []() { server.sendHeader("Connection", "close"); server.send(200, "text/html", ROMFS::find_string("web/uploader.html")); }); server.on("/js/jquery.min.js", HTTP_GET, []() { server.sendHeader("Connection", "close"); server.send(200, "text/html", ROMFS::find_string("web/js/jquery.min.js")); }); #endif /*handling uploading firmware file */ server.on("/update", HTTP_POST, []() { server.sendHeader("Connection", "close"); server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); ESP.restart(); }, []() { HTTPUpload& upload = server.upload(); if (upload.status == UPLOAD_FILE_START) { Serial.printf("Update: %s\n", upload.filename.c_str()); if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size Update.printError(Serial); } } else if (upload.status == UPLOAD_FILE_WRITE) { /* flashing firmware to ESP*/ if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { Update.printError(Serial); } } else if (upload.status == UPLOAD_FILE_END) { if (Update.end(true)) { //true to set the size to the current progress Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); } else { Update.printError(Serial); } } }); Serial.printf("WAP started\n"); server.begin(); } void WebInterface::update() { if (!initialised) { init(); initialised = true; } server.handleClient(); }