81 lines
2.7 KiB
C++
81 lines
2.7 KiB
C++
#pragma once
|
|
|
|
/**
|
|
* @file ConfigManager.h
|
|
* @brief Persistent storage of application configuration using LittleFS + JSON.
|
|
*
|
|
* Responsibilities:
|
|
* - Load/save AppConfig (pages, tiles, WiFi credentials) to /config.json
|
|
* - Provide typed accessors for individual fields
|
|
* - Expose helpers for the web UI to read/write pages as JSON fragments
|
|
*/
|
|
|
|
#include "Config.h"
|
|
#include <ArduinoJson.h>
|
|
|
|
class ConfigManager {
|
|
public:
|
|
/**
|
|
* @brief Initialise LittleFS and load configuration from disk.
|
|
* Must be called before any other method.
|
|
* @return true on success, false if LittleFS mount failed.
|
|
*/
|
|
bool begin();
|
|
|
|
/** @brief Access the live configuration object (read-only). */
|
|
const AppConfig& config() const { return _cfg; }
|
|
|
|
/** @brief Write a complete AppConfig and persist it immediately. */
|
|
bool save(const AppConfig& cfg);
|
|
|
|
/** @brief Persist the current in-memory config to disk. */
|
|
bool persist();
|
|
|
|
// ── Page helpers ──────────────────────────────────────────────────────────
|
|
|
|
/** @brief Return the currently active page index. */
|
|
uint8_t activePage() const { return _cfg.activePage; }
|
|
|
|
/** @brief Switch active page; persists. */
|
|
bool setActivePage(uint8_t index);
|
|
|
|
/** @brief Advance to the next page (wraps around). */
|
|
void nextPage();
|
|
|
|
/** @brief Go back to the previous page (wraps around). */
|
|
void prevPage();
|
|
|
|
/** @brief Update a single tile in a given page; persists. */
|
|
bool setTile(uint8_t page, uint8_t tile, DataField field, const char* label = nullptr);
|
|
|
|
// ── JSON serialisation for the web UI ─────────────────────────────────────
|
|
|
|
/**
|
|
* @brief Serialise the full configuration to a JSON string.
|
|
* @param[out] out Destination String.
|
|
*/
|
|
void toJson(String& out) const;
|
|
|
|
/**
|
|
* @brief Parse a JSON string produced by the web UI and merge it into
|
|
* the current config. Persists on success.
|
|
* @return true if parsing and saving succeeded.
|
|
*/
|
|
bool fromJson(const String& json);
|
|
|
|
// ── Factory reset ─────────────────────────────────────────────────────────
|
|
void resetToDefaults();
|
|
|
|
private:
|
|
static constexpr const char* CONFIG_PATH = "/config.json";
|
|
|
|
AppConfig _cfg;
|
|
|
|
bool _load();
|
|
bool _write();
|
|
void _applyDefaults();
|
|
};
|
|
|
|
// Singleton accessor
|
|
extern ConfigManager configManager;
|