From c1d84aaf8111c86d55813a9530b4b95036581bc9 Mon Sep 17 00:00:00 2001 From: liuzhu Date: Thu, 12 Mar 2026 16:54:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../top/wms/admin/WmsAdminApplication.java | 2 + .../tcp/service/CommandService.java | 19 +++++++ .../controller/vm/VmCommandController.java | 53 +++++-------------- 3 files changed, 33 insertions(+), 41 deletions(-) diff --git a/wms-webapi/src/main/java/top/wms/admin/WmsAdminApplication.java b/wms-webapi/src/main/java/top/wms/admin/WmsAdminApplication.java index e8ec3ed..be304a8 100644 --- a/wms-webapi/src/main/java/top/wms/admin/WmsAdminApplication.java +++ b/wms-webapi/src/main/java/top/wms/admin/WmsAdminApplication.java @@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j; import org.dromara.x.file.storage.spring.EnableFileStorage; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import top.continew.starter.core.autoconfigure.project.ProjectProperties; @@ -29,6 +30,7 @@ import top.continew.starter.web.model.R; @RestController @SpringBootApplication @RequiredArgsConstructor +@EnableScheduling public class WmsAdminApplication { private final ProjectProperties projectProperties; diff --git a/wms-webapi/src/main/java/top/wms/admin/controller/tcp/service/CommandService.java b/wms-webapi/src/main/java/top/wms/admin/controller/tcp/service/CommandService.java index 14e644c..d431183 100644 --- a/wms-webapi/src/main/java/top/wms/admin/controller/tcp/service/CommandService.java +++ b/wms-webapi/src/main/java/top/wms/admin/controller/tcp/service/CommandService.java @@ -1,5 +1,11 @@ package top.wms.admin.controller.tcp.service; +import io.netty.channel.Channel; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.web.bind.annotation.RequestParam; +import top.continew.starter.core.validation.CheckUtils; +import top.wms.admin.controller.tcp.manager.ChannelManager; import top.wms.admin.controller.tcp.model.VMResult; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -55,4 +61,17 @@ public class CommandService { public VMResult getLatestResult() { return resultQueue.peek(); } + + + @Autowired + private ChannelManager channelManager; + +// @Scheduled(cron = "*/3 * * * * ?") + public void sendAndWait() { + // 1. 检查连接 + log.info("查询时间========"); + Channel channel = channelManager.getFirstChannel(); + channel.writeAndFlush("001"); + } + } diff --git a/wms-webapi/src/main/java/top/wms/admin/controller/vm/VmCommandController.java b/wms-webapi/src/main/java/top/wms/admin/controller/vm/VmCommandController.java index 9c85a5f..32c0faa 100644 --- a/wms-webapi/src/main/java/top/wms/admin/controller/vm/VmCommandController.java +++ b/wms-webapi/src/main/java/top/wms/admin/controller/vm/VmCommandController.java @@ -1,9 +1,11 @@ package top.wms.admin.controller.vm; +import cn.hutool.core.util.StrUtil; import io.netty.channel.Channel; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import top.continew.starter.core.validation.CheckUtils; import top.wms.admin.controller.tcp.config.SimpleRequestMatcher; import top.wms.admin.controller.tcp.manager.ChannelManager; @@ -25,53 +27,22 @@ public class VmCommandController { if (channel == null) { return "ERROR: VM未连接"; } - - // 2. 直接发送消息(不加requestId) String sendMsg = msg; channel.writeAndFlush(sendMsg); log.info("发送指令: {}", sendMsg); - - // 3. 等待响应(不传requestId) + // 3. 等待响应 String response = requestMatcher.waitForResponse(20); - - // 4. 返回结果 - if ("TIMEOUT".equals(response)) { - return "ERROR: 处理超时"; + CheckUtils.throwIf("TIMEOUT".equals(response),"响应超时,请重试"); + if (StrUtil.equals(response, "success") || StrUtil.equals(response, "failed")) { + return response; } + if (StrUtil.equals(response, msg)) { + response = "success"; + }else{ + response = "failed"; + } + // 4. 返回结果 return response; // 直接返回VM的响应 } - @PostMapping("/command") - public String sendCommand(@RequestBody CommandRequest request) { - Channel channel = channelManager.getFirstChannel(); - if (channel == null) { - return "ERROR: VM未连接"; - } - - // 直接发送请求的命令 - String sendMsg = request.getCommand(); - channel.writeAndFlush(sendMsg + "\n"); - log.info("发送指令: {}", sendMsg); - - // 等待响应 - String response = requestMatcher.waitForResponse(request.getTimeout()); - - if ("TIMEOUT".equals(response)) { - return "ERROR: 处理超时"; - } - return response; - } - - /** - * 请求体类 - */ - public static class CommandRequest { - private String command; - private int timeout = 5; // 默认5秒 - - public String getCommand() { return command; } - public void setCommand(String command) { this.command = command; } - public int getTimeout() { return timeout; } - public void setTimeout(int timeout) { this.timeout = timeout; } - } }