调整agent布局
This commit is contained in:
@@ -128,41 +128,190 @@ def scheme_convert_to_json():
|
||||
12. step_output 类型: 字符串; 说明: 步骤的输出标识符,用于后续步骤的输入。限制: 标识符应唯一且有意义。
|
||||
"""
|
||||
|
||||
def send_json_to_robot(json_data: str):
|
||||
import socket
|
||||
def send_instruction_to_robot_platform():
|
||||
"""从S3获取最新的json文件并返回其URL链接
|
||||
|
||||
Returns:
|
||||
str: 最新json文件的预签名URL
|
||||
"""
|
||||
import boto3
|
||||
from botocore.exceptions import NoCredentialsError
|
||||
|
||||
# S3配置
|
||||
endpoint_url = "http://100.85.52.31:9000" or "https://s3-api.siat-mic.com"
|
||||
aws_access_key_id = "9bUtQL1Gpo9JB6o3pSGr"
|
||||
aws_secret_access_key = "1Qug5H73R3kP8boIHvdVcFtcb1jU9GRWnlmMpx0g"
|
||||
bucket_name = "temp"
|
||||
|
||||
try:
|
||||
# 创建S3客户端
|
||||
s3 = boto3.client(
|
||||
's3',
|
||||
endpoint_url=endpoint_url,
|
||||
aws_access_key_id=aws_access_key_id,
|
||||
aws_secret_access_key=aws_secret_access_key
|
||||
)
|
||||
|
||||
# 列出bucket中的所有json文件
|
||||
response = s3.list_objects_v2(
|
||||
Bucket=bucket_name,
|
||||
Prefix='',
|
||||
Delimiter='/'
|
||||
)
|
||||
|
||||
# 过滤出json文件并按最后修改时间排序
|
||||
json_files = [
|
||||
obj for obj in response.get('Contents', [])
|
||||
if obj['Key'].endswith('.json')
|
||||
]
|
||||
json_files.sort(key=lambda x: x['LastModified'], reverse=True)
|
||||
|
||||
if not json_files:
|
||||
return "No JSON files found in S3 bucket"
|
||||
|
||||
# 获取最新文件
|
||||
latest_file = json_files[0]
|
||||
|
||||
# 生成预签名URL
|
||||
url = s3.generate_presigned_url(
|
||||
'get_object',
|
||||
Params={
|
||||
'Bucket': bucket_name,
|
||||
'Key': latest_file['Key']
|
||||
},
|
||||
ExpiresIn=3600 # URL有效期1小时
|
||||
)
|
||||
|
||||
# 将内部URL转换为外部可访问URL
|
||||
external_url = url.replace("http://100.85.52.31:9000", "https://s3-api.siat-mic.com")
|
||||
|
||||
# 发送URL到FastAPI服务器
|
||||
try:
|
||||
response = requests.post(
|
||||
"http://localhost:8030/receive",
|
||||
json={"url": external_url}
|
||||
)
|
||||
response.raise_for_status()
|
||||
return external_url
|
||||
except requests.exceptions.RequestException as e:
|
||||
return f"Error sending URL to server: {str(e)}"
|
||||
|
||||
except NoCredentialsError:
|
||||
return "Credentials not available"
|
||||
except Exception as e:
|
||||
return f"Error: {str(e)}"
|
||||
|
||||
|
||||
def upload_to_s3(json_data: str):
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import datetime
|
||||
|
||||
def install_boto3():
|
||||
try:
|
||||
# 检查 boto3 是否已安装
|
||||
import boto3
|
||||
print("boto3 已安装。")
|
||||
except ImportError:
|
||||
# 如果未安装,动态安装 boto3
|
||||
print("正在安装 boto3...")
|
||||
subprocess.check_call([sys.executable, "-m", "pip", "install", "boto3"])
|
||||
print("boto3 安装完成。")
|
||||
|
||||
def handle_minio_upload(file_path: str, file_name: str) -> str:
|
||||
"""统一处理MinIO上传"""
|
||||
import boto3
|
||||
try:
|
||||
client = boto3.client(
|
||||
's3',
|
||||
endpoint_url="http://100.85.52.31:9000" or "https://s3-api.siat-mic.com",
|
||||
aws_access_key_id="9bUtQL1Gpo9JB6o3pSGr",
|
||||
aws_secret_access_key="1Qug5H73R3kP8boIHvdVcFtcb1jU9GRWnlmMpx0g"
|
||||
)
|
||||
client.upload_file(file_path, "temp", file_name, ExtraArgs={"ACL": "private"})
|
||||
|
||||
# 生成预签名 URL
|
||||
url = client.generate_presigned_url(
|
||||
'get_object',
|
||||
Params={'Bucket': "temp", 'Key': file_name},
|
||||
ExpiresIn=3600
|
||||
)
|
||||
return url.replace("http://100.85.52.31:9000" or "", "https://s3-api.siat-mic.com")
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return e
|
||||
|
||||
install_boto3()
|
||||
# 去掉可能存在的 ```json 和 ``` 标记
|
||||
json_data_cleaned = re.sub(r'```json|```', '', json_data).strip()
|
||||
|
||||
try:
|
||||
# 尝试解析清理后的JSON数据
|
||||
data = json.loads(json_data_cleaned)
|
||||
with open('1.json') as f:
|
||||
json.dump(data, f, indent=4)
|
||||
# print("解析后的JSON数据:", data)
|
||||
with tempfile.NamedTemporaryFile(mode='w', delete=False) as temp_file:
|
||||
try:
|
||||
json.dump(data, temp_file, indent=4, ensure_ascii=False)
|
||||
temp_file.flush() # 确保数据写入文件
|
||||
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
file_name = f"robot_expriment_results_{timestamp}.json"
|
||||
url = handle_minio_upload(temp_file.name, file_name)
|
||||
print(f"文件上传成功, 唯一链接为{url}, 请将该URL记下来并传递给机器人平台。")
|
||||
except Exception as e:
|
||||
print(f"写入临时文件或上传文件时出错: {e}")
|
||||
raise # 重新抛出异常以便上层调用者处理
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"JSON解析错误: {e}")
|
||||
return
|
||||
raise # 重新抛出异常以便上层调用者处理
|
||||
|
||||
def get_latest_exp_log():
|
||||
def get_uv_latest_file():
|
||||
import os
|
||||
import glob
|
||||
# UV数据缓存文件夹路径 (请将此路径修改为实际的文件夹路径)
|
||||
current_folder = os.path.dirname(os.path.abspath(__file__))
|
||||
folder_path = os.path.join(current_folder, 'data/UV/')
|
||||
|
||||
# 查找文件夹中的所有 .wls 文件
|
||||
uv_files = sorted(glob.glob(os.path.join(folder_path, '*.[Tt][Xx][Tt]')))
|
||||
|
||||
if not uv_files:
|
||||
res = f"ERROR: 缓存文件夹{current_folder}中没有找到任何UV文件"
|
||||
return res
|
||||
|
||||
# 找到最新修改的文件
|
||||
latest_file = uv_files[-1]
|
||||
res = f"找到最新的UV数据文件: {latest_file}"
|
||||
|
||||
return res
|
||||
|
||||
# 创建UDP套接字
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
def get_pl_latest_file():
|
||||
import os
|
||||
import glob
|
||||
|
||||
# 目标地址和端口
|
||||
server_address = ('172.20.103.79', 10000)
|
||||
current_folder = os.path.dirname(os.path.abspath(__file__))
|
||||
folder_path = os.path.join(current_folder, 'data/PL/')
|
||||
|
||||
try:
|
||||
# 序列化为JSON字符串并编码为字节
|
||||
json_bytes = json.dumps(data).encode('utf-8')
|
||||
# 查找文件夹中的所有 .txt 或 .TXT 文件
|
||||
pl_files = sorted(glob.glob(os.path.join(folder_path, '*.[Tt][Xx][Tt]')))
|
||||
|
||||
# 发送数据
|
||||
sock.sendto(json_bytes, server_address)
|
||||
print("指令发送成功")
|
||||
except Exception as e:
|
||||
print(f"发送数据时发生错误: {e}")
|
||||
finally:
|
||||
# 关闭套接字
|
||||
sock.close()
|
||||
if not pl_files:
|
||||
res = f"ERROR: 缓存文件夹{current_folder}中没有找到任何PL文件"
|
||||
return res
|
||||
|
||||
# 找到最新修改的文件
|
||||
latest_file = pl_files[-1]
|
||||
res = f"找到最新的PL数据文件: {latest_file}"
|
||||
# print(res)
|
||||
return res
|
||||
|
||||
pl_latest = get_pl_latest_file()
|
||||
uv_latest = get_uv_latest_file()
|
||||
|
||||
return pl_latest + "\n" + uv_latest
|
||||
|
||||
def default_func():
|
||||
return "Approved. Proceed as planned!"
|
||||
return "Approved. Proceed as planned!"
|
||||
|
||||
Reference in New Issue
Block a user