Skip to content

Commit

Permalink
feat(Update): add abort option
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed Jun 20, 2024
1 parent 991e0f0 commit abfb73c
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
3 changes: 2 additions & 1 deletion include/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace ESPAdmin

private:
static Logger _logger;
static void _onUpdate(const String &message);
static void _onUpdateTrigger(const String &message);
static void _onUpdateAbort(const String &message);
static void _onConfig(const String &message);
static void _onRestart();
static void _onLog(const String &message);
Expand Down
2 changes: 2 additions & 0 deletions include/OTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ namespace ESPAdmin
{
public:
static void start(const String &downloadURL);
static void abort();

private:
static Logger _logger;
static bool _aborted;
static esp_https_ota_handle_t _otaHandle;
static void task(void *pvParameters);
};
Expand Down
1 change: 1 addition & 0 deletions include/Update.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace ESPAdmin
static void checkAndUpdate(const UpdateMessage &message);
static void onChange(const UpdateStatus &status);
static void onProgress(int imageRead);
static void abort(const String &releaseId);

private:
static UpdateMessage _message;
Expand Down
28 changes: 25 additions & 3 deletions src/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ namespace ESPAdmin
_onRestart();
}

else if (type == "update")
else if (type == "update_trigger")
{
_onUpdate(message);
_onUpdateTrigger(message);
}

else if (type == "update_abort")
{
_onUpdateAbort(message);
}

else if (type == "config")
Expand All @@ -36,7 +41,7 @@ namespace ESPAdmin
}
}

void Command::_onUpdate(const String &message)
void Command::_onUpdateTrigger(const String &message)
{
StaticJsonDocument<300> doc; // 192 recommended

Expand All @@ -59,6 +64,23 @@ namespace ESPAdmin
}
}

void Command::_onUpdateAbort(const String &message)
{
StaticJsonDocument<100> doc;

DeserializationError error = deserializeJson(doc, message);

if (error == DeserializationError::Ok)
{
String releaseId = doc["releaseId"];
Update::abort(releaseId);
}
else
{
_logger.warn("Failed to deserializeJson");
}
}

void Command::_onConfig(const String &message)
{
Store::set(STORE_CONFIG, message.c_str());
Expand Down
28 changes: 27 additions & 1 deletion src/OTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ESPAdmin
{
Logger OTA::_logger("OTA");
bool OTA::_aborted = false;
esp_https_ota_handle_t OTA::_otaHandle;

void OTA::start(const String &downloadURL)
Expand Down Expand Up @@ -33,6 +34,11 @@ namespace ESPAdmin
}
}

void OTA::abort()
{
_aborted = true;
}

void OTA::task(void *)
{
int imageReadPrev = 0;
Expand All @@ -41,6 +47,18 @@ namespace ESPAdmin

while (Store::updateRunning)
{
if (_aborted)
{
esp_err_t ret = esp_https_ota_abort(_otaHandle);

if (ret == ESP_OK)
{
Store::updateRunning = false;
Update::onChange(UPDATE_FAILED);
break;
}
}

esp_err_t ret = esp_https_ota_perform(_otaHandle);

int imageReadNow = esp_https_ota_get_image_len_read(_otaHandle);
Expand Down Expand Up @@ -79,7 +97,15 @@ namespace ESPAdmin
}
}

esp_https_ota_finish(_otaHandle);
if (_aborted == false)
{
esp_https_ota_finish(_otaHandle);
}
else
{
_aborted = false;
}

vTaskDelete(nullptr);
}
}
8 changes: 8 additions & 0 deletions src/Update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ namespace ESPAdmin
Report::sendUpdateProgress(_message, progress);
}
}

void Update::abort(const String &releaseId)
{
if (_message.releaseId == releaseId)
{
OTA::abort();
}
}
}

0 comments on commit abfb73c

Please sign in to comment.