diff --git a/GitOTA.cpp b/GitOTA.cpp index 822440a..ff16fba 100644 --- a/GitOTA.cpp +++ b/GitOTA.cpp @@ -372,20 +372,38 @@ bool GitUpdater::beginUpdate(const char *version) { somfy.commit(); strcpy(this->currentFile, "SomfyController.littlefs.bin"); this->partition = U_SPIFFS; + this->lockFS = true; this->error = this->downloadFile(); + this->lockFS = false; if(this->error == 0) { settings.fwVersion.parse(version); delay(100); Serial.println("Committing Configuration..."); somfy.commit(); - rebootDelay.reboot = true; - rebootDelay.rebootTime = millis() + 500; } + rebootDelay.reboot = true; + rebootDelay.rebootTime = millis() + 500; } this->status = GIT_UPDATE_COMPLETE; this->emitUpdateCheck(); return true; } +bool GitUpdater::recoverFilesystem() { + sprintf(this->baseUrl, "https://github.com/rstrouse/ESPSomfy-RTS/releases/download/%s/", settings.fwVersion.name); + strcpy(this->currentFile, "SomfyController.littlefs.bin"); + this->partition = U_SPIFFS; + this->lockFS = true; + this->error = this->downloadFile(); + this->lockFS = false; + if(this->error == 0) { + delay(100); + Serial.println("Committing Configuration..."); + somfy.commit(); + } + rebootDelay.reboot = true; + rebootDelay.rebootTime = millis() + 500; + return true; +} bool GitUpdater::endUpdate() { return true; } int8_t GitUpdater::downloadFile() { WiFiClientSecure *client = new WiFiClientSecure; @@ -457,7 +475,7 @@ int8_t GitUpdater::downloadFile() { } else { timeouts++; - if(timeouts >= 300) { + if(timeouts >= 500) { Update.abort(); https.end(); free(buff); diff --git a/GitOTA.h b/GitOTA.h index 459a89d..360fce4 100644 --- a/GitOTA.h +++ b/GitOTA.h @@ -37,6 +37,7 @@ class GitRepo { }; class GitUpdater { public: + bool lockFS = false; uint8_t status = 0; uint32_t lastCheck = 0; bool updateAvailable = false; @@ -55,6 +56,7 @@ class GitUpdater { void setCurrentRelease(GitRepo &repo); void loop(); void toJSON(JsonObject &obj); + bool recoverFilesystem(); int checkInternet(); void emitUpdateCheck(uint8_t num=255); void emitDownloadProgress(size_t total, size_t loaded, const char *evt = "updateProgress"); diff --git a/Somfy.cpp b/Somfy.cpp index 470b831..8943a29 100644 --- a/Somfy.cpp +++ b/Somfy.cpp @@ -7,12 +7,14 @@ #include "Sockets.h" #include "MQTT.h" #include "ConfigFile.h" +#include "GitOTA.h" extern Preferences pref; extern SomfyShadeController somfy; extern SocketEmitter sockEmit; extern ConfigSettings settings; extern MQTTClass mqtt; +extern GitUpdater git; uint8_t rxmode = 0; // Indicates whether the radio is in receive mode. Just to ensure there isn't more than one interrupt hooked. @@ -545,6 +547,7 @@ bool SomfyShadeController::begin() { return true; } void SomfyShadeController::commit() { + if(git.lockFS) return; ShadeConfigFile file; file.begin(); file.save(this); @@ -553,6 +556,7 @@ void SomfyShadeController::commit() { this->lastCommit = millis(); } void SomfyShadeController::writeBackup() { + if(git.lockFS) return; ShadeConfigFile file; file.begin("/controller.backup", false); file.backup(this); diff --git a/SomfyController.ino.esp32.bin b/SomfyController.ino.esp32.bin index b287284..2c2f9c1 100644 Binary files a/SomfyController.ino.esp32.bin and b/SomfyController.ino.esp32.bin differ diff --git a/SomfyController.ino.esp32s3.bin b/SomfyController.ino.esp32s3.bin index 454e929..7642c72 100644 Binary files a/SomfyController.ino.esp32s3.bin and b/SomfyController.ino.esp32s3.bin differ diff --git a/Web.cpp b/Web.cpp index 043393d..a8fa582 100644 --- a/Web.cpp +++ b/Web.cpp @@ -203,6 +203,10 @@ void Web::handleLogin(WebServer &server) { return; } void Web::handleStreamFile(WebServer &server, const char *filename, const char *encoding) { + if(git.lockFS) { + server.send(500, _encoding_json, F("{\"status\":\"ERROR\",\"desc\":\"Filesystem update in progress\"}")); + return; + } webServer.sendCORSHeaders(server); if(server.method() == HTTP_OPTIONS) { server.send(200, "OK"); return; } @@ -1883,6 +1887,10 @@ void Web::begin() { } }); server.on("/updateShadeConfig", HTTP_POST, []() { + if(git.lockFS) { + server.send(500, _encoding_json, F("{\"status\":\"ERROR\",\"desc\":\"Filesystem update in progress\"}")); + return; + } webServer.sendCORSHeaders(server); if(server.method() == HTTP_OPTIONS) { server.send(200, "OK"); return; } server.sendHeader("Connection", "close"); @@ -2438,7 +2446,12 @@ void Web::begin() { serializeJson(doc, g_content); server.send(200, _encoding_json, g_content); }); - + server.on("/recoverFilesystem", [] () { + if(server.method() == HTTP_OPTIONS) { server.send(200, "OK"); return; } + webServer.sendCORSHeaders(server); + git.recoverFilesystem(); + server.send(200, "application/json", "{\"status\":\"OK\",\"desc\":\"Recovering filesystem from github please wait!!!\"}"); + }); server.begin(); apiServer.begin(); }