feat: 后端添加严格的下拉表数据校验逻辑,添加新ws消息类型task_dict
This commit is contained in:
@@ -48,7 +48,7 @@ async def connect_websocket(websocket: WebSocket):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
data = await asyncio.wait_for(websocket.receive_json(), timeout=15.0)
|
data = await asyncio.wait_for(websocket.receive_json(), timeout=1000005.0)
|
||||||
if data.get("type") == "Signal" and "Pong" in data.get("data", {}):
|
if data.get("type") == "Signal" and "Pong" in data.get("data", {}):
|
||||||
last_pong = time.monotonic()
|
last_pong = time.monotonic()
|
||||||
elif data.get("type") == "Signal" and "Ping" in data.get("data", {}):
|
elif data.get("type") == "Signal" and "Ping" in data.get("data", {}):
|
||||||
|
|||||||
@@ -162,10 +162,17 @@ class GlobalConfig(ConfigBase):
|
|||||||
class QueueItem(ConfigBase):
|
class QueueItem(ConfigBase):
|
||||||
"""队列项配置"""
|
"""队列项配置"""
|
||||||
|
|
||||||
|
related_config: dict[str, MultipleConfig] = {}
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
self.Info_ScriptId = ConfigItem("Info", "ScriptId", None, UidValidator())
|
self.Info_ScriptId = ConfigItem(
|
||||||
|
"Info",
|
||||||
|
"ScriptId",
|
||||||
|
None,
|
||||||
|
MultipleUIDValidator(None, self.related_config, "ScriptConfig"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TimeSet(ConfigBase):
|
class TimeSet(ConfigBase):
|
||||||
@@ -175,7 +182,7 @@ class TimeSet(ConfigBase):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
self.Info_Enabled = ConfigItem("Info", "Enabled", False, BoolValidator())
|
self.Info_Enabled = ConfigItem("Info", "Enabled", False, BoolValidator())
|
||||||
self.Info_Time = ConfigItem("Info", "Time", "00:00")
|
self.Info_Time = ConfigItem("Info", "Time", "00:00", DateTimeValidator("%H:%M"))
|
||||||
|
|
||||||
|
|
||||||
class QueueConfig(ConfigBase):
|
class QueueConfig(ConfigBase):
|
||||||
@@ -214,6 +221,8 @@ class QueueConfig(ConfigBase):
|
|||||||
class MaaUserConfig(ConfigBase):
|
class MaaUserConfig(ConfigBase):
|
||||||
"""MAA用户配置"""
|
"""MAA用户配置"""
|
||||||
|
|
||||||
|
related_config: dict[str, MultipleConfig] = {}
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
@@ -222,7 +231,12 @@ class MaaUserConfig(ConfigBase):
|
|||||||
self.Info_Mode = ConfigItem(
|
self.Info_Mode = ConfigItem(
|
||||||
"Info", "Mode", "简洁", OptionsValidator(["简洁", "详细"])
|
"Info", "Mode", "简洁", OptionsValidator(["简洁", "详细"])
|
||||||
)
|
)
|
||||||
self.Info_StageMode = ConfigItem("Info", "StageMode", "Fixed")
|
self.Info_StageMode = ConfigItem(
|
||||||
|
"Info",
|
||||||
|
"StageMode",
|
||||||
|
"Fixed",
|
||||||
|
MultipleUIDValidator("Fixed", self.related_config, "PlanConfig"),
|
||||||
|
)
|
||||||
self.Info_Server = ConfigItem(
|
self.Info_Server = ConfigItem(
|
||||||
"Info",
|
"Info",
|
||||||
"Server",
|
"Server",
|
||||||
@@ -501,7 +515,9 @@ class GeneralUserConfig(ConfigBase):
|
|||||||
)
|
)
|
||||||
self.Info_Notes = ConfigItem("Info", "Notes", "无")
|
self.Info_Notes = ConfigItem("Info", "Notes", "无")
|
||||||
|
|
||||||
self.Data_LastProxyDate = ConfigItem("Data", "LastProxyDate", "2000-01-01")
|
self.Data_LastProxyDate = ConfigItem(
|
||||||
|
"Data", "LastProxyDate", "2000-01-01", DateTimeValidator("%Y-%m-%d")
|
||||||
|
)
|
||||||
self.Data_ProxyTimes = ConfigItem(
|
self.Data_ProxyTimes = ConfigItem(
|
||||||
"Data", "ProxyTimes", 0, RangeValidator(0, 9999)
|
"Data", "ProxyTimes", 0, RangeValidator(0, 9999)
|
||||||
)
|
)
|
||||||
@@ -643,6 +659,8 @@ class AppConfig(GlobalConfig):
|
|||||||
self.ScriptConfig = MultipleConfig([MaaConfig, GeneralConfig])
|
self.ScriptConfig = MultipleConfig([MaaConfig, GeneralConfig])
|
||||||
self.PlanConfig = MultipleConfig([MaaPlanConfig])
|
self.PlanConfig = MultipleConfig([MaaPlanConfig])
|
||||||
self.QueueConfig = MultipleConfig([QueueConfig])
|
self.QueueConfig = MultipleConfig([QueueConfig])
|
||||||
|
QueueItem.related_config["ScriptConfig"] = self.ScriptConfig
|
||||||
|
MaaUserConfig.related_config["PlanConfig"] = self.PlanConfig
|
||||||
|
|
||||||
truststore.inject_into_ssl()
|
truststore.inject_into_ssl()
|
||||||
|
|
||||||
|
|||||||
@@ -125,38 +125,74 @@ class _TaskManager:
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
|
# 初始化任务列表
|
||||||
if task_id in Config.QueueConfig:
|
if task_id in Config.QueueConfig:
|
||||||
|
|
||||||
queue = Config.QueueConfig[task_id]
|
queue = Config.QueueConfig[task_id]
|
||||||
if not isinstance(queue, QueueConfig):
|
if not isinstance(queue, QueueConfig):
|
||||||
logger.error(
|
|
||||||
f"不支持的队列类型: {type(Config.QueueConfig[task_id]).__name__}"
|
|
||||||
)
|
|
||||||
await Config.send_json(
|
|
||||||
WebSocketMessage(
|
|
||||||
id=str(task_id),
|
|
||||||
type="Info",
|
|
||||||
data={"Error": "队列类型不支持"},
|
|
||||||
).model_dump()
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
task_list = []
|
task_list = []
|
||||||
for queue_item in queue.QueueItem.values():
|
for queue_item in queue.QueueItem.values():
|
||||||
if queue_item.get("Info", "ScriptId") is None:
|
if queue_item.get("Info", "ScriptId") is None:
|
||||||
continue
|
continue
|
||||||
uid = uuid.UUID(queue_item.get("Info", "ScriptId"))
|
script_id = uuid.UUID(queue_item.get("Info", "ScriptId"))
|
||||||
|
script = Config.ScriptConfig[script_id]
|
||||||
|
if not isinstance(script, (MaaConfig | GeneralConfig)):
|
||||||
|
logger.error(f"不支持的脚本类型: {type(script).__name__}")
|
||||||
|
continue
|
||||||
task_list.append(
|
task_list.append(
|
||||||
{
|
{
|
||||||
"script_id": str(uid),
|
"script_id": str(script_id),
|
||||||
"status": "等待",
|
"status": "等待",
|
||||||
"name": Config.ScriptConfig[uid].get("Info", "Name"),
|
"name": script.get("Info", "Name"),
|
||||||
|
"user_list": [
|
||||||
|
{
|
||||||
|
"user_id": str(user_id),
|
||||||
|
"status": "等待",
|
||||||
|
"name": config.get("Info", "Name"),
|
||||||
|
}
|
||||||
|
for user_id, config in script.UserData.items()
|
||||||
|
if config.get("Info", "Status")
|
||||||
|
and config.get("Info", "RemainedDay") != 0
|
||||||
|
],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
elif actual_id is not None and actual_id in Config.ScriptConfig:
|
elif actual_id is not None and actual_id in Config.ScriptConfig:
|
||||||
|
|
||||||
task_list = [{"script_id": str(actual_id), "status": "等待"}]
|
script = Config.ScriptConfig[actual_id]
|
||||||
|
if not isinstance(script, (MaaConfig | GeneralConfig)):
|
||||||
|
logger.error(f"不支持的脚本类型: {type(script).__name__}")
|
||||||
|
return
|
||||||
|
|
||||||
|
task_list = [
|
||||||
|
{
|
||||||
|
"script_id": str(actual_id),
|
||||||
|
"status": "等待",
|
||||||
|
"name": script.get("Info", "Name"),
|
||||||
|
"user_list": [
|
||||||
|
{
|
||||||
|
"user_id": str(user_id),
|
||||||
|
"status": "等待",
|
||||||
|
"name": config.get("Info", "Name"),
|
||||||
|
}
|
||||||
|
for user_id, config in script.UserData.items()
|
||||||
|
if config.get("Info", "Status")
|
||||||
|
and config.get("Info", "RemainedDay") != 0
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
await Config.send_json(
|
||||||
|
WebSocketMessage(
|
||||||
|
id=str(task_id), type="Update", data={"task_dict": task_list}
|
||||||
|
).model_dump()
|
||||||
|
)
|
||||||
|
|
||||||
|
# 清理用户列表初值
|
||||||
|
for task in task_list:
|
||||||
|
task.pop("user_list", None)
|
||||||
|
|
||||||
for task in task_list:
|
for task in task_list:
|
||||||
|
|
||||||
@@ -176,6 +212,20 @@ class _TaskManager:
|
|||||||
logger.info(f"跳过任务: {script_id}, 该任务已在运行列表中")
|
logger.info(f"跳过任务: {script_id}, 该任务已在运行列表中")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# 检查任务对应脚本是否仍存在
|
||||||
|
if script_id in self.task_dict:
|
||||||
|
|
||||||
|
task["status"] = "异常"
|
||||||
|
await Config.send_json(
|
||||||
|
WebSocketMessage(
|
||||||
|
id=str(task_id),
|
||||||
|
type="Update",
|
||||||
|
data={"task_list": task_list},
|
||||||
|
).model_dump()
|
||||||
|
)
|
||||||
|
logger.info(f"跳过任务: {script_id}, 该任务对应脚本已被删除")
|
||||||
|
continue
|
||||||
|
|
||||||
# 标记为运行中
|
# 标记为运行中
|
||||||
task["status"] = "运行"
|
task["status"] = "运行"
|
||||||
await Config.send_json(
|
await Config.send_json(
|
||||||
|
|||||||
@@ -83,22 +83,6 @@ class OptionsValidator(ConfigValidator):
|
|||||||
return value if self.validate(value) else self.options[0]
|
return value if self.validate(value) else self.options[0]
|
||||||
|
|
||||||
|
|
||||||
class UidValidator(ConfigValidator):
|
|
||||||
"""UID验证器"""
|
|
||||||
|
|
||||||
def validate(self, value: Any) -> bool:
|
|
||||||
if value is None:
|
|
||||||
return True
|
|
||||||
try:
|
|
||||||
uuid.UUID(value)
|
|
||||||
return True
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
return False
|
|
||||||
|
|
||||||
def correct(self, value: Any) -> Any:
|
|
||||||
return value if self.validate(value) else None
|
|
||||||
|
|
||||||
|
|
||||||
class UUIDValidator(ConfigValidator):
|
class UUIDValidator(ConfigValidator):
|
||||||
"""UUID验证器"""
|
"""UUID验证器"""
|
||||||
|
|
||||||
@@ -333,14 +317,20 @@ class ConfigItem:
|
|||||||
if not self.validator.validate(self.value):
|
if not self.validator.validate(self.value):
|
||||||
self.value = self.validator.correct(self.value)
|
self.value = self.validator.correct(self.value)
|
||||||
|
|
||||||
def getValue(self) -> Any:
|
def getValue(self, if_decrypt: bool = True) -> Any:
|
||||||
"""
|
"""
|
||||||
获取配置项值
|
获取配置项值
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if isinstance(self.validator, EncryptValidator):
|
v = (
|
||||||
return dpapi_decrypt(self.value)
|
self.value
|
||||||
return self.value
|
if self.validator.validate(self.value)
|
||||||
|
else self.validator.correct(self.value)
|
||||||
|
)
|
||||||
|
|
||||||
|
if isinstance(self.validator, EncryptValidator) and if_decrypt:
|
||||||
|
return dpapi_decrypt(v)
|
||||||
|
return v
|
||||||
|
|
||||||
def lock(self):
|
def lock(self):
|
||||||
"""
|
"""
|
||||||
@@ -452,9 +442,7 @@ class ConfigBase:
|
|||||||
if not data.get(item.group):
|
if not data.get(item.group):
|
||||||
data[item.group] = {}
|
data[item.group] = {}
|
||||||
if item.name:
|
if item.name:
|
||||||
data[item.group][item.name] = (
|
data[item.group][item.name] = item.getValue(if_decrypt)
|
||||||
item.getValue() if if_decrypt else item.value
|
|
||||||
)
|
|
||||||
|
|
||||||
elif not ignore_multi_config and isinstance(item, MultipleConfig):
|
elif not ignore_multi_config and isinstance(item, MultipleConfig):
|
||||||
|
|
||||||
@@ -838,3 +826,32 @@ class MultipleConfig:
|
|||||||
"""返回配置项的所有唯一标识符和实例的元组"""
|
"""返回配置项的所有唯一标识符和实例的元组"""
|
||||||
|
|
||||||
return zip(self.keys(), self.values())
|
return zip(self.keys(), self.values())
|
||||||
|
|
||||||
|
|
||||||
|
class MultipleUIDValidator(ConfigValidator):
|
||||||
|
"""多配置管理类UID验证器"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, default: Any, related_config: Dict[str, MultipleConfig], config_name: str
|
||||||
|
):
|
||||||
|
self.default = default
|
||||||
|
self.related_config = related_config
|
||||||
|
self.config_name = config_name
|
||||||
|
|
||||||
|
def validate(self, value: Any) -> bool:
|
||||||
|
if value == self.default:
|
||||||
|
return True
|
||||||
|
if not isinstance(value, str):
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
uid = uuid.UUID(value)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return False
|
||||||
|
if uid in self.related_config.get(self.config_name, {}):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def correct(self, value: Any) -> Any:
|
||||||
|
if self.validate(value):
|
||||||
|
return value
|
||||||
|
return self.default
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ AUTO-MAS 后端采用基于 AsyncIO 的异步任务调度系统,主要由以
|
|||||||
|
|
||||||
#### 3.2.1 Update 类型 - 数据更新
|
#### 3.2.1 Update 类型 - 数据更新
|
||||||
|
|
||||||
**用途**: 通知前端更新界面数据
|
**用途**: 通知前端更新界面数据,"user_list"仅给出当前处于`运行`状态的脚本的用户列表值
|
||||||
|
|
||||||
**常见数据格式:**
|
**常见数据格式:**
|
||||||
|
|
||||||
@@ -93,6 +93,29 @@ AUTO-MAS 后端采用基于 AsyncIO 的异步任务调度系统,主要由以
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "task-uuid",
|
||||||
|
"type": "Update",
|
||||||
|
"data": {
|
||||||
|
"task_dict": [
|
||||||
|
{
|
||||||
|
"script_id": "脚本ID",
|
||||||
|
"status": "等待/运行/完成/跳过",
|
||||||
|
"name": "脚本名称",
|
||||||
|
"user_list": [
|
||||||
|
{
|
||||||
|
"name": "用户名",
|
||||||
|
"status": "运行状态",
|
||||||
|
"config": "配置信息"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"id": "task-uuid",
|
"id": "task-uuid",
|
||||||
|
|||||||
Reference in New Issue
Block a user