1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-12 08:12:22 +01:00

single ota path

This commit is contained in:
J. Nick Koston
2025-07-01 10:25:48 -05:00
parent 943d0f103d
commit 8aa8af735d
2 changed files with 33 additions and 2 deletions

View File

@@ -37,12 +37,17 @@ void WebServerBase::add_handler(AsyncWebHandler *handler) {
void OTARequestHandler::report_ota_progress_(AsyncWebServerRequest *request) { void OTARequestHandler::report_ota_progress_(AsyncWebServerRequest *request) {
const uint32_t now = millis(); const uint32_t now = millis();
if (now - this->last_ota_progress_ > 1000) { if (now - this->last_ota_progress_ > 1000) {
float percentage = 0.0f;
if (request->contentLength() != 0) { if (request->contentLength() != 0) {
float percentage = (this->ota_read_length_ * 100.0f) / request->contentLength(); percentage = (this->ota_read_length_ * 100.0f) / request->contentLength();
ESP_LOGD(TAG, "OTA in progress: %0.1f%%", percentage); ESP_LOGD(TAG, "OTA in progress: %0.1f%%", percentage);
} else { } else {
ESP_LOGD(TAG, "OTA in progress: %u bytes read", this->ota_read_length_); ESP_LOGD(TAG, "OTA in progress: %u bytes read", this->ota_read_length_);
} }
#ifdef USE_OTA_STATE_CALLBACK
// Report progress - use call_deferred since we're in web server task
this->parent_->state_callback_.call_deferred(ota_base::OTA_IN_PROGRESS, percentage, 0);
#endif
this->last_ota_progress_ = now; this->last_ota_progress_ = now;
} }
} }
@@ -68,6 +73,11 @@ void OTARequestHandler::handleUpload(AsyncWebServerRequest *request, const Strin
// Initialize OTA on first call // Initialize OTA on first call
this->ota_init_(filename.c_str()); this->ota_init_(filename.c_str());
#ifdef USE_OTA_STATE_CALLBACK
// Notify OTA started - use call_deferred since we're in web server task
this->parent_->state_callback_.call_deferred(ota_base::OTA_STARTED, 0.0f, 0);
#endif
// Platform-specific pre-initialization // Platform-specific pre-initialization
#ifdef USE_ARDUINO #ifdef USE_ARDUINO
#ifdef USE_ESP8266 #ifdef USE_ESP8266
@@ -83,6 +93,10 @@ void OTARequestHandler::handleUpload(AsyncWebServerRequest *request, const Strin
this->ota_backend_ = ota_base::make_ota_backend(); this->ota_backend_ = ota_base::make_ota_backend();
if (!this->ota_backend_) { if (!this->ota_backend_) {
ESP_LOGE(TAG, "Failed to create OTA backend"); ESP_LOGE(TAG, "Failed to create OTA backend");
#ifdef USE_OTA_STATE_CALLBACK
this->parent_->state_callback_.call_deferred(ota_base::OTA_ERROR, 0.0f,
static_cast<uint8_t>(ota_base::OTA_RESPONSE_ERROR_UNKNOWN));
#endif
return; return;
} }
@@ -96,6 +110,9 @@ void OTARequestHandler::handleUpload(AsyncWebServerRequest *request, const Strin
if (error_code != ota_base::OTA_RESPONSE_OK) { if (error_code != ota_base::OTA_RESPONSE_OK) {
ESP_LOGE(TAG, "OTA begin failed: %d", error_code); ESP_LOGE(TAG, "OTA begin failed: %d", error_code);
this->ota_backend_.reset(); this->ota_backend_.reset();
#ifdef USE_OTA_STATE_CALLBACK
this->parent_->state_callback_.call_deferred(ota_base::OTA_ERROR, 0.0f, static_cast<uint8_t>(error_code));
#endif
return; return;
} }
} }
@@ -111,6 +128,9 @@ void OTARequestHandler::handleUpload(AsyncWebServerRequest *request, const Strin
ESP_LOGE(TAG, "OTA write failed: %d", error_code); ESP_LOGE(TAG, "OTA write failed: %d", error_code);
this->ota_backend_->abort(); this->ota_backend_->abort();
this->ota_backend_.reset(); this->ota_backend_.reset();
#ifdef USE_OTA_STATE_CALLBACK
this->parent_->state_callback_.call_deferred(ota_base::OTA_ERROR, 0.0f, static_cast<uint8_t>(error_code));
#endif
return; return;
} }
this->ota_read_length_ += len; this->ota_read_length_ += len;
@@ -121,9 +141,16 @@ void OTARequestHandler::handleUpload(AsyncWebServerRequest *request, const Strin
if (final) { if (final) {
error_code = this->ota_backend_->end(); error_code = this->ota_backend_->end();
if (error_code == ota_base::OTA_RESPONSE_OK) { if (error_code == ota_base::OTA_RESPONSE_OK) {
#ifdef USE_OTA_STATE_CALLBACK
// Report completion before reboot - use call_deferred since we're in web server task
this->parent_->state_callback_.call_deferred(ota_base::OTA_COMPLETED, 100.0f, 0);
#endif
this->schedule_ota_reboot_(); this->schedule_ota_reboot_();
} else { } else {
ESP_LOGE(TAG, "OTA end failed: %d", error_code); ESP_LOGE(TAG, "OTA end failed: %d", error_code);
#ifdef USE_OTA_STATE_CALLBACK
this->parent_->state_callback_.call_deferred(ota_base::OTA_ERROR, 0.0f, static_cast<uint8_t>(error_code));
#endif
} }
this->ota_backend_.reset(); this->ota_backend_.reset();
} }
@@ -139,6 +166,10 @@ void OTARequestHandler::handleRequest(AsyncWebServerRequest *request) {
void WebServerBase::add_ota_handler() { void WebServerBase::add_ota_handler() {
this->add_handler(new OTARequestHandler(this)); // NOLINT this->add_handler(new OTARequestHandler(this)); // NOLINT
#ifdef USE_OTA_STATE_CALLBACK
// Register with global OTA callback system
ota_base::register_ota_platform(this);
#endif
} }
#endif #endif

View File

@@ -83,7 +83,7 @@ class AuthMiddlewareHandler : public MiddlewareHandler {
} // namespace internal } // namespace internal
class WebServerBase : public Component { class WebServerBase : public Component, public ota_base::OTAComponent {
public: public:
void init() { void init() {
if (this->initialized_) { if (this->initialized_) {