Skip to content

Commit

Permalink
feat: Implement Update class
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed Jun 30, 2023
1 parent b95ccdb commit d247f2c
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 25 deletions.
2 changes: 1 addition & 1 deletion include/ESPAdmin.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "Command.h"
#include "MQTT.h"
#include "HTTP.h"
#include "OTA.h"
#include "Update.h"

namespace ESPAdmin
{
Expand Down
1 change: 1 addition & 0 deletions include/OTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <esp_https_ota.h>
#include "Store.h"
#include "Logger.h"
#include "Update.h"

namespace ESPAdmin
{
Expand Down
12 changes: 10 additions & 2 deletions include/Update.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
#define H_ESP_ADMIN_UPDATE

#include <Arduino.h>
#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;
};
}

Expand Down
10 changes: 8 additions & 2 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ namespace ESPAdmin
struct UpdateMessage
{
String downloadURL;
String version;
String projectId;
String releaseId;
};

enum DebugMessage
Expand All @@ -34,6 +33,13 @@ namespace ESPAdmin
STORE_UPDATE_RELEASE_ID,
STORE_UPDATE_CERT,
};

enum UpdateStatus
{
UPDATE_RUNNING,
UPDATE_FAILED,
UPDATE_SUCCESS
};
}

#endif
2 changes: 1 addition & 1 deletion src/ESPAdmin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace ESPAdmin
{
Store::begin();

// MQTT::connect();
MQTT::connect();
}
}
14 changes: 7 additions & 7 deletions src/OTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand All @@ -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;
}
Expand All @@ -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);
}
}
25 changes: 23 additions & 2 deletions src/Update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
22 changes: 12 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit d247f2c

Please sign in to comment.