Commit 887bf175affa1c88ebad5870f87a3ade899bd259

Authored by YunaiV
1 parent 7d911760

review:【iot 物联网】场景联动相关实现

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/enums/rule/IotSceneRuleTriggerTypeEnum.java
@@ -64,17 +64,5 @@ public enum IotSceneRuleTriggerTypeEnum implements ArrayValuable<Integer> { @@ -64,17 +64,5 @@ public enum IotSceneRuleTriggerTypeEnum implements ArrayValuable<Integer> {
64 public static IotSceneRuleTriggerTypeEnum typeOf(Integer type) { 64 public static IotSceneRuleTriggerTypeEnum typeOf(Integer type) {
65 return ArrayUtil.firstMatch(item -> item.getType().equals(type), values()); 65 return ArrayUtil.firstMatch(item -> item.getType().equals(type), values());
66 } 66 }
67 - /**  
68 - * 根据类型值查找触发器类型枚举  
69 - *  
70 - * @param typeValue 类型值  
71 - * @return 触发器类型枚举  
72 - */  
73 - public static IotSceneRuleTriggerTypeEnum findTriggerTypeEnum(Integer typeValue) {  
74 - return Arrays.stream(IotSceneRuleTriggerTypeEnum.values())  
75 - .filter(type -> type.getType().equals(typeValue))  
76 - .findFirst()  
77 - .orElse(null);  
78 - }  
79 67
80 } 68 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/scene/matcher/IotSceneRuleMatcherManager.java
@@ -29,21 +29,18 @@ public class IotSceneRuleMatcherManager { @@ -29,21 +29,18 @@ public class IotSceneRuleMatcherManager {
29 29
30 /** 30 /**
31 * 触发器匹配器映射表 31 * 触发器匹配器映射表
32 - * Key: 触发器类型枚举  
33 - * Value: 对应的匹配器实例  
34 */ 32 */
35 private final Map<IotSceneRuleTriggerTypeEnum, IotSceneRuleTriggerMatcher> triggerMatchers; 33 private final Map<IotSceneRuleTriggerTypeEnum, IotSceneRuleTriggerMatcher> triggerMatchers;
36 34
37 /** 35 /**
38 * 条件匹配器映射表 36 * 条件匹配器映射表
39 - * Key: 条件类型枚举  
40 - * Value: 对应的匹配器实例  
41 */ 37 */
42 private final Map<IotSceneRuleConditionTypeEnum, IotSceneRuleConditionMatcher> conditionMatchers; 38 private final Map<IotSceneRuleConditionTypeEnum, IotSceneRuleConditionMatcher> conditionMatchers;
43 39
44 /** 40 /**
45 * 所有匹配器列表(按优先级排序) 41 * 所有匹配器列表(按优先级排序)
46 */ 42 */
  43 + // TODO @puhui999:貌似 local variable 也可以
47 private final List<IotSceneRuleMatcher> allMatchers; 44 private final List<IotSceneRuleMatcher> allMatchers;
48 45
49 public IotSceneRuleMatcherManager(List<IotSceneRuleMatcher> matchers) { 46 public IotSceneRuleMatcherManager(List<IotSceneRuleMatcher> matchers) {
@@ -152,13 +149,13 @@ public class IotSceneRuleMatcherManager { @@ -152,13 +149,13 @@ public class IotSceneRuleMatcherManager {
152 log.warn("[isConditionMatched][conditionType({}) 未知的条件类型]", condition.getType()); 149 log.warn("[isConditionMatched][conditionType({}) 未知的条件类型]", condition.getType());
153 return false; 150 return false;
154 } 151 }
155 -  
156 IotSceneRuleConditionMatcher matcher = conditionMatchers.get(conditionType); 152 IotSceneRuleConditionMatcher matcher = conditionMatchers.get(conditionType);
157 if (matcher == null) { 153 if (matcher == null) {
158 log.warn("[isConditionMatched][conditionType({}) 没有对应的匹配器]", conditionType); 154 log.warn("[isConditionMatched][conditionType({}) 没有对应的匹配器]", conditionType);
159 return false; 155 return false;
160 } 156 }
161 157
  158 + // 执行匹配逻辑
162 try { 159 try {
163 return matcher.isMatched(message, condition); 160 return matcher.isMatched(message, condition);
164 } catch (Exception e) { 161 } catch (Exception e) {
@@ -174,12 +171,15 @@ public class IotSceneRuleMatcherManager { @@ -174,12 +171,15 @@ public class IotSceneRuleMatcherManager {
174 * @return 条件类型枚举 171 * @return 条件类型枚举
175 */ 172 */
176 private IotSceneRuleConditionTypeEnum findConditionTypeEnum(Integer typeValue) { 173 private IotSceneRuleConditionTypeEnum findConditionTypeEnum(Integer typeValue) {
  174 + // TODO @puhui999:是不是搞到枚举类里?
177 return Arrays.stream(IotSceneRuleConditionTypeEnum.values()) 175 return Arrays.stream(IotSceneRuleConditionTypeEnum.values())
178 .filter(type -> type.getType().equals(typeValue)) 176 .filter(type -> type.getType().equals(typeValue))
179 .findFirst() 177 .findFirst()
180 .orElse(null); 178 .orElse(null);
181 } 179 }
182 180
  181 + // TODO @puhui999:下面两个方法,是不是也可以删除哈?
  182 +
183 /** 183 /**
184 * 获取所有支持的触发器类型 184 * 获取所有支持的触发器类型
185 * 185 *
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/scene/matcher/condition/CurrentTimeConditionMatcher.java
@@ -123,6 +123,7 @@ public class CurrentTimeConditionMatcher implements IotSceneRuleConditionMatcher @@ -123,6 +123,7 @@ public class CurrentTimeConditionMatcher implements IotSceneRuleConditionMatcher
123 isDateTimeOperator(operatorEnum); 123 isDateTimeOperator(operatorEnum);
124 } 124 }
125 125
  126 + // TODO @puhui999:switch 兼容下 jdk8
126 /** 127 /**
127 * 匹配日期时间(时间戳) 128 * 匹配日期时间(时间戳)
128 * 直接实现时间戳比较逻辑 129 * 直接实现时间戳比较逻辑
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/scene/matcher/condition/DevicePropertyConditionMatcher.java
@@ -22,6 +22,7 @@ public class DevicePropertyConditionMatcher implements IotSceneRuleConditionMatc @@ -22,6 +22,7 @@ public class DevicePropertyConditionMatcher implements IotSceneRuleConditionMatc
22 return IotSceneRuleConditionTypeEnum.DEVICE_PROPERTY; 22 return IotSceneRuleConditionTypeEnum.DEVICE_PROPERTY;
23 } 23 }
24 24
  25 + // TODO @puhui999:matches 会不会更好?参考的 org.hamcrest.Matcher jdk 接口
25 @Override 26 @Override
26 public boolean isMatched(IotDeviceMessage message, IotSceneRuleDO.TriggerCondition condition) { 27 public boolean isMatched(IotDeviceMessage message, IotSceneRuleDO.TriggerCondition condition) {
27 // 1.1 基础参数校验 28 // 1.1 基础参数校验
yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/rule/scene/matcher/trigger/DeviceServiceInvokeTriggerMatcherTest.java
@@ -31,6 +31,7 @@ public class DeviceServiceInvokeTriggerMatcherTest extends BaseMockitoUnitTest { @@ -31,6 +31,7 @@ public class DeviceServiceInvokeTriggerMatcherTest extends BaseMockitoUnitTest {
31 @Test 31 @Test
32 public void testGetSupportedTriggerType() { 32 public void testGetSupportedTriggerType() {
33 // when & then 33 // when & then
  34 + // TODO @puhui999:单测按照现有项目的注释风格哈;类似 // 调用;// 断言
34 assertEquals(IotSceneRuleTriggerTypeEnum.DEVICE_SERVICE_INVOKE, matcher.getSupportedTriggerType()); 35 assertEquals(IotSceneRuleTriggerTypeEnum.DEVICE_SERVICE_INVOKE, matcher.getSupportedTriggerType());
35 } 36 }
36 37