From cd1ba55b26a2998fa19f2898949e9b0cff52cc16 Mon Sep 17 00:00:00 2001 From: liuzhu Date: Fri, 13 Mar 2026 10:30: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 --- wms-common/.flattened-pom.xml | 5 + wms-module-system/.flattened-pom.xml | 6 + .../controller/tcp/config/NettyTcpServer.java | 35 +++-- .../tcp/service/CommandService.java | 11 +- .../controller/vm/VmCommandController.java | 121 +++++++++++++++++- .../weighManage/ah/AHDZCConnect.java | 11 +- 6 files changed, 155 insertions(+), 34 deletions(-) diff --git a/wms-common/.flattened-pom.xml b/wms-common/.flattened-pom.xml index 2645cfc..5ac84e1 100644 --- a/wms-common/.flattened-pom.xml +++ b/wms-common/.flattened-pom.xml @@ -119,5 +119,10 @@ org.springframework spring-test + + io.netty + netty-all + 4.1.100.Final + diff --git a/wms-module-system/.flattened-pom.xml b/wms-module-system/.flattened-pom.xml index e1939ad..a4dcdca 100644 --- a/wms-module-system/.flattened-pom.xml +++ b/wms-module-system/.flattened-pom.xml @@ -21,5 +21,11 @@ top.wms wms-common + + javax.annotation + javax.annotation-api + 1.3.2 + provided + diff --git a/wms-webapi/src/main/java/top/wms/admin/controller/tcp/config/NettyTcpServer.java b/wms-webapi/src/main/java/top/wms/admin/controller/tcp/config/NettyTcpServer.java index c84e76e..fccfe7e 100644 --- a/wms-webapi/src/main/java/top/wms/admin/controller/tcp/config/NettyTcpServer.java +++ b/wms-webapi/src/main/java/top/wms/admin/controller/tcp/config/NettyTcpServer.java @@ -33,7 +33,6 @@ public class NettyTcpServer { private EventLoopGroup workerGroup; private Channel serverChannel; - @PostConstruct public void start() throws InterruptedException { log.info("正在启动TCP服务端,端口: {}", port); @@ -43,27 +42,27 @@ public class NettyTcpServer { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup) - .channel(NioServerSocketChannel.class) - .option(ChannelOption.SO_BACKLOG, 128) - .childOption(ChannelOption.SO_KEEPALIVE, true) - .childHandler(new ChannelInitializer() { - @Override - protected void initChannel(SocketChannel ch) { - ChannelPipeline pipeline = ch.pipeline(); + .channel(NioServerSocketChannel.class) + .option(ChannelOption.SO_BACKLOG, 128) + .childOption(ChannelOption.SO_KEEPALIVE, true) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel ch) { + ChannelPipeline pipeline = ch.pipeline(); - // 解决TCP粘包问题 - pipeline.addLast(new LineBasedFrameDecoder(1024)); + // 解决TCP粘包问题 + pipeline.addLast(new LineBasedFrameDecoder(1024)); - // 字符串编解码 - pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8)); - pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8)); + // 字符串编解码 + pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8)); + pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8)); - // 关键修复:直接new,不要从Spring容器获取 - pipeline.addLast(new TcpServerHandler()); + // 关键修复:直接new,不要从Spring容器获取 + pipeline.addLast(new TcpServerHandler()); - log.debug("新连接接入,处理器已添加"); - } - }); + log.debug("新连接接入,处理器已添加"); + } + }); ChannelFuture future = bootstrap.bind(port).sync(); if (future.isSuccess()) { 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 d431183..bbdffaa 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 @@ -3,8 +3,6 @@ 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; @@ -51,10 +49,8 @@ public class CommandService { // 获取统计信息 public String getStatistics() { - return String.format("成功: %d, 失败: %d, 总数: %d", - successCount.get(), - failCount.get(), - successCount.get() + failCount.get()); + return String.format("成功: %d, 失败: %d, 总数: %d", successCount.get(), failCount.get(), successCount + .get() + failCount.get()); } // 获取最新结果 @@ -62,11 +58,10 @@ public class CommandService { return resultQueue.peek(); } - @Autowired private ChannelManager channelManager; -// @Scheduled(cron = "*/3 * * * * ?") + @Scheduled(cron = "*/1 * * * * ?") public void sendAndWait() { // 1. 检查连接 log.info("查询时间========"); 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 32c0faa..0c0effc 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,13 +1,30 @@ package top.wms.admin.controller.vm; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import io.netty.channel.Channel; import lombok.extern.slf4j.Slf4j; +import org.dromara.x.file.storage.core.FileInfo; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.mock.web.MockMultipartFile; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import top.continew.starter.core.validation.CheckUtils; import top.wms.admin.controller.tcp.config.SimpleRequestMatcher; import top.wms.admin.controller.tcp.manager.ChannelManager; +import top.wms.admin.system.service.FileService; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; @Slf4j @RestController @@ -20,6 +37,9 @@ public class VmCommandController { @Autowired private SimpleRequestMatcher requestMatcher; + @Autowired + private FileService fileService; + @GetMapping("/send") public String sendAndWait(@RequestParam String msg) { // 1. 检查连接 @@ -32,17 +52,114 @@ public class VmCommandController { log.info("发送指令: {}", sendMsg); // 3. 等待响应 String response = requestMatcher.waitForResponse(20); - CheckUtils.throwIf("TIMEOUT".equals(response),"响应超时,请重试"); + CheckUtils.throwIf("TIMEOUT".equals(response), "响应超时,请重试"); if (StrUtil.equals(response, "success") || StrUtil.equals(response, "failed")) { return response; } if (StrUtil.equals(response, msg)) { response = "success"; - }else{ + } else { response = "failed"; } // 4. 返回结果 return response; // 直接返回VM的响应 } + // 基础路径 + private static final String BASE_PATH = "C:/Users/14725/Desktop/material"; + + // 固定照片名称 + private static final String PHOTO_NAME = "001.bmp"; + + /** + * 获取最新的001.bmp照片 + * + * @return 图片文件 + */ + /*@GetMapping("/latest-photo") + public ResponseEntity getLatestPhoto() { + try { + // 获取当前日期 + LocalDate now = LocalDate.now(); + String yearMonth = now.format(DateTimeFormatter.ofPattern("yyyyMM")); + String day = now.format(DateTimeFormatter.ofPattern("dd")); + + // 构建完整的文件路径 + // 格式: C:/Users/14725/Desktop/material/202603/20260312/001.bmp + String filePath = String.format("%s/%s/%s%s/%s", BASE_PATH, yearMonth, yearMonth, day, PHOTO_NAME); + + // 读取图片文件 + Path imagePath = Paths.get(filePath); + + // 检查文件是否存在 + if (!Files.exists(imagePath)) { + return ResponseEntity.notFound().build(); + } + + // 读取文件字节 + byte[] imageBytes = Files.readAllBytes(imagePath); + + // 设置响应头 + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.valueOf("image/bmp")); + headers.setContentLength(imageBytes.length); + + // 返回图片 + return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK); + + } catch (IOException e) { + e.printStackTrace(); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + }*/ + + @GetMapping("/latest-photo") + public String getLatestPhoto() { + try { + // 获取当前日期 + LocalDate now = LocalDate.now(); + String yearMonth = now.format(DateTimeFormatter.ofPattern("yyyyMM")); + String day = now.format(DateTimeFormatter.ofPattern("dd")); + + // 构建完整的本地文件路径 + // 格式: C:/Users/14725/Desktop/material/202603/20260312/001.bmp + String filePath = String.format("%s/%s/%s%s/%s", BASE_PATH, yearMonth, yearMonth, day, PHOTO_NAME); + + // 读取图片文件 + Path imagePath = Paths.get(filePath); + + // 检查文件是否存在 + if (!Files.exists(imagePath)) { + return null; // 或者抛出异常,根据业务需求决定 + } + + // 将文件转换为MultipartFile + File file = imagePath.toFile(); + FileInputStream input = new FileInputStream(file); + MultipartFile multipartFile = new MockMultipartFile( + file.getName(), // 文件名 + file.getName(), // 原始文件名 + "image/bmp", // 内容类型 + input // 文件输入流 + ); + + // 构建MinIO存储路径 + String photoStoragePath = "catch" + DateUtil.today() + "/"; + + // 使用现有的fileService上传到MinIO + FileInfo fileInfo = fileService.upload(multipartFile, photoStoragePath, null, true, true); + + // 检查上传结果 + CheckUtils.throwIfNull(fileInfo, "照片上传失败"); + + // 关闭输入流 + input.close(); + return fileInfo.getUrl(); + + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException("处理图片失败: " + e.getMessage()); + } + } + } diff --git a/wms-webapi/src/main/java/top/wms/admin/controller/weighManage/ah/AHDZCConnect.java b/wms-webapi/src/main/java/top/wms/admin/controller/weighManage/ah/AHDZCConnect.java index 39da65b..c8eceb2 100644 --- a/wms-webapi/src/main/java/top/wms/admin/controller/weighManage/ah/AHDZCConnect.java +++ b/wms-webapi/src/main/java/top/wms/admin/controller/weighManage/ah/AHDZCConnect.java @@ -25,7 +25,7 @@ import java.util.regex.Pattern; @Slf4j public class AHDZCConnect { - private static final String PORT_NAME = "COM5"; + private static final String PORT_NAME = "COM12"; private static final int BAUD_RATE = 9600; private static final int DATA_BITS = 8; private static final int STOP_BITS = 1; @@ -52,10 +52,9 @@ public class AHDZCConnect { @PostConstruct public void init() { // 项目启动时初始化并启动服务 - if (false) { - ScaleService(); - start(); - } + ScaleService(); + start(); + } @PreDestroy @@ -324,4 +323,4 @@ public class AHDZCConnect { log.info("线程已完全停止"); } -} \ No newline at end of file +}