NTP: Fix nullptr if network is connected after setup()

This commit is contained in:
Petr Kracik 2026-02-04 14:45:47 +01:00 committed by Petr Kracík
parent 891fc906bb
commit 8a16be1bea
No known key found for this signature in database
GPG key ID: 616BA0418005810D
2 changed files with 20 additions and 6 deletions

View file

@ -24,7 +24,7 @@
namespace NTP_Utils {
void setup();
bool setup();
void update();
String getFormatedTime();

View file

@ -27,26 +27,40 @@
extern Configuration Config;
extern NetworkManager *networkManager;
WiFiUDP ntpUDP;
NTPClient* timeClient;
NTPClient* timeClient = nullptr;
namespace NTP_Utils {
void setup() {
bool setup() {
if (networkManager->isConnected() && Config.digi.ecoMode == 0 && Config.callsign != "NOCALL-10") {
int gmt = Config.ntp.gmtCorrection * 3600;
Serial.println("[NTP] Setting up, TZ offset: " + String(gmt) + " Server: " + Config.ntp.server);
timeClient = new NTPClient(ntpUDP, Config.ntp.server.c_str(), gmt, 15 * 60 * 1000); // Update interval 15 min
timeClient->begin();
return true;
}
return false;
}
void update() {
if (networkManager->isConnected() && Config.digi.ecoMode == 0 && Config.callsign != "NOCALL-10") timeClient->update();
if (!networkManager->isConnected() || Config.digi.ecoMode != 0 || Config.callsign == "NOCALL-10") {
return;
}
if (timeClient == nullptr) {
if (!setup()) {
return;
}
}
timeClient->update();
}
String getFormatedTime() {
if (networkManager->isConnected() && Config.digi.ecoMode == 0) return timeClient->getFormattedTime();
if (networkManager->isConnected() && Config.digi.ecoMode == 0 && timeClient != nullptr) {
return timeClient->getFormattedTime();
}
return "DigiEcoMode Active";
}
}
}