添加过滤无效设备
This commit is contained in:
parent
296d535b25
commit
ec4985d819
|
|
@ -16,13 +16,16 @@ import com.ruoyi.device.service.enums.DeviceType;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Service
|
||||
public class SynService {
|
||||
|
|
@ -31,6 +34,18 @@ public class SynService {
|
|||
|
||||
private final IThingsBoardDomain iThingsBoardDomain;
|
||||
|
||||
/**
|
||||
* 设备名称过滤正则表达式列表(从配置文件读取)
|
||||
* 匹配这些正则表达式的设备将被跳过,不进行同步
|
||||
*/
|
||||
@Value("${device.sync.exclude-patterns:}")
|
||||
private String excludePatterns;
|
||||
|
||||
/**
|
||||
* 编译后的正则表达式模式列表(延迟初始化)
|
||||
*/
|
||||
private List<Pattern> excludePatternList;
|
||||
|
||||
@Autowired
|
||||
private IDeviceDomain deviceDomain;
|
||||
|
||||
|
|
@ -47,6 +62,61 @@ public class SynService {
|
|||
this.iThingsBoardDomain = iThingsBoardDomain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化设备名称过滤正则表达式列表
|
||||
* 延迟初始化,在第一次使用时编译正则表达式
|
||||
*/
|
||||
private void initExcludePatterns() {
|
||||
if (excludePatternList == null) {
|
||||
excludePatternList = new ArrayList<>();
|
||||
|
||||
if (StringUtils.hasText(excludePatterns)) {
|
||||
String[] patterns = excludePatterns.split(",");
|
||||
for (String pattern : patterns) {
|
||||
String trimmedPattern = pattern.trim();
|
||||
if (StringUtils.hasText(trimmedPattern)) {
|
||||
try {
|
||||
excludePatternList.add(Pattern.compile(trimmedPattern));
|
||||
log.info("加载设备名称过滤规则: {}", trimmedPattern);
|
||||
} catch (Exception e) {
|
||||
log.error("无效的正则表达式: {}, error={}", trimmedPattern, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (excludePatternList.isEmpty()) {
|
||||
log.info("未配置设备名称过滤规则");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断设备名称是否应该被过滤(跳过同步)
|
||||
*
|
||||
* @param deviceName 设备名称
|
||||
* @return true 表示应该被过滤,false 表示不过滤
|
||||
*/
|
||||
private boolean shouldExcludeDevice(String deviceName) {
|
||||
// 延迟初始化
|
||||
initExcludePatterns();
|
||||
|
||||
// 如果没有配置过滤规则,不过滤任何设备
|
||||
if (excludePatternList.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查设备名称是否匹配任何过滤规则
|
||||
for (Pattern pattern : excludePatternList) {
|
||||
if (pattern.matcher(deviceName).matches()) {
|
||||
log.debug("设备 {} 匹配过滤规则 {},跳过同步", deviceName, pattern.pattern());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定时任务:同步基础表数据
|
||||
* 执行时间:启动后1分钟开始,每2分钟执行一次(可通过配置文件修改)
|
||||
|
|
@ -64,6 +134,16 @@ public class SynService {
|
|||
|
||||
for (List<DeviceInfo> deviceBatch : allDevices) {
|
||||
for (DeviceInfo deviceInfo : deviceBatch) {
|
||||
// 通过设备名字,过滤出不需要同步的一些设备,比如
|
||||
// 设备名字是 THJSQ03A2302FAWQZBJ1 和 THJSQ03A23045BLPGYC5
|
||||
// 支持配置简单的正则表达式;该正则表达式需要维护在配置文件中
|
||||
|
||||
// 检查设备名称是否应该被过滤
|
||||
if (shouldExcludeDevice(deviceInfo.getName())) {
|
||||
log.info("设备 {} 匹配过滤规则,跳过同步", deviceInfo.getName());
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
// 获取设备属性
|
||||
AttributeMap attributes = iThingsBoardDomain.getDeviceAttributes(deviceInfo.getId());
|
||||
|
|
|
|||
|
|
@ -27,3 +27,10 @@ spring:
|
|||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
# 设备同步配置
|
||||
device:
|
||||
sync:
|
||||
# 设备名称过滤正则表达式(多个用逗号分隔)
|
||||
# 匹配这些正则表达式的设备将被跳过,不进行同步
|
||||
# 当前配置:过滤所有以 TH 开头的设备
|
||||
exclude-patterns: TH.*
|
||||
|
|
|
|||
Loading…
Reference in New Issue