Skip to content

Commit

Permalink
fix(Update): avoid sending duplicate progress values
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed Jun 21, 2024
1 parent 4ea06b6 commit 53232b7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion include/Report.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ESPAdmin
static void send(const ReportMessage &reportMessage);
static void sendStatus(const String &status);
static void sendUpdateStatus(UpdateMessage &updateMessage, const String &status);
static void sendUpdateProgress(UpdateMessage &updateMessage, int progress);
static void sendUpdateProgress(UpdateMessage &updateMessage, unsigned int progress);

private:
static Logger _logger;
Expand Down
1 change: 1 addition & 0 deletions include/Update.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace ESPAdmin
private:
static UpdateMessage _message;
static Logger _logger;
static unsigned int _progress;
static void _onSuccess();
static void _onFail();
static void _onStart();
Expand Down
17 changes: 5 additions & 12 deletions src/OTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ namespace ESPAdmin

void OTA::_task(void *)
{
int imageReadPrev = 0;

Update::onProgress(0);

while (Store::updateRunning)
Expand All @@ -79,28 +77,23 @@ namespace ESPAdmin

esp_err_t err = esp_https_ota_perform(_otaHandle);

int imageReadNow = esp_https_ota_get_image_len_read(_otaHandle);
int imageRead = esp_https_ota_get_image_len_read(_otaHandle);

if (err == ESP_ERR_HTTPS_OTA_IN_PROGRESS)
{
if (imageReadNow - imageReadPrev > 50000) // progress state is updated every 50Kb
{
Update::onProgress(imageReadNow);
imageReadPrev = imageReadNow;
}

Update::onProgress(imageRead);
continue;
}

Update::onProgress(imageReadNow);
Update::onProgress(imageRead);

if (err == ESP_OK)
{
Store::updateRunning = false;

bool completed = esp_https_ota_is_complete_data_received(_otaHandle);
bool success = esp_https_ota_is_complete_data_received(_otaHandle);

if (completed)
if (success)
{
Update::onChange(UPDATE_SUCCEDED);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ namespace ESPAdmin
*
* @throws None
*/
void Report::sendUpdateProgress(UpdateMessage &updateMessage, int progress)
void Report::sendUpdateProgress(UpdateMessage &updateMessage, unsigned int progress)
{
String mqttMessage;

Expand Down
12 changes: 9 additions & 3 deletions src/Update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ESPAdmin
{
UpdateMessage Update::_message;
unsigned int Update::_progress = 0;
Logger Update::_logger("Update");

/**
Expand Down Expand Up @@ -101,10 +102,15 @@ namespace ESPAdmin
*/
void Update::onProgress(int imageRead)
{
if (_message.downloadSize > 0)
if (_message.downloadSize > 0 && imageRead >= 0)
{
int progress = (imageRead * 100) / _message.downloadSize;
Report::sendUpdateProgress(_message, progress);
int currentProgress = (imageRead * 100) / _message.downloadSize;

if (currentProgress > _progress)
{
_progress = currentProgress;
Report::sendUpdateProgress(_message, _progress);
}
}
}

Expand Down

0 comments on commit 53232b7

Please sign in to comment.