Update registry. Update uuid loop figure method. Update install docs.

This commit is contained in:
Xuwznln
2025-10-12 22:38:04 +08:00
parent 40fb1ea49c
commit 9fa3688196
8 changed files with 285 additions and 65 deletions

View File

@@ -1,4 +1,3 @@
import copy
import json
import threading
import time
@@ -183,14 +182,22 @@ def slave(
)
tree_response: SerialCommand_Response = rclient.call_async(request).result()
uuid_mapping = json.loads(tree_response.response)
# 创建反向映射new_uuid -> old_uuid
reverse_uuid_mapping = {new_uuid: old_uuid for old_uuid, new_uuid in uuid_mapping.items()}
for node in resources_config.root_nodes:
if node.res_content.type == "device":
for sub_node in node.children:
# 只有二级子设备
if sub_node.res_content.type != "device":
device_tracker = devices_instances[node.res_content.id].resource_tracker
resource_instance = device_tracker.figure_resource(
{"uuid": sub_node.res_content.uuid})
# sub_node.res_content.uuid 已经是新UUID需要用旧UUID去查找
old_uuid = reverse_uuid_mapping.get(sub_node.res_content.uuid)
if old_uuid:
# 找到旧UUID使用UUID查找
resource_instance = device_tracker.figure_resource({"uuid": old_uuid})
else:
# 未找到旧UUID使用name查找
resource_instance = device_tracker.figure_resource({"name": sub_node.res_content.name})
device_tracker.loop_update_uuid(resource_instance, uuid_mapping)
else:
logger.error("Slave模式不允许新增非设备节点下的物料")

View File

