Update GUI 2.0.6 components and longCommand

This commit is contained in:
2026-04-14 15:09:25 +08:00
parent fc2c642d6c
commit fc00ecb762
4 changed files with 227 additions and 90 deletions
+56 -19
View File
@@ -467,14 +467,13 @@ class DroneMonitor(Node):
self.serial_receivers = []
# ================================================================================
# 【新增】初始化 CommandLongClient(持久化,不會每次調用都創建/銷毀)
# 【新增】初始化 CommandLongClient 字典(為每個 drone 維護獨立的 client)
# ================================================================================
self.command_long_client = None
try:
self.command_long_client = CommandLongClient()
except Exception as e:
print(f"⚠️ 警告: 無法初始化 CommandLongClient: {e}")
self.command_long_client = None
# 改為為每個 drone 創建獨立的 client,避免多機並行時的競態條件
self.command_long_clients = {} # {drone_id: CommandLongClient}
self.client_lock = Lock() # 保護 clients 字典的訪問
self.client_counter = 0 # 用於生成唯一的 client 節點名稱
self.executor = None # 將在 gui.py 中設置,用於添加新的 clients
# ================================================================================
# 主题检测定时器
@@ -501,6 +500,38 @@ class DroneMonitor(Node):
return self.socket_id_mapping[original_socket_id]
def get_or_create_client(self, drone_id):
"""為每個 drone 獲取或創建獨立的 CommandLongClient,避免競態條件"""
with self.client_lock:
if drone_id not in self.command_long_clients:
try:
# 生成唯一的 client 節點名稱
self.client_counter += 1
unique_name = f"cmd_long_client_{drone_id}_{self.client_counter}"
client = CommandLongClient(node_name=unique_name)
self.command_long_clients[drone_id] = client
print(f" ✓ 為 {drone_id} 創建新的 CommandLongClient (node={unique_name})")
# 將新 client 添加到主執行器(這樣它的回調才能被處理)
if self.executor:
self.executor.add_node(client)
print(f" ✓ 將 {drone_id} 的 client 添加到主執行器")
except TypeError:
# 舊版 CommandLongClient 不支持 node_name 參數,使用預設
client = CommandLongClient()
self.command_long_clients[drone_id] = client
print(f" ✓ 為 {drone_id} 創建新的 CommandLongClient (使用預設名稱)")
if self.executor:
self.executor.add_node(client)
print(f" ✓ 將 {drone_id} 的 client 添加到主執行器")
except Exception as e:
print(f"⚠️ 無法為 {drone_id} 創建 CommandLongClient: {e}")
return None
return self.command_long_clients[drone_id]
def scan_topics(self):
topics = self.get_topic_names_and_types()
drone_pattern = re.compile(r'/fc_network/vehicle/(sys\d+)/(\w+)')
@@ -619,17 +650,19 @@ class DroneMonitor(Node):
print(f"\n📢 [SET_MODE] {drone_id} → {mode_name} (custom_mode={custom_mode})")
if not self.command_long_client:
print(f"❌ [SET_MODE] CommandLongClient 未初始化")
# 獲取或創建該 drone 專用的 client(避免多機並行時的競態條件)
client = self.get_or_create_client(drone_id)
if not client:
print(f"❌ [SET_MODE] CommandLongClient 無法初始化")
return False
# 直接調用 async 方法,無需線程池(避免嵌套執行器衝突)
result = await self.command_long_client.change_mode_async(
result = await client.change_mode_async(
target_sysid=sysid,
custom_mode=float(custom_mode),
target_compid=0,
base_mode=1.0,
timeout_sec=2.0,
timeout_sec=5.0, # 增加超時時間以提高多機操作的可靠性
)
if result and result.success:
@@ -656,16 +689,18 @@ class DroneMonitor(Node):
action_name = "解鎖" if arm else "上鎖"
print(f"\n📢 [ARM] {drone_id} → {action_name}")
if not self.command_long_client:
print(f"❌ [ARM] CommandLongClient 未初始化")
# 獲取或創建該 drone 專用的 client(避免多機並行時的競態條件)
client = self.get_or_create_client(drone_id)
if not client:
print(f"❌ [ARM] CommandLongClient 無法初始化")
return False
# 直接調用 async 方法,無需線程池(避免嵌套執行器衝突)
result = await self.command_long_client.arm_disarm_async(
result = await client.arm_disarm_async(
target_sysid=sysid,
arm=arm,
target_compid=0,
timeout_sec=2.0,
timeout_sec=5.0, # 增加超時時間以提高多機操作的可靠性
)
if result and result.success:
@@ -691,16 +726,18 @@ class DroneMonitor(Node):
print(f"\n📢 [TAKEOFF] {drone_id} → 起飛 (高度={altitude}m)")
if not self.command_long_client:
print(f"❌ [TAKEOFF] CommandLongClient 未初始化")
# 獲取或創建該 drone 專用的 client(避免多機並行時的競態條件)
client = self.get_or_create_client(drone_id)
if not client:
print(f"❌ [TAKEOFF] CommandLongClient 無法初始化")
return False
# 直接調用 async 方法,無需線程池(避免嵌套執行器衝突)
result = await self.command_long_client.takeoff_async(
result = await client.takeoff_async(
target_sysid=sysid,
altitude_m=float(altitude),
target_compid=0,
timeout_sec=2.0,
timeout_sec=5.0, # 增加超時時間以提高多機操作的可靠性
)
if result and result.success: