From 1c98d1c605840ccc8fc44dc4caee0ff015a1629b Mon Sep 17 00:00:00 2001 From: agessaman Date: Tue, 24 Mar 2026 05:54:38 -0700 Subject: [PATCH] Update MQTTBridge to adjust minimum valid epoch time for NTP synchronization and enhance slot management. Implement logic to re-create JWT tokens for slots if time correction indicates stale tokens after NTP sync. --- src/helpers/bridges/MQTTBridge.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/helpers/bridges/MQTTBridge.cpp b/src/helpers/bridges/MQTTBridge.cpp index 98d44dd6..50665922 100644 --- a/src/helpers/bridges/MQTTBridge.cpp +++ b/src/helpers/bridges/MQTTBridge.cpp @@ -2211,7 +2211,7 @@ void MQTTBridge::syncTimeWithNTP() { bool ntp_ok = false; unsigned long epochTime = 0; - const unsigned long kMinValidEpoch = 1704067200; // 2024-01-01 00:00:00 UTC + const unsigned long kMinValidEpoch = 1767225600; // 2026-01-01 00:00:00 UTC _ntp_client.begin(); const int kMaxNtpRetries = 3; @@ -2260,8 +2260,24 @@ void MQTTBridge::syncTimeWithNTP() { MQTT_DEBUG_PRINTLN("Time synced: %lu", epochTime); - // Note: Slot setup (including token creation) is deferred until after NTP sync - // via _slots_setup_done flag in the main loop, so no token fixup needed here. + // If slots are already set up and the time jumped significantly (e.g., SNTP + // initially returned stale RTC time, then a later sync corrected it), tear down + // and re-setup all JWT-authenticated slots so they get fresh tokens. + if (_slots_setup_done && was_ntp_synced) { + unsigned long current_time = (unsigned long)time(nullptr); + for (int i = 0; i < _max_active_slots; i++) { + if (_slots[i].enabled && _slots[i].preset && + _slots[i].preset->auth_type == MQTT_AUTH_JWT) { + // Check if the slot's token was created with a stale time + // (token_expires_at would be far in the past relative to current time) + if (_slots[i].token_expires_at > 0 && current_time > _slots[i].token_expires_at) { + MQTT_DEBUG_PRINTLN("Slot %d token stale after time correction, re-creating", i); + teardownSlot(i); + setupSlot(i); + } + } + } + } // Set timezone from string (with DST support) - only if changed static char last_timezone[64] = "";