1 changed files with 72 additions and 0 deletions
@ -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<Void> 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; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue