🪄 Formats DownloadFirmwareService
This commit is contained in:
@@ -19,30 +19,27 @@ static EventSocket *_socket = nullptr;
|
||||
static int previousProgress = 0;
|
||||
JsonDocument doc;
|
||||
|
||||
void update_started()
|
||||
{
|
||||
void update_started() {
|
||||
String output;
|
||||
doc["status"] = "preparing";
|
||||
serializeJson(doc, output);
|
||||
_socket->emit(EVENT_DOWNLOAD_OTA, output.c_str());
|
||||
}
|
||||
|
||||
void update_progress(int currentBytes, int totalBytes)
|
||||
{
|
||||
void update_progress(int currentBytes, int totalBytes) {
|
||||
String output;
|
||||
doc["status"] = "progress";
|
||||
int progress = ((currentBytes * 100) / totalBytes);
|
||||
if (progress > previousProgress)
|
||||
{
|
||||
if (progress > previousProgress) {
|
||||
doc["progress"] = progress;
|
||||
_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;
|
||||
}
|
||||
|
||||
void update_finished()
|
||||
{
|
||||
void update_finished() {
|
||||
String output;
|
||||
doc["status"] = "finished";
|
||||
serializeJson(doc, output);
|
||||
@@ -52,8 +49,7 @@ void update_finished()
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
void updateTask(void *param)
|
||||
{
|
||||
void updateTask(void *param) {
|
||||
WiFiClientSecure client;
|
||||
client.setCACertBundle(rootca_crt_bundle_start);
|
||||
client.setTimeout(10);
|
||||
@@ -69,56 +65,46 @@ void updateTask(void *param)
|
||||
|
||||
t_httpUpdate_return ret = httpUpdate.update(client, url.c_str());
|
||||
|
||||
switch (ret)
|
||||
{
|
||||
case HTTP_UPDATE_FAILED:
|
||||
switch (ret) {
|
||||
case HTTP_UPDATE_FAILED:
|
||||
|
||||
doc["status"] = "error";
|
||||
doc["error"] = httpUpdate.getLastErrorString().c_str();
|
||||
serializeJson(doc, output);
|
||||
_socket->emit(EVENT_DOWNLOAD_OTA, output.c_str());
|
||||
doc["status"] = "error";
|
||||
doc["error"] = httpUpdate.getLastErrorString().c_str();
|
||||
serializeJson(doc, output);
|
||||
_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());
|
||||
break;
|
||||
case HTTP_UPDATE_NO_UPDATES:
|
||||
ESP_LOGE("Download OTA", "HTTP Update failed with error (%d): %s", httpUpdate.getLastError(),
|
||||
httpUpdate.getLastErrorString().c_str());
|
||||
break;
|
||||
case HTTP_UPDATE_NO_UPDATES:
|
||||
|
||||
doc["status"] = "error";
|
||||
doc["error"] = "Update failed, has same firmware version";
|
||||
serializeJson(doc, output);
|
||||
_socket->emit(EVENT_DOWNLOAD_OTA, output.c_str());
|
||||
doc["status"] = "error";
|
||||
doc["error"] = "Update failed, has same firmware version";
|
||||
serializeJson(doc, output);
|
||||
_socket->emit(EVENT_DOWNLOAD_OTA, output.c_str());
|
||||
|
||||
ESP_LOGE("Download OTA", "HTTP Update failed, has same firmware version");
|
||||
break;
|
||||
case HTTP_UPDATE_OK:
|
||||
ESP_LOGI("Download OTA", "HTTP Update successful - Restarting");
|
||||
break;
|
||||
ESP_LOGE("Download OTA", "HTTP Update failed, has same firmware version");
|
||||
break;
|
||||
case HTTP_UPDATE_OK: ESP_LOGI("Download OTA", "HTTP Update successful - Restarting"); break;
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
DownloadFirmwareService::DownloadFirmwareService(PsychicHttpServer *server,
|
||||
SecurityManager *securityManager,
|
||||
EventSocket *socket, TaskManager *taskManager) : _server(server),
|
||||
_securityManager(securityManager),
|
||||
_socket(socket), _taskManager(taskManager)
|
||||
{
|
||||
}
|
||||
DownloadFirmwareService::DownloadFirmwareService(PsychicHttpServer *server, SecurityManager *securityManager,
|
||||
EventSocket *socket, TaskManager *taskManager)
|
||||
: _server(server), _securityManager(securityManager), _socket(socket), _taskManager(taskManager) {}
|
||||
|
||||
void DownloadFirmwareService::begin()
|
||||
{
|
||||
_server->on(GITHUB_FIRMWARE_PATH,
|
||||
HTTP_POST,
|
||||
_securityManager->wrapCallback(
|
||||
std::bind(&DownloadFirmwareService::downloadUpdate, this, std::placeholders::_1, std::placeholders::_2),
|
||||
AuthenticationPredicates::IS_ADMIN));
|
||||
void DownloadFirmwareService::begin() {
|
||||
_server->on(GITHUB_FIRMWARE_PATH, HTTP_POST,
|
||||
_securityManager->wrapCallback(std::bind(&DownloadFirmwareService::downloadUpdate, this,
|
||||
std::placeholders::_1, std::placeholders::_2),
|
||||
AuthenticationPredicates::IS_ADMIN));
|
||||
|
||||
ESP_LOGV("DownloadFirmwareService", "Registered POST endpoint: %s", GITHUB_FIRMWARE_PATH);
|
||||
}
|
||||
|
||||
esp_err_t DownloadFirmwareService::downloadUpdate(PsychicRequest *request, JsonVariant &json)
|
||||
{
|
||||
if (!json.is<JsonObject>())
|
||||
{
|
||||
esp_err_t DownloadFirmwareService::downloadUpdate(PsychicRequest *request, JsonVariant &json) {
|
||||
if (!json.is<JsonObject>()) {
|
||||
return request->reply(400);
|
||||
}
|
||||
|
||||
@@ -134,16 +120,14 @@ esp_err_t DownloadFirmwareService::downloadUpdate(PsychicRequest *request, JsonV
|
||||
|
||||
_socket->emit(EVENT_DOWNLOAD_OTA, output.c_str());
|
||||
|
||||
if (_taskManager->createTask(
|
||||
&updateTask, // Function that should be called
|
||||
"Firmware download", // Name of the task (for debugging)
|
||||
OTA_TASK_STACK_SIZE, // Stack size (bytes)
|
||||
&downloadURL, // Pass reference to this class instance
|
||||
(configMAX_PRIORITIES - 1), // Pretty high task priority
|
||||
NULL, // Task handle
|
||||
1 // Have it on application core
|
||||
) != pdPASS)
|
||||
{
|
||||
if (_taskManager->createTask(&updateTask, // Function that should be called
|
||||
"Firmware download", // Name of the task (for debugging)
|
||||
OTA_TASK_STACK_SIZE, // Stack size (bytes)
|
||||
&downloadURL, // Pass reference to this class instance
|
||||
(configMAX_PRIORITIES - 1), // Pretty high task priority
|
||||
NULL, // Task handle
|
||||
1 // Have it on application core
|
||||
) != pdPASS) {
|
||||
ESP_LOGE("Download OTA", "Couldn't create download OTA task");
|
||||
return request->reply(500);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user