Fixed OTA problems, Clock Alerts and MQTT Logs. V151

This commit is contained in:
2026-01-19 19:02:25 +02:00
parent 7e279c6e45
commit 11b98166d1
10 changed files with 200 additions and 44 deletions

View File

@@ -493,9 +493,57 @@ bool OTAManager::downloadDirectToFlash(const String& url, size_t expectedSize) {
LOG_INFO("OTA: Checksum validation will be performed by ESP32 bootloader");
setStatus(Status::INSTALLING);
// Stream directly to flash
// Stream directly to flash with periodic watchdog feeding
WiFiClient* stream = http.getStreamPtr();
size_t written = Update.writeStream(*stream);
uint8_t buffer[4096]; // 4KB buffer for efficient transfer
size_t written = 0;
size_t lastLoggedPercent = 0;
unsigned long lastWatchdogReset = millis();
while (http.connected() && written < (size_t)contentLength) {
size_t available = stream->available();
if (available) {
size_t toRead = min(available, sizeof(buffer));
size_t bytesRead = stream->readBytes(buffer, toRead);
if (bytesRead > 0) {
// Write to flash
size_t bytesWritten = Update.write(buffer, bytesRead);
if (bytesWritten != bytesRead) {
LOG_ERROR("OTA: Flash write failed at offset %u (%u/%u bytes written)",
written, bytesWritten, bytesRead);
http.end();
// Resume systems
if (_timeKeeper) _timeKeeper->resumeClockUpdates();
if (_telemetry) _telemetry->resume();
setStatus(Status::FAILED, ErrorCode::WRITE_FAILED);
return false;
}
written += bytesWritten;
// Log progress every 20%
size_t currentPercent = (written * 100) / contentLength;
if (currentPercent >= lastLoggedPercent + 20) {
LOG_INFO("OTA: Flash write progress: %u%% (%u/%u bytes)",
currentPercent, written, contentLength);
lastLoggedPercent = currentPercent;
}
}
}
// Feed watchdog every 500ms to prevent timeout
if (millis() - lastWatchdogReset > 500) {
esp_task_wdt_reset();
lastWatchdogReset = millis();
}
// Small yield to prevent tight loop
yield();
}
http.end();
@@ -1038,7 +1086,7 @@ bool OTAManager::performManualUpdate(const String& channel) {
// CUSTOM FIRMWARE UPDATE
// ════════════════════════════════════════════════════════════════════════════
bool OTAManager::performCustomUpdate(const String& firmwareUrl, const String& checksum, size_t fileSize) {
bool OTAManager::performCustomUpdate(const String& firmwareUrl, const String& checksum, size_t fileSize, uint16_t version) {
if (_status != Status::IDLE) {
LOG_WARNING("OTA update already in progress");
return false;
@@ -1059,12 +1107,25 @@ bool OTAManager::performCustomUpdate(const String& firmwareUrl, const String& ch
LOG_INFO(" Checksum: %s (NOTE: ESP32 will validate after flash)", checksum.c_str());
}
if (version > 0) {
LOG_INFO(" Target Version: %u", version);
}
setStatus(Status::DOWNLOADING);
// Download directly to flash
bool result = downloadDirectToFlash(firmwareUrl, fileSize);
if (result) {
// Update version in config if provided
if (version > 0) {
_configManager.setFwVersion(String(version));
_configManager.saveDeviceConfig();
LOG_INFO("✅ Custom firmware version %u saved to NVS", version);
} else {
LOG_WARNING("⚠️ No version provided - NVS version unchanged");
}
LOG_INFO("🚀 Custom firmware installed - device will reboot");
} else {
LOG_ERROR("❌ Custom firmware installation failed");