Refactor GatewayController to integrate GatewayCache

- Updated CMakeLists.txt to require gateway_cache component.
- Modified gateway_controller.hpp to include GatewayCache and adjust constructor.
- Removed internal scene and group management logic from GatewayController, delegating to GatewayCache.
- Simplified scene and group operations by utilizing GatewayCache methods for enabling, setting details, and deleting.
- Eliminated NVS storage handling code, as scene and group data is now managed by GatewayCache.
- Updated command handling methods to use cached data instead of internal structures.

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
Tony
2026-05-01 04:39:58 +08:00
parent d16c289626
commit 70c39ea1e1
10 changed files with 781 additions and 400 deletions
@@ -3,15 +3,14 @@
#include <array>
#include <cstdint>
#include <functional>
#include <map>
#include <string>
#include <string_view>
#include <vector>
#include "gateway_cache.hpp"
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs.h"
namespace gateway {
@@ -64,6 +63,7 @@ class GatewayController {
using GatewayNameSink = std::function<void(uint8_t gateway_id)>;
GatewayController(GatewayRuntime& runtime, DaliDomainService& dali_domain,
GatewayCache& cache,
GatewayControllerConfig config = {});
~GatewayController();
@@ -83,26 +83,6 @@ class GatewayController {
GatewayControllerSnapshot snapshot();
private:
struct InternalScene {
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 InternalGroup {
bool enabled{false};
uint8_t target_type{2};
uint8_t target_value{0};
std::string name;
};
using SceneStore = std::array<InternalScene, 16>;
using GroupStore = std::array<InternalGroup, 16>;
static void TaskEntry(void* arg);
void taskLoop();
void dispatchCommand(const std::vector<uint8_t>& command);
@@ -124,11 +104,6 @@ class GatewayController {
static int shortAddressFromRaw(uint8_t raw_addr);
static int reverseInRange(int value, int min_value, int max_value);
SceneStore& sceneStore(uint8_t gateway_id);
GroupStore& groupStore(uint8_t gateway_id);
InternalScene* scene(uint8_t gateway_id, uint8_t scene_id);
InternalGroup* 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);
@@ -151,33 +126,15 @@ class GatewayController {
void handleInternalSceneCommand(uint8_t gateway_id, const std::vector<uint8_t>& command);
void handleInternalGroupCommand(uint8_t gateway_id, const std::vector<uint8_t>& command);
bool openStorage();
void closeStorage();
void loadSceneStore(uint8_t gateway_id, SceneStore& scenes);
void loadGroupStore(uint8_t gateway_id, GroupStore& groups);
bool saveScene(uint8_t gateway_id, uint8_t scene_id);
bool deleteSceneStorage(uint8_t gateway_id, uint8_t scene_id);
bool saveSceneName(uint8_t gateway_id, uint8_t scene_id, std::string_view name);
bool deleteSceneNameStorage(uint8_t gateway_id, uint8_t scene_id);
bool saveGroup(uint8_t gateway_id, uint8_t group_id);
bool deleteGroupStorage(uint8_t gateway_id, uint8_t group_id);
bool saveGroupName(uint8_t gateway_id, uint8_t group_id, std::string_view name);
bool deleteGroupNameStorage(uint8_t gateway_id, uint8_t group_id);
std::string readString(std::string_view key) const;
bool writeString(std::string_view key, std::string_view value);
bool eraseKey(std::string_view key);
GatewayRuntime& runtime_;
DaliDomainService& dali_domain_;
GatewayCache& cache_;
GatewayControllerConfig config_;
TaskHandle_t task_handle_{nullptr};
nvs_handle_t storage_{0};
std::vector<NotificationSink> notification_sinks_;
std::vector<BleStateSink> ble_state_sinks_;
std::vector<WifiStateSink> wifi_state_sinks_;
std::vector<GatewayNameSink> gateway_name_sinks_;
std::map<uint8_t, SceneStore> scenes_;
std::map<uint8_t, GroupStore> groups_;
bool setup_mode_{false};
bool wireless_setup_mode_{false};
bool ble_enabled_{false};