// // Created by Johnathon Slightham on 2025-06-12. // #ifndef CONFIGMANAGER_H #define CONFIGMANAGER_H #include #include #include #include #include #include "flatbuffers_generated/RobotModule_generated.h" // Singleton to r/w config from the ESP32 nvs (thread safe and cached) class ConfigManager { public: ConfigManager(const ConfigManager&) = delete; void operator=(ConfigManager &) = delete; static ConfigManager& get_instance() { // Thread safe as of C++11 static ConfigManager instance; instance.init_config(); return instance; } uint16_t get_module_id() const; ModuleType get_module_type() const; std::string get_wifi_ssid() const; std::string get_wifi_password() const; void set_module_id(uint16_t id); void set_module_type(ModuleType type); void set_wifi_ssid(const char* ssid); void set_wifi_password(const char* password); private: ConfigManager() = default; esp_err_t get_string(const char *key, std::string &out) const; esp_err_t get_uint16(const char *key, uint16_t *out) const; static esp_err_t nvs_set_cpp_str(nvs_handle_t handle, const std::string& key, const std::string& str); static esp_err_t nvs_get_cpp_str(nvs_handle_t handle, const std::string& key, std::string& out_str, size_t *length); template std::optional get_config_from_cache(const std::string& key) const; template esp_err_t set(const char* key, T value); template bool write_to_cache(const std::string& key, T value); void init_config(); std::unordered_map> cache; mutable std::shared_mutex rw_lock; bool initialized = false; }; #endif //CONFIGMANAGER_H