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
@@ -2,6 +2,7 @@
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <set>
@@ -41,6 +42,7 @@ struct GatewayBridgeServiceConfig {
uint32_t knx_task_stack_size{12288};
UBaseType_t knx_task_priority{5};
std::optional<GatewayKnxConfig> default_knx_config;
std::function<std::string()> gateway_device_name_provider;
};
struct GatewayBridgeHttpResponse {
@@ -62,6 +64,7 @@ class GatewayBridgeService {
GatewayBridgeHttpResponse handlePost(const std::string& action, int gateway_id,
const std::string& body);
std::string handleTransportRequest(uint8_t gateway_id, std::string_view request);
void handleGatewayNameChanged(uint8_t gateway_id);
private:
struct ChannelRuntime;
@@ -2100,8 +2100,17 @@ struct GatewayBridgeService::ChannelRuntime {
#if defined(CONFIG_GATEWAY_BACNET_BRIDGE_SUPPORTED)
GatewayBacnetServerConfig bacnetServerConfigLocked() const {
GatewayBacnetServerConfig config;
config.device_name = channel.name.empty() ? "DALI Gateway " + std::to_string(channel.gateway_id)
: channel.name;
config.channel_name = channel.name.empty()
? "Gateway " + std::to_string(channel.gateway_id)
: channel.name;
if (service_config.gateway_device_name_provider) {
config.device_name = service_config.gateway_device_name_provider();
}
if (config.device_name.empty()) {
config.device_name = channel.name.empty()
? "DALI Gateway " + std::to_string(channel.gateway_id)
: channel.name;
}
config.task_stack_size = service_config.bacnet_task_stack_size;
config.task_priority = service_config.bacnet_task_priority;
if (bacnet_server_config.has_value()) {
@@ -2320,6 +2329,21 @@ struct GatewayBridgeService::ChannelRuntime {
}
#endif
void updateChannelInfo(DaliChannelInfo next_channel) {
LockGuard guard(lock);
channel = std::move(next_channel);
#if defined(CONFIG_GATEWAY_BACNET_BRIDGE_SUPPORTED)
if (bacnet_started) {
const esp_err_t err = syncBacnetServerLocked();
if (err != ESP_OK && err != ESP_ERR_NOT_FOUND) {
ESP_LOGW(kTag, "gateway=%u BACnet name refresh failed: %s", channel.gateway_id,
esp_err_to_name(err));
}
bacnet_started = err == ESP_OK;
}
#endif
}
esp_err_t startKnx(std::set<uint16_t>* used_ports = nullptr,
std::set<int>* used_uarts = nullptr) {
LockGuard guard(lock);
@@ -4379,6 +4403,24 @@ const GatewayBridgeService::ChannelRuntime* GatewayBridgeService::findRuntime(
return nullptr;
}
void GatewayBridgeService::handleGatewayNameChanged(uint8_t gateway_id) {
const auto channels = dali_domain_.channelInfo();
for (const auto& runtime : runtimes_) {
if (gateway_id != 0xff && runtime->channel.gateway_id != gateway_id) {
continue;
}
const uint8_t runtime_gateway = runtime->channel.gateway_id;
const auto channel =
std::find_if(channels.begin(), channels.end(), [runtime_gateway](
const DaliChannelInfo& item) {
return item.gateway_id == runtime_gateway;
});
if (channel != channels.end()) {
runtime->updateChannelInfo(*channel);
}
}
}
GatewayBridgeService::ChannelRuntime* GatewayBridgeService::selectKnxEndpointRuntime() {
auto eligible = [](ChannelRuntime* runtime) {
if (runtime == nullptr) {