feat(rtc): add loopRTC and appendTimeStatus hook
This commit is contained in:
@@ -92,3 +92,40 @@ bool syncFromNTP() {
|
||||
lastNtpSyncEpoch = after;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Aus loop() aufgerufen. Triggert syncFromNTP():
|
||||
// - bei steigender Flanke von WiFi.isConnected() (STA-Reconnect)
|
||||
// - alle 24h, sobald STA verbunden ist
|
||||
// Reine Vergleichsoperationen, NTP-Roundtrip selbst ist blockierend (~ms..5s).
|
||||
void loopRTC() {
|
||||
bool sta = (WiFi.status() == WL_CONNECTED);
|
||||
|
||||
// Edge: false -> true (frischer STA-Connect)
|
||||
if (sta && !lastStaConnected) {
|
||||
Serial.println("[RTC] STA-Reconnect erkannt — NTP-Sync");
|
||||
syncFromNTP();
|
||||
}
|
||||
lastStaConnected = sta;
|
||||
|
||||
// 24h-Periodik (nur wenn STA online)
|
||||
if (sta && ntpEverSynced) {
|
||||
time_t nowEpoch = time(NULL);
|
||||
if (nowEpoch - lastNtpSyncEpoch >= 86400) {
|
||||
Serial.println("[RTC] 24h-Periodik — NTP-Sync");
|
||||
syncFromNTP();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Weak-Hook-Override aus timesync.h — erweitert /api/time um RTC-Status.
|
||||
extern "C" void appendTimeStatus(JsonDocument &doc) {
|
||||
doc["rtc_available"] = rtcAvailable;
|
||||
doc["rtc_synced_from_ntp"] = ntpEverSynced;
|
||||
doc["last_ntp_sync_ago_s"] =
|
||||
ntpEverSynced ? (long)(time(NULL) - lastNtpSyncEpoch) : (long)-1;
|
||||
if (rtcAvailable) {
|
||||
doc["rtc_time_utc"] = (long)rtc.now().unixtime();
|
||||
} else {
|
||||
doc["rtc_time_utc"] = (long)0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user