_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.
52 lines
1.3 KiB
C++
52 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include <Arduino.h>
|
|
#include <helpers/IdentityStore.h>
|
|
#include <helpers/NetworkStateProvider.h>
|
|
|
|
#if defined(ESP_PLATFORM)
|
|
#include <WiFi.h>
|
|
#endif
|
|
|
|
#include "NetworkPrefs.h"
|
|
|
|
class NetworkService : public NetworkStateProvider {
|
|
public:
|
|
NetworkService();
|
|
|
|
void begin(FILESYSTEM* fs,
|
|
uint8_t legacy_wifi_powersave = 0,
|
|
const char* legacy_wifi_ssid = nullptr,
|
|
const char* legacy_wifi_pwd = nullptr);
|
|
void end();
|
|
void loop(bool network_required);
|
|
|
|
bool setWifiSSID(const char* ssid);
|
|
bool setWifiPassword(const char* pwd);
|
|
const char* getWifiSSID() const { return _prefs.wifi_ssid; }
|
|
bool setWifiPowerSave(const char* mode);
|
|
const char* getWifiPowerSave() const;
|
|
void formatWifiStatusReply(char* reply, size_t reply_size) const;
|
|
void reconnectWifi();
|
|
|
|
bool isWifiConnected() const override;
|
|
bool hasTimeSync() const override;
|
|
|
|
private:
|
|
#if defined(ESP_PLATFORM)
|
|
static wifi_ps_type_t toEspPowerSave(uint8_t mode);
|
|
static const char* getPowerSaveLabel(uint8_t mode);
|
|
void ensureWifi(bool network_required);
|
|
void updateTimeSync();
|
|
#endif
|
|
bool savePrefs();
|
|
|
|
FILESYSTEM* _fs;
|
|
NetworkPrefs _prefs;
|
|
bool _wifi_started;
|
|
bool _sntp_started;
|
|
bool _have_time_sync;
|
|
unsigned long _last_wifi_attempt;
|
|
time_t _last_time_sync;
|
|
};
|