add: bind_parent_ids to resource create action

fix: message convert string
This commit is contained in:
wznln
2025-05-06 16:24:19 +08:00
parent 852d10d751
commit 32e370a562
4 changed files with 33 additions and 29 deletions

View File

@@ -8,7 +8,7 @@ import traceback
from typing import Dict, Any, Type, TypedDict, Optional from typing import Dict, Any, Type, TypedDict, Optional
from rclpy.action import ActionClient, ActionServer from rclpy.action import ActionClient, ActionServer
from rosidl_parser.definition import UnboundedSequence, NamespacedType, BasicType from rosidl_parser.definition import UnboundedSequence, NamespacedType, BasicType, UnboundedString
from unilabos.ros.msgs.message_converter import msg_converter_manager from unilabos.ros.msgs.message_converter import msg_converter_manager
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode
@@ -74,7 +74,6 @@ def get_yaml_from_goal_type(goal_type) -> str:
for ind, slot_info in enumerate(goal_type._fields_and_field_types.items()): for ind, slot_info in enumerate(goal_type._fields_and_field_types.items()):
slot_name, slot_type = slot_info slot_name, slot_type = slot_info
type_info = goal_type.SLOT_TYPES[ind] type_info = goal_type.SLOT_TYPES[ind]
default_value = "unknown"
if isinstance(type_info, UnboundedSequence): if isinstance(type_info, UnboundedSequence):
inner_type = type_info.value_type inner_type = type_info.value_type
if isinstance(inner_type, NamespacedType): if isinstance(inner_type, NamespacedType):
@@ -83,8 +82,10 @@ def get_yaml_from_goal_type(goal_type) -> str:
default_value = [get_ros_msg_instance_as_dict(type_class())] default_value = [get_ros_msg_instance_as_dict(type_class())]
elif isinstance(inner_type, BasicType): elif isinstance(inner_type, BasicType):
default_value = [get_default_value_for_ros_type(inner_type.typename)] default_value = [get_default_value_for_ros_type(inner_type.typename)]
elif isinstance(inner_type, UnboundedString):
default_value = [""]
else: else:
default_value = "unknown" default_value = []
elif isinstance(type_info, NamespacedType): elif isinstance(type_info, NamespacedType):
cls_name = ".".join(type_info.namespaces) + ":" + type_info.name cls_name = ".".join(type_info.namespaces) + ":" + type_info.name
type_class = msg_converter_manager.get_class(cls_name) type_class = msg_converter_manager.get_class(cls_name)
@@ -93,6 +94,8 @@ def get_yaml_from_goal_type(goal_type) -> str:
default_value = get_ros_msg_instance_as_dict(type_class()) default_value = get_ros_msg_instance_as_dict(type_class())
elif isinstance(type_info, BasicType): elif isinstance(type_info, BasicType):
default_value = get_default_value_for_ros_type(type_info.typename) default_value = get_default_value_for_ros_type(type_info.typename)
elif isinstance(type_info, UnboundedString):
default_value = ""
else: else:
type_class = msg_converter_manager.search_class(slot_type, search_lower=True) type_class = msg_converter_manager.search_class(slot_type, search_lower=True)
if type_class is not None: if type_class is not None:

View File

@@ -439,8 +439,8 @@ class BaseROS2DeviceNode(Node, Generic[T]):
action_kwargs = convert_from_ros_msg_with_mapping(goal, action_value_mapping["goal"]) action_kwargs = convert_from_ros_msg_with_mapping(goal, action_value_mapping["goal"])
self.lab_logger().debug(f"接收到原始目标: {action_kwargs}") self.lab_logger().debug(f"接收到原始目标: {action_kwargs}")
# 向Host查询物料当前状态如果是host本身的增加物料的请求则直接跳过 # 向Host查询物料当前状态如果是host本身的增加物料的请求则直接跳过
if action_name != "add_resource_from_outer":
for k, v in goal.get_fields_and_field_types().items(): for k, v in goal.get_fields_and_field_types().items():
if v in ["unilabos_msgs/Resource", "sequence<unilabos_msgs/Resource>"]: if v in ["unilabos_msgs/Resource", "sequence<unilabos_msgs/Resource>"]:
self.lab_logger().info(f"查询资源状态: Key: {k} Type: {v}") self.lab_logger().info(f"查询资源状态: Key: {k} Type: {v}")

View File

@@ -7,11 +7,12 @@ import uuid
from typing import Optional, Dict, Any, List, ClassVar, Set from typing import Optional, Dict, Any, List, ClassVar, Set
from action_msgs.msg import GoalStatus from action_msgs.msg import GoalStatus
from unilabos_msgs.msg import Resource # type: ignore
from unilabos_msgs.srv import ResourceAdd, ResourceGet, ResourceDelete, ResourceUpdate, ResourceList, SerialCommand # type: ignore
from rclpy.action import ActionClient, get_action_server_names_and_types_by_node from rclpy.action import ActionClient, get_action_server_names_and_types_by_node
from rclpy.callback_groups import ReentrantCallbackGroup from rclpy.callback_groups import ReentrantCallbackGroup
from rclpy.service import Service from rclpy.service import Service
from unilabos_msgs.msg import Resource # type: ignore
from unilabos_msgs.srv import ResourceAdd, ResourceGet, ResourceDelete, ResourceUpdate, ResourceList, \
SerialCommand # type: ignore
from unique_identifier_msgs.msg import UUID from unique_identifier_msgs.msg import UUID
from unilabos.registry.registry import lab_registry from unilabos.registry.registry import lab_registry
@@ -23,11 +24,9 @@ from unilabos.ros.msgs.message_converter import (
convert_from_ros_msg, convert_from_ros_msg,
convert_to_ros_msg, convert_to_ros_msg,
msg_converter_manager, msg_converter_manager,
ros_action_to_json_schema,
) )
from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode, ROS2DeviceNode, DeviceNodeResourceTracker from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode, ROS2DeviceNode, DeviceNodeResourceTracker
from unilabos.ros.nodes.presets.controller_node import ControllerNode from unilabos.ros.nodes.presets.controller_node import ControllerNode
from unilabos.utils.type_check import TypeEncoder
class HostNode(BaseROS2DeviceNode): class HostNode(BaseROS2DeviceNode):
@@ -76,7 +75,7 @@ class HostNode(BaseROS2DeviceNode):
driver_instance=self, driver_instance=self,
device_id=device_id, device_id=device_id,
status_types={}, status_types={},
action_value_mappings={}, action_value_mappings=lab_registry.device_type_registry["host_node"]["class"]["action_value_mappings"],
hardware_interface={}, hardware_interface={},
print_publish=False, print_publish=False,
resource_tracker=DeviceNodeResourceTracker(), # host node并不是通过initialize 包一层传进来的 resource_tracker=DeviceNodeResourceTracker(), # host node并不是通过initialize 包一层传进来的
@@ -268,7 +267,8 @@ class HostNode(BaseROS2DeviceNode):
except Exception as e: except Exception as e:
self.lab_logger().error(f"[Host Node] Failed to create ActionClient for {action_id}: {str(e)}") self.lab_logger().error(f"[Host Node] Failed to create ActionClient for {action_id}: {str(e)}")
def add_resource_from_outer(self, resources: list["Resource"], device_ids): def add_resource_from_outer(self, resources: list["Resource"], device_ids: list[str], bind_parent_ids: list[str]):
for resource, device_id, bind_parent_id in zip(resources, device_ids, bind_parent_ids):
print("111") print("111")
pass pass

View File

@@ -1,5 +1,6 @@
Resource[] resources Resource[] resources
string[] device_ids string[] device_ids
string[] bind_parent_ids
--- ---
bool success bool success
--- ---