写日志

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

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

@ -18,7 +18,7 @@ public class Encoder extends MessageToByteEncoder<String> {
@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));
}
}

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

Loading…
Cancel
Save