20250904-初步功能已完成
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
import json
|
||||
import os
|
||||
import shlex
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
import unicodedata
|
||||
import wda
|
||||
|
||||
from Entity.Variables import WdaAppBundleId
|
||||
from Utils.LogManager import LogManager
|
||||
import xml.etree.ElementTree as ET
|
||||
import re, html
|
||||
@@ -709,4 +714,72 @@ class AiUtils(object):
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
except Exception as e:
|
||||
LogManager.error(f"[delete_anchors_by_ids] 写入失败: {e}")
|
||||
return deleted
|
||||
return deleted
|
||||
|
||||
@staticmethod
|
||||
def run_tidevice_command(udid, action, bundle_id, timeout=30):
|
||||
"""
|
||||
执行tidevice命令的辅助函数
|
||||
|
||||
:param udid: 设备UDID
|
||||
:param action: 动作类型 ('kill' 或 'launch')
|
||||
:param bundle_id: 应用的Bundle ID
|
||||
:param timeout: 命令执行超时时间(秒)
|
||||
:return: (bool) 成功返回True,失败返回False
|
||||
"""
|
||||
# 构建命令列表
|
||||
cmd = ["tidevice", "--udid", udid, action, bundle_id]
|
||||
try:
|
||||
# 执行命令并捕获输出
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=timeout
|
||||
)
|
||||
# 检查命令是否成功执行(返回码为0通常表示成功)
|
||||
if result.returncode == 0:
|
||||
LogManager.info(f"Successfully {action}ed {bundle_id} on device {udid}.")
|
||||
return True
|
||||
else:
|
||||
# 记录错误信息
|
||||
LogManager.error(f"Failed to {action} {bundle_id} on device {udid}. Error: {result.stderr}")
|
||||
return False
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
# 处理命令执行超时
|
||||
LogManager.error(f"Command 'tidevice {action}' timed out after {timeout} seconds for device {udid}.")
|
||||
return False
|
||||
except FileNotFoundError:
|
||||
# 处理tidevice命令未找到的情况(通常意味着tidevice未安装或不在PATH中)
|
||||
LogManager.error("The 'tidevice' command was not found. Please ensure it is installed and in your system PATH.")
|
||||
return False
|
||||
except Exception as e:
|
||||
# 捕获其他可能异常
|
||||
LogManager.error(f"An unexpected error occurred while trying to {action} the app: {e}")
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def kill_wda(cls, udid, bundle_id="com.yolozsAgent.wda.xctrunner"):
|
||||
"""
|
||||
杀死指定设备上的WDA应用
|
||||
|
||||
:param udid: 设备UDID
|
||||
:param bundle_id: WDA的Bundle ID,默认为 com.yolozsAgent.wda.xctrunner
|
||||
:return: (bool) 成功返回True,失败返回False
|
||||
"""
|
||||
return cls.run_tidevice_command(udid, "kill", bundle_id)
|
||||
|
||||
@classmethod
|
||||
def launch_wda(cls, udid, bundle_id="com.yolozsAgent.wda.xctrunner", timeout=60):
|
||||
"""
|
||||
启动指定设备上的WDA应用
|
||||
|
||||
:param udid: 设备UDID
|
||||
:param bundle_id: WDA的Bundle ID,默认为 com.yolozsAgent.wda.xctrunner
|
||||
:param timeout: 启动命令超时时间,默认为60秒(启动可能较慢)
|
||||
:return: (bool) 成功返回True,失败返回False
|
||||
"""
|
||||
return cls.run_tidevice_command(udid, "launch", bundle_id, timeout)
|
||||
|
||||
|
||||
|
||||
@@ -179,17 +179,31 @@ class JsonUtils:
|
||||
|
||||
return updated
|
||||
|
||||
# @classmethod
|
||||
# def query_all_json_items(cls, filename="log/last_message.json") -> list:
|
||||
# """
|
||||
# 查询 JSON 文件(数组)中的所有项
|
||||
# :param filename: JSON 文件路径
|
||||
# :return: list,可能为空
|
||||
# """
|
||||
# file_path = Path(filename)
|
||||
# print(file_path)
|
||||
# data = cls._read_json_list(file_path)
|
||||
# return data if isinstance(data, list) else []
|
||||
|
||||
@classmethod
|
||||
def query_all_json_items(cls, filename="log/last_message.json") -> list:
|
||||
"""
|
||||
查询 JSON 文件(数组)中的所有项
|
||||
查询 JSON 文件(数组)中的所有项,并剔除 sender 为空的记录
|
||||
:param filename: JSON 文件路径
|
||||
:return: list,可能为空
|
||||
"""
|
||||
file_path = Path(filename)
|
||||
print(file_path)
|
||||
data = cls._read_json_list(file_path)
|
||||
return data if isinstance(data, list) else []
|
||||
if not isinstance(data, list):
|
||||
return []
|
||||
# 过滤 sender 为空字符串的项
|
||||
return [item for item in data if isinstance(item, dict) and item.get("sender", "").strip()]
|
||||
|
||||
@classmethod
|
||||
def delete_json_items(cls,
|
||||
|
||||
@@ -240,7 +240,7 @@ def _force_utf8_everywhere():
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
_force_utf8_everywhere()
|
||||
# _force_utf8_everywhere()
|
||||
|
||||
|
||||
# ========= 全局:强制 UTF-8 + 关闭缓冲(运行期立刻生效) =========
|
||||
|
||||
@@ -29,15 +29,19 @@ class Requester():
|
||||
@classmethod
|
||||
def translation(cls, msg, country="英国"):
|
||||
try:
|
||||
parame = {
|
||||
param = {
|
||||
"msg": msg,
|
||||
"country": country,
|
||||
}
|
||||
url = "https://ai.yolozs.com/translation"
|
||||
result = requests.request(url=url, json=parame, method="POST")
|
||||
result = requests.post(url=url, json=param)
|
||||
|
||||
if result.status_code != 200:
|
||||
LogManager.error(f"翻译失败,状态码:{result.status_code},服务器返回的内容:{result.text}")
|
||||
return None
|
||||
|
||||
json = result.json()
|
||||
data = json.get("data")
|
||||
print(data)
|
||||
return data
|
||||
except Exception as e:
|
||||
LogManager.method_error(f"翻译失败,报错的原因:{e}", "翻译失败异常")
|
||||
@@ -52,19 +56,25 @@ class Requester():
|
||||
contact = aiConfig.get("contact", "")
|
||||
|
||||
inputs = {
|
||||
"agentName":agentName,
|
||||
"guildName":guildName,
|
||||
"contactTool":contactTool,
|
||||
"contact":contact
|
||||
"name":agentName,
|
||||
"Trade_union":guildName,
|
||||
"contcat_method":contactTool,
|
||||
"contcat_info":contact
|
||||
}
|
||||
|
||||
param["inputs"] = inputs
|
||||
|
||||
print(param)
|
||||
|
||||
try:
|
||||
url = "https://ai.yolozs.com/chat"
|
||||
result = requests.request(url=url, json=param, method="POST")
|
||||
result = requests.post(url=url, json=param)
|
||||
|
||||
|
||||
json = result.json()
|
||||
data = json.get("data", {})
|
||||
return data
|
||||
|
||||
data = json.get("answer", {})
|
||||
session_id = json.get("conversation_id", {})
|
||||
return data,session_id
|
||||
except Exception as e:
|
||||
LogManager.method_error(f"ai聊天失败,ai聊天出现异常,报错的原因:{e}", "ai聊天接口异常")
|
||||
|
||||
Reference in New Issue
Block a user