|
|
|
@ -38,26 +38,10 @@ public class TCPClient { |
|
|
|
} |
|
|
|
|
|
|
|
private void startTCPClient(List<EquipmentFolderLocation> serverConfigs) { |
|
|
|
// 如果设备配置为空,清除所有现有连接 |
|
|
|
if (serverConfigs == null || serverConfigs.isEmpty()) { |
|
|
|
if (!socketMap.isEmpty()){ |
|
|
|
clearAllConnections(); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 获取当前设备的 key(格式为 "IP:Port") |
|
|
|
Set<String> newDeviceKeys = serverConfigs.stream() |
|
|
|
.map(config -> config.getIp() + ":" + config.getPort()) |
|
|
|
.collect(Collectors.toSet()); |
|
|
|
|
|
|
|
// 关闭不在新设备列表中的连接 |
|
|
|
for (String key : socketMap.keySet()) { |
|
|
|
if (!newDeviceKeys.contains(key)) { |
|
|
|
closeConnection(key); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
log.info("需要连接的设备数量: {}", serverConfigs.size()); |
|
|
|
|
|
|
|
// 为每个设备配置启动一个新的连接任务 |
|
|
|
for (EquipmentFolderLocation config : serverConfigs) { |
|
|
|
executorService.submit(new Runnable() { |
|
|
|
@ -98,7 +82,7 @@ public class TCPClient { |
|
|
|
if (StringUtils.hasText(config.getRegexp())){ |
|
|
|
log.info("正则表达式: {}",config.getRegexp()); |
|
|
|
Pattern pattern = Pattern.compile(config.getRegexp()); |
|
|
|
Matcher matcher = pattern.matcher(responseLine); |
|
|
|
Matcher matcher = pattern.matcher(responseLine.trim()); |
|
|
|
if (matcher.find()){ |
|
|
|
saveData(config, matcher.group(1)); |
|
|
|
} |
|
|
|
|