chore: Refactor stores to use PersistableStore CRTP template (#2464)

This commit is contained in:
Uri Tauber
2026-07-08 12:44:52 +03:00
committed by GitHub
parent 5776911af0
commit c2c1badc7e
16 changed files with 277 additions and 667 deletions
+8 -23
View File
@@ -1,4 +1,7 @@
#pragma once
#include <ArduinoJson.h>
#include <PersistableStore.h>
#include <string>
#include <vector>
@@ -9,37 +12,25 @@ struct OpdsServer {
std::string password; // Plaintext in memory; obfuscated with hardware key on disk
};
class OpdsServerStore;
namespace JsonSettingsIO {
bool saveOpds(const OpdsServerStore& store, const char* path);
bool loadOpds(OpdsServerStore& store, const char* json, bool* needsResave);
} // namespace JsonSettingsIO
/**
* Singleton class for storing OPDS server configurations on the SD card.
* Passwords are XOR-obfuscated with the device's unique hardware MAC address
* and base64-encoded before writing to JSON.
*/
class OpdsServerStore {
class OpdsServerStore : public PersistableStore<OpdsServerStore> {
private:
static OpdsServerStore instance;
std::vector<OpdsServer> servers;
static constexpr size_t MAX_SERVERS = 8;
OpdsServerStore() = default;
friend bool JsonSettingsIO::saveOpds(const OpdsServerStore&, const char*);
friend bool JsonSettingsIO::loadOpds(OpdsServerStore&, const char*, bool*);
friend class PersistableStore<OpdsServerStore>;
public:
OpdsServerStore(const OpdsServerStore&) = delete;
OpdsServerStore& operator=(const OpdsServerStore&) = delete;
static OpdsServerStore& getInstance() { return instance; }
bool saveToFile() const;
bool loadFromFile();
static const char* getFilePath() { return "/.crosspoint/opds.json"; }
void toJson(JsonDocument& doc) const;
bool fromJson(JsonVariantConst doc);
bool addServer(const OpdsServer& server);
bool updateServer(size_t index, const OpdsServer& server);
@@ -49,12 +40,6 @@ class OpdsServerStore {
const OpdsServer* getServer(size_t index) const;
size_t getCount() const { return servers.size(); }
bool hasServers() const { return !servers.empty(); }
/**
* Migrate from legacy single-server settings in CrossPointSettings.
* Called once during first load if no opds.json exists.
*/
bool migrateFromSettings();
};
#define OPDS_STORE OpdsServerStore::getInstance()