优化
This commit is contained in:
@@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.dromara.x.file.storage.spring.EnableFileStorage;
|
import org.dromara.x.file.storage.spring.EnableFileStorage;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import top.continew.starter.core.autoconfigure.project.ProjectProperties;
|
import top.continew.starter.core.autoconfigure.project.ProjectProperties;
|
||||||
@@ -29,6 +30,7 @@ import top.continew.starter.web.model.R;
|
|||||||
@RestController
|
@RestController
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@EnableScheduling
|
||||||
public class WmsAdminApplication {
|
public class WmsAdminApplication {
|
||||||
|
|
||||||
private final ProjectProperties projectProperties;
|
private final ProjectProperties projectProperties;
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
package top.wms.admin.controller.tcp.service;
|
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 top.wms.admin.controller.tcp.model.VMResult;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -55,4 +61,17 @@ public class CommandService {
|
|||||||
public VMResult getLatestResult() {
|
public VMResult getLatestResult() {
|
||||||
return resultQueue.peek();
|
return resultQueue.peek();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ChannelManager channelManager;
|
||||||
|
|
||||||
|
// @Scheduled(cron = "*/3 * * * * ?")
|
||||||
|
public void sendAndWait() {
|
||||||
|
// 1. 检查连接
|
||||||
|
log.info("查询时间========");
|
||||||
|
Channel channel = channelManager.getFirstChannel();
|
||||||
|
channel.writeAndFlush("001");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package top.wms.admin.controller.vm;
|
package top.wms.admin.controller.vm;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
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.config.SimpleRequestMatcher;
|
||||||
import top.wms.admin.controller.tcp.manager.ChannelManager;
|
import top.wms.admin.controller.tcp.manager.ChannelManager;
|
||||||
|
|
||||||
@@ -25,53 +27,22 @@ public class VmCommandController {
|
|||||||
if (channel == null) {
|
if (channel == null) {
|
||||||
return "ERROR: VM未连接";
|
return "ERROR: VM未连接";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 直接发送消息(不加requestId)
|
|
||||||
String sendMsg = msg;
|
String sendMsg = msg;
|
||||||
channel.writeAndFlush(sendMsg);
|
channel.writeAndFlush(sendMsg);
|
||||||
log.info("发送指令: {}", sendMsg);
|
log.info("发送指令: {}", sendMsg);
|
||||||
|
// 3. 等待响应
|
||||||
// 3. 等待响应(不传requestId)
|
|
||||||
String response = requestMatcher.waitForResponse(20);
|
String response = requestMatcher.waitForResponse(20);
|
||||||
|
CheckUtils.throwIf("TIMEOUT".equals(response),"响应超时,请重试");
|
||||||
// 4. 返回结果
|
if (StrUtil.equals(response, "success") || StrUtil.equals(response, "failed")) {
|
||||||
if ("TIMEOUT".equals(response)) {
|
return response;
|
||||||
return "ERROR: 处理超时";
|
|
||||||
}
|
}
|
||||||
|
if (StrUtil.equals(response, msg)) {
|
||||||
|
response = "success";
|
||||||
|
}else{
|
||||||
|
response = "failed";
|
||||||
|
}
|
||||||
|
// 4. 返回结果
|
||||||
return response; // 直接返回VM的响应
|
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; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user