diff --git a/arknights_mower/utils/config/__init__.py b/arknights_mower/utils/config/__init__.py index 1a119fb9..d3bb7566 100644 --- a/arknights_mower/utils/config/__init__.py +++ b/arknights_mower/utils/config/__init__.py @@ -54,17 +54,20 @@ def load_conf(): load_conf() -def load_plan() -> PlanModel: +def load_plan(): + global plan with open(conf.planFile, "r", encoding="utf-8") as f: plan = PlanModel(**json.load(f)) - return plan -def save_plan(plan: PlanModel): +def save_plan(): with open(conf.planFile, "w", encoding="utf-8") as f: json.dump(plan.model_dump(exclude_none=True), f, ensure_ascii=False, indent=2) +plan: PlanModel +load_plan() + stop_mower = Event() # 日志 diff --git a/server.py b/server.py index 39de010d..7406688f 100755 --- a/server.py +++ b/server.py @@ -6,7 +6,6 @@ import pathlib import sys import time -from copy import deepcopy from functools import wraps from threading import Thread @@ -30,8 +29,6 @@ sock = Sock(app) CORS(app) -plan = {} - mower_thread = None log_lines = [] ws_connections = [] @@ -93,19 +90,17 @@ def load_config(): @app.route("/plan", methods=["GET", "POST"]) @require_token def load_plan_from_json(): - global plan - if request.method == "GET": try: - plan = config.load_plan() + config.load_plan() except Exception as e: - logger.exception(f"plan.json路径错误{e},重置为plan.json") + logger.exception(f"排班表文件路径错误{e},重置为plan.json") config.conf.planFile = "./plan.json" - plan = config.load_plan() - return plan.model_dump(exclude_none=True) + config.load_plan() + return config.plan.model_dump(exclude_none=True) else: - plan = config.PlanModel(**request.json) - config.save_plan(plan) + config.plan = config.PlanModel(**request.json) + config.save_plan() return f"New plan saved at {config.conf.planFile}" @@ -149,7 +144,6 @@ def start(): tmp_dir.mkdir(exist_ok=True) config.stop_mower.clear() - config.plan = deepcopy(plan) config.operators = {} from arknights_mower.__main__ import main @@ -234,11 +228,10 @@ def import_from_image(): from arknights_mower.utils import qrcode img = Image.open(img_path) - global plan - plan = qrcode.decode(img) - if plan: - plan = config.PlanModel(**plan) - config.save_plan(plan) + data = qrcode.decode(img) + if data: + config.plan = config.PlanModel(**data) + config.save_plan() return "排班已加载" return "排班表导入失败!" @@ -256,8 +249,9 @@ def save_file_dialog(): upper = Image.open(img) - global plan - img = qrcode.export(plan, upper, config.conf.theme) + img = qrcode.export( + config.plan.model_dump(exclude_none=True), upper, config.conf.theme + ) img_path = conn_send("save") if img_path == "":