mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-19 03:13:14 +00:00
Store last sync time. Implement ntp::isSynced()
This commit is contained in:
parent
90ac2ee71c
commit
fbfda9d3e8
7
Tactility/Include/Tactility/network/Ntp.h
Normal file
7
Tactility/Include/Tactility/network/Ntp.h
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace tt::network::ntp {
|
||||
|
||||
bool isSynced();
|
||||
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user