修复edge上报错误

This commit is contained in:
Xuwznln
2025-09-04 19:31:19 +08:00
parent 4e52c7d2f4
commit 6ddceb8393
2 changed files with 17 additions and 14 deletions

View File

@@ -283,7 +283,7 @@ def main():
materials.extend(lab_registry.obtain_registry_device_info()) materials.extend(lab_registry.obtain_registry_device_info())
materials = {k["id"]: k for k in materials} materials = {k["id"]: k for k in materials}
nodes = {k["id"]: k for k in data["nodes"]} nodes = {k["id"]: k for k in data["nodes"]}
edge_info = len(resource_edge_info)
for ind, i in enumerate(resource_edge_info[::-1]): for ind, i in enumerate(resource_edge_info[::-1]):
source_node = nodes[i["source"]] source_node = nodes[i["source"]]
target_node = nodes[i["target"]] target_node = nodes[i["target"]]
@@ -293,11 +293,11 @@ def main():
target_handler_keys = [h["handler_key"] for h in materials[target_node["class"]]["handles"] if h["io_type"] == 'target'] target_handler_keys = [h["handler_key"] for h in materials[target_node["class"]]["handles"] if h["io_type"] == 'target']
if not source_handle in source_handler_keys: if not source_handle in source_handler_keys:
print_status(f"节点 {source_node['id']} 的source端点 {source_handle} 不存在,请检查,支持的端点 {source_handler_keys}", "error") print_status(f"节点 {source_node['id']} 的source端点 {source_handle} 不存在,请检查,支持的端点 {source_handler_keys}", "error")
resource_edge_info.pop(-ind - 1) resource_edge_info.pop(edge_info - ind - 1)
continue continue
if not target_handle in target_handler_keys: if not target_handle in target_handler_keys:
print_status(f"节点 {target_node['id']} 的target端点 {target_handle} 不存在,请检查,支持的端点 {target_handler_keys}", "error") print_status(f"节点 {target_node['id']} 的target端点 {target_handle} 不存在,请检查,支持的端点 {target_handler_keys}", "error")
resource_edge_info.pop(-ind - 1) resource_edge_info.pop(edge_info - ind - 1)
continue continue
devices_and_resources = dict_from_graph(graph_res.physical_setup_graph) devices_and_resources = dict_from_graph(graph_res.physical_setup_graph)

View File

@@ -622,6 +622,9 @@ class WebSocketClient(BaseCommunicationClient):
self.message_queue = asyncio.Queue() if not self.is_disabled else None self.message_queue = asyncio.Queue() if not self.is_disabled else None
self.reconnect_count = 0 self.reconnect_count = 0
# 消息发送锁(解决并发写入问题)
self.send_lock = asyncio.Lock()
# 任务调度器 # 任务调度器
self.task_scheduler = None self.task_scheduler = None
@@ -825,16 +828,18 @@ class WebSocketClient(BaseCommunicationClient):
# MessageSender接口实现 # MessageSender接口实现
async def send_message(self, message: Dict[str, Any]) -> None: async def send_message(self, message: Dict[str, Any]) -> None:
"""内部发送消息方法""" """内部发送消息方法,使用锁确保线程安全"""
if not self.connected or not self.websocket: if not self.connected or not self.websocket:
logger.warning("[WebSocket] Not connected, cannot send message") logger.warning("[WebSocket] Not connected, cannot send message")
return return
try: message_str = json.dumps(message, ensure_ascii=False)
message_str = json.dumps(message, ensure_ascii=False) # 使用异步锁防止并发写入导致的竞态条件
await self.websocket.send(message_str) async with self.send_lock:
logger.debug(f"[WebSocket] Message sent: {message['action']}") try:
except Exception as e: await self.websocket.send(message_str)
logger.error(f"[WebSocket] Failed to send message: {str(e)}") logger.debug(f"[WebSocket] Message sent: {message['action']}")
except Exception as e:
logger.error(f"[WebSocket] Failed to send message: {str(e)}")
def is_connected(self) -> bool: def is_connected(self) -> bool:
"""检查是否已连接TaskScheduler调用的接口""" """检查是否已连接TaskScheduler调用的接口"""
@@ -856,8 +861,7 @@ class WebSocketClient(BaseCommunicationClient):
}, },
}, },
} }
if self.event_loop: asyncio.run_coroutine_threadsafe(self.send_message(message), self.event_loop).result()
asyncio.run_coroutine_threadsafe(self.send_message(message), self.event_loop)
logger.debug(f"[WebSocket] Device status published: {device_id}.{property_name}") logger.debug(f"[WebSocket] Device status published: {device_id}.{property_name}")
def publish_job_status( def publish_job_status(
@@ -875,8 +879,7 @@ class WebSocketClient(BaseCommunicationClient):
logger.warning("[WebSocket] Not connected, cannot send ping") logger.warning("[WebSocket] Not connected, cannot send ping")
return return
message = {"action": "ping", "data": {"ping_id": ping_id, "client_timestamp": timestamp}} message = {"action": "ping", "data": {"ping_id": ping_id, "client_timestamp": timestamp}}
if self.event_loop: asyncio.run_coroutine_threadsafe(self.send_message(message), self.event_loop).result()
asyncio.run_coroutine_threadsafe(self.send_message(message), self.event_loop)
logger.debug(f"[WebSocket] Ping sent: {ping_id}") logger.debug(f"[WebSocket] Ping sent: {ping_id}")
def cancel_goal(self, job_id: str) -> None: def cancel_goal(self, job_id: str) -> None: