🪄 Formats DownloadFirmwareService

This commit is contained in:
Rune Harlyk
2024-07-09 20:03:49 +02:00
committed by Rune Harlyk
parent bbc7498653
commit 23a2ea566d
2 changed files with 47 additions and 63 deletions
@@ -19,30 +19,27 @@ static EventSocket *_socket = nullptr;
static int previousProgress = 0; static int previousProgress = 0;
JsonDocument doc; JsonDocument doc;
void update_started() void update_started() {
{
String output; String output;
doc["status"] = "preparing"; doc["status"] = "preparing";
serializeJson(doc, output); serializeJson(doc, output);
_socket->emit(EVENT_DOWNLOAD_OTA, output.c_str()); _socket->emit(EVENT_DOWNLOAD_OTA, output.c_str());
} }
void update_progress(int currentBytes, int totalBytes) void update_progress(int currentBytes, int totalBytes) {
{
String output; String output;
doc["status"] = "progress"; doc["status"] = "progress";
int progress = ((currentBytes * 100) / totalBytes); int progress = ((currentBytes * 100) / totalBytes);
if (progress > previousProgress) if (progress > previousProgress) {
{
doc["progress"] = progress; doc["progress"] = progress;
_socket->emit(EVENT_DOWNLOAD_OTA, output.c_str()); _socket->emit(EVENT_DOWNLOAD_OTA, output.c_str());
ESP_LOGV("Download OTA", "HTTP update process at %d of %d bytes... (%d %%)", currentBytes, totalBytes, progress); ESP_LOGV("Download OTA", "HTTP update process at %d of %d bytes... (%d %%)", currentBytes, totalBytes,
progress);
} }
previousProgress = progress; previousProgress = progress;
} }
void update_finished() void update_finished() {
{
String output; String output;
doc["status"] = "finished"; doc["status"] = "finished";
serializeJson(doc, output); serializeJson(doc, output);
@@ -52,8 +49,7 @@ void update_finished()
vTaskDelay(100 / portTICK_PERIOD_MS); vTaskDelay(100 / portTICK_PERIOD_MS);
} }
void updateTask(void *param) void updateTask(void *param) {
{
WiFiClientSecure client; WiFiClientSecure client;
client.setCACertBundle(rootca_crt_bundle_start); client.setCACertBundle(rootca_crt_bundle_start);
client.setTimeout(10); client.setTimeout(10);
@@ -69,8 +65,7 @@ void updateTask(void *param)
t_httpUpdate_return ret = httpUpdate.update(client, url.c_str()); t_httpUpdate_return ret = httpUpdate.update(client, url.c_str());
switch (ret) switch (ret) {
{
case HTTP_UPDATE_FAILED: case HTTP_UPDATE_FAILED:
doc["status"] = "error"; doc["status"] = "error";
@@ -78,7 +73,8 @@ void updateTask(void *param)
serializeJson(doc, output); serializeJson(doc, output);
_socket->emit(EVENT_DOWNLOAD_OTA, output.c_str()); _socket->emit(EVENT_DOWNLOAD_OTA, output.c_str());
ESP_LOGE("Download OTA", "HTTP Update failed with error (%d): %s", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str()); ESP_LOGE("Download OTA", "HTTP Update failed with error (%d): %s", httpUpdate.getLastError(),
httpUpdate.getLastErrorString().c_str());
break; break;
case HTTP_UPDATE_NO_UPDATES: case HTTP_UPDATE_NO_UPDATES:
@@ -89,36 +85,26 @@ void updateTask(void *param)
ESP_LOGE("Download OTA", "HTTP Update failed, has same firmware version"); ESP_LOGE("Download OTA", "HTTP Update failed, has same firmware version");
break; break;
case HTTP_UPDATE_OK: case HTTP_UPDATE_OK: ESP_LOGI("Download OTA", "HTTP Update successful - Restarting"); break;
ESP_LOGI("Download OTA", "HTTP Update successful - Restarting");
break;
} }
vTaskDelete(NULL); vTaskDelete(NULL);
} }
DownloadFirmwareService::DownloadFirmwareService(PsychicHttpServer *server, DownloadFirmwareService::DownloadFirmwareService(PsychicHttpServer *server, SecurityManager *securityManager,
SecurityManager *securityManager, EventSocket *socket, TaskManager *taskManager)
EventSocket *socket, TaskManager *taskManager) : _server(server), : _server(server), _securityManager(securityManager), _socket(socket), _taskManager(taskManager) {}
_securityManager(securityManager),
_socket(socket), _taskManager(taskManager)
{
}
void DownloadFirmwareService::begin() void DownloadFirmwareService::begin() {
{ _server->on(GITHUB_FIRMWARE_PATH, HTTP_POST,
_server->on(GITHUB_FIRMWARE_PATH, _securityManager->wrapCallback(std::bind(&DownloadFirmwareService::downloadUpdate, this,
HTTP_POST, std::placeholders::_1, std::placeholders::_2),
_securityManager->wrapCallback(
std::bind(&DownloadFirmwareService::downloadUpdate, this, std::placeholders::_1, std::placeholders::_2),
AuthenticationPredicates::IS_ADMIN)); AuthenticationPredicates::IS_ADMIN));
ESP_LOGV("DownloadFirmwareService", "Registered POST endpoint: %s", GITHUB_FIRMWARE_PATH); ESP_LOGV("DownloadFirmwareService", "Registered POST endpoint: %s", GITHUB_FIRMWARE_PATH);
} }
esp_err_t DownloadFirmwareService::downloadUpdate(PsychicRequest *request, JsonVariant &json) esp_err_t DownloadFirmwareService::downloadUpdate(PsychicRequest *request, JsonVariant &json) {
{ if (!json.is<JsonObject>()) {
if (!json.is<JsonObject>())
{
return request->reply(400); return request->reply(400);
} }
@@ -134,16 +120,14 @@ esp_err_t DownloadFirmwareService::downloadUpdate(PsychicRequest *request, JsonV
_socket->emit(EVENT_DOWNLOAD_OTA, output.c_str()); _socket->emit(EVENT_DOWNLOAD_OTA, output.c_str());
if (_taskManager->createTask( if (_taskManager->createTask(&updateTask, // Function that should be called
&updateTask, // Function that should be called
"Firmware download", // Name of the task (for debugging) "Firmware download", // Name of the task (for debugging)
OTA_TASK_STACK_SIZE, // Stack size (bytes) OTA_TASK_STACK_SIZE, // Stack size (bytes)
&downloadURL, // Pass reference to this class instance &downloadURL, // Pass reference to this class instance
(configMAX_PRIORITIES - 1), // Pretty high task priority (configMAX_PRIORITIES - 1), // Pretty high task priority
NULL, // Task handle NULL, // Task handle
1 // Have it on application core 1 // Have it on application core
) != pdPASS) ) != pdPASS) {
{
ESP_LOGE("Download OTA", "Couldn't create download OTA task"); ESP_LOGE("Download OTA", "Couldn't create download OTA task");
return request->reply(500); return request->reply(500);
} }
@@ -31,14 +31,14 @@
#define EVENT_DOWNLOAD_OTA "otastatus" #define EVENT_DOWNLOAD_OTA "otastatus"
#define OTA_TASK_STACK_SIZE 9216 #define OTA_TASK_STACK_SIZE 9216
class DownloadFirmwareService class DownloadFirmwareService {
{ public:
public: DownloadFirmwareService(PsychicHttpServer *server, SecurityManager *securityManager, EventSocket *socket,
DownloadFirmwareService(PsychicHttpServer *server, SecurityManager *securityManager, EventSocket *socket, TaskManager *taskManager); TaskManager *taskManager);
void begin(); void begin();
private: private:
SecurityManager *_securityManager; SecurityManager *_securityManager;
PsychicHttpServer *_server; PsychicHttpServer *_server;
EventSocket *_socket; EventSocket *_socket;