From c3e2785d39fd7f3879c2bfd1c348383ef802f8f2 Mon Sep 17 00:00:00 2001 From: impressionyang Date: Fri, 17 Apr 2026 10:16:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=AF=A6=E7=BB=86?= =?UTF-8?q?=E7=9A=84=E4=B8=B2=E5=8F=A3=E8=B0=83=E8=AF=95=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加详细的日志输出用于调试: 1. 串口原始数据输入(字节数和 HEX) 2. HCI 命令发送详情(OPCODE、HEX、长度) 3. HCI 响应解析详情 4. 数据完整性检查日志 5. 超时时的详细提示信息 日志级别: - INFO: 关键操作和响应 - DEBUG: 详细数据流 - WARNING: 异常情况 --- .../sigmesh_gateway/hci_gateway.py | 26 ++++++++++++++----- .../sigmesh_gateway/serial_reader.py | 2 ++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/custom_components/sigmesh_gateway/hci_gateway.py b/custom_components/sigmesh_gateway/hci_gateway.py index 2472352..1c7b7be 100644 --- a/custom_components/sigmesh_gateway/hci_gateway.py +++ b/custom_components/sigmesh_gateway/hci_gateway.py @@ -83,7 +83,10 @@ class HciGateway: 响应数据或 None """ cmd = build_hci_command(opcode, payload) - _LOGGER.debug("发送 HCI 命令:0x%02X, 数据:%s", opcode, cmd.hex().upper()) + _LOGGER.info("=== 发送 HCI 命令 ===") + _LOGGER.info("OPCODE: 0x%02X", opcode) + _LOGGER.info("CMD HEX: %s", cmd.hex().upper()) + _LOGGER.info("CMD LEN: %d 字节", len(cmd)) # 创建响应 future loop = asyncio.get_event_loop() @@ -93,14 +96,19 @@ class HciGateway: try: # 发送命令 await self.serial_reader.write(cmd) + _LOGGER.info("命令已发送到串口") # 等待响应 response = await asyncio.wait_for(future, timeout) - _LOGGER.debug("收到 HCI 响应:0x%02X, 数据:%s", opcode, response.hex().upper()) + _LOGGER.info("收到 HCI 响应:opcode=0x%02X, data=%s", opcode, response.hex().upper()) return response except asyncio.TimeoutError: - _LOGGER.warning("HCI 命令超时:0x%02X", opcode) + _LOGGER.warning("HCI 命令超时:0x%02X (超时 %d 秒)", opcode, timeout) + _LOGGER.warning("未收到任何响应,请检查:") + _LOGGER.warning("1. 网关是否已正确配置 (使用 danglo 工具)") + _LOGGER.warning("2. 串口连接是否正常") + _LOGGER.warning("3. 网关固件是否支持该命令") return None except Exception as e: _LOGGER.error("HCI 命令失败:0x%02X, 错误:%s", opcode, e) @@ -115,7 +123,9 @@ class HciGateway: data: 原始串口数据 """ self._buffer.extend(data) - _LOGGER.debug("HCI 接收原始数据:%s", data.hex().upper()) + _LOGGER.debug("=== HCI 接收原始数据 (%d 字节) ===", len(data)) + _LOGGER.debug("HEX: %s", data.hex().upper()) + _LOGGER.debug("BUF: %s", bytes(self._buffer).hex().upper()) # 尝试解析数据包 while len(self._buffer) >= 3: @@ -129,11 +139,12 @@ class HciGateway: length = self._buffer[2] if len(self._buffer) < 3 + length: + _LOGGER.debug("数据不完整:需要 %d 字节,当前 %d 字节", 3 + length, len(self._buffer)) break # 数据不完整,等待更多 payload = bytes(self._buffer[3 : 3 + length]) - _LOGGER.debug("HCI 响应:opcode=0x%02X, payload=%s", opcode, payload.hex().upper()) + _LOGGER.info("HCI 响应:opcode=0x%02X, len=%d, payload=%s", opcode, length, payload.hex().upper()) # 唤醒等待的 future if opcode in self._response_futures: @@ -151,18 +162,19 @@ class HciGateway: length = self._buffer[3] if len(self._buffer) < 4 + length: + _LOGGER.debug("事件数据不完整:需要 %d 字节,当前 %d 字节", 4 + length, len(self._buffer)) break # 数据不完整 event_data = bytes(self._buffer[: 4 + length]) self._buffer = self._buffer[4 + length :] - _LOGGER.debug("HCI 事件:opcode=0x%02X, data=%s", opcode, event_data.hex().upper()) + _LOGGER.info("HCI 事件:opcode=0x%02X, len=%d, data=%s", opcode, length, event_data.hex().upper()) # 事件处理由上层负责 self._handle_event(event_data) else: # 未知数据,清空 - _LOGGER.warning("未知 HCI 数据头:%s", self._buffer[0:1].hex().upper()) + _LOGGER.warning("未知 HCI 数据头:0x%02X,清空缓冲区", self._buffer[0]) self._buffer.clear() return diff --git a/custom_components/sigmesh_gateway/serial_reader.py b/custom_components/sigmesh_gateway/serial_reader.py index 4cfa19a..e559985 100644 --- a/custom_components/sigmesh_gateway/serial_reader.py +++ b/custom_components/sigmesh_gateway/serial_reader.py @@ -270,6 +270,8 @@ class SerialReader: try: if self._serial and self._serial.in_waiting: data = self._serial.read(self._serial.in_waiting) + _LOGGER.debug("=== 串口原始输入 (%d 字节) ===", len(data)) + _LOGGER.debug("HEX: %s", data.hex().upper()) # 直接交给 HCI 网关处理 if self.hci: