diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index ac258e9..2835989 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -112,6 +112,25 @@ void WebCfgServer::initialize() waitAndProcess(false, 1000); } }); + _server.on("/ota", [&]() { + if (_hasCredentials && !_server.authenticate(_credUser, _credPassword)) { + return _server.requestAuthentication(); + } + String response = ""; + buildOtaHtml(response); + _server.send(200, "text/html", response); + }); + _server.on("/uploadota", HTTP_POST, [&]() { + if (_hasCredentials && !_server.authenticate(_credUser, _credPassword)) { + return _server.requestAuthentication(); + } + + String response = "ok"; + + _server.send(200, "text/html", response); + }, [&]() { + handleOtaUpload(); + }); _server.begin(); } @@ -351,6 +370,11 @@ void WebCfgServer::buildHtml(String& response) response.concat(""); response.concat(""); + response.concat("

OTA

"); + response.concat("
"); + response.concat(""); + response.concat("
"); + if(_allowRestartToPortal) { response.concat("

WiFi

"); @@ -423,8 +447,17 @@ void WebCfgServer::buildCredHtml(String &response) } - response.concat("\n"); - response.concat("\n"); +} + +void WebCfgServer::buildOtaHtml(String &response) +{ + buildHtmlHeader(response); + + response.concat("
"); + response.concat(""); + response.concat("Choose a file to upload:
"); + response.concat("
"); + response.concat("\n\n"); } void WebCfgServer::buildMqttConfigHtml(String &response) @@ -617,3 +650,35 @@ void WebCfgServer::waitAndProcess(const bool blocking, const uint32_t duration) } } +void WebCfgServer::handleOtaUpload() +{ + if (_server.uri() != "/uploadota") { + return; + } + HTTPUpload& upload = _server.upload(); + if (upload.status == UPLOAD_FILE_START) { + String filename = upload.filename; + if (!filename.startsWith("/")) { + filename = "/" + filename; + } + Serial.print("handleFileUpload Name: "); Serial.println(filename); +// fsUploadFile = FILESYSTEM.open(filename, "w"); + filename = String(); + } else if (upload.status == UPLOAD_FILE_WRITE) { + //DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize); +// if (fsUploadFile) { +// fsUploadFile.write(upload.buf, upload.currentSize); +// } + for(int i=0; i < upload.currentSize; i++) + { + Serial.print((char)upload.buf[i]); + } + } else if (upload.status == UPLOAD_FILE_END) { +// if (fsUploadFile) { +// fsUploadFile.close(); +// } + Serial.println(); + Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize); + } +} + diff --git a/WebCfgServer.h b/WebCfgServer.h index f8152bf..2b9a1ce 100644 --- a/WebCfgServer.h +++ b/WebCfgServer.h @@ -32,6 +32,7 @@ private: bool processArgs(String& message); void buildHtml(String& response); void buildCredHtml(String& response); + void buildOtaHtml(String& response); void buildMqttConfigHtml(String& response); void buildConfirmHtml(String& response, const String &message, uint32_t redirectDelay = 5); void buildConfigureWifiHtml(String& response); @@ -46,6 +47,7 @@ private: String generateConfirmCode(); void waitAndProcess(const bool blocking, const uint32_t duration); + void handleOtaUpload(); WebServer _server; NukiWrapper* _nuki;