|
|
|
@ -16,19 +16,22 @@ import threading
|
|
|
|
import struct
|
|
|
|
import struct
|
|
|
|
from enum import Enum, auto
|
|
|
|
from enum import Enum, auto
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
|
|
|
|
# # XBee 模組
|
|
|
|
# # XBee 模組
|
|
|
|
# from xbee.frame import APIFrame
|
|
|
|
# from xbee.frame import APIFrame
|
|
|
|
|
|
|
|
|
|
|
|
# 自定義的 import
|
|
|
|
# 自定義的 import
|
|
|
|
from .utils import setup_logger
|
|
|
|
from .utils import RingBuffer, setup_logger
|
|
|
|
|
|
|
|
|
|
|
|
# ====================== 分割線 =====================
|
|
|
|
# ====================== 分割線 =====================
|
|
|
|
|
|
|
|
|
|
|
|
logger = setup_logger(os.path.basename(__file__))
|
|
|
|
logger = setup_logger(os.path.basename(__file__))
|
|
|
|
MODULE_VER = "0.60"
|
|
|
|
MODULE_VER = "0.80"
|
|
|
|
|
|
|
|
|
|
|
|
# ====================== 分割線 =====================
|
|
|
|
rx_module_ack = RingBuffer(capacity=64, buffer_id=253)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ====================== State DEFINITION =====================
|
|
|
|
|
|
|
|
|
|
|
|
# 定義 serial 連線的模式
|
|
|
|
# 定義 serial 連線的模式
|
|
|
|
class SerialMode(Enum):
|
|
|
|
class SerialMode(Enum):
|
|
|
|
@ -38,7 +41,29 @@ class SerialMode(Enum):
|
|
|
|
NOT_USE = auto() # 不使用
|
|
|
|
NOT_USE = auto() # 不使用
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ====================== Frame Processor 基類與實現 =====================
|
|
|
|
# ====================== AT Frame Data Classes =====================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
|
|
|
class ATRequest:
|
|
|
|
|
|
|
|
"""送往 dongle 的 AT 指令"""
|
|
|
|
|
|
|
|
command: bytes # 例如 b'DB'
|
|
|
|
|
|
|
|
parameter: bytes # 寫入用指令的參數,讀取型通常為空
|
|
|
|
|
|
|
|
frame_id: int # XBee frame id;0x00 表示不要求回應
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
|
|
|
class ATResponse:
|
|
|
|
|
|
|
|
"""dongle 回傳的 AT Response (0x88)"""
|
|
|
|
|
|
|
|
frame_id: int
|
|
|
|
|
|
|
|
command: bytes
|
|
|
|
|
|
|
|
status: int
|
|
|
|
|
|
|
|
data: bytes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def is_ok(self) -> bool:
|
|
|
|
|
|
|
|
return self.status == 0x00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ====================== Frame Processor Base and Implementation =====================
|
|
|
|
|
|
|
|
|
|
|
|
class FrameProcessor(ABC):
|
|
|
|
class FrameProcessor(ABC):
|
|
|
|
"""協議處理器基類"""
|
|
|
|
"""協議處理器基類"""
|
|
|
|
@ -47,10 +72,10 @@ class FrameProcessor(ABC):
|
|
|
|
self.buffer = bytearray()
|
|
|
|
self.buffer = bytearray()
|
|
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
@abstractmethod
|
|
|
|
def process_incoming(self, data: bytes):
|
|
|
|
def process_incoming(self, data: bytes) -> bytes:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
處理接收到的數據
|
|
|
|
處理接收到的數據
|
|
|
|
返回:已完整解析的 payload 列表
|
|
|
|
返回:已完整解析的 payload 列表 後續要丟到 UDP 去
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@ -66,7 +91,7 @@ class FrameProcessor(ABC):
|
|
|
|
class RawFrameProcessor(FrameProcessor):
|
|
|
|
class RawFrameProcessor(FrameProcessor):
|
|
|
|
"""原始數據直通處理器"""
|
|
|
|
"""原始數據直通處理器"""
|
|
|
|
|
|
|
|
|
|
|
|
def process_incoming(self, data: bytes):
|
|
|
|
def process_incoming(self, data: bytes) -> bytes:
|
|
|
|
"""直接返回原始數據,不進行緩衝"""
|
|
|
|
"""直接返回原始數據,不進行緩衝"""
|
|
|
|
return [data] if data else []
|
|
|
|
return [data] if data else []
|
|
|
|
|
|
|
|
|
|
|
|
@ -76,110 +101,108 @@ class RawFrameProcessor(FrameProcessor):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class XBeeFrameProcessor(FrameProcessor):
|
|
|
|
class XBeeFrameProcessor(FrameProcessor):
|
|
|
|
"""XBee API 協議處理器"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
XBee API 協議處理器
|
|
|
|
|
|
|
|
|
|
|
|
def process_incoming(self, data: bytes):
|
|
|
|
職責:
|
|
|
|
|
|
|
|
- XBee API frame 的拆幀 / 組幀
|
|
|
|
|
|
|
|
- 0x90 (RX Packet) -> 解出 payload 回傳
|
|
|
|
|
|
|
|
- 0x88 (AT Response) -> 轉交 at_handler 處理(若有注入)
|
|
|
|
|
|
|
|
- 0x8B (TX Status) -> 目前忽略
|
|
|
|
|
|
|
|
- 其他 frame type -> 記 warning 忽略
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
若未來要做變化型 XBee(例如 API2 escape mode、不同 addressing),
|
|
|
|
|
|
|
|
繼承此類並覆寫 _encapsulate / _decapsulate / _try_extract_frame 即可。
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# XBee API frame type
|
|
|
|
|
|
|
|
FRAME_TYPE_TX_REQUEST = 0x10
|
|
|
|
|
|
|
|
FRAME_TYPE_AT_RESPONSE = 0x88
|
|
|
|
|
|
|
|
FRAME_TYPE_TX_STATUS = 0x8B
|
|
|
|
|
|
|
|
FRAME_TYPE_RX_PACKET = 0x90
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, at_handler: "ATCommandHandler" = None):
|
|
|
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
self.at_handler = at_handler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---- 對外契約 ----
|
|
|
|
|
|
|
|
def process_incoming(self, data: bytes) -> bytes:
|
|
|
|
"""處理 XBee API 幀並提取 payload"""
|
|
|
|
"""處理 XBee API 幀並提取 payload"""
|
|
|
|
self.buffer.extend(data)
|
|
|
|
self.buffer.extend(data)
|
|
|
|
payloads = []
|
|
|
|
payloads = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
frame = self._try_extract_frame()
|
|
|
|
|
|
|
|
if frame is None:
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
payload = self._dispatch_frame(frame)
|
|
|
|
|
|
|
|
if payload:
|
|
|
|
|
|
|
|
payloads.append(payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return payloads
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_outgoing(self, data: bytes) -> bytes:
|
|
|
|
|
|
|
|
"""將數據封裝為 XBee API 傳輸幀"""
|
|
|
|
|
|
|
|
return self._encapsulate(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---- 內部:拆幀與分派 ----
|
|
|
|
|
|
|
|
def _try_extract_frame(self) -> bytes:
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
從 buffer 嘗試抓一個完整 frame
|
|
|
|
|
|
|
|
- 對齊幀頭 0x7E
|
|
|
|
|
|
|
|
- 長度不足時等待下次進 buffer
|
|
|
|
|
|
|
|
- 成功則從 buffer 切掉並回傳整個 frame bytes
|
|
|
|
|
|
|
|
"""
|
|
|
|
while len(self.buffer) >= 3:
|
|
|
|
while len(self.buffer) >= 3:
|
|
|
|
# 尋找幀頭
|
|
|
|
|
|
|
|
if self.buffer[0] != 0x7E:
|
|
|
|
if self.buffer[0] != 0x7E:
|
|
|
|
self.buffer.pop(0)
|
|
|
|
self.buffer.pop(0)
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
# 讀取 payload 長度
|
|
|
|
|
|
|
|
length = (self.buffer[1] << 8) | self.buffer[2]
|
|
|
|
length = (self.buffer[1] << 8) | self.buffer[2]
|
|
|
|
full_length = 3 + length + 1 # 起始符(1) + 長度(2) + payload + 校驗和(1)
|
|
|
|
full_length = 3 + length + 1 # 起始符(1) + 長度(2) + payload + 校驗和(1)
|
|
|
|
|
|
|
|
|
|
|
|
# 等待完整幀
|
|
|
|
|
|
|
|
if len(self.buffer) < full_length:
|
|
|
|
if len(self.buffer) < full_length:
|
|
|
|
break
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: 驗證 checksum:(sum(frame[3:]) & 0xFF) 應為 0xFF;不通過則丟棄此 frame 並從 buffer pop 1 byte 再重新對齊
|
|
|
|
|
|
|
|
|
|
|
|
# 提取完整 frame 並從緩衝區移除
|
|
|
|
|
|
|
|
frame = bytes(self.buffer[:full_length])
|
|
|
|
frame = bytes(self.buffer[:full_length])
|
|
|
|
del self.buffer[:full_length]
|
|
|
|
del self.buffer[:full_length]
|
|
|
|
|
|
|
|
return frame
|
|
|
|
|
|
|
|
|
|
|
|
# 判斷 frame 類型並處理
|
|
|
|
return None
|
|
|
|
frame_type = frame[3]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if frame_type == 0x90: # RX Packet
|
|
|
|
|
|
|
|
payload = XBeeFrameHandler.decapsulate_data(frame)
|
|
|
|
|
|
|
|
if payload:
|
|
|
|
|
|
|
|
payloads.append(payload)
|
|
|
|
|
|
|
|
elif frame_type == 0x88: # AT Response
|
|
|
|
|
|
|
|
# 可以在這裡處理 AT 指令回應
|
|
|
|
|
|
|
|
# response = XBeeFrameHandler.parse_at_command_response(frame)
|
|
|
|
|
|
|
|
# 目前忽略
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
elif frame_type == 0x8B: # Transmit Status
|
|
|
|
|
|
|
|
# 傳輸狀態,目前忽略
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
logger.warning(f"Unknown XBee frame type: 0x{frame_type:02X}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return payloads
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_outgoing(self, data: bytes) -> bytes:
|
|
|
|
|
|
|
|
"""將數據封裝為 XBee API 傳輸幀"""
|
|
|
|
|
|
|
|
return XBeeFrameProcessor.encapsulate_data(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ====================== XBee Frame Handler =====================
|
|
|
|
def _dispatch_frame(self, frame: bytes) -> bytes:
|
|
|
|
|
|
|
|
"""根據 frame type 分派;若是 RX payload 回傳 bytes,其餘回傳 None"""
|
|
|
|
|
|
|
|
frame_type = frame[3]
|
|
|
|
|
|
|
|
|
|
|
|
class XBeeFrameHandler:
|
|
|
|
if frame_type == self.FRAME_TYPE_RX_PACKET:
|
|
|
|
"""XBee API Frame 處理器"""
|
|
|
|
return self._decapsulate(frame)
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
if frame_type == self.FRAME_TYPE_AT_RESPONSE:
|
|
|
|
def parse_at_command_response(frame: bytes) -> dict:
|
|
|
|
if self.at_handler is not None:
|
|
|
|
"""解析 AT Command Response (0x88)"""
|
|
|
|
self.at_handler.handle_frame(frame)
|
|
|
|
if len(frame) < 8:
|
|
|
|
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
frame_type = frame[3]
|
|
|
|
if frame_type == self.FRAME_TYPE_TX_STATUS:
|
|
|
|
if frame_type != 0x88:
|
|
|
|
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
frame_id = frame[4]
|
|
|
|
logger.warning(f"Unknown XBee frame type: 0x{frame_type:02X}")
|
|
|
|
at_command = frame[5:7]
|
|
|
|
|
|
|
|
status = frame[7]
|
|
|
|
|
|
|
|
data = frame[8:] if len(frame) > 8 else b''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
'frame_id': frame_id,
|
|
|
|
|
|
|
|
'command': at_command,
|
|
|
|
|
|
|
|
'status': status,
|
|
|
|
|
|
|
|
'data': data,
|
|
|
|
|
|
|
|
'is_ok': status == 0x00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
|
|
def parse_receive_packet(frame: bytes) -> dict:
|
|
|
|
|
|
|
|
"""解析 RX Packet (0x90) - 未來擴展用"""
|
|
|
|
|
|
|
|
# if len(frame) < 15 or frame[3] != 0x90:
|
|
|
|
|
|
|
|
# return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# return {
|
|
|
|
|
|
|
|
# 'source_addr': frame[4:12],
|
|
|
|
|
|
|
|
# 'reserved': frame[12:14],
|
|
|
|
|
|
|
|
# 'options': frame[14],
|
|
|
|
|
|
|
|
# 'data': frame[15:-1]
|
|
|
|
|
|
|
|
# }
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---- 內部:編解碼 ----
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def encapsulate_data(data: bytes, dest_addr64: bytes = b'\x00\x00\x00\x00\x00\x00\xFF\xFF', frame_id = 0x01) -> bytes:
|
|
|
|
def _encapsulate(
|
|
|
|
|
|
|
|
data: bytes,
|
|
|
|
|
|
|
|
dest_addr64: bytes = b'\x00\x00\x00\x00\x00\x00\xFF\xFF',
|
|
|
|
|
|
|
|
frame_id: int = 0x01,
|
|
|
|
|
|
|
|
) -> bytes:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
將數據封裝為 XBee API 傳輸幀
|
|
|
|
將 payload 包成 XBee TX Request (0x10)
|
|
|
|
|
|
|
|
|
|
|
|
使用 XBee API 格式封裝數據:
|
|
|
|
|
|
|
|
- 傳輸請求幀 (0x10)
|
|
|
|
|
|
|
|
- 使用廣播地址
|
|
|
|
- 使用廣播地址
|
|
|
|
- 添加適當的頭部和校驗和
|
|
|
|
- 添加適當的頭部和校驗和
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
frame_type = 0x10
|
|
|
|
frame_type = XBeeFrameProcessor.FRAME_TYPE_TX_REQUEST
|
|
|
|
dest_addr16 = b'\xFF\xFE'
|
|
|
|
dest_addr16 = b'\xFF\xFE'
|
|
|
|
broadcast_radius = 0x00
|
|
|
|
broadcast_radius = 0x00
|
|
|
|
options = 0x00
|
|
|
|
options = 0x00
|
|
|
|
@ -191,20 +214,33 @@ class XBeeFrameHandler:
|
|
|
|
return b'\x7E' + struct.pack(">H", len(frame)) + frame + struct.pack("B", checksum)
|
|
|
|
return b'\x7E' + struct.pack(">H", len(frame)) + frame + struct.pack("B", checksum)
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def decapsulate_data(data: bytes):
|
|
|
|
def _decapsulate(frame: bytes) -> bytes:
|
|
|
|
|
|
|
|
"""從 RX Packet (0x90) 取出 payload"""
|
|
|
|
# 獲取數據長度 (不包括校驗和)
|
|
|
|
length = (frame[1] << 8) | frame[2]
|
|
|
|
length = (data[1] << 8) | data[2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rf_data_start = 3 + 12
|
|
|
|
rf_data_start = 3 + 12
|
|
|
|
return data[rf_data_start:3 + length]
|
|
|
|
return frame[rf_data_start:3 + length]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ====================== Dongle Command Handler =====================
|
|
|
|
|
|
|
|
|
|
|
|
class ATCommandHandler:
|
|
|
|
class ATCommandHandler:
|
|
|
|
"""AT 指令回應處理器"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
XBee AT 指令處理器
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
職責:
|
|
|
|
|
|
|
|
- 組 AT Command Request frame (0x08) 並透過注入的 writer 送出
|
|
|
|
|
|
|
|
- 解析 AT Response frame (0x88)
|
|
|
|
|
|
|
|
- 依 AT 指令分派到對應的 handler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
writer 由 SerialHandler 在連線建立後 (connection_made) 注入,
|
|
|
|
|
|
|
|
型別為 Callable[[bytes], None](通常就是 serial transport.write)。
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FRAME_TYPE_AT_COMMAND = 0x08
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, serial_port: str):
|
|
|
|
def __init__(self, serial_port: str):
|
|
|
|
self.serial_port = serial_port
|
|
|
|
self.serial_port = serial_port
|
|
|
|
|
|
|
|
self.writer = None
|
|
|
|
self.handlers = {
|
|
|
|
self.handlers = {
|
|
|
|
b'DB': self._handle_rssi,
|
|
|
|
b'DB': self._handle_rssi,
|
|
|
|
b'SH': self._handle_serial_high,
|
|
|
|
b'SH': self._handle_serial_high,
|
|
|
|
@ -212,25 +248,96 @@ class ATCommandHandler:
|
|
|
|
# 可擴展其他 AT 指令
|
|
|
|
# 可擴展其他 AT 指令
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def handle_response(self, response: dict):
|
|
|
|
# ---- 發送端 ----
|
|
|
|
"""根據 AT 指令類型分派處理"""
|
|
|
|
def set_writer(self, writer):
|
|
|
|
if not response or not response['is_ok']:
|
|
|
|
"""由 SerialHandler 注入實際寫入 serial 的 callable"""
|
|
|
|
if response:
|
|
|
|
self.writer = writer
|
|
|
|
logger.warning(f"[{self.serial_port}] AT {response['command'].decode()} 失敗,狀態碼: {response['status']}")
|
|
|
|
|
|
|
|
|
|
|
|
def send_command(self, request: ATRequest):
|
|
|
|
|
|
|
|
"""發送一筆 AT 指令給 dongle"""
|
|
|
|
|
|
|
|
if self.writer is None:
|
|
|
|
|
|
|
|
logger.warning(
|
|
|
|
|
|
|
|
f"[{self.serial_port}] AT writer 尚未就緒,指令 {request.command!r} 丟棄"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.writer(self._build_at_request(request))
|
|
|
|
|
|
|
|
# logger.debug(
|
|
|
|
|
|
|
|
# f"[{self.serial_port}] send AT {request.command.decode(errors='replace')} "
|
|
|
|
|
|
|
|
# f"(frame_id=0x{request.frame_id:02X})"
|
|
|
|
|
|
|
|
# ) # dev
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
|
|
def _build_at_request(request: ATRequest) -> bytes:
|
|
|
|
|
|
|
|
"""將 ATRequest 組成 XBee API AT Command Request frame (0x08) bytes"""
|
|
|
|
|
|
|
|
frame_type = ATCommandHandler.FRAME_TYPE_AT_COMMAND
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frame = struct.pack(">B", frame_type) + struct.pack(">B", request.frame_id)
|
|
|
|
|
|
|
|
frame += request.command + request.parameter
|
|
|
|
|
|
|
|
checksum = 0xFF - (sum(frame) & 0xFF)
|
|
|
|
|
|
|
|
return b'\x7E' + struct.pack(">H", len(frame)) + frame + struct.pack("B", checksum)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---- 接收端 ----
|
|
|
|
|
|
|
|
def handle_frame(self, frame: bytes) -> None:
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
接收一整個 AT Response frame:
|
|
|
|
|
|
|
|
1. 解析成 ATResponse
|
|
|
|
|
|
|
|
2. 推進 rx_module_ack 供其他模組消費
|
|
|
|
|
|
|
|
3. 本地 dispatch 給對應的 _handle_xxx
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
parsed = self._parse(frame)
|
|
|
|
|
|
|
|
if parsed is None:
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
command = response['command']
|
|
|
|
if not rx_module_ack.put(parsed):
|
|
|
|
handler = self.handlers.get(command)
|
|
|
|
logger.warning(
|
|
|
|
|
|
|
|
f"[{self.serial_port}] rx_module_ack overflow, drop {parsed.command!r}"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self._dispatch(parsed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
|
|
def _parse(frame: bytes) -> ATResponse:
|
|
|
|
|
|
|
|
"""解析 AT Command Response (0x88);不符格式回傳 None"""
|
|
|
|
|
|
|
|
if len(frame) < 8:
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if frame[3] != 0x88:
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ATResponse(
|
|
|
|
|
|
|
|
frame_id=frame[4],
|
|
|
|
|
|
|
|
command=frame[5:7],
|
|
|
|
|
|
|
|
status=frame[7],
|
|
|
|
|
|
|
|
data=frame[8:] if len(frame) > 8 else b'',
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _dispatch(self, response: ATResponse) -> None:
|
|
|
|
|
|
|
|
"""根據 AT 指令類型分派處理"""
|
|
|
|
|
|
|
|
# print(f"[{self.serial_port}] AT Response: {response}") # dev
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not response.is_ok:
|
|
|
|
|
|
|
|
logger.warning(
|
|
|
|
|
|
|
|
f"[{self.serial_port}] AT {response.command.decode()} "
|
|
|
|
|
|
|
|
f"失敗,狀態碼: {response.status}"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handler = self.handlers.get(response.command)
|
|
|
|
if handler:
|
|
|
|
if handler:
|
|
|
|
handler(response['data'])
|
|
|
|
handler(response.data)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
logger.debug(f"[{self.serial_port}] 未處理的 AT 指令: {command.decode()}")
|
|
|
|
logger.debug(
|
|
|
|
|
|
|
|
f"[{self.serial_port}] 未處理的 AT 指令: "
|
|
|
|
|
|
|
|
f"{response.command.decode()}"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_rssi(self, data: bytes):
|
|
|
|
def _handle_rssi(self, data: bytes):
|
|
|
|
"""處理 DB (RSSI) 回應"""
|
|
|
|
"""處理 DB (RSSI) 回應:單 byte 無號值,單位 dBm"""
|
|
|
|
# 未來可實現 RSSI 處理邏輯
|
|
|
|
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
# if data:
|
|
|
|
|
|
|
|
# print(f"[{self.serial_port}] RSSI = -{data[0]} dBm") # dev
|
|
|
|
|
|
|
|
# logger.debug(f"[{self.serial_port}] RSSI = -{data[0]} dBm") # dev
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_serial_high(self, data: bytes):
|
|
|
|
def _handle_serial_high(self, data: bytes):
|
|
|
|
"""處理 SH (Serial Number High)"""
|
|
|
|
"""處理 SH (Serial Number High)"""
|
|
|
|
@ -241,8 +348,7 @@ class ATCommandHandler:
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ====================== Serial Handler =====================
|
|
|
|
# ================ Serial UDP Socket Object ==============
|
|
|
|
|
|
|
|
|
|
|
|
class SerialHandler(asyncio.Protocol):
|
|
|
|
class SerialHandler(asyncio.Protocol):
|
|
|
|
"""asyncio.Protocol 用於處理 Serial 收發"""
|
|
|
|
"""asyncio.Protocol 用於處理 Serial 收發"""
|
|
|
|
|
|
|
|
|
|
|
|
@ -252,28 +358,29 @@ class SerialHandler(asyncio.Protocol):
|
|
|
|
self.serial_mode = serial_mode
|
|
|
|
self.serial_mode = serial_mode
|
|
|
|
self.transport = None # Serial 自己的傳輸物件
|
|
|
|
self.transport = None # Serial 自己的傳輸物件
|
|
|
|
|
|
|
|
|
|
|
|
# 根據模式創建對應的 processor
|
|
|
|
# 根據模式建立 processor(需要 AT handler 時一併在工廠內建好注入)
|
|
|
|
self.processor = self._create_processor(serial_mode)
|
|
|
|
self.processor = self._create_processor()
|
|
|
|
|
|
|
|
|
|
|
|
# AT 指令處理器(僅 XBee 模式使用)
|
|
|
|
|
|
|
|
if serial_mode == SerialMode.XBEEAPI2AT:
|
|
|
|
|
|
|
|
self.at_handler = ATCommandHandler(serial_port_str)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.at_handler = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _create_processor(self, serial_mode: SerialMode) -> FrameProcessor:
|
|
|
|
def _create_processor(self) -> FrameProcessor:
|
|
|
|
"""工廠方法:根據模式創建處理器"""
|
|
|
|
"""工廠方法:根據 self.serial_mode 建立對應的 processor,必要時一併注入 AT handler"""
|
|
|
|
if serial_mode == SerialMode.STRAIGHT:
|
|
|
|
if self.serial_mode == SerialMode.STRAIGHT:
|
|
|
|
return RawFrameProcessor()
|
|
|
|
return RawFrameProcessor()
|
|
|
|
elif serial_mode == SerialMode.XBEEAPI2AT:
|
|
|
|
|
|
|
|
return XBeeFrameProcessor()
|
|
|
|
if self.serial_mode == SerialMode.XBEEAPI2AT:
|
|
|
|
else:
|
|
|
|
at_handler = ATCommandHandler(self.serial_port_str)
|
|
|
|
logger.warning(f"Unknown serial mode: {serial_mode}, using Raw")
|
|
|
|
return XBeeFrameProcessor(at_handler=at_handler)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.warning(f"Unknown serial mode: {self.serial_mode}, using Raw")
|
|
|
|
return RawFrameProcessor()
|
|
|
|
return RawFrameProcessor()
|
|
|
|
|
|
|
|
|
|
|
|
def connection_made(self, transport):
|
|
|
|
def connection_made(self, transport):
|
|
|
|
"""連接建立時的回調"""
|
|
|
|
"""連接建立時的回調"""
|
|
|
|
self.transport = transport
|
|
|
|
self.transport = transport
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# XBee 模式下,將 transport.write 注入給 AT handler,讓它能送指令
|
|
|
|
|
|
|
|
if self.serial_mode == SerialMode.XBEEAPI2AT:
|
|
|
|
|
|
|
|
self.processor.at_handler.set_writer(self.transport.write)
|
|
|
|
|
|
|
|
|
|
|
|
if hasattr(self.udp_handler, 'set_serial_handler'):
|
|
|
|
if hasattr(self.udp_handler, 'set_serial_handler'):
|
|
|
|
self.udp_handler.set_serial_handler(self)
|
|
|
|
self.udp_handler.set_serial_handler(self)
|
|
|
|
logger.debug(f"Serial port {self.serial_port_str} connected")
|
|
|
|
logger.debug(f"Serial port {self.serial_port_str} connected")
|
|
|
|
@ -291,8 +398,6 @@ class SerialHandler(asyncio.Protocol):
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ====================== UDP Handler =====================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UDPHandler(asyncio.DatagramProtocol):
|
|
|
|
class UDPHandler(asyncio.DatagramProtocol):
|
|
|
|
"""asyncio.DatagramProtocol 用於處理 UDP 收發"""
|
|
|
|
"""asyncio.DatagramProtocol 用於處理 UDP 收發"""
|
|
|
|
|
|
|
|
|
|
|
|
@ -561,6 +666,33 @@ class serial_manager:
|
|
|
|
ret[key] = obj.serial_port
|
|
|
|
ret[key] = obj.serial_port
|
|
|
|
return ret
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_at_command(self, serial_id, request: ATRequest) -> bool:
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
對指定 serial_id 的 XBee dongle 發送一筆 AT 指令(thread-safe)
|
|
|
|
|
|
|
|
- serial_id: create_serial_link 取得的編號
|
|
|
|
|
|
|
|
- request: ATRequest 物件,攜帶 command / parameter / frame_id
|
|
|
|
|
|
|
|
回傳是否成功排進事件圈
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not self.running or not self.loop:
|
|
|
|
|
|
|
|
logger.error("Event loop not running, cannot send AT command")
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if serial_id not in self.serial_objects:
|
|
|
|
|
|
|
|
logger.error(f"Serial object {serial_id} not found")
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
serial_obj = self.serial_objects[serial_id]
|
|
|
|
|
|
|
|
if serial_obj.serial_mode != SerialMode.XBEEAPI2AT:
|
|
|
|
|
|
|
|
logger.error(
|
|
|
|
|
|
|
|
f"Serial {serial_id} mode is {serial_obj.serial_mode.name}, "
|
|
|
|
|
|
|
|
f"AT command only supported in XBEEAPI2AT mode"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
at_handler = serial_obj.serial_handler.processor.at_handler
|
|
|
|
|
|
|
|
self.loop.call_soon_threadsafe(at_handler.send_command, request)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def check_serial_port(serial_port, baudrate):
|
|
|
|
def check_serial_port(serial_port, baudrate):
|
|
|
|
"""檢查串口是否存在與可用"""
|
|
|
|
"""檢查串口是否存在與可用"""
|
|
|
|
@ -598,20 +730,42 @@ if __name__ == '__main__':
|
|
|
|
sm = serial_manager()
|
|
|
|
sm = serial_manager()
|
|
|
|
sm.start()
|
|
|
|
sm.start()
|
|
|
|
|
|
|
|
|
|
|
|
SERIAL_PORT = '/dev/ttyUSB0' # 手動指定
|
|
|
|
|
|
|
|
SERIAL_BAUDRATE = 115200
|
|
|
|
|
|
|
|
UDP_REMOTE_PORT = 14571
|
|
|
|
|
|
|
|
sm.create_serial_link(SERIAL_PORT, SERIAL_BAUDRATE, UDP_REMOTE_PORT, SerialMode.XBEEAPI2AT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# SERIAL_PORT = '/dev/ttyACM0' # 手動指定
|
|
|
|
# SERIAL_PORT = '/dev/ttyACM0' # 手動指定
|
|
|
|
# SERIAL_BAUDRATE = 115200
|
|
|
|
# SERIAL_BAUDRATE = 115200
|
|
|
|
# UDP_REMOTE_PORT = 14571
|
|
|
|
# UDP_REMOTE_PORT = 14571
|
|
|
|
# sm.create_serial_link(SERIAL_PORT, SERIAL_BAUDRATE, UDP_REMOTE_PORT, SerialMode.STRAIGHT)
|
|
|
|
# sm.create_serial_link(SERIAL_PORT, SERIAL_BAUDRATE, UDP_REMOTE_PORT, SerialMode.STRAIGHT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SERIAL_PORT = '/dev/ttyUSB0' # 手動指定
|
|
|
|
|
|
|
|
SERIAL_BAUDRATE = 115200
|
|
|
|
|
|
|
|
UDP_REMOTE_PORT = 14561
|
|
|
|
|
|
|
|
sm.create_serial_link(SERIAL_PORT, SERIAL_BAUDRATE, UDP_REMOTE_PORT, SerialMode.XBEEAPI2AT)
|
|
|
|
|
|
|
|
|
|
|
|
linked_serial = sm.get_serial_link()
|
|
|
|
linked_serial = sm.get_serial_link()
|
|
|
|
print(linked_serial)
|
|
|
|
print(linked_serial)
|
|
|
|
time.sleep(60)
|
|
|
|
|
|
|
|
|
|
|
|
# 等 connection_made 完成 writer 注入,再發一筆 AT 指令測試
|
|
|
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
rssi_request = ATRequest(command=b'DB', parameter=b'', frame_id=0x52)
|
|
|
|
|
|
|
|
for i in range(60):
|
|
|
|
|
|
|
|
sm.send_at_command(1, rssi_request)
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
sm.remove_serial_link(1)
|
|
|
|
sm.remove_serial_link(1)
|
|
|
|
time.sleep(3)
|
|
|
|
time.sleep(3)
|
|
|
|
sm.shutdown()
|
|
|
|
sm.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
================= 改版記錄 ============================
|
|
|
|
|
|
|
|
2026.4.20
|
|
|
|
|
|
|
|
1. XBeeFrameHandler 結構移除
|
|
|
|
|
|
|
|
2. XBeeFrameProcessor 新增 _encapsulate, _decapsulate 編碼解碼 xbee 封包的功能 (原來在 XBeeFrameHandler 中)
|
|
|
|
|
|
|
|
3. XBeeFrameProcessor 新增 _try_extract_frame 處理被可能截斷的 UART 封包
|
|
|
|
|
|
|
|
4. XBeeFrameProcessor 新增 _dispatch_frame 分配封包到 UDP 或者 Dongle Command Handler
|
|
|
|
|
|
|
|
5. ATCommandHandler 新增 _parse 去拆解 0x88 AT Command Response
|
|
|
|
|
|
|
|
6. ATCommandHandler 新增 _dispatch 把拆解的結果 分配到 _handle_XXX
|
|
|
|
|
|
|
|
7. ATCommandHandler 新增各項 _handle_XXX (未實作)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|