From d247f2cfc5080b300c0532283af20b1053c5c184 Mon Sep 17 00:00:00 2001 From: becem gharbi Date: Fri, 30 Jun 2023 11:46:20 +0100 Subject: [PATCH] feat: Implement Update class --- include/ESPAdmin.h | 2 +- include/OTA.h | 1 + include/Update.h | 12 ++++++++++-- include/types.h | 10 ++++++++-- src/ESPAdmin.cpp | 2 +- src/OTA.cpp | 14 +++++++------- src/Update.cpp | 25 +++++++++++++++++++++++-- src/main.cpp | 22 ++++++++++++---------- 8 files changed, 63 insertions(+), 25 deletions(-) diff --git a/include/ESPAdmin.h b/include/ESPAdmin.h index 51447d7..198e1fe 100644 --- a/include/ESPAdmin.h +++ b/include/ESPAdmin.h @@ -7,7 +7,7 @@ #include "Command.h" #include "MQTT.h" #include "HTTP.h" -#include "OTA.h" +#include "Update.h" namespace ESPAdmin { diff --git a/include/OTA.h b/include/OTA.h index 5e4e912..131786e 100644 --- a/include/OTA.h +++ b/include/OTA.h @@ -6,6 +6,7 @@ #include #include "Store.h" #include "Logger.h" +#include "Update.h" namespace ESPAdmin { diff --git a/include/Update.h b/include/Update.h index 09ae673..0ac8b2b 100644 --- a/include/Update.h +++ b/include/Update.h @@ -2,14 +2,22 @@ #define H_ESP_ADMIN_UPDATE #include +#include "types.h" +#include "Store.h" +#include "OTA.h" +#include "Logger.h" namespace ESPAdmin { class Update { public: - void check(); - void start(String downloadURL); + static void checkAndUpdate(UpdateMessage message); + static void onChange(UpdateStatus status); + + private: + static UpdateMessage _message; + static Logger _logger; }; } diff --git a/include/types.h b/include/types.h index 0004df8..827e9f6 100644 --- a/include/types.h +++ b/include/types.h @@ -8,8 +8,7 @@ namespace ESPAdmin struct UpdateMessage { String downloadURL; - String version; - String projectId; + String releaseId; }; enum DebugMessage @@ -34,6 +33,13 @@ namespace ESPAdmin STORE_UPDATE_RELEASE_ID, STORE_UPDATE_CERT, }; + + enum UpdateStatus + { + UPDATE_RUNNING, + UPDATE_FAILED, + UPDATE_SUCCESS + }; } #endif \ No newline at end of file diff --git a/src/ESPAdmin.cpp b/src/ESPAdmin.cpp index 8339b9e..93437b4 100644 --- a/src/ESPAdmin.cpp +++ b/src/ESPAdmin.cpp @@ -6,6 +6,6 @@ namespace ESPAdmin { Store::begin(); - // MQTT::connect(); + MQTT::connect(); } } diff --git a/src/OTA.cpp b/src/OTA.cpp index 219f972..556ac8c 100644 --- a/src/OTA.cpp +++ b/src/OTA.cpp @@ -14,11 +14,11 @@ namespace ESPAdmin HttpsOTA.onHttpEvent([](HttpEvent_t *event) {}); HttpsOTA.begin(downloadURL.c_str(), _cert); - bool running = true; + Store::updateRunning = true; _onStart(); - while (running) + while (Store::updateRunning) { switch (HttpsOTA.status()) { @@ -27,12 +27,12 @@ namespace ESPAdmin break; case HTTPS_OTA_SUCCESS: - running = false; + Store::updateRunning = false; _onSuccess(); break; case HTTPS_OTA_FAIL: - running = false; + Store::updateRunning = false; _onFailed(); break; } @@ -43,19 +43,19 @@ namespace ESPAdmin void OTA::_onStart() { - Store::updateRunning = true; _logger.info("update started"); + Update::onChange(UPDATE_RUNNING); } void OTA::_onSuccess() { - Store::updateRunning = false; _logger.success("update succeeded"); + Update::onChange(UPDATE_SUCCESS); } void OTA::_onFailed() { - Store::updateRunning = false; _logger.error("update failed"); + Update::onChange(UPDATE_FAILED); } } \ No newline at end of file diff --git a/src/Update.cpp b/src/Update.cpp index 628004c..bc028fc 100644 --- a/src/Update.cpp +++ b/src/Update.cpp @@ -2,11 +2,32 @@ namespace ESPAdmin { - void Update::check() + UpdateMessage Update::_message; + Logger Update::_logger("Update"); + + void Update::checkAndUpdate(UpdateMessage message) { + _message = message; + + String currentReleaseId = Store::get(STORE_UPDATE_RELEASE_ID); + + if (currentReleaseId != _message.releaseId) + { + OTA::start(message.downloadURL); + } + else + { + _logger.info("already updated"); + } } - void Update::start(String downloadURL) + void Update::onChange(UpdateStatus status) { + switch (status) + { + case UPDATE_SUCCESS: + Store::set(STORE_UPDATE_RELEASE_ID, _message.releaseId); + break; + } } } diff --git a/src/main.cpp b/src/main.cpp index bd003f9..26d151f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,23 +25,25 @@ void setup() ESPAdmin::begin(); - ESPAdmin::OTA::start("https://esp-ota-cicd.s3.us-east-005.backblazeb2.com/becem-gharbi/esp-ota-cicd/v0.1.2.bin"); + ESPAdmin::Update::checkAndUpdate( + {.downloadURL = "https://esp-ota-cicd.s3.us-east-005.backblazeb2.com/becem-gharbi/esp-ota-cicd/v0.1.2.bin", + .releaseId = "id"}); } void loop() { - // String content; - // StaticJsonDocument<96> doc; + String content; + StaticJsonDocument<96> doc; - // doc["title"] = "foo"; - // doc["body"] = "bar"; - // doc["userId"] = 1; + doc["title"] = "foo"; + doc["body"] = "bar"; + doc["userId"] = 1; - // serializeJson(doc, content); + serializeJson(doc, content); - // String res = ESPAdmin::HTTP::post("/posts", content, "application/json"); + String res = ESPAdmin::HTTP::post("/posts", content, "application/json"); - // logger.info(res); + logger.info(res); - // delay(10000); + delay(10000); }