From 3a6bbf36772b002ea44fa89470b355445164635c Mon Sep 17 00:00:00 2001 From: Zhao Zuohong Date: Sat, 27 Jul 2024 17:56:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=AF=E5=8A=A8mower=E6=97=B6=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E5=8A=A0=E8=BD=BD=E6=8E=92=E7=8F=AD=E8=A1=A8=EF=BC=9B?= =?UTF-8?q?=E5=B0=86plan=E7=A7=BB=E8=87=B3config=E4=B8=AD=EF=BC=9B?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8E=92=E7=8F=AD=E8=A1=A8=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arknights_mower/utils/config/__init__.py | 9 ++++--- server.py | 32 ++++++++++-------------- 2 files changed, 19 insertions(+), 22 deletions(-) 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 == "":