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.
This commit is contained in:
Valentin V. Bartenev
2026-05-03 22:10:32 +03:00
parent 80851b9c25
commit dd90afba1d
2 changed files with 17 additions and 3 deletions
+2 -1
View File
@@ -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;
};