Compare commits

..

3 Commits

Author SHA1 Message Date
Ken Van Hoeylandt
fbfda9d3e8 Store last sync time. Implement ntp::isSynced() 2025-10-24 12:15:14 +02:00
Ken Van Hoeylandt
90ac2ee71c Add i64 support for Preferences 2025-10-24 12:14:56 +02:00
Ken Van Hoeylandt
7e308a5ccc Add i64 support for Bundle 2025-10-24 12:14:45 +02:00
7 changed files with 124 additions and 5 deletions

View File

@ -24,14 +24,17 @@ public:
bool hasBool(const std::string& key) const;
bool hasInt32(const std::string& key) const;
bool hasInt64(const std::string& key) const;
bool hasString(const std::string& key) const;
bool optBool(const std::string& key, bool& out) const;
bool optInt32(const std::string& key, int32_t& out) const;
bool optInt64(const std::string& key, int64_t& out) const;
bool optString(const std::string& key, std::string& out) const;
void putBool(const std::string& key, bool value);
void putInt32(const std::string& key, int32_t value);
void putInt64(const std::string& key, int64_t value);
void putString(const std::string& key, const std::string& value);
};

View File

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

View File

@ -1,14 +1,14 @@
#ifdef ESP_PLATFORM
#include "nvs_flash.h"
#include "Tactility/Preferences.h"
#include <Tactility/Preferences.h>
#include <Tactility/TactilityCore.h>
#define TAG "preferences"
#include <nvs_flash.h>
namespace tt {
constexpr auto* TAG = "Preferences";
bool Preferences::optBool(const std::string& key, bool& out) const {
nvs_handle_t handle;
if (nvs_open(namespace_, NVS_READWRITE, &handle) != ESP_OK) {
@ -37,6 +37,18 @@ bool Preferences::optInt32(const std::string& key, int32_t& out) const {
}
}
bool Preferences::optInt64(const std::string& key, int64_t& out) const {
nvs_handle_t handle;
if (nvs_open(namespace_, NVS_READWRITE, &handle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to open namespace %s", namespace_);
return false;
} else {
bool success = nvs_get_i64(handle, key.c_str(), &out) == ESP_OK;
nvs_close(handle);
return success;
}
}
bool Preferences::optString(const std::string& key, std::string& out) const {
nvs_handle_t handle;
if (nvs_open(namespace_, NVS_READWRITE, &handle) != ESP_OK) {
@ -63,6 +75,11 @@ bool Preferences::hasInt32(const std::string& key) const {
return optInt32(key, temp);
}
bool Preferences::hasInt64(const std::string& key) const {
int64_t temp;
return optInt64(key, temp);
}
bool Preferences::hasString(const std::string& key) const {
std::string temp;
return optString(key, temp);
@ -92,6 +109,18 @@ void Preferences::putInt32(const std::string& key, int32_t value) {
}
}
void Preferences::putInt64(const std::string& key, int64_t value) {
nvs_handle_t handle;
if (nvs_open(namespace_, NVS_READWRITE, &handle) == ESP_OK) {
if (nvs_set_i64(handle, key.c_str(), value) != ESP_OK) {
TT_LOG_E(TAG, "Failed to write %s:%s", namespace_, key.c_str());
}
nvs_close(handle);
} else {
TT_LOG_E(TAG, "Failed to open namespace %s", namespace_);
}
}
void Preferences::putString(const std::string& key, const std::string& text) {
nvs_handle_t handle;
if (nvs_open(namespace_, NVS_READWRITE, &handle) == ESP_OK) {

View File

@ -30,6 +30,11 @@ bool Preferences::hasInt32(const std::string& key) const {
return preferences.hasInt32(bundle_key);
}
bool Preferences::hasInt64(const std::string& key) const {
std::string bundle_key = get_bundle_key(namespace_, key);
return preferences.hasInt64(bundle_key);
}
bool Preferences::hasString(const std::string& key) const {
std::string bundle_key = get_bundle_key(namespace_, key);
return preferences.hasString(bundle_key);
@ -45,6 +50,11 @@ bool Preferences::optInt32(const std::string& key, int32_t& out) const {
return preferences.optInt32(bundle_key, out);
}
bool Preferences::optInt64(const std::string& key, int64_t& out) const {
std::string bundle_key = get_bundle_key(namespace_, key);
return preferences.optInt64(bundle_key, out);
}
bool Preferences::optString(const std::string& key, std::string& out) const {
std::string bundle_key = get_bundle_key(namespace_, key);
return preferences.optString(bundle_key, out);
@ -60,6 +70,11 @@ void Preferences::putInt32(const std::string& key, int32_t value) {
return preferences.putInt32(bundle_key, value);
}
void Preferences::putInt64(const std::string& key, int64_t value) {
std::string bundle_key = get_bundle_key(namespace_, key);
return preferences.putInt64(bundle_key, value);
}
void Preferences::putString(const std::string& key, const std::string& value) {
std::string bundle_key = get_bundle_key(namespace_, key);
return preferences.putString(bundle_key, value);

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;
}
}

View File

@ -20,6 +20,7 @@ class Bundle final {
enum class Type {
Bool,
Int32,
Int64,
String,
};
@ -28,6 +29,7 @@ class Bundle final {
union {
bool value_bool;
int32_t value_int32;
int64_t value_int64;
};
std::string value_string;
} Value;
@ -44,18 +46,22 @@ public:
bool getBool(const std::string& key) const;
int32_t getInt32(const std::string& key) const;
int64_t getInt64(const std::string& key) const;
std::string getString(const std::string& key) const;
bool hasBool(const std::string& key) const;
bool hasInt32(const std::string& key) const;
bool hasInt64(const std::string& key) const;
bool hasString(const std::string& key) const;
bool optBool(const std::string& key, bool& out) const;
bool optInt32(const std::string& key, int32_t& out) const;
bool optInt64(const std::string& key, int64_t& out) const;
bool optString(const std::string& key, std::string& out) const;
void putBool(const std::string& key, bool value);
void putInt32(const std::string& key, int32_t value);
void putInt64(const std::string& key, int64_t value);
void putString(const std::string& key, const std::string& value);
};

View File

@ -10,6 +10,10 @@ int32_t Bundle::getInt32(const std::string& key) const {
return this->entries.find(key)->second.value_int32;
}
int64_t Bundle::getInt64(const std::string& key) const {
return this->entries.find(key)->second.value_int64;
}
std::string Bundle::getString(const std::string& key) const {
return this->entries.find(key)->second.value_string;
}
@ -24,6 +28,11 @@ bool Bundle::hasInt32(const std::string& key) const {
return entry != std::end(this->entries) && entry->second.type == Type::Int32;
}
bool Bundle::hasInt64(const std::string& key) const {
auto entry = this->entries.find(key);
return entry != std::end(this->entries) && entry->second.type == Type::Int64;
}
bool Bundle::hasString(const std::string& key) const {
auto entry = this->entries.find(key);
return entry != std::end(this->entries) && entry->second.type == Type::String;
@ -49,6 +58,16 @@ bool Bundle::optInt32(const std::string& key, int32_t& out) const {
}
}
bool Bundle::optInt64(const std::string& key, int64_t& out) const {
auto entry = this->entries.find(key);
if (entry != std::end(this->entries) && entry->second.type == Type::Int64) {
out = entry->second.value_int32;
return true;
} else {
return false;
}
}
bool Bundle::optString(const std::string& key, std::string& out) const {
auto entry = this->entries.find(key);
if (entry != std::end(this->entries) && entry->second.type == Type::String) {
@ -75,6 +94,14 @@ void Bundle::putInt32(const std::string& key, int32_t value) {
};
}
void Bundle::putInt64(const std::string& key, int64_t value) {
this->entries[key] = {
.type = Type::Int64,
.value_int64 = value,
.value_string = ""
};
}
void Bundle::putString(const std::string& key, const std::string& value) {
this->entries[key] = {
.type = Type::String,