diff --git a/unilabos/app/web/client.py b/unilabos/app/web/client.py index 1957f5dd..da5d0696 100644 --- a/unilabos/app/web/client.py +++ b/unilabos/app/web/client.py @@ -9,6 +9,7 @@ from typing import List, Dict, Any, Optional import requests from unilabos.utils.log import info from unilabos.config.config import MQConfig, HTTPConfig +from unilabos.utils import logger class HTTPClient: @@ -102,6 +103,30 @@ class HTTPClient: ) return response + def upload_file(self, file_path: str, scene: str = "models") -> requests.Response: + """ + 上传文件到服务器 + + 使用multipart/form-data格式上传文件,类似curl -F "files=@filepath" + + Args: + file_path: 要上传的文件路径 + scene: 上传场景,可选值为"user"或"models",默认为"models" + + Returns: + Response: API响应对象 + """ + with open(file_path, "rb") as file: + files = {"files": file} + logger.info(f"上传文件: {file_path} 到 {scene}") + response = requests.post( + f"{self.remote_addr}/api/account/file_upload/{scene}", + files=files, + headers={"Authorization": f"lab {self.auth}"}, + timeout=30, # 上传文件可能需要更长的超时时间 + ) + return response + # 创建默认客户端实例 http_client = HTTPClient() diff --git a/unilabos/ros/nodes/presets/host_node.py b/unilabos/ros/nodes/presets/host_node.py index 5a739773..df1f3c25 100644 --- a/unilabos/ros/nodes/presets/host_node.py +++ b/unilabos/ros/nodes/presets/host_node.py @@ -97,7 +97,7 @@ class HostNode(BaseROS2DeviceNode): self.bridges = bridges # 创建设备、动作客户端和目标存储 - self.devices_names: Dict[str, str] = {} # 存储设备名称和命名空间的映射 + self.devices_names: Dict[str, str] = {device_id: self.namespace} # 存储设备名称和命名空间的映射 self.devices_instances: Dict[str, ROS2DeviceNode] = {} # 存储设备实例 self.device_machine_names: Dict[str, str] = {device_id: "本地", } # 存储设备ID到机器名称的映射 self._action_clients: Dict[str, ActionClient] = {} # 用来存储多个ActionClient实例