From 6bebd1a535ef7567502928cc64873511cce3801a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Thu, 20 Mar 2025 13:33:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=B0=83=E8=AF=95=E7=94=A8=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/controller/FrontCmdDebugController.java | 72 ++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/main/java/com/qyft/ms/system/controller/FrontCmdDebugController.java diff --git a/src/main/java/com/qyft/ms/system/controller/FrontCmdDebugController.java b/src/main/java/com/qyft/ms/system/controller/FrontCmdDebugController.java new file mode 100644 index 0000000..629bb10 --- /dev/null +++ b/src/main/java/com/qyft/ms/system/controller/FrontCmdDebugController.java @@ -0,0 +1,72 @@ +package com.qyft.ms.system.controller; + +import cn.hutool.json.JSONUtil; +import com.qyft.ms.system.common.constant.CommandStatus; +import com.qyft.ms.system.common.device.command.FrontCommand; +import com.qyft.ms.system.core.registry.CommandHandlerRegistry; +import com.qyft.ms.system.functions.CommandHandler; +import com.qyft.ms.system.model.form.FrontCmdControlForm; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +import java.util.concurrent.CompletableFuture; + +@Tag(name = "调试用前端调用指令") +@RestController +@RequestMapping("/api/function/debug") +@RequiredArgsConstructor +@Slf4j +public class FrontCmdDebugController { + private final CommandHandlerRegistry registry; + + @Operation(summary = "调试用前端统一调用一个接口") + @PostMapping + public ResponseBodyEmitter controlMethod(@RequestBody FrontCmdControlForm form) { + ResponseBodyEmitter emitter = new ResponseBodyEmitter(3600000L); + String id = form.getCmdId(); + String code = form.getCmdCode(); + try { + emitter.send(FrontCommand.backstage(id, code, CommandStatus.RECEIVE, "已收到业务指令请求,开始处理"), MediaType.APPLICATION_JSON); + CommandHandler commandHandler = registry.getHandler(code); + if (commandHandler == null) { + emitter.send(FrontCommand.backstage(id, code, CommandStatus.ERROR, "未找到对应的业务指令"), MediaType.APPLICATION_JSON); + log.error("未找到对应的业务指令"); + emitter.complete(); + return emitter; + } + emitter.send(FrontCommand.backstage(id, code, CommandStatus.START, "业务指令开始执行"), MediaType.APPLICATION_JSON); + log.info("业务指令开始执行"); + CompletableFuture future = commandHandler.handle(form, emitter); + future.whenComplete((v, ex) -> { + try { + if (ex != null) { + emitter.send(FrontCommand.backstage(id, code, CommandStatus.ERROR, "业务指令执行异常", ex.getMessage()), MediaType.APPLICATION_JSON); + log.error("执行业务指令发生异常: {}", JSONUtil.toJsonStr(form), ex); + } + Thread.sleep(50); + emitter.send(FrontCommand.backstage(id, code, CommandStatus.FINISH, "业务指令执行结束"), MediaType.APPLICATION_JSON); + log.info("业务指令执行结束"); + } catch (Exception e) { + log.error("执行业务指令发生异常: {}", JSONUtil.toJsonStr(form), e); + } finally { + log.info("业务指令 emitter complete"); + emitter.complete(); + } + }); + } catch (Exception e) { + log.error("执行业务指令发生异常: {}", JSONUtil.toJsonStr(form), e); + emitter.complete(); + } + return emitter; + } + + +}