Added MQTT Logs, and improved OTA and NTP to Async

This commit is contained in:
2025-12-28 18:39:13 +02:00
parent 8d397c6dd5
commit 0f0b67cab9
18 changed files with 568 additions and 123 deletions

View File

@@ -135,47 +135,40 @@ unsigned long Timekeeper::getTime() {
void Timekeeper::syncTimeWithNTP() {
// Check if we have network connection and required dependencies
if (!_networking || !_configManager) {
LOG_ERROR("Cannot sync time: Networking or ConfigManager not set");
LOG_WARNING("Cannot sync time: Networking or ConfigManager not set - using RTC time");
return;
}
if (!_networking->isConnected()) {
LOG_WARNING("Cannot sync time: No network connection");
LOG_INFO("No network connection - skipping NTP sync, using RTC time");
return;
}
LOG_INFO("Syncing time with NTP server...");
LOG_INFO("⏰ Starting non-blocking NTP sync...");
// Get config from ConfigManager
auto& timeConfig = _configManager->getTimeConfig();
// Configure NTP with settings from config
configTime(timeConfig.gmtOffsetSec, timeConfig.daylightOffsetSec, timeConfig.ntpServer.c_str());
// Wait for time sync with timeout
// 🔥 NON-BLOCKING: Try to get time immediately without waiting
struct tm timeInfo;
int attempts = 0;
while (!getLocalTime(&timeInfo) && attempts < 10) {
LOG_DEBUG("Waiting for NTP sync... attempt %d", attempts + 1);
delay(1000);
attempts++;
}
if (attempts >= 10) {
LOG_ERROR("Failed to obtain time from NTP server after 10 attempts");
return;
}
if (getLocalTime(&timeInfo, 100)) { // 100ms timeout instead of blocking
// Success! Update RTC with synchronized time
rtc.adjust(DateTime(timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday,
timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec));
// Update RTC with synchronized time
rtc.adjust(DateTime(timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday,
timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec));
LOG_INFO("✅ NTP sync successful: %04d-%02d-%02d %02d:%02d:%02d",
timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday,
timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec);
LOG_INFO("Time synced successfully: %04d-%02d-%02d %02d:%02d:%02d",
timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday,
timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec);
// Reload today's events since the time might have changed significantly
loadTodaysEvents();
// Reload today's events since the time might have changed significantly
loadTodaysEvents();
} else {
// No internet or NTP server unreachable - this is NORMAL for local networks
LOG_INFO("⚠️ NTP sync skipped (no internet) - using RTC time. This is normal for local networks.");
}
}
// ════════════════════════════════════════════════════════════════════════════