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.

58 lines
2.2 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  1. package com.qyft.ms.app.controller;
  2. import cn.hutool.json.JSONUtil;
  3. import com.qyft.ms.app.common.command.CurrentSendCmdMapInstance;
  4. import com.qyft.ms.app.core.registry.CommandHandlerRegistry;
  5. import com.qyft.ms.app.handler.CommandHandler;
  6. import com.qyft.ms.app.model.form.CMDFormV2;
  7. import com.qyft.ms.device.service.DeviceTcpCMDServiceV2;
  8. import io.swagger.v3.oas.annotations.Operation;
  9. import io.swagger.v3.oas.annotations.tags.Tag;
  10. import lombok.RequiredArgsConstructor;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.springframework.web.bind.annotation.PostMapping;
  13. import org.springframework.web.bind.annotation.RequestBody;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;
  17. @Tag(name = "前端调用指令")
  18. @RestController
  19. @RequestMapping("/api/device/front")
  20. @RequiredArgsConstructor
  21. @Slf4j
  22. public class FrontCmdController {
  23. private final DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2;
  24. private final CommandHandlerRegistry registry;
  25. @Operation(summary = "前端统一调用一个接口")
  26. @PostMapping("/control")
  27. public ResponseBodyEmitter controlMethod(@RequestBody CMDFormV2 cmdForm) {
  28. ResponseBodyEmitter emitter = new ResponseBodyEmitter();
  29. String frontCmdName = cmdForm.getCmdName(); // 获取前端传入的命令字符串
  30. CommandHandler handler = registry.getHandler(frontCmdName);
  31. if (handler != null) {
  32. try {
  33. handler.handle(cmdForm, emitter);
  34. } catch (Exception e) {
  35. log.error("指令执行失败:{}", JSONUtil.toJsonStr(cmdForm), e);
  36. emitter.completeWithError(e);
  37. }
  38. } else {
  39. try {
  40. emitter.send("未找到对应的业务指令: " + frontCmdName);
  41. emitter.complete();
  42. } catch (Exception e) {
  43. emitter.completeWithError(e);
  44. }
  45. }
  46. return emitter;
  47. }
  48. @RequestMapping("/controlTest")
  49. public void controlMethodTest() {
  50. CurrentSendCmdMapInstance.getInstance().getCommand(1).commandContinue();
  51. }
  52. }