From 91c336cd4124201d92660a2bf9a9b4bd59108d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?LAPTOP-S9HJSOEB=5C=E6=98=8A=E5=A4=A9?= Date: Tue, 12 Aug 2025 10:33:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=99=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/zhehekeji/web/service/PlcService.java | 1 + .../zhehekeji/web/service/client/Encoder.java | 2 +- .../service/cron/PLCConnectionExample.java | 92 ++++++++++++++++--- 3 files changed, 81 insertions(+), 14 deletions(-) diff --git a/web/src/main/java/com/zhehekeji/web/service/PlcService.java b/web/src/main/java/com/zhehekeji/web/service/PlcService.java index 2330392..56e329b 100644 --- a/web/src/main/java/com/zhehekeji/web/service/PlcService.java +++ b/web/src/main/java/com/zhehekeji/web/service/PlcService.java @@ -912,6 +912,7 @@ public class PlcService { if (times==2) { Thread.sleep(configProperties.getCameraConfig().getC2DelayCaptureTime()); //C1底部拍照 + log.info(plcId+" C1"); ClientChanel.sendMessage(plcId, "C1:1"); Thread.sleep(configProperties.getCameraConfig().getC3DelayCaptureTime()); diff --git a/web/src/main/java/com/zhehekeji/web/service/client/Encoder.java b/web/src/main/java/com/zhehekeji/web/service/client/Encoder.java index 1f9bbb1..615165e 100644 --- a/web/src/main/java/com/zhehekeji/web/service/client/Encoder.java +++ b/web/src/main/java/com/zhehekeji/web/service/client/Encoder.java @@ -18,7 +18,7 @@ public class Encoder extends MessageToByteEncoder { @Override protected void encode(ChannelHandlerContext channelHandlerContext, String data, ByteBuf byteBuf) throws Exception { data = data + END_STRING; - tcpLogger.info("send to client:{}, length:{}",data, data.length()); + tcpLogger.info("send to client:{}, length:{},ip:{}",data, data.length(), channelHandlerContext.channel().remoteAddress()); byteBuf.writeBytes(data.getBytes(StandardCharsets.UTF_8)); } } diff --git a/web/src/main/java/com/zhehekeji/web/service/cron/PLCConnectionExample.java b/web/src/main/java/com/zhehekeji/web/service/cron/PLCConnectionExample.java index 5ba5f82..a550390 100644 --- a/web/src/main/java/com/zhehekeji/web/service/cron/PLCConnectionExample.java +++ b/web/src/main/java/com/zhehekeji/web/service/cron/PLCConnectionExample.java @@ -15,6 +15,8 @@ import javax.annotation.Resource; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.concurrent.*; @@ -70,46 +72,110 @@ public class PLCConnectionExample { } } + // ... existing code ... /** * 替换连接到池中 */ public void updateConnection(S7Client client) { try { - // 如果连接不可用,则重新创建 + // 先断开并关闭旧连接 + if (client != null) { + client.Disconnect(); + } connectionPool.remove(client); + + // 创建新连接 S7Client newClient = new S7Client(); - newClient.ConnectTo(plcIp, plcRack, plcSlot); - connectionPool.offer(newClient); + int result = newClient.ConnectTo(plcIp, plcRack, plcSlot); + if (result == 0) { + connectionPool.offer(newClient); + log.info("成功创建并添加新PLC连接到连接池"); + } else { + log.error("创建新PLC连接失败,错误码: {}", result); + } } catch (Exception e) { + log.error("更新PLC连接失败", e); throw new RuntimeException("归还 PLC 连接失败", e); } } + /** * 归还连接到池中 */ public void returnConnection(S7Client client) { try { - connectionPool.offer(client); // 将连接归还到连接池 + // 检查连接是否仍然有效后再归还 + if (client != null && client.Connected) { + connectionPool.offer(client); + } else { + // 如果连接无效,创建新连接 + updateConnection(client); + } } catch (Exception e) { + log.error("归还PLC连接时发生错误", e); throw new RuntimeException("归还 PLC 连接失败", e); } } - + // ... existing code ... /** * 定期清理无效连接 */ @Scheduled(fixedRate = 60000) public void cleanUpConnections() { - for (S7Client client : connectionPool) { - if (!client.Connected) { - // 如果连接不可用,则重新创建 - connectionPool.remove(client); - S7Client newClient = new S7Client(); - newClient.ConnectTo(plcIp, plcRack, plcSlot); - connectionPool.offer(newClient); + log.info("开始清理PLC连接池,当前池大小: {}", connectionPool.size()); + int cleanedCount = 0; + + // 创建一个新的列表来存储有效连接 + List validConnections = new ArrayList<>(); + S7Client[] clients = connectionPool.toArray(new S7Client[0]); + + for (S7Client client : clients) { + // 从池中移除连接进行检查 + connectionPool.remove(client); + + try { + // 检查连接是否有效 + if (client != null && client.Connected) { + // 连接有效,放回池中 + connectionPool.offer(client); + validConnections.add(client); + } else { + // 连接无效,断开并创建新连接 + if (client != null) { + client.Disconnect(); + } + + S7Client newClient = new S7Client(); + int result = newClient.ConnectTo(plcIp, plcRack, plcSlot); + if (result == 0) { + connectionPool.offer(newClient); + validConnections.add(newClient); + cleanedCount++; + log.info("已替换无效PLC连接"); + } else { + log.error("尝试创建新PLC连接失败,错误码: {}", result); + } + } + } catch (Exception e) { + log.error("清理连接时发生异常", e); + // 即使出错也尝试创建新连接 + try { + S7Client newClient = new S7Client(); + int result = newClient.ConnectTo(plcIp, plcRack, plcSlot); + if (result == 0) { + connectionPool.offer(newClient); + validConnections.add(newClient); + cleanedCount++; + } + } catch (Exception ex) { + log.error("创建新连接时发生异常", ex); + } } } + + log.info("PLC连接池清理完成,替换了 {} 个连接,当前池大小: {}", cleanedCount, connectionPool.size()); } +// ... existing code ... // @Bean // public S7Client s7Client(){ // // 创建S7Client实例 @@ -190,7 +256,7 @@ public class PLCConnectionExample { if (i == 0) continue; if (taskMap.get(key) == null || taskMap.get(key) != i) { log.info("任务号变化" + key + ":" + i); - log.info("任务号变化" + key + ":" + i); + taskMap.put(key, i); String plcId = key.split("-")[0]; executorService.submit(() -> processKey(i, plcId, key));