From 5805d9ea14edfb54ef42e539de24c612d258b3ed Mon Sep 17 00:00:00 2001 From: Peter Siegmund Date: Fri, 20 Jun 2025 23:43:31 +0200 Subject: [PATCH] move PersistenceManager into platform project Signed-off-by: Peter Siegmund --- components/insa/CMakeLists.txt | 3 - main/CMakeLists.txt | 2 + .../common => main}/PersistenceManager.cpp | 5 +- .../common => main}/PersistenceManager.h | 2 +- main/app_task.cpp | 2 +- src/manager/PersistenceManager.cpp | 312 ++++++++++-------- 6 files changed, 187 insertions(+), 139 deletions(-) rename {components/insa/src/common => main}/PersistenceManager.cpp (98%) rename {components/insa/include/common => main}/PersistenceManager.h (96%) diff --git a/components/insa/CMakeLists.txt b/components/insa/CMakeLists.txt index 69a145f..e5a8995 100644 --- a/components/insa/CMakeLists.txt +++ b/components/insa/CMakeLists.txt @@ -16,10 +16,7 @@ set(SOURCE_FILES if (DEFINED ENV{IDF_PATH}) idf_component_register(SRCS ${SOURCE_FILES} - src/common/PersistenceManager.cpp INCLUDE_DIRS "include" - REQUIRES - nvs_flash PRIV_REQUIRES u8g2 ) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index b962100..b9e8eaf 100755 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,6 +1,7 @@ idf_component_register(SRCS "main.cpp" "app_task.cpp" + "PersistenceManager.cpp" "button_handling.c" "hal/u8g2_esp32_hal.c" INCLUDE_DIRS "." @@ -9,4 +10,5 @@ idf_component_register(SRCS u8g2 driver esp_timer + nvs_flash ) diff --git a/components/insa/src/common/PersistenceManager.cpp b/main/PersistenceManager.cpp similarity index 98% rename from components/insa/src/common/PersistenceManager.cpp rename to main/PersistenceManager.cpp index 7e0d299..911d318 100644 --- a/components/insa/src/common/PersistenceManager.cpp +++ b/main/PersistenceManager.cpp @@ -1,5 +1,4 @@ -#ifdef ESP_PLATFORM -#include "common/PersistenceManager.h" +#include "PersistenceManager.h" #include #include @@ -291,5 +290,3 @@ std::string PersistenceManager::GetValueImpl(const std::string &key, const std:: return value; } - -#endif // ESP32 \ No newline at end of file diff --git a/components/insa/include/common/PersistenceManager.h b/main/PersistenceManager.h similarity index 96% rename from components/insa/include/common/PersistenceManager.h rename to main/PersistenceManager.h index c846264..85c5a6f 100644 --- a/components/insa/include/common/PersistenceManager.h +++ b/main/PersistenceManager.h @@ -1,6 +1,6 @@ #pragma once -#include "IPersistenceManager.h" +#include "../components/insa/include/common/IPersistenceManager.h" #include #include diff --git a/main/app_task.cpp b/main/app_task.cpp index 0805257..9bf3dbb 100644 --- a/main/app_task.cpp +++ b/main/app_task.cpp @@ -7,7 +7,7 @@ #include "button_handling.h" #include "common/InactivityTracker.h" -#include "common/PersistenceManager.h" +#include "PersistenceManager.h" #include "ui/ScreenSaver.h" #include "ui/SplashScreen.h" diff --git a/src/manager/PersistenceManager.cpp b/src/manager/PersistenceManager.cpp index de1ff22..ef68e39 100644 --- a/src/manager/PersistenceManager.cpp +++ b/src/manager/PersistenceManager.cpp @@ -3,73 +3,86 @@ #include -#ifndef ESP_PLATFORM -PersistenceManager::PersistenceManager(std::string filename) - : filename_(std::move(filename)) { +PersistenceManager::PersistenceManager(std::string filename) : filename_(std::move(filename)) +{ // Initialize SDL3 if not already done - if (!SDL_WasInit(SDL_INIT_EVENTS)) { + if (!SDL_WasInit(SDL_INIT_EVENTS)) + { SDL_Init(SDL_INIT_EVENTS); } } -PersistenceManager::~PersistenceManager() { +PersistenceManager::~PersistenceManager() +{ Save(); // Automatically save on destruction } -bool PersistenceManager::HasKey(const std::string& key) const { +bool PersistenceManager::HasKey(const std::string &key) const +{ return data_.contains(key); } -void PersistenceManager::RemoveKey(const std::string& key) { +void PersistenceManager::RemoveKey(const std::string &key) +{ data_.erase(key); } -void PersistenceManager::Clear() { +void PersistenceManager::Clear() +{ data_.clear(); } -bool PersistenceManager::Save() { +bool PersistenceManager::Save() +{ return SaveToFile(filename_); } -bool PersistenceManager::Load() { +bool PersistenceManager::Load() +{ return LoadFromFile(filename_); } -bool PersistenceManager::SaveToFile(const std::string& filename) { - SDL_IOStream* stream = SDL_IOFromFile(filename.c_str(), "wb"); - if (!stream) { +bool PersistenceManager::SaveToFile(const std::string &filename) +{ + SDL_IOStream *stream = SDL_IOFromFile(filename.c_str(), "wb"); + if (!stream) + { SDL_Log("Error opening file for writing: %s", SDL_GetError()); return false; } // Write number of entries const size_t count = data_.size(); - if (SDL_WriteIO(stream, &count, sizeof(count)) != sizeof(count)) { + if (SDL_WriteIO(stream, &count, sizeof(count)) != sizeof(count)) + { SDL_Log("Error writing count: %s", SDL_GetError()); SDL_CloseIO(stream); return false; } // Write each entry - for (const auto& [key, value] : data_) { + for (const auto &[key, value] : data_) + { // Write key length size_t keyLength = key.length(); - if (SDL_WriteIO(stream, &keyLength, sizeof(keyLength)) != sizeof(keyLength)) { + if (SDL_WriteIO(stream, &keyLength, sizeof(keyLength)) != sizeof(keyLength)) + { SDL_Log("Error writing key length: %s", SDL_GetError()); SDL_CloseIO(stream); return false; } // Write key - if (SDL_WriteIO(stream, key.c_str(), keyLength) != keyLength) { + if (SDL_WriteIO(stream, key.c_str(), keyLength) != keyLength) + { SDL_Log("Error writing key: %s", SDL_GetError()); SDL_CloseIO(stream); return false; } // Write value - if (!WriteValueToStream(stream, value)) { + if (!WriteValueToStream(stream, value)) + { SDL_CloseIO(stream); return false; } @@ -79,9 +92,11 @@ bool PersistenceManager::SaveToFile(const std::string& filename) { return true; } -bool PersistenceManager::LoadFromFile(const std::string& filename) { - SDL_IOStream* stream = SDL_IOFromFile(filename.c_str(), "rb"); - if (!stream) { +bool PersistenceManager::LoadFromFile(const std::string &filename) +{ + SDL_IOStream *stream = SDL_IOFromFile(filename.c_str(), "rb"); + if (!stream) + { SDL_Log("File not found or error opening: %s", SDL_GetError()); return false; } @@ -90,17 +105,20 @@ bool PersistenceManager::LoadFromFile(const std::string& filename) { // Read number of entries size_t count; - if (SDL_ReadIO(stream, &count, sizeof(count)) != sizeof(count)) { + if (SDL_ReadIO(stream, &count, sizeof(count)) != sizeof(count)) + { SDL_Log("Error reading count: %s", SDL_GetError()); SDL_CloseIO(stream); return false; } // Read each entry - for (size_t i = 0; i < count; ++i) { + for (size_t i = 0; i < count; ++i) + { // Read key length size_t keyLength; - if (SDL_ReadIO(stream, &keyLength, sizeof(keyLength)) != sizeof(keyLength)) { + if (SDL_ReadIO(stream, &keyLength, sizeof(keyLength)) != sizeof(keyLength)) + { SDL_Log("Error reading key length: %s", SDL_GetError()); SDL_CloseIO(stream); return false; @@ -108,7 +126,8 @@ bool PersistenceManager::LoadFromFile(const std::string& filename) { // Read key std::string key(keyLength, '\0'); - if (SDL_ReadIO(stream, key.data(), keyLength) != keyLength) { + if (SDL_ReadIO(stream, key.data(), keyLength) != keyLength) + { SDL_Log("Error reading key: %s", SDL_GetError()); SDL_CloseIO(stream); return false; @@ -116,7 +135,8 @@ bool PersistenceManager::LoadFromFile(const std::string& filename) { // Read value ValueType value; - if (!ReadValueFromStream(stream, value)) { + if (!ReadValueFromStream(stream, value)) + { SDL_CloseIO(stream); return false; } @@ -129,176 +149,208 @@ bool PersistenceManager::LoadFromFile(const std::string& filename) { } // Template-specific implementations -void PersistenceManager::SetValueImpl(const std::string& key, bool value) { +void PersistenceManager::SetValueImpl(const std::string &key, bool value) +{ data_[key] = value; } -void PersistenceManager::SetValueImpl(const std::string& key, int value) { +void PersistenceManager::SetValueImpl(const std::string &key, int value) +{ data_[key] = value; } -void PersistenceManager::SetValueImpl(const std::string& key, float value) { +void PersistenceManager::SetValueImpl(const std::string &key, float value) +{ data_[key] = value; } -void PersistenceManager::SetValueImpl(const std::string& key, double value) { +void PersistenceManager::SetValueImpl(const std::string &key, double value) +{ data_[key] = value; } -void PersistenceManager::SetValueImpl(const std::string& key, const std::string& value) { +void PersistenceManager::SetValueImpl(const std::string &key, const std::string &value) +{ data_[key] = value; } -bool PersistenceManager::GetValueImpl(const std::string& key, bool defaultValue) const { - if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative(it->second)) { +bool PersistenceManager::GetValueImpl(const std::string &key, bool defaultValue) const +{ + if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative(it->second)) + { return std::get(it->second); } return defaultValue; } -int PersistenceManager::GetValueImpl(const std::string& key, int defaultValue) const { - if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative(it->second)) { +int PersistenceManager::GetValueImpl(const std::string &key, int defaultValue) const +{ + if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative(it->second)) + { return std::get(it->second); } return defaultValue; } -float PersistenceManager::GetValueImpl(const std::string& key, float defaultValue) const { - if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative(it->second)) { +float PersistenceManager::GetValueImpl(const std::string &key, float defaultValue) const +{ + if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative(it->second)) + { return std::get(it->second); } return defaultValue; } -double PersistenceManager::GetValueImpl(const std::string& key, double defaultValue) const { - if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative(it->second)) { +double PersistenceManager::GetValueImpl(const std::string &key, double defaultValue) const +{ + if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative(it->second)) + { return std::get(it->second); } return defaultValue; } -std::string PersistenceManager::GetValueImpl(const std::string& key, const std::string& defaultValue) const { - if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative(it->second)) { +std::string PersistenceManager::GetValueImpl(const std::string &key, const std::string &defaultValue) const +{ + if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative(it->second)) + { return std::get(it->second); } return defaultValue; } // Private helper methods -bool PersistenceManager::WriteValueToStream(SDL_IOStream* stream, const ValueType& value) { +bool PersistenceManager::WriteValueToStream(SDL_IOStream *stream, const ValueType &value) +{ const TypeId typeId = GetTypeId(value); - + // Write type ID - if (SDL_WriteIO(stream, &typeId, sizeof(typeId)) != sizeof(typeId)) { + if (SDL_WriteIO(stream, &typeId, sizeof(typeId)) != sizeof(typeId)) + { SDL_Log("Error writing type ID: %s", SDL_GetError()); return false; } // Write value based on type - switch (typeId) { - case TypeId::BOOL: { - const bool val = std::get(value); - return SDL_WriteIO(stream, &val, sizeof(val)) == sizeof(val); - } - case TypeId::INT: { - const int val = std::get(value); - return SDL_WriteIO(stream, &val, sizeof(val)) == sizeof(val); - } - case TypeId::FLOAT: { - const float val = std::get(value); - return SDL_WriteIO(stream, &val, sizeof(val)) == sizeof(val); - } - case TypeId::DOUBLE: { - const double val = std::get(value); - return SDL_WriteIO(stream, &val, sizeof(val)) == sizeof(val); - } - case TypeId::STRING: { - const auto& str = std::get(value); - const size_t length = str.length(); - - // Write string length - if (SDL_WriteIO(stream, &length, sizeof(length)) != sizeof(length)) { - return false; - } - - // Write string data - return SDL_WriteIO(stream, str.c_str(), length) == length; + switch (typeId) + { + case TypeId::BOOL: { + const bool val = std::get(value); + return SDL_WriteIO(stream, &val, sizeof(val)) == sizeof(val); + } + case TypeId::INT: { + const int val = std::get(value); + return SDL_WriteIO(stream, &val, sizeof(val)) == sizeof(val); + } + case TypeId::FLOAT: { + const float val = std::get(value); + return SDL_WriteIO(stream, &val, sizeof(val)) == sizeof(val); + } + case TypeId::DOUBLE: { + const double val = std::get(value); + return SDL_WriteIO(stream, &val, sizeof(val)) == sizeof(val); + } + case TypeId::STRING: { + const auto &str = std::get(value); + const size_t length = str.length(); + + // Write string length + if (SDL_WriteIO(stream, &length, sizeof(length)) != sizeof(length)) + { + return false; } + + // Write string data + return SDL_WriteIO(stream, str.c_str(), length) == length; + } } return false; } -bool PersistenceManager::ReadValueFromStream(SDL_IOStream* stream, ValueType& value) { +bool PersistenceManager::ReadValueFromStream(SDL_IOStream *stream, ValueType &value) +{ TypeId typeId; - + // Read type ID - if (SDL_ReadIO(stream, &typeId, sizeof(typeId)) != sizeof(typeId)) { + if (SDL_ReadIO(stream, &typeId, sizeof(typeId)) != sizeof(typeId)) + { SDL_Log("Error reading type ID: %s", SDL_GetError()); return false; } // Read value based on type - switch (typeId) { - case TypeId::BOOL: { - bool val; - if (SDL_ReadIO(stream, &val, sizeof(val)) == sizeof(val)) { - value = val; - return true; - } - break; - } - case TypeId::INT: { - int val; - if (SDL_ReadIO(stream, &val, sizeof(val)) == sizeof(val)) { - value = val; - return true; - } - break; - } - case TypeId::FLOAT: { - float val; - if (SDL_ReadIO(stream, &val, sizeof(val)) == sizeof(val)) { - value = val; - return true; - } - break; - } - case TypeId::DOUBLE: { - double val; - if (SDL_ReadIO(stream, &val, sizeof(val)) == sizeof(val)) { - value = val; - return true; - } - break; - } - case TypeId::STRING: { - size_t length; - if (SDL_ReadIO(stream, &length, sizeof(length)) != sizeof(length)) { - return false; - } - - std::string str(length, '\0'); - if (SDL_ReadIO(stream, str.data(), length) == length) { - value = str; - return true; - } - break; + switch (typeId) + { + case TypeId::BOOL: { + bool val; + if (SDL_ReadIO(stream, &val, sizeof(val)) == sizeof(val)) + { + value = val; + return true; } + break; } - + case TypeId::INT: { + int val; + if (SDL_ReadIO(stream, &val, sizeof(val)) == sizeof(val)) + { + value = val; + return true; + } + break; + } + case TypeId::FLOAT: { + float val; + if (SDL_ReadIO(stream, &val, sizeof(val)) == sizeof(val)) + { + value = val; + return true; + } + break; + } + case TypeId::DOUBLE: { + double val; + if (SDL_ReadIO(stream, &val, sizeof(val)) == sizeof(val)) + { + value = val; + return true; + } + break; + } + case TypeId::STRING: { + size_t length; + if (SDL_ReadIO(stream, &length, sizeof(length)) != sizeof(length)) + { + return false; + } + + std::string str(length, '\0'); + if (SDL_ReadIO(stream, str.data(), length) == length) + { + value = str; + return true; + } + break; + } + } + SDL_Log("Error reading value: %s", SDL_GetError()); return false; } -PersistenceManager::TypeId PersistenceManager::GetTypeId(const ValueType& value) +PersistenceManager::TypeId PersistenceManager::GetTypeId(const ValueType &value) { - if (std::holds_alternative(value)) return TypeId::BOOL; - if (std::holds_alternative(value)) return TypeId::INT; - if (std::holds_alternative(value)) return TypeId::FLOAT; - if (std::holds_alternative(value)) return TypeId::DOUBLE; - if (std::holds_alternative(value)) return TypeId::STRING; - + if (std::holds_alternative(value)) + return TypeId::BOOL; + if (std::holds_alternative(value)) + return TypeId::INT; + if (std::holds_alternative(value)) + return TypeId::FLOAT; + if (std::holds_alternative(value)) + return TypeId::DOUBLE; + if (std::holds_alternative(value)) + return TypeId::STRING; + // Should never be reached return TypeId::BOOL; -} -#endif // !ESP_PLATFORM \ No newline at end of file +} \ No newline at end of file