This commit is contained in:
2026-03-12 16:54:55 +08:00
parent dec15eb913
commit c1d84aaf81
3 changed files with 33 additions and 41 deletions

View File

@@ -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;

View File

@@ -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");
}
}

View File

@@ -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; }
}
}