From 08e66d2ead630fde175a261c78f94aae1a222182 Mon Sep 17 00:00:00 2001 From: "aj.mdc.ng" Date: Thu, 2 Mar 2023 11:23:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0MDC=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E7=AB=AF=E7=9B=B8=E5=85=B3=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/command.py b/command.py index 2489d2e..64d167e 100644 --- a/command.py +++ b/command.py @@ -20,21 +20,45 @@ bin_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "mdc_ng") bin_path_new = bin_path + ".new" +backend_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "backend") +backend_path_new = backend_path + ".new" + +mdc_server = None + @plugin.command( name="update", - title="更新MDC lib", - desc="拉取最新MDC核心库", + title="更新MDC", + desc="拉取最新MDC核心库和后端服务", icon="AlarmOn", run_in_background=True, ) def update(ctx: PluginCommandContext): update_bin() + update_server() _LOGGER.info("MDC更新成功") return PluginCommandResponse(True, "更新成功") +@plugin.command( + name="start_mdc_server", + title="启动/停止MDC服务端", + desc="启动/停止MDC服务端", + icon="AlarmOn", + run_in_background=True, +) +def start_mdc_server(ctx: PluginCommandContext): + global mdc_server + + if not mdc_server: + start_server() + else: + stop_server() + + return PluginCommandResponse(True, "启动成功,请通过9208端口访问Web UI") + + @plugin.command( name="mdc_manual", title="MDC手动执行", @@ -80,6 +104,35 @@ def update_bin(): ) +def update_server(): + download_file( + "https://github.com/mdc-ng/mdc-ng/releases/download/latest/backend", + backend_path_new, + ) + try: + os.remove(backend_path) + except: + pass + os.rename(backend_path_new, backend_path) + run_command( + [ + "chmod", + "+x", + "backend", + ], + False, + cwd=pathlib.Path(__file__).parent.absolute(), + ) + run_command( + [ + "./backend", + "-v", + ], + True, + cwd=pathlib.Path(__file__).parent.absolute(), + ) + + def mdc_main(path: str, config_ini: str = config.config_path): run_command( [ @@ -163,3 +216,23 @@ def run_command(command, capture, **kwargs): break if capture: _LOGGER.info(line.decode().strip()) + + +def start_server(): + global mdc_server + if not mdc_server: + mdc_server = subprocess.Popen( + [ + "./backend", + "-p", + "9208", + ], + cwd=pathlib.Path(__file__).parent.absolute(), + ) + + +def stop_server(): + global mdc_server + if mdc_server: + mdc_server.terminate() + mdc_server = None