perf:【IoT 物联网】场景联动优化 review 提到的逻辑

This commit is contained in:
puhui999
2025-08-24 15:25:36 +08:00
parent 9c6c1584e7
commit 2b3e2d6dbd
21 changed files with 1223 additions and 1025 deletions

View File

@@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.iot.enums.rule;
import cn.hutool.core.util.ArrayUtil;
import cn.iocoder.yudao.framework.common.core.ArrayValuable;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@@ -32,4 +33,8 @@ public enum IotSceneRuleConditionTypeEnum implements ArrayValuable<Integer> {
return ARRAYS;
}
public static IotSceneRuleConditionTypeEnum typeOf(Integer type) {
return ArrayUtil.firstMatch(item -> item.getType().equals(type), values());
}
}

View File

@@ -55,9 +55,8 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
@Resource
private IotDeviceService deviceService;
// TODO @puhui999sceneRuleMatcherManager 变量名
@Resource
private IotSceneRuleMatcherManager matcherManager;
private IotSceneRuleMatcherManager sceneRuleMatcherManager;
@Resource
private List<IotSceneRuleAction> sceneRuleActions;
@@ -275,7 +274,7 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
private boolean matchSingleTrigger(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger, IotSceneRuleDO sceneRule) {
try {
// 2. 检查触发器的条件分组
return matcherManager.isMatched(message, trigger) && isTriggerConditionGroupsMatched(message, trigger, sceneRule);
return sceneRuleMatcherManager.isMatched(message, trigger) && isTriggerConditionGroupsMatched(message, trigger, sceneRule);
} catch (Exception e) {
log.error("[matchSingleTrigger][触发器匹配异常] sceneRuleId: {}, triggerType: {}, message: {}",
sceneRule.getId(), trigger.getType(), message, e);
@@ -334,7 +333,7 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
private boolean isTriggerConditionMatched(IotDeviceMessage message, IotSceneRuleDO.TriggerCondition condition,
IotSceneRuleDO sceneRule, IotSceneRuleDO.Trigger trigger) {
try {
return matcherManager.isConditionMatched(message, condition);
return sceneRuleMatcherManager.isConditionMatched(message, condition);
} catch (Exception e) {
log.error("[isTriggerConditionMatched][规则场景编号({}) 的触发器({}) 条件匹配异常]",
sceneRule.getId(), trigger, e);

View File

@@ -12,7 +12,6 @@ import org.springframework.stereotype.Component;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
@@ -37,33 +36,26 @@ public class IotSceneRuleMatcherManager {
*/
private final Map<IotSceneRuleConditionTypeEnum, IotSceneRuleConditionMatcher> conditionMatchers;
/**
* 所有匹配器列表(按优先级排序)
*/
// TODO @puhui999貌似 local variable 也可以
private final List<IotSceneRuleMatcher> allMatchers;
public IotSceneRuleMatcherManager(List<IotSceneRuleMatcher> matchers) {
if (CollUtil.isEmpty(matchers)) {
log.warn("[IotSceneRuleMatcherManager][没有找到任何匹配器]");
this.triggerMatchers = new HashMap<>();
this.conditionMatchers = new HashMap<>();
this.allMatchers = new ArrayList<>();
return;
}
// 按优先级排序并过滤启用的匹配器
this.allMatchers = matchers.stream()
List<IotSceneRuleMatcher> allMatchers = matchers.stream()
.filter(IotSceneRuleMatcher::isEnabled)
.sorted(Comparator.comparing(IotSceneRuleMatcher::getPriority))
.collect(Collectors.toList());
.toList();
// 分离触发器匹配器和条件匹配器
List<IotSceneRuleTriggerMatcher> triggerMatchers = this.allMatchers.stream()
List<IotSceneRuleTriggerMatcher> triggerMatchers = allMatchers.stream()
.filter(matcher -> matcher instanceof IotSceneRuleTriggerMatcher)
.map(matcher -> (IotSceneRuleTriggerMatcher) matcher)
.toList();
List<IotSceneRuleConditionMatcher> conditionMatchers = this.allMatchers.stream()
List<IotSceneRuleConditionMatcher> conditionMatchers = allMatchers.stream()
.filter(matcher -> matcher instanceof IotSceneRuleConditionMatcher)
.map(matcher -> (IotSceneRuleConditionMatcher) matcher)
.toList();
@@ -92,7 +84,7 @@ public class IotSceneRuleMatcherManager {
// 日志输出初始化信息
log.info("[IotSceneRuleMatcherManager][初始化完成,共加载({})个匹配器,其中触发器匹配器({})个,条件匹配器({})个]",
this.allMatchers.size(), this.triggerMatchers.size(), this.conditionMatchers.size());
allMatchers.size(), this.triggerMatchers.size(), this.conditionMatchers.size());
this.triggerMatchers.forEach((type, matcher) ->
log.info("[IotSceneRuleMatcherManager][触发器匹配器类型: ({}), 优先级: ({})] ", type, matcher.getPriority()));
this.conditionMatchers.forEach((type, matcher) ->
@@ -123,7 +115,7 @@ public class IotSceneRuleMatcherManager {
}
try {
return matcher.isMatched(message, trigger);
return matcher.matches(message, trigger);
} catch (Exception e) {
log.error("[isMatched][触发器匹配异常] message: {}, trigger: {}", message, trigger, e);
return false;
@@ -144,7 +136,7 @@ public class IotSceneRuleMatcherManager {
}
// 根据条件类型查找对应的匹配器
IotSceneRuleConditionTypeEnum conditionType = findConditionTypeEnum(condition.getType());
IotSceneRuleConditionTypeEnum conditionType = IotSceneRuleConditionTypeEnum.typeOf(condition.getType());
if (conditionType == null) {
log.warn("[isConditionMatched][conditionType({}) 未知的条件类型]", condition.getType());
return false;
@@ -157,45 +149,11 @@ public class IotSceneRuleMatcherManager {
// 执行匹配逻辑
try {
return matcher.isMatched(message, condition);
return matcher.matches(message, condition);
} catch (Exception e) {
log.error("[isConditionMatched][message({}) condition({}) 条件匹配异常]", message, condition, e);
return false;
}
}
/**
* 根据类型值查找条件类型枚举
*
* @param typeValue 类型值
* @return 条件类型枚举
*/
private IotSceneRuleConditionTypeEnum findConditionTypeEnum(Integer typeValue) {
// TODO @puhui999是不是搞到枚举类里
return Arrays.stream(IotSceneRuleConditionTypeEnum.values())
.filter(type -> type.getType().equals(typeValue))
.findFirst()
.orElse(null);
}
// TODO @puhui999下面两个方法是不是也可以删除哈
/**
* 获取所有支持的触发器类型
*
* @return 支持的触发器类型列表
*/
public Set<IotSceneRuleTriggerTypeEnum> getSupportedTriggerTypes() {
return new HashSet<>(triggerMatchers.keySet());
}
/**
* 获取所有支持的条件类型
*
* @return 支持的条件类型列表
*/
public Set<IotSceneRuleConditionTypeEnum> getSupportedConditionTypes() {
return new HashSet<>(conditionMatchers.keySet());
}
}

View File

@@ -43,7 +43,7 @@ public class CurrentTimeConditionMatcher implements IotSceneRuleConditionMatcher
}
@Override
public boolean isMatched(IotDeviceMessage message, IotSceneRuleDO.TriggerCondition condition) {
public boolean matches(IotDeviceMessage message, IotSceneRuleDO.TriggerCondition condition) {
// 1.1 基础参数校验
if (!IotSceneRuleMatcherHelper.isBasicConditionValid(condition)) {
IotSceneRuleMatcherHelper.logConditionMatchFailure(message, condition, "条件基础参数无效");

View File

@@ -22,9 +22,8 @@ public class DevicePropertyConditionMatcher implements IotSceneRuleConditionMatc
return IotSceneRuleConditionTypeEnum.DEVICE_PROPERTY;
}
// TODO @puhui999matches 会不会更好?参考的 org.hamcrest.Matcher jdk 接口
@Override
public boolean isMatched(IotDeviceMessage message, IotSceneRuleDO.TriggerCondition condition) {
public boolean matches(IotDeviceMessage message, IotSceneRuleDO.TriggerCondition condition) {
// 1.1 基础参数校验
if (!IotSceneRuleMatcherHelper.isBasicConditionValid(condition)) {
IotSceneRuleMatcherHelper.logConditionMatchFailure(message, condition, "条件基础参数无效");

View File

@@ -22,7 +22,7 @@ public class DeviceStateConditionMatcher implements IotSceneRuleConditionMatcher
}
@Override
public boolean isMatched(IotDeviceMessage message, IotSceneRuleDO.TriggerCondition condition) {
public boolean matches(IotDeviceMessage message, IotSceneRuleDO.TriggerCondition condition) {
// 1.1 基础参数校验
if (!IotSceneRuleMatcherHelper.isBasicConditionValid(condition)) {
IotSceneRuleMatcherHelper.logConditionMatchFailure(message, condition, "条件基础参数无效");

View File

@@ -33,6 +33,6 @@ public interface IotSceneRuleConditionMatcher extends IotSceneRuleMatcher {
* @param condition 触发条件
* @return 是否匹配
*/
boolean isMatched(IotDeviceMessage message, IotSceneRuleDO.TriggerCondition condition);
boolean matches(IotDeviceMessage message, IotSceneRuleDO.TriggerCondition condition);
}

View File

@@ -25,7 +25,7 @@ public class DeviceEventPostTriggerMatcher implements IotSceneRuleTriggerMatcher
}
@Override
public boolean isMatched(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger) {
public boolean matches(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger) {
// 1.1 基础参数校验
if (!IotSceneRuleMatcherHelper.isBasicTriggerValid(trigger)) {
IotSceneRuleMatcherHelper.logTriggerMatchFailure(message, trigger, "触发器基础参数无效");

View File

@@ -24,7 +24,7 @@ public class DevicePropertyPostTriggerMatcher implements IotSceneRuleTriggerMatc
}
@Override
public boolean isMatched(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger) {
public boolean matches(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger) {
// 1.1 基础参数校验
if (!IotSceneRuleMatcherHelper.isBasicTriggerValid(trigger)) {
IotSceneRuleMatcherHelper.logTriggerMatchFailure(message, trigger, "触发器基础参数无效");

View File

@@ -24,7 +24,7 @@ public class DeviceServiceInvokeTriggerMatcher implements IotSceneRuleTriggerMat
}
@Override
public boolean isMatched(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger) {
public boolean matches(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger) {
// 1.1 基础参数校验
if (!IotSceneRuleMatcherHelper.isBasicTriggerValid(trigger)) {
IotSceneRuleMatcherHelper.logTriggerMatchFailure(message, trigger, "触发器基础参数无效");

View File

@@ -23,7 +23,7 @@ public class DeviceStateUpdateTriggerMatcher implements IotSceneRuleTriggerMatch
}
@Override
public boolean isMatched(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger) {
public boolean matches(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger) {
// 1.1 基础参数校验
if (!IotSceneRuleMatcherHelper.isBasicTriggerValid(trigger)) {
IotSceneRuleMatcherHelper.logTriggerMatchFailure(message, trigger, "触发器基础参数无效");

View File

@@ -33,6 +33,6 @@ public interface IotSceneRuleTriggerMatcher extends IotSceneRuleMatcher {
* @param trigger 触发器配置
* @return 是否匹配
*/
boolean isMatched(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger);
boolean matches(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger);
}

View File

@@ -25,7 +25,7 @@ public class TimerTriggerMatcher implements IotSceneRuleTriggerMatcher {
}
@Override
public boolean isMatched(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger) {
public boolean matches(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger) {
// 1.1 基础参数校验
if (!IotSceneRuleMatcherHelper.isBasicTriggerValid(trigger)) {
IotSceneRuleMatcherHelper.logTriggerMatchFailure(message, trigger, "触发器基础参数无效");