feat: implement device name handling and update gateway name features

Signed-off-by: Tony <tonylu@tony-cloud.com>
This commit is contained in:
Tony
2026-06-12 01:26:31 +08:00
parent dceede8602
commit 40a0e8e303
11 changed files with 192 additions and 23 deletions
@@ -74,6 +74,8 @@ class GatewaySettingsStore {
bool setWifiCredentials(std::string_view ssid, std::string_view password);
bool clearWifiCredentials();
std::string getDeviceName(std::string_view fallback) const;
bool setDeviceName(std::string_view name);
std::string getGatewayName(uint8_t gateway_id, std::string_view fallback) const;
bool setGatewayName(uint8_t gateway_id, std::string_view name);
uint8_t getChannelGatewayId(uint8_t channel_index, uint8_t fallback) const;
@@ -140,6 +142,8 @@ class GatewayRuntime {
bool setCacheEnabled(bool enabled);
uint8_t gatewayIdForChannel(uint8_t channel_index, uint8_t fallback) const;
bool setGatewayIdForChannel(uint8_t channel_index, uint8_t gateway_id);
std::string deviceName() const;
bool setDeviceName(std::string_view name);
std::string gatewayName(uint8_t gateway_id) const;
bool setGatewayName(uint8_t gateway_id, std::string_view name);
std::vector<uint8_t> serialNumberBytes() const;
@@ -154,6 +158,7 @@ class GatewayRuntime {
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 defaultDeviceName() 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);
@@ -167,6 +172,7 @@ class GatewayRuntime {
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::optional<std::string> device_name_;
mutable std::map<uint8_t, std::string> gateway_names_;
mutable std::map<uint8_t, uint8_t> channel_gateway_ids_;
size_t gateway_count_{0};
@@ -19,6 +19,7 @@ constexpr const char* kTag = "gateway_runtime";
constexpr const char* kNamespace = "gateway_rt";
constexpr const char* kBleEnabledKey = "ble_enabled";
constexpr const char* kCacheEnabledKey = "cache_enabled";
constexpr const char* kDeviceNameKey = "device_name";
constexpr const char* kWifiSsidKey = "wifi_ssid";
constexpr const char* kWifiPasswordKey = "wifi_passwd";
constexpr size_t kMaxGatewayNameBytes = 32;
@@ -174,6 +175,19 @@ bool GatewaySettingsStore::clearWifiCredentials() {
return ssid_err == ESP_OK && password_err == ESP_OK && nvs_commit(handle_) == ESP_OK;
}
std::string GatewaySettingsStore::getDeviceName(std::string_view fallback) const {
const auto value = readString(kDeviceNameKey);
if (!value.has_value() || value->empty()) {
return std::string(fallback);
}
return *value;
}
bool GatewaySettingsStore::setDeviceName(std::string_view name) {
return writeString(kDeviceNameKey, name);
}
std::string GatewaySettingsStore::getGatewayName(uint8_t gateway_id,
std::string_view fallback) const {
const auto value = readString(makeGatewayNameKey(gateway_id));
@@ -584,6 +598,41 @@ bool GatewayRuntime::setGatewayIdForChannel(uint8_t channel_index, uint8_t gatew
return true;
}
std::string GatewayRuntime::deviceName() const {
LockGuard guard(command_lock_);
if (device_name_.has_value()) {
return device_name_.value();
}
auto name = settings_.getDeviceName(defaultDeviceName());
if (name.size() > kMaxGatewayNameBytes) {
name.resize(kMaxGatewayNameBytes);
}
device_name_ = name;
return name;
}
bool GatewayRuntime::setDeviceName(std::string_view name) {
std::string normalized(name);
if (normalized.size() > kMaxGatewayNameBytes) {
normalized.resize(kMaxGatewayNameBytes);
}
if (normalized.empty()) {
normalized = defaultDeviceName();
}
if (deviceName() == normalized) {
return true;
}
if (!settings_.setDeviceName(normalized)) {
return false;
}
LockGuard guard(command_lock_);
device_name_ = normalized;
return true;
}
std::string GatewayRuntime::gatewayName(uint8_t gateway_id) const {
LockGuard guard(command_lock_);
const auto cached = gateway_names_.find(gateway_id);
@@ -640,11 +689,13 @@ std::string GatewayRuntime::bleMacHex() const {
}
std::string GatewayRuntime::bleGatewayName(uint8_t gateway_id, std::string_view gateway_name) const {
std::string normalized(gateway_name);
std::string normalized(deviceName());
if (normalized.size() > kMaxGatewayNameBytes) {
normalized.resize(kMaxGatewayNameBytes);
}
if (!normalized.empty() && normalized != defaultGatewayName(gateway_id)) {
(void)gateway_id;
(void)gateway_name;
if (!normalized.empty() && normalized != defaultDeviceName()) {
return normalized;
}
return defaultBleGatewayName();
@@ -718,7 +769,7 @@ std::optional<std::string> GatewayRuntime::queryCommandKey(
return std::string(key);
}
std::string GatewayRuntime::defaultGatewayName(uint8_t) const {
std::string GatewayRuntime::defaultDeviceName() const {
const std::string serial_hex = bleMacHex();
if (serial_hex.size() <= 6) {
return "DALIGW_" + serial_hex;
@@ -726,6 +777,10 @@ std::string GatewayRuntime::defaultGatewayName(uint8_t) const {
return "DALIGW_" + serial_hex.substr(serial_hex.size() - 6);
}
std::string GatewayRuntime::defaultGatewayName(uint8_t gateway_id) const {
return "Channel " + std::to_string(gateway_id);
}
std::vector<uint8_t> GatewayRuntime::serialBytes() const {
std::vector<uint8_t> bytes;
const std::string& serial = config_.serial_id;