加酸仪(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

package com.iflytop.handacid.app.controller;
import cn.hutool.json.JSONUtil;
import com.iflytop.handacid.app.common.constant.CommandStatus;
import com.iflytop.handacid.app.core.command.CommandDebugHandlerRegistry;
import com.iflytop.handacid.app.core.command.CommandHandler;
import com.iflytop.handacid.app.model.dto.CommandDTO;
import com.iflytop.handacid.app.websocket.server.DebugGenerator;
import com.iflytop.handacid.app.websocket.server.WebSocketSender;
import com.iflytop.handacid.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
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 java.util.concurrent.CompletableFuture;
@Tag(name = "前端调试指令")
@RestController
@RequestMapping("/api/debug/cmd")
@RequiredArgsConstructor
@Slf4j
public class CmdDebugController {
private final CommandDebugHandlerRegistry registry;
private final WebSocketSender webSocketService;
@Operation(summary = "前端调试指令")
@PostMapping
public Result<?> controlMethod(@Valid @RequestBody CommandDTO commandDTO) {
String commandId = commandDTO.getCommandId();
String command = commandDTO.getCommand();
try {
webSocketService.pushDebug(DebugGenerator.generateJson(commandId, command, CommandStatus.RECEIVE, "已收到调试指令请求,开始处理"));
CommandHandler commandHandler = registry.getCommandHandler(command);
if (commandHandler == null) {
webSocketService.pushDebug(DebugGenerator.generateJson(commandId, command, CommandStatus.DEVICE_ERROR, "未找到对应的调试指令"));
log.error("未找到对应的调试指令");
return Result.failed();
}
webSocketService.pushCMDResponse(DebugGenerator.generateJson(commandId, command, CommandStatus.START, "调试指令开始执行"));
log.info("调试指令开始执行");
CompletableFuture<Void> future = commandHandler.handle(commandDTO);
future.whenComplete((v, ex) -> {
if (ex != null) {
webSocketService.pushCMDResponse(DebugGenerator.generateJson(commandId, command, CommandStatus.FAIL, "执行调试指令发生异常", ex.getMessage()));
log.error("执行调试指令发生异常: {}", JSONUtil.toJsonStr(commandDTO), ex);
} else {
webSocketService.pushCMDResponse(DebugGenerator.generateJson(commandId, command, CommandStatus.SUCCESS, "调试指令执行成功"));
log.info("调试指令执行成功");
}
webSocketService.pushCMDResponse(DebugGenerator.generateJson(commandId, command, CommandStatus.FINISH, "调试指令执行结束"));
log.info("调试指令执行结束");
});
} catch (Exception e) {
log.error("执行调试指令发生异常: {}", JSONUtil.toJsonStr(commandDTO), e);
return Result.failed(e.getMessage());
}
return Result.success();
}
}