Compare commits

..

No commits in common. "fbfda9d3e8ab6816b0e93fe816f74e309ad233e4" and "2e5f6cc0e82b7723ca138877c4614c6fa01e104b" have entirely different histories.

7 changed files with 5 additions and 124 deletions

View File

@ -24,17 +24,14 @@ 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

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

View File

@ -1,14 +1,14 @@
#ifdef ESP_PLATFORM
#include <Tactility/Preferences.h>
#include "nvs_flash.h"
#include "Tactility/Preferences.h"
#include <Tactility/TactilityCore.h>
#include <nvs_flash.h>
#define TAG "preferences"
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,18 +37,6 @@ 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) {
@ -75,11 +63,6 @@ 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);
@ -109,18 +92,6 @@ 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,11 +30,6 @@ 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);
@ -50,11 +45,6 @@ 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);
@ -70,11 +60,6 @@ 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,7 +1,5 @@
#include "Tactility/network/NtpPrivate.h"
#include <Tactility/Preferences.h>
#ifdef ESP_PLATFORM
#include <Tactility/kernel/SystemEvents.h>
#include <Tactility/TactilityCore.h>
@ -13,35 +11,10 @@
namespace tt::network::ntp {
static bool processedSyncEvent = false;
#ifdef ESP_PLATFORM
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) {
static void onTimeSynced(struct timeval* tv) {
TT_LOG_I(TAG, "Time synced (%llu)", tv->tv_sec);
processedSyncEvent = true;
esp_netif_sntp_deinit();
storeTimeInNvs();
kernel::publishSystemEvent(kernel::SystemEvent::Time);
}
@ -54,13 +27,8 @@ void init() {
#else
void init() {
processedSyncEvent = true;
}
#endif
bool isSynced() {
return processedSyncEvent;
}
}

View File

@ -20,7 +20,6 @@ class Bundle final {
enum class Type {
Bool,
Int32,
Int64,
String,
};
@ -29,7 +28,6 @@ class Bundle final {
union {
bool value_bool;
int32_t value_int32;
int64_t value_int64;
};
std::string value_string;
} Value;
@ -46,22 +44,18 @@ 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,10 +10,6 @@ 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;
}
@ -28,11 +24,6 @@ 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;
@ -58,16 +49,6 @@ 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) {
@ -94,14 +75,6 @@ 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,