From dd90afba1d9b6d95b49278c4f1eebe4c0191883b Mon Sep 17 00:00:00 2001 From: "Valentin V. Bartenev" Date: Sun, 3 May 2026 22:10:32 +0300 Subject: [PATCH] NetworkService: keep hasTimeSync() true for 24h after WiFi drops _have_time_sync is reset to false whenever WiFi disconnects, even though the ESP32 RTC continues to hold accurate time after a successful SNTP sync. This caused hasTimeSync() to return false during transient WiFi outages, unnecessarily tearing down MQTT broker connections and suppressing packet publishing. Introduce _last_time_sync to record the wall-clock time of the most recent confirmed sync. Move hasTimeSync() out of the header into NetworkService.cpp and extend its logic: in addition to the existing _have_time_sync flag, return true if the system clock is still sane (>= kMinSaneEpoch) and no more than kMaxOutOfSync (24h) has elapsed since the last confirmed sync. This makes the MQTT uplink resilient to brief WiFi dropouts without requiring any changes to callers of hasTimeSync(). Also bump kMinSaneEpoch from 2025-01-01 to 2026-01-01. --- src/helpers/NetworkService.cpp | 17 +++++++++++++++-- src/helpers/NetworkService.h | 3 ++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/helpers/NetworkService.cpp b/src/helpers/NetworkService.cpp index 3ac5ec12..dad1ee05 100644 --- a/src/helpers/NetworkService.cpp +++ b/src/helpers/NetworkService.cpp @@ -14,7 +14,8 @@ namespace { #if defined(ESP_PLATFORM) constexpr unsigned long kWifiRetryMillis = 15000; constexpr unsigned long kWifiConnectTimeoutMillis = 45000; -constexpr time_t kMinSaneEpoch = 1735689600; // 2025-01-01T00:00:00Z +constexpr time_t kMinSaneEpoch = 1767225600; // 2026-01-01T00:00:00Z +constexpr time_t kMaxOutOfSync = 86400; // 24h int getWifiQualityPercent(int rssi_dbm) { if (rssi_dbm <= -100) { @@ -43,7 +44,7 @@ const char* getWifiQualityLabel(int rssi_dbm) { } // namespace NetworkService::NetworkService() - : _fs(nullptr), _prefs{}, _wifi_started(false), _sntp_started(false), _have_time_sync(false), _last_wifi_attempt(0) { + : _fs(nullptr), _prefs{}, _wifi_started(false), _sntp_started(false), _have_time_sync(false), _last_wifi_attempt(0), _last_time_sync(0) { NetworkPrefsStore::setDefaults(_prefs); } @@ -213,6 +214,16 @@ bool NetworkService::isWifiConnected() const { #endif } +bool NetworkService::hasTimeSync() const { +#if defined(ESP_PLATFORM) + if (_have_time_sync) return true; + time_t now = time(nullptr); + return now >= kMinSaneEpoch && now < (_last_time_sync + kMaxOutOfSync); +#else + return false; +#endif +} + #if defined(ESP_PLATFORM) wifi_ps_type_t NetworkService::toEspPowerSave(uint8_t mode) { switch (mode) { @@ -295,5 +306,7 @@ void NetworkService::updateTimeSync() { bool sane_time = now >= kMinSaneEpoch; bool sync_ready = sync_status == SNTP_SYNC_STATUS_COMPLETED || sync_status == SNTP_SYNC_STATUS_IN_PROGRESS; _have_time_sync = sane_time && (sync_ready || prev_have_time_sync); + + if (_have_time_sync) _last_time_sync = now; } #endif diff --git a/src/helpers/NetworkService.h b/src/helpers/NetworkService.h index 350bd20e..925c8e6f 100644 --- a/src/helpers/NetworkService.h +++ b/src/helpers/NetworkService.h @@ -30,7 +30,7 @@ public: void reconnectWifi(); bool isWifiConnected() const override; - bool hasTimeSync() const override { return _have_time_sync; } + bool hasTimeSync() const override; private: #if defined(ESP_PLATFORM) @@ -47,4 +47,5 @@ private: bool _sntp_started; bool _have_time_sync; unsigned long _last_wifi_attempt; + time_t _last_time_sync; };