@@ -252,6 +252,8 @@ class HostNode(BaseROS2DeviceNode):
)
# resources_config 通过各个设备的 resource_tracker 进行uuid更新利用uuid_mapping
# resources_config 的 root node 是
# 创建反向映射new_uuid -> old_uuid
reverse_uuid_mapping = {new_uuid: old_uuid for old_uuid, new_uuid in uuid_mapping.items()}
for tree in resources_config.trees:
node = tree.root_node
if node.res_content.type == "device":
@@ -260,8 +262,16 @@ class HostNode(BaseROS2DeviceNode):
if sub_node.res_content.type != "device":
# slave节点走c2s更新接口拿到add自行update uuid
device_tracker = self.devices_instances[node.res_content.id].resource_tracker
resource_instance = device_tracker.figure_resource(
{"uuid": sub_node.res_content.uuid})
# sub_node.res_content.uuid 已经是新UUID需要用旧UUID去查找
old_uuid = reverse_uuid_mapping.get(sub_node.res_content.uuid)
if old_uuid:
# 找到旧UUID使用UUID查找
resource_instance = device_tracker.figure_resource({"uuid": old_uuid})
else:
# 未找到旧UUID使用name查找
resource_instance = device_tracker.figure_resource(
{"name": sub_node.res_content.name}
)
device_tracker.loop_update_uuid(resource_instance, uuid_mapping)
else:
try:
@@ -897,6 +907,7 @@ class HostNode(BaseROS2DeviceNode):
uuid_list: List[str] = data["data"]
with_children: bool = data["with_children"]
from unilabos.app.web.client import http_client
resource_response = http_client.resource_tree_get(uuid_list, with_children)
response.response = json.dumps(resource_response)
@@ -920,6 +931,7 @@ class HostNode(BaseROS2DeviceNode):
)
from unilabos.app.web.client import http_client
resource_start_time = time.time()
uuid_mapping = http_client.resource_tree_update(resource_tree_set, "", False)
success = bool(uuid_mapping)
@@ -1254,7 +1266,9 @@ class HostNode(BaseROS2DeviceNode):
"status": "success",
}
def test_resource(self, resource: ResourceSlot, resources: List[ResourceSlot], device: DeviceSlot, devices: List[DeviceSlot]):
def test_resource(
self, resource: ResourceSlot, resources: List[ResourceSlot], device: DeviceSlot, devices: List[DeviceSlot]
):
return {
"resources": ResourceTreeSet.from_plr_resources([resource, *resources]).dump(),
"devices": [device, *devices],
@@ -1280,9 +1294,7 @@ class HostNode(BaseROS2DeviceNode):
else:
self.lab_logger().warning("⚠️ 收到无效的Pong响应缺少ping_id")
def notify_resource_tree_update(
self, device_id: str, action: str, resource_uuid_list: List[str]
) -> bool:
def notify_resource_tree_update(self, device_id: str, action: str, resource_uuid_list: List[str]) -> bool:
"""
通知设备节点更新资源树

View File

@@ -1,13 +1,8 @@
import json
import time
import traceback
<<<<<<<< HEAD:unilabos/ros/nodes/presets/workstation.py
from pprint import pformat
from typing import List, Dict, Any, Optional, TYPE_CHECKING
========
from pprint import pprint, saferepr, pformat
from typing import Union
>>>>>>>> main:unilabos/ros/nodes/presets/protocol_node.py
import rclpy
from rosidl_runtime_py import message_to_ordereddict
@@ -198,13 +193,8 @@ class ROS2WorkstationNode(BaseROS2DeviceNode):
execute_callback=self._create_protocol_execute_callback(action_name, protocol_steps_generator),
callback_group=ReentrantCallbackGroup(),
)
<<<<<<<< HEAD:unilabos/ros/nodes/presets/workstation.py
self.lab_logger().trace(f"发布动作: {action_name}, 类型: {str_action_type}")
return
========
self.lab_logger().trace(f"发布动作: {action_name}, 类型: {str_action_type}")
>>>>>>>> main:unilabos/ros/nodes/presets/protocol_node.py
def _create_protocol_execute_callback(self, protocol_name, protocol_steps_generator):
async def execute_protocol(goal_handle: ServerGoalHandle):
@@ -255,15 +245,10 @@ class ROS2WorkstationNode(BaseROS2DeviceNode):
logs.append(step)
elif isinstance(step, list):
logs.append(step)
<<<<<<<< HEAD:unilabos/ros/nodes/presets/workstation.py
self.lab_logger().info(
f"Goal received: {protocol_kwargs}, running steps: "
f"{json.dumps(logs, indent=4, ensure_ascii=False)}"
)
========
self.lab_logger().info(f"Goal received: {protocol_kwargs}, running steps: "
f"{json.dumps(logs, indent=4, ensure_ascii=False)}")
>>>>>>>> main:unilabos/ros/nodes/presets/protocol_node.py
time_start = time.time()
time_overall = 100
@@ -278,7 +263,6 @@ class ROS2WorkstationNode(BaseROS2DeviceNode):
time.sleep(action["action_kwargs"]["time"])
step_results.append({"step": i + 1, "action": "wait", "result": "completed"})
else:
<<<<<<<< HEAD:unilabos/ros/nodes/presets/workstation.py
try:
result = await self.execute_single_action(**action)
step_results.append({"step": i + 1, "action": action["action_name"], "result": result})
@@ -289,13 +273,6 @@ class ROS2WorkstationNode(BaseROS2DeviceNode):
step_results.append(
{"step": i + 1, "action": action["action_name"], "result": ex.args[0]}
)
========
result = await self.execute_single_action(**action)
step_results.append({"step": i + 1, "action": action["action_name"], "result": result})
ret_info = json.loads(getattr(result, "return_info", "{}"))
if not ret_info.get("suc", False):
raise RuntimeError(f"Step {i + 1} failed.")
>>>>>>>> main:unilabos/ros/nodes/presets/protocol_node.py
elif isinstance(action, list):
# 如果是并行动作,同时执行
actions = action
@@ -333,7 +310,6 @@ class ROS2WorkstationNode(BaseROS2DeviceNode):
except Exception as e:
# 捕获并记录错误信息
<<<<<<<< HEAD:unilabos/ros/nodes/presets/workstation.py
str_step_results = [
{
k: dict(message_to_ordereddict(v)) if k == "result" and hasattr(v, "SLOT_TYPES") else v
@@ -341,9 +317,6 @@ class ROS2WorkstationNode(BaseROS2DeviceNode):
}
for i in step_results
]
========
str_step_results = [{k: dict(message_to_ordereddict(v)) if k == "result" and hasattr(v, "SLOT_TYPES") else v for k, v in i.items()} for i in step_results]
>>>>>>>> main:unilabos/ros/nodes/presets/protocol_node.py
execution_error = f"{traceback.format_exc()}\n\nStep Result: {pformat(str_step_results)}"
execution_success = False
self.lab_logger().error(f"协议 {protocol_name} 执行出错: {str(e)} \n{traceback.format_exc()}")

View File

@@ -1060,6 +1060,8 @@ class DeviceNodeResourceTracker(object):
else:
# 对于实例类型,需要特殊处理 uuid 字段
# 如果查找的是 unilabos_uuid使用 getattr
if identifier_key == "uuid":
identifier_key = "unilabos_uuid"
if hasattr(resource, identifier_key):
if getattr(resource, identifier_key) == compare_value:
res_list.append((parent_res, resource))