review:【iot 物联网】场景联动

This commit is contained in:
YunaiV 2025-09-03 23:24:22 +08:00
parent 67a44fd334
commit e639dbfeb7
19 changed files with 71 additions and 123 deletions

View File

@ -19,7 +19,6 @@ import java.util.Arrays;
@Getter
public enum IotSceneRuleTriggerTypeEnum implements ArrayValuable<Integer> {
// TODO @芋艿后续对应部分 @下等包结构梳理完
/**
* 设备上下线变更
*

View File

@ -84,13 +84,14 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
@Override
public void updateSceneRuleStatus(Long id, Integer status) {
// 校验存在
// 1. 校验存在
validateSceneRuleExists(id);
// 更新状态
// 2. 更新状态
IotSceneRuleDO updateObj = new IotSceneRuleDO().setId(id).setStatus(status);
sceneRuleMapper.updateById(updateObj);
// 根据状态管理定时触发器
// 3. 根据状态管理定时触发器
if (CommonStatusEnum.isEnable(status)) {
// 启用时获取完整的场景规则信息并注册定时触发器
IotSceneRuleDO sceneRule = sceneRuleMapper.selectById(id);
@ -105,12 +106,14 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
@Override
public void deleteSceneRule(Long id) {
// 校验存在
// 1. 校验存在
validateSceneRuleExists(id);
// 删除定时触发器
timerHandler.unregisterTimerTriggers(id);
// 删除
// 2. 删除
sceneRuleMapper.deleteById(id);
// 3. 删除定时触发器
timerHandler.unregisterTimerTriggers(id);
}
private void validateSceneRuleExists(Long id) {
@ -146,16 +149,17 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
return sceneRuleMapper.selectListByStatus(status);
}
// TODO 芋艿缓存待实现 @puhui999
// TODO @puhui999缓存待实现
@Override
@TenantIgnore // 忽略租户隔离因为 IotSceneRuleMessageHandler 调用时一般未传递租户所以需要忽略
public List<IotSceneRuleDO> getSceneRuleListByProductIdAndDeviceIdFromCache(Long productId, Long deviceId) {
// 1. 查询启用状态的规则场景
// TODO @puhui999这里查询 enable
List<IotSceneRuleDO> list = sceneRuleMapper.selectList();
// 只返回启用状态的规则场景
List<IotSceneRuleDO> enabledList = filterList(list,
sceneRule -> CommonStatusEnum.isEnable(sceneRule.getStatus()));
// 根据 productKey deviceName 进行匹配
// 2. 根据 productKey deviceName 进行匹配
return filterList(enabledList, sceneRule -> {
if (CollUtil.isEmpty(sceneRule.getTriggers())) {
return false;
@ -164,21 +168,19 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
for (IotSceneRuleDO.Trigger trigger : sceneRule.getTriggers()) {
// 检查触发器是否匹配指定的产品和设备
try {
// 1. 检查产品是否匹配
if (trigger.getProductId() == null) {
return false;
}
if (trigger.getDeviceId() == null) {
// 检查产品是否匹配
if (trigger.getProductId() == null || trigger.getDeviceId() == null) {
return false;
}
// 检查是否是全部设备的特殊标识
if (IotDeviceDO.DEVICE_ID_ALL.equals(trigger.getDeviceId())) {
return true; // 匹配所有设备
return true;
}
// 检查具体设备 ID 是否匹配
return ObjUtil.equal(productId, trigger.getProductId()) && ObjUtil.equal(deviceId, trigger.getDeviceId());
} catch (Exception e) {
log.warn("[isMatchProductAndDevice][产品({}) 设备({}) 匹配触发器异常]", productId, deviceId, e);
log.warn("[getSceneRuleListByProductIdAndDeviceIdFromCache][产品({}) 设备({}) 匹配触发器异常]",
productId, deviceId, e);
return false;
}
}
@ -188,7 +190,7 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
@Override
public void executeSceneRuleByDevice(IotDeviceMessage message) {
// TODO @芋艿这里的 tenantId通过设备获取@puhui999
// TODO @puhui999这里的 tenantId通过设备获取
TenantUtils.execute(message.getTenantId(), () -> {
// 1. 获得设备匹配的规则场景
List<IotSceneRuleDO> sceneRules = getMatchedSceneRuleListByMessage(message);
@ -234,7 +236,7 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
*/
private List<IotSceneRuleDO> getMatchedSceneRuleListByMessage(IotDeviceMessage message) {
// 1. 匹配设备
// TODO @芋艿可能需要 getSelf(); 缓存 @puhui999
// TODO 缓存 @puhui999可能需要 getSelf()
// 1.1 通过 deviceId 获取设备信息
IotDeviceDO device = deviceService.getDeviceFromCache(message.getDeviceId());
if (device == null) {
@ -293,7 +295,6 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
*/
private boolean matchSingleTrigger(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger, IotSceneRuleDO sceneRule) {
try {
// 2. 检查触发器的条件分组
return sceneRuleMatcherManager.isMatched(message, trigger) && isTriggerConditionGroupsMatched(message, trigger, sceneRule);
} catch (Exception e) {
log.error("[matchSingleTrigger][触发器匹配异常] sceneRuleId: {}, triggerType: {}, message: {}",
@ -310,18 +311,19 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
* @param sceneRule 场景规则用于日志
* @return 是否匹配
*/
private boolean isTriggerConditionGroupsMatched(IotDeviceMessage message, IotSceneRuleDO.Trigger trigger, IotSceneRuleDO sceneRule) {
// 如果没有条件分组则认为匹配成功只依赖基础触发器匹配
private boolean isTriggerConditionGroupsMatched(IotDeviceMessage message,
IotSceneRuleDO.Trigger trigger,
IotSceneRuleDO sceneRule) {
// 1. 如果没有条件分组则认为匹配成功只依赖基础触发器匹配
if (CollUtil.isEmpty(trigger.getConditionGroups())) {
return true;
}
// 检查条件分组分组与分组之间是""的关系条件与条件之间是""的关系
// 2. 检查条件分组分组与分组之间是""的关系条件与条件之间是""的关系
for (List<IotSceneRuleDO.TriggerCondition> conditionGroup : trigger.getConditionGroups()) {
if (CollUtil.isEmpty(conditionGroup)) {
continue;
}
// 检查当前分组中的所有条件是否都匹配且关系
boolean allConditionsMatched = true;
for (IotSceneRuleDO.TriggerCondition condition : conditionGroup) {
@ -330,14 +332,13 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
break;
}
}
// 如果当前分组的所有条件都匹配则整个触发器匹配成功
if (allConditionsMatched) {
return true;
}
}
// 所有分组都不匹配
// 3. 所有分组都不匹配
return false;
}
@ -372,13 +373,13 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
sceneRules.forEach(sceneRule -> {
// 2. 遍历规则场景的动作
sceneRule.getActions().forEach(actionConfig -> {
// 3.1 获取对应的动作 Action 数组
// 2.1 获取对应的动作 Action 数组
List<IotSceneRuleAction> actions = filterList(sceneRuleActions,
action -> action.getType().getType().equals(actionConfig.getType()));
if (CollUtil.isEmpty(actions)) {
return;
}
// 3.2 执行动作
// 2.2 执行动作
actions.forEach(action -> {
try {
action.execute(message, sceneRule, actionConfig);

View File

@ -14,7 +14,6 @@ import java.util.List;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
// TODO @puhui999@芋艿未测试需要场景联动开发完
/**
* IoT 告警恢复的 {@link IotSceneRuleAction} 实现类
*

View File

@ -17,7 +17,6 @@ import org.springframework.stereotype.Component;
import javax.annotation.Nullable;
import java.util.List;
// TODO @puhui999@芋艿未测试需要场景联动开发完
/**
* IoT 告警触发的 {@link IotSceneRuleAction} 实现类
*

View File

@ -4,10 +4,8 @@ import cn.iocoder.yudao.module.iot.service.rule.scene.matcher.condition.IotScene
import cn.iocoder.yudao.module.iot.service.rule.scene.matcher.trigger.IotSceneRuleTriggerMatcher;
/**
* IoT 场景规则匹配器基础接口
* <p>
* 定义所有匹配器的通用行为包括优先级名称和启用状态
* <p>
* IoT 场景规则匹配器基础接口定义所有匹配器的通用行为包括优先级名称和启用状态
*
* - {@link IotSceneRuleTriggerMatcher} 触发器匹配器
* - {@link IotSceneRuleConditionMatcher} 条件匹配器
*

View File

@ -18,10 +18,8 @@ import java.util.Map;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
/**
* IoT 场景规则匹配器工具类
* <p>
* 提供通用的条件评估逻辑和工具方法供触发器和条件匹配器使用
* <p>
* IoT 场景规则匹配器工具类提供通用的条件评估逻辑和工具方法供触发器和条件匹配器使用
*
* 该类包含了匹配器实现中常用的工具方法如条件评估参数校验日志记录等
*
* @author HUIHUI

View File

@ -16,9 +16,7 @@ import java.util.function.Function;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
/**
* IoT 场景规则匹配器统一管理器
* <p>
* 负责管理所有匹配器触发器匹配器和条件匹配器并提供统一的匹配入口
* IoT 场景规则匹配器统一管理器负责管理所有匹配器触发器匹配器和条件匹配器并提供统一的匹配入口
*
* @author HUIHUI
*/
@ -44,13 +42,12 @@ public class IotSceneRuleMatcherManager {
return;
}
// 按优先级排序并过滤启用的匹配器
// 1.1 按优先级排序并过滤启用的匹配器
List<IotSceneRuleMatcher> allMatchers = matchers.stream()
.filter(IotSceneRuleMatcher::isEnabled)
.sorted(Comparator.comparing(IotSceneRuleMatcher::getPriority))
.toList();
// 分离触发器匹配器和条件匹配器
// 1.2 分离触发器匹配器和条件匹配器
List<IotSceneRuleTriggerMatcher> triggerMatchers = allMatchers.stream()
.filter(matcher -> matcher instanceof IotSceneRuleTriggerMatcher)
.map(matcher -> (IotSceneRuleTriggerMatcher) matcher)
@ -60,7 +57,7 @@ public class IotSceneRuleMatcherManager {
.map(matcher -> (IotSceneRuleConditionMatcher) matcher)
.toList();
// 构建触发器匹配器映射表
// 2.1 构建触发器匹配器映射表
this.triggerMatchers = convertMap(triggerMatchers, IotSceneRuleTriggerMatcher::getSupportedTriggerType,
Function.identity(),
(existing, replacement) -> {
@ -70,7 +67,7 @@ public class IotSceneRuleMatcherManager {
existing.getSupportedTriggerType() : replacement.getSupportedTriggerType());
return existing.getPriority() <= replacement.getPriority() ? existing : replacement;
}, LinkedHashMap::new);
// 构建条件匹配器映射表
// 2.2 构建条件匹配器映射表
this.conditionMatchers = convertMap(conditionMatchers, IotSceneRuleConditionMatcher::getSupportedConditionType,
Function.identity(),
(existing, replacement) -> {
@ -82,7 +79,7 @@ public class IotSceneRuleMatcherManager {
},
LinkedHashMap::new);
// 日志输出初始化信息
// 3. 日志输出初始化信息
log.info("[IotSceneRuleMatcherManager][初始化完成,共加载({})个匹配器,其中触发器匹配器({})个,条件匹配器({})个]",
allMatchers.size(), this.triggerMatchers.size(), this.conditionMatchers.size());
this.triggerMatchers.forEach((type, matcher) ->
@ -135,7 +132,7 @@ public class IotSceneRuleMatcherManager {
return false;
}
// 根据条件类型查找对应的匹配器
// 1. 根据条件类型查找对应的匹配器
IotSceneRuleConditionTypeEnum conditionType = IotSceneRuleConditionTypeEnum.typeOf(condition.getType());
if (conditionType == null) {
log.warn("[isConditionMatched][conditionType({}) 未知的条件类型]", condition.getType());
@ -147,7 +144,7 @@ public class IotSceneRuleMatcherManager {
return false;
}
// 执行匹配逻辑
// 2. 执行匹配逻辑
try {
return matcher.matches(message, condition);
} catch (Exception e) {

View File

@ -16,10 +16,9 @@ import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
// TODO @puhui999是不是 IoT 的前缀都加下哈
/**
* 当前时间条件匹配器
* <p>
* 处理时间相关的子条件匹配逻辑
* 当前时间条件匹配器处理时间相关的子条件匹配逻辑
*
* @author HUIHUI
*/

View File

@ -10,9 +10,7 @@ import org.springframework.stereotype.Component;
/**
* 设备属性条件匹配器
* <p>
* 处理设备属性相关的子条件匹配逻辑
* 设备属性条件匹配器处理设备属性相关的子条件匹配逻辑
*
* @author HUIHUI
*/

View File

@ -8,9 +8,7 @@ import cn.iocoder.yudao.module.iot.service.rule.scene.matcher.IotSceneRuleMatche
import org.springframework.stereotype.Component;
/**
* 设备状态条件匹配器
* <p>
* 处理设备状态相关的子条件匹配逻辑
* 设备状态条件匹配器处理设备状态相关的子条件匹配逻辑
*
* @author HUIHUI
*/

View File

@ -6,12 +6,9 @@ import cn.iocoder.yudao.module.iot.enums.rule.IotSceneRuleConditionTypeEnum;
import cn.iocoder.yudao.module.iot.service.rule.scene.matcher.IotSceneRuleMatcher;
/**
* IoT 场景规则条件匹配器接口
* <p>
* 专门处理子条件的匹配逻辑如设备状态属性值时间条件等
* <p>
* 条件匹配器负责判断设备消息是否满足场景规则的附加条件
* 在触发器匹配成功后进行进一步的条件筛选
* IoT 场景规则条件匹配器接口专门处理子条件的匹配逻辑如设备状态属性值时间条件等
*
* 条件匹配器负责判断设备消息是否满足场景规则的附加条件在触发器匹配成功后进行进一步的条件筛选
*
* @author HUIHUI
*/

View File

@ -10,9 +10,7 @@ import cn.iocoder.yudao.module.iot.service.rule.scene.matcher.IotSceneRuleMatche
import org.springframework.stereotype.Component;
/**
* 设备事件上报触发器匹配器
* <p>
* 处理设备事件上报的触发器匹配逻辑
* 设备事件上报触发器匹配器处理设备事件上报的触发器匹配逻辑
*
* @author HUIHUI
*/

View File

@ -9,9 +9,7 @@ import cn.iocoder.yudao.module.iot.service.rule.scene.matcher.IotSceneRuleMatche
import org.springframework.stereotype.Component;
/**
* 设备属性上报触发器匹配器
* <p>
* 处理设备属性数据上报的触发器匹配逻辑
* 设备属性上报触发器匹配器处理设备属性数据上报的触发器匹配逻辑
*
* @author HUIHUI
*/

View File

@ -9,9 +9,7 @@ import cn.iocoder.yudao.module.iot.service.rule.scene.matcher.IotSceneRuleMatche
import org.springframework.stereotype.Component;
/**
* 设备服务调用触发器匹配器
* <p>
* 处理设备服务调用的触发器匹配逻辑
* 设备服务调用触发器匹配器处理设备服务调用的触发器匹配逻辑
*
* @author HUIHUI
*/

View File

@ -9,9 +9,7 @@ import cn.iocoder.yudao.module.iot.service.rule.scene.matcher.IotSceneRuleMatche
import org.springframework.stereotype.Component;
/**
* 设备状态更新触发器匹配器
* <p>
* 处理设备上下线状态变更的触发器匹配逻辑
* 设备状态更新触发器匹配器处理设备上下线状态变更的触发器匹配逻辑
*
* @author HUIHUI
*/

View File

@ -6,12 +6,9 @@ import cn.iocoder.yudao.module.iot.enums.rule.IotSceneRuleTriggerTypeEnum;
import cn.iocoder.yudao.module.iot.service.rule.scene.matcher.IotSceneRuleMatcher;
/**
* IoT 场景规则触发器匹配器接口
* <p>
* 专门处理主触发条件的匹配逻辑如设备消息类型定时器等
* <p>
* 触发器匹配器负责判断设备消息是否满足场景规则的主触发条件
* 是场景规则执行的第一道门槛
* IoT 场景规则触发器匹配器接口专门处理主触发条件的匹配逻辑如设备消息类型定时器等
*
* 触发器匹配器负责判断设备消息是否满足场景规则的主触发条件是场景规则执行的第一道门槛
*
* @author HUIHUI
*/

View File

@ -9,9 +9,8 @@ import org.quartz.CronExpression;
import org.springframework.stereotype.Component;
/**
* 定时触发器匹配器
* <p>
* 处理定时触发的触发器匹配逻辑
* 定时触发器匹配器处理定时触发的触发器匹配逻辑
*
* 注意定时触发器不依赖设备消息主要用于定时任务场景
*
* @author HUIHUI

View File

@ -18,9 +18,7 @@ import java.util.List;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
/**
* IoT 场景规则定时触发器处理器
* <p>
* 负责管理定时触发器的注册更新删除等操作
* IoT 场景规则定时触发器处理器负责管理定时触发器的注册更新删除等操作
*
* @author HUIHUI
*/
@ -37,19 +35,17 @@ public class IotSceneRuleTimerHandler {
* @param sceneRule 场景规则
*/
public void registerTimerTriggers(IotSceneRuleDO sceneRule) {
// 1. 过滤出定时触发器
if (sceneRule == null || CollUtil.isEmpty(sceneRule.getTriggers())) {
return;
}
// 过滤出定时触发器
List<IotSceneRuleDO.Trigger> timerTriggers = filterList(sceneRule.getTriggers(),
trigger -> ObjUtil.equals(trigger.getType(), IotSceneRuleTriggerTypeEnum.TIMER.getType()));
if (CollUtil.isEmpty(timerTriggers)) {
return;
}
// 注册每个定时触发器
// 2. 注册每个定时触发器
timerTriggers.forEach(trigger -> registerSingleTimerTrigger(sceneRule, trigger));
}
@ -63,23 +59,23 @@ public class IotSceneRuleTimerHandler {
return;
}
// 先删除旧的定时任务
// 1. 先删除旧的定时任务
unregisterTimerTriggers(sceneRule.getId());
// 如果场景规则已禁用则不重新注册
// 2.1 如果场景规则已禁用则不重新注册
if (CommonStatusEnum.isDisable(sceneRule.getStatus())) {
log.info("[updateTimerTriggers][场景规则({}) 已禁用,不注册定时触发器]", sceneRule.getId());
return;
}
// 重新注册定时触发器
// 2.2 重新注册定时触发器
registerTimerTriggers(sceneRule);
}
/**
* 注销场景规则的定时触发器
*
* @param sceneRuleId 场景规则ID
* @param sceneRuleId 场景规则 ID
*/
public void unregisterTimerTriggers(Long sceneRuleId) {
if (sceneRuleId == null) {
@ -98,7 +94,7 @@ public class IotSceneRuleTimerHandler {
/**
* 暂停场景规则的定时触发器
*
* @param sceneRuleId 场景规则ID
* @param sceneRuleId 场景规则 ID
*/
public void pauseTimerTriggers(Long sceneRuleId) {
if (sceneRuleId == null) {
@ -114,25 +110,6 @@ public class IotSceneRuleTimerHandler {
}
}
/**
* 恢复场景规则的定时触发器
*
* @param sceneRuleId 场景规则ID
*/
public void resumeTimerTriggers(Long sceneRuleId) {
if (sceneRuleId == null) {
return;
}
String jobName = buildJobName(sceneRuleId);
try {
schedulerManager.resumeJob(jobName);
log.info("[resumeTimerTriggers][场景规则({}) 定时触发器恢复成功]", sceneRuleId);
} catch (SchedulerException e) {
log.error("[resumeTimerTriggers][场景规则({}) 定时触发器恢复失败]", sceneRuleId, e);
}
}
/**
* 注册单个定时触发器
*
@ -146,18 +123,16 @@ public class IotSceneRuleTimerHandler {
return;
}
// 2. 构建任务名称和数据
String jobName = buildJobName(sceneRule.getId());
try {
// 3. 注册定时任务
// 2.1 构建任务名称和数据
String jobName = buildJobName(sceneRule.getId());
// 2.2 注册定时任务
schedulerManager.addOrUpdateJob(
IotSceneRuleJob.class,
jobName,
trigger.getCronExpression(),
IotSceneRuleJob.buildJobDataMap(sceneRule.getId())
);
log.info("[registerSingleTimerTrigger][场景规则({}) 定时触发器注册成功CRON: {}]",
sceneRule.getId(), trigger.getCronExpression());
} catch (SchedulerException e) {
@ -169,10 +144,11 @@ public class IotSceneRuleTimerHandler {
/**
* 构建任务名称
*
* @param sceneRuleId 场景规则ID
* @param sceneRuleId 场景规则 ID
* @return 任务名称
*/
private String buildJobName(Long sceneRuleId) {
return "iot_scene_rule_timer_" + sceneRuleId;
}
}

View File

@ -6,6 +6,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
// TODO @puhui999建议改成 IotBaseConditionMatcherTest
/**
* Matcher 测试基类
* 提供通用的 Spring 测试配置