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