Store last sync time. Implement ntp::isSynced()

This commit is contained in:
Ken Van Hoeylandt 2025-10-24 12:15:14 +02:00
parent 90ac2ee71c
commit fbfda9d3e8
2 changed files with 40 additions and 1 deletions

View File

@ -0,0 +1,7 @@
#pragma once
namespace tt::network::ntp {
bool isSynced();
}

View File

@ -1,5 +1,7 @@
#include "Tactility/network/NtpPrivate.h"
#include <Tactility/Preferences.h>
#ifdef ESP_PLATFORM
#include <Tactility/kernel/SystemEvents.h>
#include <Tactility/TactilityCore.h>
@ -11,10 +13,35 @@
namespace tt::network::ntp {
static bool processedSyncEvent = false;
#ifdef ESP_PLATFORM
static void onTimeSynced(struct timeval* tv) {
void storeTimeInNvs() {
time_t now;
time(&now);
auto preferences = std::make_unique<Preferences>("time");
preferences->putInt64("syncTime", now);
TT_LOG_I(TAG, "Stored time %llu", now);
}
void setTimeFromNvs() {
auto preferences = std::make_unique<Preferences>("time");
time_t synced_time;
if (preferences->optInt64("syncTime", synced_time)) {
TT_LOG_I(TAG, "Restoring last known time to %llu", synced_time);
timeval get_nvs_time;
get_nvs_time.tv_sec = synced_time;
settimeofday(&get_nvs_time, nullptr);
}
}
static void onTimeSynced(timeval* tv) {
TT_LOG_I(TAG, "Time synced (%llu)", tv->tv_sec);
processedSyncEvent = true;
esp_netif_sntp_deinit();
storeTimeInNvs();
kernel::publishSystemEvent(kernel::SystemEvent::Time);
}
@ -27,8 +54,13 @@ void init() {
#else
void init() {
processedSyncEvent = true;
}
#endif
bool isSynced() {
return processedSyncEvent;
}
}