639fdd860e
- Introduced a reconciliation job structure to manage the reconciliation process for gateway channels. - Added methods to schedule and run reconciliation steps, including group, scene, and settings reconciliation. - Implemented a locking mechanism to ensure thread safety during reconciliation operations. - Enhanced command handling in GatewayRuntime to classify commands by priority (control, normal, maintenance). - Updated command enqueueing and processing to respect command priorities, ensuring maintenance commands are handled appropriately. - Added configuration options for enabling/disabling cache functionality in GatewayRuntime. - Improved logging to include cache status during runtime initialization. Co-authored-by: Copilot <copilot@github.com>
165 lines
5.2 KiB
C++
165 lines
5.2 KiB
C++
#pragma once
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <deque>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
#include "esp_err.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/semphr.h"
|
|
#include "gateway_core.hpp"
|
|
#include "nvs.h"
|
|
|
|
namespace gateway {
|
|
|
|
esp_err_t InitializeRuntimeNvs();
|
|
std::string ReadRuntimeSerialId();
|
|
|
|
class DaliDomainService;
|
|
|
|
struct WirelessInfo {
|
|
std::string ssid;
|
|
std::string password;
|
|
std::string mac;
|
|
std::string ip;
|
|
};
|
|
|
|
struct GatewayRuntimeConfig {
|
|
std::string_view project_name;
|
|
std::string_view version;
|
|
std::string serial_id;
|
|
bool default_ble_enabled{true};
|
|
bool default_cache_enabled{true};
|
|
size_t command_queue_capacity{16};
|
|
};
|
|
|
|
struct GatewayDeviceInfo {
|
|
std::string serial_id;
|
|
std::string type;
|
|
std::string project;
|
|
std::string version;
|
|
size_t dali_gateway_count{0};
|
|
bool ble_enabled{false};
|
|
std::optional<WirelessInfo> wlan;
|
|
};
|
|
|
|
class GatewaySettingsStore {
|
|
public:
|
|
GatewaySettingsStore();
|
|
~GatewaySettingsStore();
|
|
|
|
esp_err_t open();
|
|
void close();
|
|
|
|
bool getBleEnabled(bool default_value = false) const;
|
|
bool setBleEnabled(bool enabled);
|
|
|
|
bool getCacheEnabled(bool default_value = true) const;
|
|
bool setCacheEnabled(bool enabled);
|
|
|
|
std::optional<std::string> getWifiSsid() const;
|
|
std::optional<std::string> getWifiPassword() const;
|
|
bool setWifiCredentials(std::string_view ssid, std::string_view password);
|
|
bool clearWifiCredentials();
|
|
|
|
std::string getGatewayName(uint8_t gateway_id, std::string_view fallback) const;
|
|
bool setGatewayName(uint8_t gateway_id, std::string_view name);
|
|
|
|
private:
|
|
std::optional<std::string> readString(std::string_view key) const;
|
|
bool writeString(std::string_view key, std::string_view value);
|
|
std::string makeGatewayNameKey(uint8_t gateway_id) const;
|
|
|
|
mutable nvs_handle_t handle_{0};
|
|
};
|
|
|
|
class GatewayRuntime {
|
|
public:
|
|
enum class CommandDropReason {
|
|
kNone,
|
|
kDuplicate,
|
|
kQueueFull,
|
|
};
|
|
|
|
enum class CommandPriority : uint8_t {
|
|
kControl = 0,
|
|
kNormal = 1,
|
|
kMaintenance = 2,
|
|
};
|
|
|
|
GatewayRuntime(BootProfile profile, GatewayRuntimeConfig config,
|
|
DaliDomainService* dali_domain);
|
|
~GatewayRuntime();
|
|
|
|
esp_err_t start();
|
|
|
|
static std::vector<uint8_t> checksum(std::vector<uint8_t> frame);
|
|
static bool hasValidChecksum(const std::vector<uint8_t>& frame);
|
|
static bool isGatewayCommandFrame(const std::vector<uint8_t>& frame);
|
|
static std::vector<uint8_t> buildNotificationFrame(const std::vector<uint8_t>& payload);
|
|
static CommandPriority classifyCommandPriority(const std::vector<uint8_t>& command);
|
|
|
|
bool enqueueCommand(std::vector<uint8_t> command,
|
|
CommandPriority priority = CommandPriority::kNormal);
|
|
std::optional<std::vector<uint8_t>> popNextCommand();
|
|
void completeCurrentCommand();
|
|
bool hasPendingQueryCommand(const std::vector<uint8_t>& command) const;
|
|
bool hasPendingControlCommand(uint8_t gateway_id) const;
|
|
bool shouldYieldMaintenance(uint8_t gateway_id) const;
|
|
bool hasActiveCommand(uint8_t gateway_id) const;
|
|
bool hasActiveQueryCommand(uint8_t gateway_id) const;
|
|
CommandDropReason lastEnqueueDropReason() const;
|
|
|
|
void setGatewayCount(size_t gateway_count);
|
|
void setWirelessInfo(WirelessInfo info);
|
|
bool clearWirelessInfo();
|
|
void setCommandAddressResolver(std::function<uint8_t(uint8_t gw, uint8_t raw_addr)> resolver);
|
|
|
|
GatewayDeviceInfo deviceInfo() const;
|
|
bool bleEnabled() const;
|
|
bool setBleEnabled(bool enabled);
|
|
bool cacheEnabled() const;
|
|
bool setCacheEnabled(bool enabled);
|
|
std::string gatewayName(uint8_t gateway_id) const;
|
|
bool setGatewayName(uint8_t gateway_id, std::string_view name);
|
|
std::string gatewaySerialHex(uint8_t gateway_id) const;
|
|
std::string bleMacHex() const;
|
|
std::string bleGatewayName(uint8_t gateway_id, std::string_view gateway_name) const;
|
|
std::string defaultBleGatewayName() const;
|
|
|
|
private:
|
|
bool isQueryCommand(const std::vector<uint8_t>& command) const;
|
|
size_t pendingCommandCountLocked() const;
|
|
std::deque<std::vector<uint8_t>>& queueForPriorityLocked(CommandPriority priority);
|
|
const std::deque<std::vector<uint8_t>>& queueForPriorityLocked(CommandPriority priority) const;
|
|
std::optional<std::string> queryCommandKey(const std::vector<uint8_t>& command) const;
|
|
std::string defaultGatewayName(uint8_t gateway_id) const;
|
|
std::vector<uint8_t> serialBytes() const;
|
|
static std::string toHex(const std::vector<uint8_t>& bytes);
|
|
|
|
BootProfile profile_;
|
|
GatewayRuntimeConfig config_;
|
|
DaliDomainService* dali_domain_;
|
|
GatewaySettingsStore settings_;
|
|
std::optional<std::vector<uint8_t>> current_command_;
|
|
CommandPriority current_command_priority_{CommandPriority::kNormal};
|
|
std::deque<std::vector<uint8_t>> control_commands_;
|
|
std::deque<std::vector<uint8_t>> normal_commands_;
|
|
std::deque<std::vector<uint8_t>> maintenance_commands_;
|
|
mutable std::map<uint8_t, std::string> gateway_names_;
|
|
size_t gateway_count_{0};
|
|
bool ble_enabled_{false};
|
|
bool cache_enabled_{true};
|
|
CommandDropReason last_enqueue_drop_reason_{CommandDropReason::kNone};
|
|
std::function<uint8_t(uint8_t gw, uint8_t raw_addr)> command_address_resolver_;
|
|
std::optional<WirelessInfo> wireless_info_;
|
|
SemaphoreHandle_t command_lock_{nullptr};
|
|
};
|
|
|
|
} // namespace gateway
|