Skip to content

Commit

Permalink
Merge pull request #145 from regevbr/#123
Browse files Browse the repository at this point in the history
fix #123 - No autoforward after initiated update
  • Loading branch information
technyon authored Mar 15, 2023
2 parents 8e6fa00 + f48773d commit c1d5926
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
14 changes: 12 additions & 2 deletions Ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ void Ota::updateFirmware(uint8_t* buf, size_t size)
Log->println("EndOTA");
if (ESP_OK == esp_ota_set_boot_partition(esp_ota_get_next_update_partition(NULL)))
{
delay(2000);
restartEsp(RestartReason::OTACompleted);
_updateCompleted = true;
}
else
{
Expand All @@ -40,3 +39,14 @@ bool Ota::updateStarted()
{
return _updateStarted;
}

bool Ota::updateCompleted()
{
return _updateCompleted;
}

bool Ota::restart()
{
_updateCompleted = false;
_updateStarted = false;
}
3 changes: 3 additions & 0 deletions Ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ class Ota
void updateFirmware(uint8_t* buf, size_t size);

bool updateStarted();
bool updateCompleted();
void restart();

private:
bool _updateStarted = false;
bool _updateCompleted = false;
esp_ota_handle_t otaHandler = 0;
};
34 changes: 30 additions & 4 deletions WebCfgServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,25 @@ void WebCfgServer::initialize()
return _server.requestAuthentication();
}
String response = "";
buildOtaHtml(response);
buildOtaHtml(response, _server.arg("errored") != "");
_server.send(200, "text/html", response);
});
_server.on("/uploadota", HTTP_POST, [&]() {
if (_hasCredentials && !_server.authenticate(_credUser, _credPassword)) {
return _server.requestAuthentication();
}

_server.send(200, "text/html", "");
if (_ota->updateStarted() && _ota->updateCompleted()) {
String response = "";
buildOtaCompletedHtml(response);
_server.send(200, "text/html", response);
delay(2000);
restartEsp(RestartReason::OTACompleted);
} else {
ota->restart();
_server.sendHeader("Location", "/ota?errored=true");
_server.send(302, "text/plain", "");
}
}, [&]() {
if (_hasCredentials && !_server.authenticate(_credUser, _credPassword)) {
return _server.requestAuthentication();
Expand Down Expand Up @@ -639,7 +649,7 @@ void WebCfgServer::buildCredHtml(String &response)
response.concat("</BODY></HTML>");
}

void WebCfgServer::buildOtaHtml(String &response)
void WebCfgServer::buildOtaHtml(String &response, bool errored)
{
buildHtmlHeader(response);

Expand All @@ -650,6 +660,10 @@ void WebCfgServer::buildOtaHtml(String &response)
return;
}

if (errored) {
response.concat("<div>Over-the-air update errored. Please check the logs for more info</div><br/>");
}

response.concat("<form id=\"upform\" enctype=\"multipart/form-data\" action=\"/uploadota\" method=\"POST\"><input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"100000\" />Choose the updated nuki_hub.bin file to upload: <input name=\"uploadedfile\" type=\"file\" accept=\".bin\" /><br/>");
response.concat("<br><input id=\"submitbtn\" type=\"submit\" value=\"Upload File\" /></form>");
response.concat("<div id=\"msgdiv\" style=\"visibility:hidden\">Initiating Over-the-air update. This will take about two minutes, please be patient.<br>You will be forwarded automatically when the update is complete.</div>");
Expand All @@ -660,13 +674,25 @@ void WebCfgServer::buildOtaHtml(String &response)
response.concat(" function hideshow() {");
response.concat(" document.getElementById('upform').style.visibility = 'hidden';");
response.concat(" document.getElementById('msgdiv').style.visibility = 'visible';");
response.concat(" setTimeout(\"location.href = '/';\",120000);");
response.concat(" }");
response.concat("});");
response.concat("</script>");
response.concat("</BODY></HTML>");
}

void WebCfgServer::buildOtaCompletedHtml(String &response)
{
buildHtmlHeader(response);

response.concat("<div>Over-the-air update completed.<br>You will be forwarded automatically.</div>");
response.concat("<script type=\"text/javascript\">");
response.concat("window.addEventListener('load', function () {");
response.concat(" setTimeout(\"location.href = '/';\",10000);");
response.concat("});");
response.concat("</script>");
response.concat("</BODY></HTML>");
}

void WebCfgServer::buildMqttConfigHtml(String &response)
{
buildHtmlHeader(response);
Expand Down
3 changes: 2 additions & 1 deletion WebCfgServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class WebCfgServer
bool processArgs(String& message);
void buildHtml(String& response);
void buildCredHtml(String& response);
void buildOtaHtml(String& response);
void buildOtaHtml(String& response, bool errored);
void buildOtaCompletedHtml(String& response);
void buildMqttConfigHtml(String& response);
void buildNukiConfigHtml(String& response);
void buildConfirmHtml(String& response, const String &message, uint32_t redirectDelay = 5);
Expand Down

0 comments on commit c1d5926

Please sign in to comment.