From c17503abd51c47152047f41882d89fe415ec86bd Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 29 Jun 2025 15:03:48 -0500 Subject: [PATCH] cleanup --- .../web_server_base/web_server_base.cpp | 22 ++++++++++++------- .../web_server_base/web_server_base.h | 2 ++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/esphome/components/web_server_base/web_server_base.cpp b/esphome/components/web_server_base/web_server_base.cpp index b504b08525..052bc5df26 100644 --- a/esphome/components/web_server_base/web_server_base.cpp +++ b/esphome/components/web_server_base/web_server_base.cpp @@ -36,6 +36,16 @@ void OTARequestHandler::report_ota_progress_(AsyncWebServerRequest *request) { this->last_ota_progress_ = now; } } + +void OTARequestHandler::schedule_ota_reboot_() { + ESP_LOGI(TAG, "OTA update successful!"); + this->parent_->set_timeout(100, []() { App.safe_reboot(); }); +} + +void OTARequestHandler::ota_init_(const char *filename) { + ESP_LOGI(TAG, "OTA Update Start: %s", filename); + this->ota_read_length_ = 0; +} #endif void WebServerBase::add_handler(AsyncWebHandler *handler) { @@ -64,8 +74,7 @@ void OTARequestHandler::handleUpload(AsyncWebServerRequest *request, const Strin #ifdef USE_ARDUINO bool success; if (index == 0) { - ESP_LOGI(TAG, "OTA Update Start: %s", filename.c_str()); - this->ota_read_length_ = 0; + this->ota_init_(filename.c_str()); #ifdef USE_ESP8266 Update.runAsync(true); // NOLINTNEXTLINE(readability-static-accessed-through-instance) @@ -96,8 +105,7 @@ void OTARequestHandler::handleUpload(AsyncWebServerRequest *request, const Strin if (final) { if (Update.end(true)) { - ESP_LOGI(TAG, "OTA update successful!"); - this->parent_->set_timeout(100, []() { App.safe_reboot(); }); + this->schedule_ota_reboot_(); } else { report_ota_error(); } @@ -107,8 +115,7 @@ void OTARequestHandler::handleUpload(AsyncWebServerRequest *request, const Strin #ifdef USE_ESP_IDF // ESP-IDF implementation if (index == 0) { - ESP_LOGI(TAG, "OTA Update Start: %s", filename.c_str()); - this->ota_read_length_ = 0; + this->ota_init_(filename.c_str()); this->ota_started_ = false; // Create OTA backend @@ -145,8 +152,7 @@ void OTARequestHandler::handleUpload(AsyncWebServerRequest *request, const Strin if (final) { auto result = this->ota_backend_->end(); if (result == ota::OTA_RESPONSE_OK) { - ESP_LOGI(TAG, "OTA update successful!"); - this->parent_->set_timeout(100, []() { App.safe_reboot(); }); + this->schedule_ota_reboot_(); } else { ESP_LOGE(TAG, "OTA end failed: %d", result); } diff --git a/esphome/components/web_server_base/web_server_base.h b/esphome/components/web_server_base/web_server_base.h index 61add4ecea..965a36e929 100644 --- a/esphome/components/web_server_base/web_server_base.h +++ b/esphome/components/web_server_base/web_server_base.h @@ -141,6 +141,8 @@ class OTARequestHandler : public AsyncWebHandler { protected: #ifdef USE_WEBSERVER_OTA void report_ota_progress_(AsyncWebServerRequest *request); + void schedule_ota_reboot_(); + void ota_init_(const char *filename); uint32_t last_ota_progress_{0}; uint32_t ota_read_length_{0};