Compare commits

...

5 Commits

Author SHA1 Message Date
Xuwznln
a1e9332b51 temp fix for resource get 2025-10-16 03:20:37 +08:00
Xuwznln
357fc038ef temp fix for resource get 2025-10-16 03:15:56 +08:00
Xuwznln
fd58ef07f3 Update boot example 2025-10-16 02:33:15 +08:00
Xuwznln
93dee2c1dc fix workstation node error 2025-10-16 01:59:48 +08:00
Xuwznln
70fbf19009 fix workstation node error 2025-10-16 01:58:15 +08:00
5 changed files with 36 additions and 13 deletions

View File

@@ -91,7 +91,7 @@
使用以下命令启动模拟反应器: 使用以下命令启动模拟反应器:
```bash ```bash
unilab -g test/experiments/mock_reactor.json --app_bridges "" unilab -g test/experiments/mock_reactor.json
``` ```
### 2. 执行抽真空和充气操作 ### 2. 执行抽真空和充气操作

View File

@@ -17,6 +17,7 @@ from unilabos.ros.nodes.resource_tracker import (
ResourceDictInstance, ResourceDictInstance,
ResourceTreeSet, ResourceTreeSet,
) )
from unilabos.utils import logger
from unilabos.utils.banner_print import print_status from unilabos.utils.banner_print import print_status
try: try:
@@ -67,6 +68,10 @@ def canonicalize_nodes_data(
z = node.pop("z", None) z = node.pop("z", None)
if z is not None: if z is not None:
node["position"]["position"]["z"] = z node["position"]["position"]["z"] = z
if "sample_id" in node:
sample_id = node.pop("sample_id")
if sample_id:
logger.error(f"{node}的sample_id参数已弃用sample_id: {sample_id}")
for k in list(node.keys()): for k in list(node.keys()):
if k not in ["id", "uuid", "name", "description", "schema", "model", "icon", "parent_uuid", "parent", "type", "class", "position", "config", "data", "children"]: if k not in ["id", "uuid", "name", "description", "schema", "model", "icon", "parent_uuid", "parent", "type", "class", "position", "config", "data", "children"]:
v = node.pop(k) v = node.pop(k)

View File

@@ -6,7 +6,7 @@ import threading
import time import time
import traceback import traceback
import uuid import uuid
from typing import get_type_hints, TypeVar, Generic, Dict, Any, Type, TypedDict, Optional, List, TYPE_CHECKING from typing import get_type_hints, TypeVar, Generic, Dict, Any, Type, TypedDict, Optional, List, TYPE_CHECKING, Union
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
import asyncio import asyncio
@@ -657,15 +657,27 @@ class BaseROS2DeviceNode(Node, Generic[T]):
results.append({"success": True, "action": "update"}) results.append({"success": True, "action": "update"})
elif action == "remove": elif action == "remove":
# 移除资源 # 移除资源
plr_resources: List[ResourcePLR] = [ found_resources: List[List[Union[ResourcePLR, dict]]] = self.resource_tracker.figure_resource(
self.resource_tracker.uuid_to_resources[i] for i in resources_uuid [{"uuid": uid} for uid in resources_uuid], try_mode=True
] )
found_plr_resources = []
other_plr_resources = []
for res_list in found_resources:
for res in res_list:
if issubclass(res.__class__, ResourcePLR):
found_plr_resources.append(res)
else:
other_plr_resources.append(res)
func = getattr(self.driver_instance, "resource_tree_remove", None) func = getattr(self.driver_instance, "resource_tree_remove", None)
if callable(func): if callable(func):
func(plr_resources) func(found_plr_resources)
for plr_resource in plr_resources: for plr_resource in found_plr_resources:
plr_resource.parent.unassign_child_resource(plr_resource) plr_resource.parent.unassign_child_resource(plr_resource)
self.resource_tracker.remove_resource(plr_resource) self.resource_tracker.remove_resource(plr_resource)
self.lab_logger().info(f"移除物料 {plr_resource} 及其子节点")
for res in other_plr_resources:
self.resource_tracker.remove_resource(res)
self.lab_logger().info(f"移除物料 {res} 及其子节点")
results.append({"success": True, "action": "remove"}) results.append({"success": True, "action": "remove"})
except Exception as e: except Exception as e:
error_msg = f"Error processing {action} operation: {str(e)}" error_msg = f"Error processing {action} operation: {str(e)}"
@@ -945,7 +957,10 @@ class BaseROS2DeviceNode(Node, Generic[T]):
# 通过资源跟踪器获取本地实例 # 通过资源跟踪器获取本地实例
final_resources = queried_resources if is_sequence else queried_resources[0] final_resources = queried_resources if is_sequence else queried_resources[0]
action_kwargs[k] = self.resource_tracker.figure_resource(final_resources, try_mode=False) final_resources = self.resource_tracker.figure_resource({"name": final_resources.name}, try_mode=False) if not is_sequence else [
self.resource_tracker.figure_resource({"name": res.name}, try_mode=False) for res in queried_resources
]
action_kwargs[k] = final_resources
except Exception as e: except Exception as e:
self.lab_logger().error(f"{action_name} 物料实例获取失败: {e}\n{traceback.format_exc()}") self.lab_logger().error(f"{action_name} 物料实例获取失败: {e}\n{traceback.format_exc()}")

View File

@@ -6,6 +6,8 @@ from typing import List, Dict, Any, Optional, TYPE_CHECKING
import rclpy import rclpy
from rosidl_runtime_py import message_to_ordereddict from rosidl_runtime_py import message_to_ordereddict
from unilabos_msgs.msg import Resource
from unilabos_msgs.srv import ResourceUpdate
from unilabos.messages import * # type: ignore # protocol names from unilabos.messages import * # type: ignore # protocol names
from rclpy.action import ActionServer, ActionClient from rclpy.action import ActionServer, ActionClient
@@ -241,7 +243,7 @@ class ROS2WorkstationNode(BaseROS2DeviceNode):
raw_data = json.loads(response.response) raw_data = json.loads(response.response)
tree_set = ResourceTreeSet.from_raw_list(raw_data) tree_set = ResourceTreeSet.from_raw_list(raw_data)
target = tree_set.dump() target = tree_set.dump()
protocol_kwargs[k] = target[0] protocol_kwargs[k] = target[0][0] if v == "unilabos_msgs/Resource" else target
except Exception as ex: except Exception as ex:
self.lab_logger().error(f"查询资源失败: {k}, 错误: {ex}\n{traceback.format_exc()}") self.lab_logger().error(f"查询资源失败: {k}, 错误: {ex}\n{traceback.format_exc()}")
raise raise

View File

@@ -774,7 +774,8 @@ class DeviceNodeResourceTracker(object):
else: else:
return getattr(resource, uuid_attr, None) return getattr(resource, uuid_attr, None)
def _set_resource_uuid(self, resource, new_uuid: str): @classmethod
def set_resource_uuid(cls, resource, new_uuid: str):
""" """
设置资源的 uuid统一处理 dict 和 instance 两种类型 设置资源的 uuid统一处理 dict 和 instance 两种类型
@@ -827,7 +828,7 @@ class DeviceNodeResourceTracker(object):
resource_name = self._get_resource_attr(res, "name") resource_name = self._get_resource_attr(res, "name")
if resource_name and resource_name in name_to_uuid_map: if resource_name and resource_name in name_to_uuid_map:
new_uuid = name_to_uuid_map[resource_name] new_uuid = name_to_uuid_map[resource_name]
self._set_resource_uuid(res, new_uuid) self.set_resource_uuid(res, new_uuid)
self.uuid_to_resources[new_uuid] = res self.uuid_to_resources[new_uuid] = res
logger.debug(f"设置资源UUID: {resource_name} -> {new_uuid}") logger.debug(f"设置资源UUID: {resource_name} -> {new_uuid}")
return 1 return 1
@@ -839,7 +840,7 @@ class DeviceNodeResourceTracker(object):
""" """
递归遍历资源树更新所有节点的uuid 递归遍历资源树更新所有节点的uuid
Args: Args:0
resource: 资源对象可以是dict或实例 resource: 资源对象可以是dict或实例
uuid_map: uuid映射字典{old_uuid: new_uuid} uuid_map: uuid映射字典{old_uuid: new_uuid}
@@ -853,7 +854,7 @@ class DeviceNodeResourceTracker(object):
if current_uuid and current_uuid in uuid_map: if current_uuid and current_uuid in uuid_map:
new_uuid = uuid_map[current_uuid] new_uuid = uuid_map[current_uuid]
if current_uuid != new_uuid: if current_uuid != new_uuid:
self._set_resource_uuid(res, new_uuid) self.set_resource_uuid(res, new_uuid)
# 更新uuid_to_resources映射 # 更新uuid_to_resources映射
if current_uuid in self.uuid_to_resources: if current_uuid in self.uuid_to_resources:
self.uuid_to_resources.pop(current_uuid) self.uuid_to_resources.pop(current_uuid)