加酸仪(java版本)
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.

67 lines
3.3 KiB

1 week ago
  1. package com.iflytop.handacid.app.controller;
  2. import cn.hutool.json.JSONUtil;
  3. import com.iflytop.handacid.app.common.constant.CommandStatus;
  4. import com.iflytop.handacid.app.core.command.CommandDebugHandlerRegistry;
  5. import com.iflytop.handacid.app.core.command.CommandHandler;
  6. import com.iflytop.handacid.app.model.dto.CommandDTO;
  7. import com.iflytop.handacid.app.websocket.server.DebugGenerator;
  8. import com.iflytop.handacid.app.websocket.server.WebSocketSender;
  9. import com.iflytop.handacid.common.result.Result;
  10. import io.swagger.v3.oas.annotations.Operation;
  11. import io.swagger.v3.oas.annotations.tags.Tag;
  12. import jakarta.validation.Valid;
  13. import lombok.RequiredArgsConstructor;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import org.springframework.web.bind.annotation.RequestBody;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import java.util.concurrent.CompletableFuture;
  20. @Tag(name = "前端调试指令")
  21. @RestController
  22. @RequestMapping("/api/debug/cmd")
  23. @RequiredArgsConstructor
  24. @Slf4j
  25. public class CmdDebugController {
  26. private final CommandDebugHandlerRegistry registry;
  27. private final WebSocketSender webSocketService;
  28. @Operation(summary = "前端调试指令")
  29. @PostMapping
  30. public Result<?> controlMethod(@Valid @RequestBody CommandDTO commandDTO) {
  31. String commandId = commandDTO.getCommandId();
  32. String command = commandDTO.getCommand();
  33. try {
  34. webSocketService.pushDebug(DebugGenerator.generateJson(commandId, command, CommandStatus.RECEIVE, "已收到调试指令请求,开始处理"));
  35. CommandHandler commandHandler = registry.getCommandHandler(command);
  36. if (commandHandler == null) {
  37. webSocketService.pushDebug(DebugGenerator.generateJson(commandId, command, CommandStatus.DEVICE_ERROR, "未找到对应的调试指令"));
  38. log.error("未找到对应的调试指令");
  39. return Result.failed();
  40. }
  41. webSocketService.pushCMDResponse(DebugGenerator.generateJson(commandId, command, CommandStatus.START, "调试指令开始执行"));
  42. log.info("调试指令开始执行");
  43. CompletableFuture<Void> future = commandHandler.handle(commandDTO);
  44. future.whenComplete((v, ex) -> {
  45. if (ex != null) {
  46. webSocketService.pushCMDResponse(DebugGenerator.generateJson(commandId, command, CommandStatus.FAIL, "执行调试指令发生异常", ex.getMessage()));
  47. log.error("执行调试指令发生异常: {}", JSONUtil.toJsonStr(commandDTO), ex);
  48. } else {
  49. webSocketService.pushCMDResponse(DebugGenerator.generateJson(commandId, command, CommandStatus.SUCCESS, "调试指令执行成功"));
  50. log.info("调试指令执行成功");
  51. }
  52. webSocketService.pushCMDResponse(DebugGenerator.generateJson(commandId, command, CommandStatus.FINISH, "调试指令执行结束"));
  53. log.info("调试指令执行结束");
  54. });
  55. } catch (Exception e) {
  56. log.error("执行调试指令发生异常: {}", JSONUtil.toJsonStr(commandDTO), e);
  57. return Result.failed(e.getMessage());
  58. }
  59. return Result.success();
  60. }
  61. }