package com.qyft.ms.app.controller; import cn.hutool.json.JSONUtil; import com.qyft.ms.app.common.command.CurrentSendCmdMapInstance; import com.qyft.ms.app.core.registry.CommandHandlerRegistry; import com.qyft.ms.app.handler.CommandHandler; import com.qyft.ms.app.model.form.CMDFormV2; import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; 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.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; @Tag(name = "前端调用指令") @RestController @RequestMapping("/api/device/front") @RequiredArgsConstructor @Slf4j public class FrontCmdController { private final DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2; private final CommandHandlerRegistry registry; @Operation(summary = "前端统一调用一个接口") @PostMapping("/control") public ResponseBodyEmitter controlMethod(@RequestBody CMDFormV2 cmdForm) { ResponseBodyEmitter emitter = new ResponseBodyEmitter(); String frontCmdName = cmdForm.getCmdName(); // 获取前端传入的命令字符串 CommandHandler handler = registry.getHandler(frontCmdName); if (handler != null) { try { handler.handle(cmdForm, emitter); } catch (Exception e) { log.error("指令执行失败:{}", JSONUtil.toJsonStr(cmdForm), e); emitter.completeWithError(e); } } else { try { emitter.send("未找到对应的业务指令: " + frontCmdName); emitter.complete(); } catch (Exception e) { emitter.completeWithError(e); } } return emitter; } @RequestMapping("/controlTest") public void controlMethodTest() { CurrentSendCmdMapInstance.getInstance().getCommand(1).commandContinue(); } }