写日志

hubei-jinshennong
LAPTOP-S9HJSOEB\昊天 7 months ago
parent 7a343121f0
commit 91c336cd41

@ -912,6 +912,7 @@ public class PlcService {
if (times==2) { if (times==2) {
Thread.sleep(configProperties.getCameraConfig().getC2DelayCaptureTime()); Thread.sleep(configProperties.getCameraConfig().getC2DelayCaptureTime());
//C1底部拍照 //C1底部拍照
log.info(plcId+" C1");
ClientChanel.sendMessage(plcId, "C1:1"); ClientChanel.sendMessage(plcId, "C1:1");
Thread.sleep(configProperties.getCameraConfig().getC3DelayCaptureTime()); Thread.sleep(configProperties.getCameraConfig().getC3DelayCaptureTime());

@ -18,7 +18,7 @@ public class Encoder extends MessageToByteEncoder<String> {
@Override @Override
protected void encode(ChannelHandlerContext channelHandlerContext, String data, ByteBuf byteBuf) throws Exception { protected void encode(ChannelHandlerContext channelHandlerContext, String data, ByteBuf byteBuf) throws Exception {
data = data + END_STRING; 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)); byteBuf.writeBytes(data.getBytes(StandardCharsets.UTF_8));
} }
} }

@ -15,6 +15,8 @@ import javax.annotation.Resource;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.*; import java.util.concurrent.*;
@ -70,46 +72,110 @@ public class PLCConnectionExample {
} }
} }
// ... existing code ...
/** /**
* *
*/ */
public void updateConnection(S7Client client) { public void updateConnection(S7Client client) {
try { try {
// 如果连接不可用,则重新创建 // 先断开并关闭旧连接
if (client != null) {
client.Disconnect();
}
connectionPool.remove(client); connectionPool.remove(client);
// 创建新连接
S7Client newClient = new S7Client(); S7Client newClient = new S7Client();
newClient.ConnectTo(plcIp, plcRack, plcSlot); int result = newClient.ConnectTo(plcIp, plcRack, plcSlot);
connectionPool.offer(newClient); if (result == 0) {
connectionPool.offer(newClient);
log.info("成功创建并添加新PLC连接到连接池");
} else {
log.error("创建新PLC连接失败错误码: {}", result);
}
} catch (Exception e) { } catch (Exception e) {
log.error("更新PLC连接失败", e);
throw new RuntimeException("归还 PLC 连接失败", e); throw new RuntimeException("归还 PLC 连接失败", e);
} }
} }
/** /**
* *
*/ */
public void returnConnection(S7Client client) { public void returnConnection(S7Client client) {
try { try {
connectionPool.offer(client); // 将连接归还到连接池 // 检查连接是否仍然有效后再归还
if (client != null && client.Connected) {
connectionPool.offer(client);
} else {
// 如果连接无效,创建新连接
updateConnection(client);
}
} catch (Exception e) { } catch (Exception e) {
log.error("归还PLC连接时发生错误", e);
throw new RuntimeException("归还 PLC 连接失败", e); throw new RuntimeException("归还 PLC 连接失败", e);
} }
} }
// ... existing code ...
/** /**
* *
*/ */
@Scheduled(fixedRate = 60000) @Scheduled(fixedRate = 60000)
public void cleanUpConnections() { public void cleanUpConnections() {
for (S7Client client : connectionPool) { log.info("开始清理PLC连接池当前池大小: {}", connectionPool.size());
if (!client.Connected) { int cleanedCount = 0;
// 如果连接不可用,则重新创建
connectionPool.remove(client); // 创建一个新的列表来存储有效连接
S7Client newClient = new S7Client(); List<S7Client> validConnections = new ArrayList<>();
newClient.ConnectTo(plcIp, plcRack, plcSlot); S7Client[] clients = connectionPool.toArray(new S7Client[0]);
connectionPool.offer(newClient);
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 // @Bean
// public S7Client s7Client(){ // public S7Client s7Client(){
// // 创建S7Client实例 // // 创建S7Client实例
@ -190,7 +256,7 @@ public class PLCConnectionExample {
if (i == 0) continue; if (i == 0) continue;
if (taskMap.get(key) == null || taskMap.get(key) != i) { if (taskMap.get(key) == null || taskMap.get(key) != i) {
log.info("任务号变化" + key + ":" + i); log.info("任务号变化" + key + ":" + i);
log.info("任务号变化" + key + ":" + i);
taskMap.put(key, i); taskMap.put(key, i);
String plcId = key.split("-")[0]; String plcId = key.split("-")[0];
executorService.submit(() -> processKey(i, plcId, key)); executorService.submit(() -> processKey(i, plcId, key));

Loading…
Cancel
Save