fix(gateway): enhance DALI logging and error handling in gateway_knx
Signed-off-by: Tony <tonylu@tony-cloud.com>
This commit is contained in:
@@ -15,10 +15,14 @@
|
||||
#include "gateway_knx.hpp"
|
||||
#include "gateway_modbus.hpp"
|
||||
#include "gateway_provisioning.hpp"
|
||||
#include "log.hpp"
|
||||
#include "security_storage.h"
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "driver/uart.h"
|
||||
#ifndef LOG_LOCAL_LEVEL
|
||||
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
|
||||
#endif
|
||||
#include "esp_log.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "lwip/inet.h"
|
||||
@@ -44,6 +48,7 @@ namespace gateway {
|
||||
namespace {
|
||||
|
||||
constexpr const char* kTag = "gateway_bridge";
|
||||
constexpr const char* kDaliCppLogTag = "dali_cpp";
|
||||
constexpr const char* kBridgeConfigKey = "bridge_cfg";
|
||||
constexpr const char* kDiscoveryInventoryKey = "bridge_disc";
|
||||
constexpr int kMaxDaliShortAddress = 63;
|
||||
@@ -58,6 +63,16 @@ constexpr uint32_t kBacnetReliabilityCommunicationFailure = 12;
|
||||
constexpr const char* kModbusManagementPrefix = "@DALIGW";
|
||||
constexpr uint8_t kDaliGroupRawMin = 0x80;
|
||||
constexpr uint8_t kDaliGroupRawMax = 0x9F;
|
||||
|
||||
void ConfigureDaliCppLogging() {
|
||||
static bool configured = false;
|
||||
if (configured) return;
|
||||
configured = true;
|
||||
DaliLog::instance().setLevel(LogLevel::debug);
|
||||
DaliLog::instance().setSink([](const std::string& line) {
|
||||
ESP_LOGD(kDaliCppLogTag, "%s", line.c_str());
|
||||
});
|
||||
}
|
||||
constexpr uint8_t kDaliCmdOff = 0x00;
|
||||
constexpr uint8_t kDaliCmdRecallMax = 0x05;
|
||||
|
||||
@@ -3938,6 +3953,7 @@ GatewayBridgeService::GatewayBridgeService(DaliDomainService& dali_domain,
|
||||
GatewayBridgeService::~GatewayBridgeService() = default;
|
||||
|
||||
esp_err_t GatewayBridgeService::start() {
|
||||
ConfigureDaliCppLogging();
|
||||
if (!config_.bridge_enabled) {
|
||||
ESP_LOGI(kTag, "bridge service disabled");
|
||||
return ESP_OK;
|
||||
|
||||
@@ -679,11 +679,27 @@ std::optional<int> ExecuteRawQuery(DaliBridgeEngine& engine, uint8_t addr, uint8
|
||||
const char* sequence) {
|
||||
const auto result = ExecuteRaw(engine, BridgeOperation::query, addr, cmd, sequence);
|
||||
if (!result.ok || !result.data.has_value()) {
|
||||
ESP_LOGD(kTag, "DALI query failed seq=%s addr=0x%02x cmd=0x%02x error=%s",
|
||||
sequence == nullptr ? "" : sequence, static_cast<unsigned>(addr),
|
||||
static_cast<unsigned>(cmd),
|
||||
result.error.empty() ? "no response" : result.error.c_str());
|
||||
return std::nullopt;
|
||||
}
|
||||
ESP_LOGD(kTag, "DALI query result seq=%s addr=0x%02x cmd=0x%02x data=0x%02x",
|
||||
sequence == nullptr ? "" : sequence, static_cast<unsigned>(addr),
|
||||
static_cast<unsigned>(cmd),
|
||||
static_cast<unsigned>(result.data.value() & 0xff));
|
||||
return result.data.value();
|
||||
}
|
||||
|
||||
bool DaliQueryResultHasStatus(const DaliBridgeResult& result, const char* status) {
|
||||
if (status == nullptr) return false;
|
||||
const auto it = result.metadata.find("queryStatus");
|
||||
if (it == result.metadata.end()) return false;
|
||||
const auto value = it->second.asString();
|
||||
return value.has_value() && value.value() == status;
|
||||
}
|
||||
|
||||
std::optional<uint8_t> RawCommandAddressForTarget(const GatewayKnxDaliTarget& target) {
|
||||
switch (target.kind) {
|
||||
case GatewayKnxDaliTargetKind::kBroadcast:
|
||||
@@ -954,11 +970,27 @@ std::optional<bool> CompareSelectedSearchAddress(DaliBridgeEngine& engine, uint3
|
||||
if (!SetSearchAddress(engine, search_address, sequence)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto raw = ExecuteRawQuery(engine, DALI_CMD_SPECIAL_COMPARE, DALI_CMD_OFF, sequence);
|
||||
if (!raw.has_value()) {
|
||||
const auto result = ExecuteRaw(engine, BridgeOperation::query, DALI_CMD_SPECIAL_COMPARE,
|
||||
DALI_CMD_OFF, sequence);
|
||||
if (result.ok && result.data.has_value()) {
|
||||
return true;
|
||||
}
|
||||
if (DaliQueryResultHasStatus(result, "noResponse") ||
|
||||
DaliQueryResultHasStatus(result, "timeout")) {
|
||||
ESP_LOGD(kTag, "DALI compare no match seq=%s search=0x%06x status=%s",
|
||||
sequence == nullptr ? "" : sequence, static_cast<unsigned>(search_address),
|
||||
DaliQueryResultHasStatus(result, "timeout") ? "timeout" : "noResponse");
|
||||
return false;
|
||||
}
|
||||
if (!result.error.empty()) {
|
||||
ESP_LOGW(kTag, "DALI compare failed seq=%s search=0x%06x error=%s",
|
||||
sequence == nullptr ? "" : sequence, static_cast<unsigned>(search_address),
|
||||
result.error.c_str());
|
||||
}
|
||||
if (!result.ok || !result.data.has_value()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return raw.value() == 0xff;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::optional<uint32_t> FindLowestSelectedRandomAddress(DaliBridgeEngine& engine) {
|
||||
|
||||
Reference in New Issue
Block a user