Skip to content

Commit

Permalink
feat(OTA): publish progress on device/+/report/update/progress
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed Jun 19, 2024
1 parent 7c4763f commit 8b86b72
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 9 deletions.
3 changes: 2 additions & 1 deletion include/Report.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace ESPAdmin
public:
static void send(const ReportMessage &reportMessage);
static void sendStatus(const String &status);
static void sendUpdate(UpdateMessage &updateMessage, const String &status);
static void sendUpdateStatus(UpdateMessage &updateMessage, const String &status);
static void sendUpdateProgress(UpdateMessage &updateMessage, 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 @@ -17,6 +17,7 @@ namespace ESPAdmin
public:
static void checkAndUpdate(const UpdateMessage &message);
static void onChange(const UpdateStatus &status);
static void onProgress(int imageRead);

private:
static UpdateMessage _message;
Expand Down
1 change: 1 addition & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace ESPAdmin
struct UpdateMessage
{
String downloadPath;
int downloadSize;
String releaseId;
String version;
String deploymentId;
Expand Down
1 change: 1 addition & 0 deletions src/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace ESPAdmin
{
UpdateMessage updateMessage = {
.downloadPath = doc["downloadPath"],
.downloadSize = doc["downloadSize"],
.releaseId = doc["releaseId"],
.version = doc["version"],
};
Expand Down
15 changes: 13 additions & 2 deletions src/OTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace ESPAdmin

void OTA::task(void *)
{

esp_http_client_config_t httpConfig = {
.url = _downloadURL.c_str(),
.cert_pem = Store::options.httpCert,
Expand All @@ -37,15 +36,27 @@ namespace ESPAdmin
Update::onChange(UPDATE_FAILED);
}

int imageReadPrev = 0;

while (Store::updateRunning)
{
esp_err_t ret = esp_https_ota_perform(otaHandle);

int imageReadNow = esp_https_ota_get_image_len_read(otaHandle);

if (ret == ESP_ERR_HTTPS_OTA_IN_PROGRESS)
{
if (imageReadNow - imageReadPrev > 10000) // progress state is updated every 10Kb
{
Update::onProgress(imageReadNow);
imageReadPrev = imageReadNow;
}
continue;
}
else if (ret == ESP_OK)

Update::onProgress(imageReadNow);

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

Expand Down
21 changes: 18 additions & 3 deletions src/Report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace ESPAdmin
MQTT::publish("/report/status", message, 1, true);
}

void Report::sendUpdate(UpdateMessage &updateMessage, const String &status)
void Report::sendUpdateStatus(UpdateMessage &updateMessage, const String &status)
{
if (status == "started")
{
Expand All @@ -75,7 +75,7 @@ namespace ESPAdmin

serializeJson(mqttDoc, mqttMessage);

MQTT::publish("/report/update", mqttMessage, 1, false);
MQTT::publish("/report/update_status", mqttMessage, 1, false);
}
else
{
Expand All @@ -92,8 +92,23 @@ namespace ESPAdmin
{
HTTP::post("/report/update", message, "application/json");

MQTT::publish("/report/update", message, 1, false);
MQTT::publish("/report/update_status", message, 1, false);
}
}
}

void Report::sendUpdateProgress(UpdateMessage &updateMessage, int progress)
{
String mqttMessage;

StaticJsonDocument<150> mqttDoc;

mqttDoc["releaseId"] = updateMessage.releaseId;
mqttDoc["deploymentId"] = updateMessage.deploymentId;
mqttDoc["progress"] = progress;

serializeJson(mqttDoc, mqttMessage);

MQTT::publish("/report/update_progress", mqttMessage, 0, false);
}
}
12 changes: 9 additions & 3 deletions src/Update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace ESPAdmin
[[noreturn]] void Update::_onSuccess()
{
_logger.success("succeded");
Report::sendUpdate(_message, "succeded");
Report::sendUpdateStatus(_message, "succeded");

Store::set(STORE_UPDATE_RELEASE_ID, _message.releaseId.c_str());
Store::set(STORE_UPDATE_VERSION, _message.version.c_str());
Expand All @@ -66,13 +66,19 @@ namespace ESPAdmin
{
_logger.error("failed");

Report::sendUpdate(_message, "failed");
Report::sendUpdateStatus(_message, "failed");
}

void Update::_onStart()
{
_logger.info("started");

Report::sendUpdate(_message, "started");
Report::sendUpdateStatus(_message, "started");
}

void Update::onProgress(int imageRead)
{
int progress = (imageRead * 100) / _message.downloadSize;
Report::sendUpdateProgress(_message, progress);
}
}

0 comments on commit 8b86b72

Please sign in to comment.