石墨消解仪后端服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.0 KiB

  1. package com.iflytop.gd.app.controller;
  2. import com.iflytop.gd.app.model.dto.CmdDTO;
  3. import com.iflytop.gd.common.result.Result;
  4. import com.iflytop.gd.app.service.cmd.CommandHandler;
  5. import com.iflytop.gd.app.service.cmd.CommandHandlerRegistry;
  6. import io.swagger.v3.oas.annotations.Operation;
  7. import io.swagger.v3.oas.annotations.tags.Tag;
  8. import lombok.RequiredArgsConstructor;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.bind.annotation.RequestBody;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import java.util.concurrent.CompletableFuture;
  15. @Tag(name = "前端调试指令")
  16. @RestController
  17. @RequestMapping("/api/debug/cmd")
  18. @RequiredArgsConstructor
  19. @Slf4j
  20. public class CmdDebugController {
  21. private final CommandHandlerRegistry registry;
  22. @Operation(summary = "前端调试指令")
  23. @PostMapping
  24. public Result<?> controlMethod(@RequestBody CmdDTO cmdDTO) {
  25. String cmdId = cmdDTO.getCmdId();
  26. String cmdCode = cmdDTO.getCmdCode();
  27. try {
  28. CommandHandler commandHandler = registry.getHandler(cmdCode);
  29. if (commandHandler == null) {
  30. log.error("未找到对应的业务指令");
  31. return Result.failed();
  32. }
  33. log.info("业务指令开始执行");
  34. CompletableFuture<Void> future = commandHandler.handle(cmdDTO);
  35. future.whenComplete((v, ex) -> {
  36. if (ex != null) {
  37. log.error("执行业务指令发生异常: {}", cmdDTO, ex);
  38. } else {
  39. log.info("业务指令执行成功");
  40. }
  41. log.info("业务指令执行结束");
  42. });
  43. } catch (Exception e) {
  44. log.error("执行业务指令发生异常: {}", cmdDTO, e);
  45. return Result.failed(e.getMessage());
  46. }
  47. return Result.success();
  48. }
  49. }