From e5749a80584b2c776c0993749e8a8f02a7371111 Mon Sep 17 00:00:00 2001 From: wznln <18435084+Xuwznln@users.noreply.github.com> Date: Tue, 29 Apr 2025 13:42:30 +0800 Subject: [PATCH] close #12 feat: slave node registry --- unilabos/app/main.py | 2 +- unilabos/app/web/pages.py | 2 +- unilabos/app/web/templates/status.html | 2 +- unilabos/registry/registry.py | 9 +++------ unilabos/ros/main_slave_run.py | 14 +++++++------- unilabos/ros/nodes/presets/host_node.py | 6 ++++++ 6 files changed, 19 insertions(+), 16 deletions(-) diff --git a/unilabos/app/main.py b/unilabos/app/main.py index dab1b347..8b87736f 100644 --- a/unilabos/app/main.py +++ b/unilabos/app/main.py @@ -61,7 +61,7 @@ def parse_args(): parser.add_argument( "--port", type=int, - default=None, + default=8002, help="信息页web服务的启动端口", ) parser.add_argument( diff --git a/unilabos/app/web/pages.py b/unilabos/app/web/pages.py index add64c6a..a08cebb5 100644 --- a/unilabos/app/web/pages.py +++ b/unilabos/app/web/pages.py @@ -92,7 +92,7 @@ def setup_web_pages(router: APIRouter) -> None: # 获取已加载的设备 if lab_registry: - devices = lab_registry.obtain_registry_device_info() + devices = json.loads(json.dumps(lab_registry.obtain_registry_device_info(), ensure_ascii=False, cls=TypeEncoder)) # 资源类型 for resource_id, resource_info in lab_registry.resource_type_registry.items(): resources.append( diff --git a/unilabos/app/web/templates/status.html b/unilabos/app/web/templates/status.html index 30c3e6b8..9217691c 100644 --- a/unilabos/app/web/templates/status.html +++ b/unilabos/app/web/templates/status.html @@ -329,7 +329,7 @@
-
{{ device.class_json }}
+
{{ device.class|tojson(indent=4) }}
{% if device.is_online %}
在线
diff --git a/unilabos/registry/registry.py b/unilabos/registry/registry.py index b8120330..feba3fef 100644 --- a/unilabos/registry/registry.py +++ b/unilabos/registry/registry.py @@ -7,7 +7,7 @@ from typing import Any import yaml from unilabos.utils import logger -from unilabos.ros.msgs.message_converter import msg_converter_manager +from unilabos.ros.msgs.message_converter import msg_converter_manager, ros_action_to_json_schema from unilabos.utils.decorator import singleton from unilabos.utils.type_check import TypeEncoder @@ -131,6 +131,7 @@ class Registry: action_config["type"] = self._replace_type_with_class( action_config["type"], device_id, f"动作 {action_name}" ) + action_config["schema"] = ros_action_to_json_schema(action_config["type"]) self.device_type_registry.update(data) @@ -150,11 +151,7 @@ class Registry: for device_id, device_info in self.device_type_registry.items(): msg = { "id": device_id, - "name": device_info.get("name", "未命名"), - "file_path": device_info.get("file_path", ""), - "class_json": json.dumps( - device_info.get("class", {}), indent=4, ensure_ascii=False, cls=TypeEncoder - ), + **device_info } devices.append(msg) return devices diff --git a/unilabos/ros/main_slave_run.py b/unilabos/ros/main_slave_run.py index 5c038bdc..974359a3 100644 --- a/unilabos/ros/main_slave_run.py +++ b/unilabos/ros/main_slave_run.py @@ -1,3 +1,4 @@ +import copy import json import os import traceback @@ -85,7 +86,7 @@ def slave( """从节点函数""" rclpy.init(args=args) rclpy.__executor = executor = MultiThreadedExecutor() - + devices_config_copy = copy.deepcopy(devices_config) for device_id, device_config in devices_config.items(): d = initialize_device_from_dict(device_id, device_config) if d is None: @@ -109,20 +110,19 @@ def slave( request.command = json.dumps({ "machine_name": machine_name, "type": "slave", - "devices_config": devices_config, + "devices_config": devices_config_copy, "registry_config": lab_registry.obtain_registry_device_info() }, ensure_ascii=False, cls=TypeEncoder) - response = sclient.call_async(request) - logger.info(f"Slave node info update response: {response}") + response = sclient.call_async(request).result() + logger.info(f"Slave node info updated.") rclient = n.create_client(ResourceAdd, "/resources/add") rclient.wait_for_service() # FIXME 可能一直等待,加一个参数 request = ResourceAdd.Request() request.resources = [convert_to_ros_msg(Resource, resource) for resource in resources_config] - response = rclient.call_async(request) - logger.info(f"Slave resource add response: {response}") - + response = rclient.call_async(request).result() + logger.info(f"Slave resource added.") run_event_loop_in_thread() diff --git a/unilabos/ros/nodes/presets/host_node.py b/unilabos/ros/nodes/presets/host_node.py index 8e07e05c..e8d43b35 100644 --- a/unilabos/ros/nodes/presets/host_node.py +++ b/unilabos/ros/nodes/presets/host_node.py @@ -516,7 +516,13 @@ class HostNode(BaseROS2DeviceNode): """ self.lab_logger().info(f"[Host Node] Node info update request received: {request}") try: + from unilabos.app.mq import mqtt_client info = json.loads(request.command) + machine_name = info["machine_name"] + devices_config = info["devices_config"] + registry_config = info["registry_config"] + for device_config in registry_config: + mqtt_client.publish_registry(device_config["id"], device_config) self.lab_logger().info(f"[Host Node] Node info update: {info}") response.response = "OK" except Exception as e: