From 2c38376f9c6bea0361cbea42a0515e3b84423b46 Mon Sep 17 00:00:00 2001 From: ken910606 Date: Tue, 14 Apr 2026 14:10:57 +0800 Subject: [PATCH] Update master with local 2.0.5 --- src/GUI/communication.py | 34 ++++++++++++++-------------------- src/GUI/gui.py | 2 +- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/GUI/communication.py b/src/GUI/communication.py index 4aa904c..250f5eb 100644 --- a/src/GUI/communication.py +++ b/src/GUI/communication.py @@ -602,7 +602,7 @@ class DroneMonitor(Node): # ================================================================================ async def set_mode(self, drone_id, mode_name): - """使用 CommandLongClient 切換無人機飛行模式""" + """使用 CommandLongClient 切換無人機飛行模式(使用非阻塞的 async 方法)""" try: # 解析 drone_id 提取 sysid parts = drone_id.split('_') @@ -619,30 +619,24 @@ class DroneMonitor(Node): print(f"\n📢 [SET_MODE] {drone_id} → {mode_name} (custom_mode={custom_mode})") - # 在線程池中調用 CommandLongClient - loop = asyncio.get_event_loop() - executor = ThreadPoolExecutor(max_workers=1) + if not self.command_long_client: + print(f"❌ [SET_MODE] CommandLongClient 未初始化") + return False - def _call_set_mode(): - client = CommandLongClient() if CommandLongClient else None - if not client: - return False - result = client.change_mode( - target_sysid=sysid, - custom_mode=float(custom_mode), - target_compid=0, - base_mode=1.0, - timeout_sec=2.0, - ) - client.destroy_node() - return result.success if result else False + # 直接調用 async 方法,無需線程池(避免嵌套執行器衝突) + result = await self.command_long_client.change_mode_async( + target_sysid=sysid, + custom_mode=float(custom_mode), + target_compid=0, + base_mode=1.0, + timeout_sec=2.0, + ) - result = await loop.run_in_executor(executor, _call_set_mode) - if result: + if result and result.success: print(f"✅ [SET_MODE] 模式切換成功") return True else: - print(f"❌ [SET_MODE] 模式切換失敗") + print(f"❌ [SET_MODE] 模式切換失敗 (message={result.message if result else 'None'})") return False except Exception as e: print(f"❌ [SET_MODE] 例外錯誤: {e}") diff --git a/src/GUI/gui.py b/src/GUI/gui.py index 5f94975..67734c4 100644 --- a/src/GUI/gui.py +++ b/src/GUI/gui.py @@ -33,7 +33,7 @@ from mission_group import ( # ================================================================================ class ControlStationUI(QMainWindow): - VERSION = '2.0.4' + VERSION = '2.0.5' def __init__(self): super().__init__()