Files
gateway/components/gateway_cache/include/gateway_cache.hpp
T
Tony 555c2ec5bd Refactor GatewayController to integrate DaliGateway types
- Updated GatewayController to use DaliGateway types instead of GatewayCache types for channel flags, targets, and states.
- Modified methods to reflect changes in data structures, including flag checks and state management.
- Adjusted cache handling to utilize DaliGatewayCache for address and group management.
- Ensured compatibility with existing functionality while enhancing clarity and maintainability of the code.

Signed-off-by: Tony <tonylu@tony-cloud.com>
2026-08-01 01:19:13 +08:00

106 lines
3.2 KiB
C++

#pragma once
#include <array>
#include <cstdint>
#include <map>
#include <optional>
#include <string>
#include <vector>
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "nvs.h"
class DaliGatewayCache;
namespace gateway {
struct GatewayCacheConfig {
std::string storage_namespace{"gateway_rt"};
bool cache_enabled{true};
uint32_t flush_interval_ms{10000};
uint32_t task_stack_size{4096};
UBaseType_t task_priority{3};
};
class GatewayCache {
public:
struct SceneEntry {
bool enabled{false};
uint8_t brightness{254};
uint8_t color_mode{2};
uint8_t data1{0};
uint8_t data2{0};
uint8_t data3{0};
std::string name;
};
struct GroupEntry {
bool enabled{false};
uint8_t target_type{2};
uint8_t target_value{0};
std::string name;
};
using SceneStore = std::array<SceneEntry, 16>;
using GroupStore = std::array<GroupEntry, 16>;
GatewayCache(DaliGatewayCache& dali_cache, GatewayCacheConfig config = {});
~GatewayCache();
esp_err_t start();
SceneStore scenes(uint8_t gateway_id);
GroupStore groups(uint8_t gateway_id);
SceneEntry scene(uint8_t gateway_id, uint8_t scene_id);
GroupEntry group(uint8_t gateway_id, uint8_t group_id);
bool setSceneEnabled(uint8_t gateway_id, uint8_t scene_id, bool enabled);
bool setSceneDetail(uint8_t gateway_id, uint8_t scene_id, uint8_t brightness,
uint8_t color_mode, uint8_t data1, uint8_t data2, uint8_t data3);
bool setSceneName(uint8_t gateway_id, uint8_t scene_id, std::string_view name);
bool deleteScene(uint8_t gateway_id, uint8_t scene_id);
std::pair<uint8_t, uint8_t> sceneMask(uint8_t gateway_id);
bool setGroupEnabled(uint8_t gateway_id, uint8_t group_id, bool enabled);
bool setGroupDetail(uint8_t gateway_id, uint8_t group_id, uint8_t target_type,
uint8_t target_value);
bool setGroupName(uint8_t gateway_id, uint8_t group_id, std::string_view name);
bool deleteGroup(uint8_t gateway_id, uint8_t group_id);
std::pair<uint8_t, uint8_t> groupMask(uint8_t gateway_id);
bool cacheEnabled() const;
bool setCacheEnabled(bool enabled);
private:
static void TaskEntry(void* arg);
void taskLoop();
bool flushDirty();
bool openStorageLocked();
void closeStorageLocked();
bool persistSceneLocked(uint8_t gateway_id, uint8_t scene_id, const SceneEntry& scene);
bool persistGroupLocked(uint8_t gateway_id, uint8_t group_id, const GroupEntry& group);
bool commitStorageLocked();
SceneStore& ensureSceneStoreLocked(uint8_t gateway_id);
GroupStore& ensureGroupStoreLocked(uint8_t gateway_id);
void loadSceneStoreLocked(uint8_t gateway_id, SceneStore& scenes);
void loadGroupStoreLocked(uint8_t gateway_id, GroupStore& groups);
std::string readStringLocked(std::string_view key);
bool writeStringLocked(std::string_view key, std::string_view value);
bool eraseKeyLocked(std::string_view key);
DaliGatewayCache& dali_cache_;
GatewayCacheConfig config_;
TaskHandle_t task_handle_{nullptr};
SemaphoreHandle_t lock_{nullptr};
nvs_handle_t storage_{0};
std::map<uint8_t, SceneStore> scenes_;
std::map<uint8_t, GroupStore> groups_;
bool dirty_{false};
};
} // namespace gateway