Commit 9e675fcc970dc6d333f94df8c3b80ced7911d1ee
1 parent
c276e67d
refactor: 场景联动逻辑调整
1、触发器内部逻辑为或 2、执行条件的逻辑为且 3、触发器和执行条件的逻辑为且
Showing
26 changed files
with
142 additions
and
86 deletions
... | ... | @@ -17,8 +17,8 @@ import org.thingsboard.server.common.data.yunteng.common.DeleteGroup; |
17 | 17 | import org.thingsboard.server.common.data.yunteng.common.UpdateGroup; |
18 | 18 | import org.thingsboard.server.common.data.yunteng.dto.DeleteDTO; |
19 | 19 | import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO; |
20 | -import org.thingsboard.server.common.data.yunteng.dto.SceneLinkageDTO; | |
21 | -import org.thingsboard.server.common.data.yunteng.dto.TriggerDTO; | |
20 | +import org.thingsboard.server.common.data.yunteng.dto.scene.SceneLinkageDTO; | |
21 | +import org.thingsboard.server.common.data.yunteng.dto.scene.TriggerDTO; | |
22 | 22 | import org.thingsboard.server.common.data.yunteng.enums.DeviceTypeEnum; |
23 | 23 | import org.thingsboard.server.common.data.yunteng.id.SceneId; |
24 | 24 | import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData; | ... | ... |
... | ... | @@ -3,6 +3,7 @@ package org.thingsboard.server.common.data.yunteng.dto; |
3 | 3 | import io.swagger.annotations.ApiModelProperty; |
4 | 4 | import lombok.Data; |
5 | 5 | import org.thingsboard.server.common.data.alarm.AlarmSeverity; |
6 | +import org.thingsboard.server.common.data.yunteng.dto.scene.TriggerDTO; | |
6 | 7 | |
7 | 8 | import java.util.List; |
8 | 9 | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/yunteng/dto/scene/DoActionDTO.java
renamed from
common/data/src/main/java/org/thingsboard/server/common/data/yunteng/dto/DoActionDTO.java
1 | -package org.thingsboard.server.common.data.yunteng.dto; | |
1 | +package org.thingsboard.server.common.data.yunteng.dto.scene; | |
2 | 2 | |
3 | 3 | import com.fasterxml.jackson.databind.JsonNode; |
4 | 4 | import io.swagger.annotations.ApiModelProperty; |
5 | 5 | import lombok.Data; |
6 | 6 | import lombok.EqualsAndHashCode; |
7 | +import org.thingsboard.server.common.data.yunteng.dto.TenantDTO; | |
7 | 8 | import org.thingsboard.server.common.data.yunteng.enums.ActionTypeEnum; |
8 | 9 | import org.thingsboard.server.common.data.yunteng.enums.CallTypeEnum; |
9 | 10 | import org.thingsboard.server.common.data.yunteng.enums.DeviceTypeEnum; | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/yunteng/dto/scene/DoConditionDTO.java
renamed from
common/data/src/main/java/org/thingsboard/server/common/data/yunteng/dto/DoConditionDTO.java
1 | -package org.thingsboard.server.common.data.yunteng.dto; | |
1 | +package org.thingsboard.server.common.data.yunteng.dto.scene; | |
2 | 2 | |
3 | 3 | import io.swagger.annotations.ApiModelProperty; |
4 | +import java.util.List; | |
4 | 5 | import lombok.Data; |
5 | 6 | import lombok.EqualsAndHashCode; |
6 | -import org.thingsboard.server.common.data.device.profile.AlarmCondition; | |
7 | 7 | import org.thingsboard.server.common.data.device.profile.AlarmRule; |
8 | +import org.thingsboard.server.common.data.yunteng.dto.TenantDTO; | |
8 | 9 | import org.thingsboard.server.common.data.yunteng.enums.DeviceTypeEnum; |
9 | 10 | import org.thingsboard.server.common.data.yunteng.enums.ScopeEnum; |
10 | 11 | import org.thingsboard.server.common.data.yunteng.enums.TriggerTypeEnum; |
11 | 12 | |
12 | -import java.util.List; | |
13 | - | |
14 | 13 | /** |
15 | 14 | * @Description 场景联动执行条件数据传输表 @Author cxy @Date 2021/11/24 17:33 |
16 | 15 | */ | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/yunteng/dto/scene/RuleFilterDTO.java
0 → 100644
1 | +package org.thingsboard.server.common.data.yunteng.dto.scene; | |
2 | + | |
3 | +import com.fasterxml.jackson.databind.JsonNode; | |
4 | +import io.swagger.annotations.ApiModelProperty; | |
5 | + | |
6 | +import java.io.Serializable; | |
7 | +import java.util.List; | |
8 | +import lombok.Data; | |
9 | +import lombok.EqualsAndHashCode; | |
10 | +import org.thingsboard.server.common.data.yunteng.dto.TenantDTO; | |
11 | +import org.thingsboard.server.common.data.yunteng.enums.ActionTypeEnum; | |
12 | +import org.thingsboard.server.common.data.yunteng.enums.CallTypeEnum; | |
13 | +import org.thingsboard.server.common.data.yunteng.enums.DeviceTypeEnum; | |
14 | +import org.thingsboard.server.common.data.yunteng.enums.ScopeEnum; | |
15 | + | |
16 | +/** | |
17 | + * @Description 场景联动执行动作数据传输表 @Author cxy @Date 2021/11/24 17:32 | |
18 | + */ | |
19 | +@Data | |
20 | +public class RuleFilterDTO implements Serializable { | |
21 | + | |
22 | + @ApiModelProperty(value = "触发条件ID") | |
23 | + private String ruleId; | |
24 | + | |
25 | + @ApiModelProperty(value = "场景联动ID") | |
26 | + private String scenId; | |
27 | + | |
28 | +} | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/yunteng/dto/scene/SceneLinkageDTO.java
renamed from
common/data/src/main/java/org/thingsboard/server/common/data/yunteng/dto/SceneLinkageDTO.java
1 | -package org.thingsboard.server.common.data.yunteng.dto; | |
1 | +package org.thingsboard.server.common.data.yunteng.dto.scene; | |
2 | 2 | |
3 | 3 | import io.swagger.annotations.ApiModelProperty; |
4 | 4 | import lombok.Data; |
5 | 5 | import lombok.EqualsAndHashCode; |
6 | 6 | import org.thingsboard.server.common.data.HasName; |
7 | 7 | import org.thingsboard.server.common.data.yunteng.common.AddGroup; |
8 | +import org.thingsboard.server.common.data.yunteng.dto.TenantDTO; | |
8 | 9 | |
9 | 10 | import javax.validation.constraints.NotEmpty; |
10 | 11 | import java.util.List; | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/yunteng/dto/scene/TriggerDTO.java
renamed from
common/data/src/main/java/org/thingsboard/server/common/data/yunteng/dto/TriggerDTO.java
1 | -package org.thingsboard.server.common.data.yunteng.dto; | |
1 | +package org.thingsboard.server.common.data.yunteng.dto.scene; | |
2 | 2 | |
3 | 3 | import io.swagger.annotations.ApiModelProperty; |
4 | 4 | import lombok.Data; |
5 | 5 | import lombok.EqualsAndHashCode; |
6 | 6 | import org.thingsboard.server.common.data.device.profile.AlarmRule; |
7 | +import org.thingsboard.server.common.data.yunteng.dto.TenantDTO; | |
7 | 8 | import org.thingsboard.server.common.data.yunteng.enums.DeviceTypeEnum; |
8 | 9 | import org.thingsboard.server.common.data.yunteng.enums.ScopeEnum; |
9 | 10 | import org.thingsboard.server.common.data.yunteng.enums.TriggerTypeEnum; | ... | ... |
... | ... | @@ -13,6 +13,7 @@ import org.thingsboard.server.common.data.yunteng.core.exception.NoneTenantAsset |
13 | 13 | import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidationException; |
14 | 14 | import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; |
15 | 15 | import org.thingsboard.server.common.data.yunteng.dto.*; |
16 | +import org.thingsboard.server.common.data.yunteng.dto.scene.DoActionDTO; | |
16 | 17 | import org.thingsboard.server.common.data.yunteng.utils.ReflectUtils; |
17 | 18 | import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData; |
18 | 19 | import org.thingsboard.server.dao.yunteng.entities.TkAlarmProfileEntity; | ... | ... |
... | ... | @@ -7,7 +7,7 @@ import java.util.Set; |
7 | 7 | import java.util.stream.Collectors; |
8 | 8 | import lombok.RequiredArgsConstructor; |
9 | 9 | import org.springframework.stereotype.Service; |
10 | -import org.thingsboard.server.common.data.yunteng.dto.DoActionDTO; | |
10 | +import org.thingsboard.server.common.data.yunteng.dto.scene.DoActionDTO; | |
11 | 11 | import org.thingsboard.server.common.data.yunteng.enums.ScopeEnum; |
12 | 12 | import org.thingsboard.server.dao.yunteng.entities.TenantBaseEntity; |
13 | 13 | import org.thingsboard.server.dao.yunteng.entities.TkDeviceEntity; | ... | ... |
... | ... | @@ -18,6 +18,7 @@ import org.thingsboard.server.common.data.yunteng.constant.ModelConstants; |
18 | 18 | import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidationException; |
19 | 19 | import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; |
20 | 20 | import org.thingsboard.server.common.data.yunteng.dto.*; |
21 | +import org.thingsboard.server.common.data.yunteng.dto.scene.*; | |
21 | 22 | import org.thingsboard.server.common.data.yunteng.enums.ActionTypeEnum; |
22 | 23 | import org.thingsboard.server.common.data.yunteng.enums.DeviceTypeEnum; |
23 | 24 | import org.thingsboard.server.common.data.yunteng.enums.ScopeEnum; |
... | ... | @@ -495,9 +496,10 @@ public class TkSceneLinkageServiceImpl |
495 | 496 | return null; |
496 | 497 | } |
497 | 498 | |
498 | - Map<String, List<String>> matchedDevices = new HashMap<>(); | |
499 | - Map<String, List<String>> matchedProjectes = new HashMap<>(); | |
499 | + Map<String, List<RuleFilterDTO>> matchedDevices = new HashMap<>(); | |
500 | + Map<String, List<RuleFilterDTO>> matchedProjectes = new HashMap<>(); | |
500 | 501 | |
502 | + Set<String> projectFilter = new HashSet<>(); | |
501 | 503 | List<TkTriggerEntity> triggers = |
502 | 504 | triggerMapper.selectList( |
503 | 505 | new QueryWrapper<TkTriggerEntity>() |
... | ... | @@ -507,9 +509,11 @@ public class TkSceneLinkageServiceImpl |
507 | 509 | .in(TkTriggerEntity::getSceneLinkageId, enableIds)); |
508 | 510 | triggers.forEach( |
509 | 511 | trigger -> { |
512 | + projectFilter.add(trigger.getDeviceProfileId()); | |
510 | 513 | deviceSceneMap( |
511 | 514 | matchedProjectes, |
512 | 515 | matchedDevices, |
516 | + trigger.getId(), | |
513 | 517 | trigger.getSceneLinkageId(), |
514 | 518 | trigger.getEntityType(), |
515 | 519 | trigger.getDeviceProfileId(), |
... | ... | @@ -525,9 +529,11 @@ public class TkSceneLinkageServiceImpl |
525 | 529 | .in(TkDoConditionEntity::getSceneLinkageId, enableIds)); |
526 | 530 | conditions.forEach( |
527 | 531 | condition -> { |
532 | + projectFilter.add(condition.getDeviceProfileId()); | |
528 | 533 | deviceSceneMap( |
529 | 534 | matchedProjectes, |
530 | 535 | matchedDevices, |
536 | + condition.getId(), | |
531 | 537 | condition.getSceneLinkageId(), |
532 | 538 | condition.getEntityType(), |
533 | 539 | condition.getDeviceProfileId(), |
... | ... | @@ -538,10 +544,12 @@ public class TkSceneLinkageServiceImpl |
538 | 544 | return null; |
539 | 545 | } |
540 | 546 | |
547 | + | |
548 | + | |
541 | 549 | List<TkDeviceProfileEntity> profiles = |
542 | 550 | profileMapper.selectList( |
543 | 551 | new LambdaQueryWrapper<TkDeviceProfileEntity>() |
544 | - .eq(TkDeviceProfileEntity::getTenantId, tenantId)); | |
552 | + .eq(TkDeviceProfileEntity::getTenantId, tenantId).in(TkDeviceProfileEntity::getId, projectFilter)); | |
545 | 553 | Map<String, String> projects = new HashMap<>(); |
546 | 554 | profiles.forEach(f -> projects.put(f.getTbProfileId(), f.getId())); |
547 | 555 | |
... | ... | @@ -556,8 +564,8 @@ public class TkSceneLinkageServiceImpl |
556 | 564 | } |
557 | 565 | |
558 | 566 | private void deviceSceneMap( |
559 | - Map<String, List<String>> matchedProjectes, | |
560 | - Map<String, List<String>> matchedDevices, | |
567 | + Map<String, List<RuleFilterDTO>> matchedProjectes, | |
568 | + Map<String, List<RuleFilterDTO>> matchedDevices,String ruleId, | |
561 | 569 | String scenId, |
562 | 570 | ScopeEnum scope, |
563 | 571 | String profileId, |
... | ... | @@ -565,9 +573,9 @@ public class TkSceneLinkageServiceImpl |
565 | 573 | if (ScopeEnum.ALL.equals(scope)) { |
566 | 574 | List<String> profileIds = new ArrayList<>(); |
567 | 575 | profileIds.add(profileId); |
568 | - deviceSceneMap(matchedProjectes, profileIds, scenId); | |
576 | + deviceSceneMap(matchedProjectes, profileIds,ruleId, scenId); | |
569 | 577 | } else { |
570 | - deviceSceneMap(matchedDevices, deviceIds, scenId); | |
578 | + deviceSceneMap(matchedDevices, deviceIds,ruleId, scenId); | |
571 | 579 | } |
572 | 580 | } |
573 | 581 | |
... | ... | @@ -575,15 +583,18 @@ public class TkSceneLinkageServiceImpl |
575 | 583 | * 设备与场景联动的映射集合 |
576 | 584 | * |
577 | 585 | * @param resultMap 缓存设备和场景联动映射结果的集合 |
578 | - * @param triggerIds 设备或产品主键集合 | |
586 | + * @param postfixIds 设备或产品主键集合 | |
579 | 587 | * @param scenId 场景联动主键 |
580 | 588 | */ |
581 | 589 | private void deviceSceneMap( |
582 | - Map<String, List<String>> resultMap, List<String> triggerIds, String scenId) { | |
583 | - for (String deviceId : triggerIds) { | |
584 | - List<String> scenes = resultMap.computeIfAbsent(deviceId, k -> new ArrayList<String>()); | |
585 | - if (!scenes.contains(scenId)) { | |
586 | - scenes.add(scenId); | |
590 | + Map<String, List<RuleFilterDTO>> resultMap, List<String> postfixIds, String prefixId, String scenId) { | |
591 | + for (String deviceId : postfixIds) { | |
592 | + List<RuleFilterDTO> scenes = resultMap.computeIfAbsent(deviceId, k -> new ArrayList<RuleFilterDTO>()); | |
593 | + RuleFilterDTO filter = new RuleFilterDTO(); | |
594 | + filter.setRuleId(prefixId); | |
595 | + filter.setScenId(scenId); | |
596 | + if (!scenes.contains(filter)) { | |
597 | + scenes.add(filter); | |
587 | 598 | } |
588 | 599 | resultMap.put(deviceId, scenes); |
589 | 600 | } | ... | ... |
... | ... | @@ -4,7 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
4 | 4 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
5 | 5 | import lombok.RequiredArgsConstructor; |
6 | 6 | import org.springframework.stereotype.Service; |
7 | -import org.thingsboard.server.common.data.yunteng.dto.TriggerDTO; | |
7 | +import org.thingsboard.server.common.data.yunteng.dto.scene.TriggerDTO; | |
8 | 8 | import org.thingsboard.server.dao.yunteng.entities.TkTriggerEntity; |
9 | 9 | import org.thingsboard.server.dao.yunteng.mapper.TriggerMapper; |
10 | 10 | import org.thingsboard.server.dao.yunteng.service.AbstractBaseService; | ... | ... |
... | ... | @@ -3,7 +3,7 @@ package org.thingsboard.server.dao.yunteng.mapper; |
3 | 3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
4 | 4 | import org.apache.ibatis.annotations.Mapper; |
5 | 5 | import org.apache.ibatis.annotations.Param; |
6 | -import org.thingsboard.server.common.data.yunteng.dto.DoActionDTO; | |
6 | +import org.thingsboard.server.common.data.yunteng.dto.scene.DoActionDTO; | |
7 | 7 | import org.thingsboard.server.dao.yunteng.entities.TkDoActionEntity; |
8 | 8 | |
9 | 9 | import java.util.List; | ... | ... |
... | ... | @@ -3,7 +3,7 @@ package org.thingsboard.server.dao.yunteng.mapper; |
3 | 3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
4 | 4 | import org.apache.ibatis.annotations.Mapper; |
5 | 5 | import org.apache.ibatis.annotations.Param; |
6 | -import org.thingsboard.server.common.data.yunteng.dto.DoConditionDTO; | |
6 | +import org.thingsboard.server.common.data.yunteng.dto.scene.DoConditionDTO; | |
7 | 7 | import org.thingsboard.server.dao.yunteng.entities.TkDoConditionEntity; |
8 | 8 | |
9 | 9 | import java.util.List; | ... | ... |
... | ... | @@ -4,7 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
4 | 4 | import com.baomidou.mybatisplus.core.metadata.IPage; |
5 | 5 | import org.apache.ibatis.annotations.Mapper; |
6 | 6 | import org.apache.ibatis.annotations.Param; |
7 | -import org.thingsboard.server.common.data.yunteng.dto.SceneLinkageDTO; | |
7 | +import org.thingsboard.server.common.data.yunteng.dto.scene.SceneLinkageDTO; | |
8 | 8 | import org.thingsboard.server.dao.yunteng.entities.TkSceneLinkageEntity; |
9 | 9 | |
10 | 10 | import java.util.Map; | ... | ... |
... | ... | @@ -3,7 +3,7 @@ package org.thingsboard.server.dao.yunteng.mapper; |
3 | 3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
4 | 4 | import org.apache.ibatis.annotations.Mapper; |
5 | 5 | import org.apache.ibatis.annotations.Param; |
6 | -import org.thingsboard.server.common.data.yunteng.dto.TriggerDTO; | |
6 | +import org.thingsboard.server.common.data.yunteng.dto.scene.TriggerDTO; | |
7 | 7 | import org.thingsboard.server.dao.yunteng.entities.TkTriggerEntity; |
8 | 8 | |
9 | 9 | import java.util.List; | ... | ... |
1 | 1 | package org.thingsboard.server.dao.yunteng.service; |
2 | 2 | |
3 | -import org.thingsboard.server.common.data.yunteng.dto.DoActionDTO; | |
3 | +import org.thingsboard.server.common.data.yunteng.dto.scene.DoActionDTO; | |
4 | 4 | import org.thingsboard.server.dao.yunteng.entities.TkDoActionEntity; |
5 | 5 | |
6 | 6 | import java.util.List; | ... | ... |
1 | 1 | package org.thingsboard.server.dao.yunteng.service; |
2 | 2 | import com.fasterxml.jackson.databind.JsonNode; |
3 | 3 | import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO; |
4 | -import org.thingsboard.server.common.data.yunteng.dto.SceneLinkageDTO; | |
4 | +import org.thingsboard.server.common.data.yunteng.dto.scene.SceneLinkageDTO; | |
5 | 5 | import org.thingsboard.server.common.data.yunteng.enums.DeviceTypeEnum; |
6 | 6 | import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData; |
7 | 7 | import org.thingsboard.server.dao.yunteng.entities.TkSceneLinkageEntity; | ... | ... |
1 | 1 | package org.thingsboard.server.dao.yunteng.service; |
2 | 2 | |
3 | -import org.thingsboard.server.common.data.yunteng.dto.TriggerDTO; | |
3 | +import org.thingsboard.server.common.data.yunteng.dto.scene.TriggerDTO; | |
4 | 4 | import org.thingsboard.server.dao.yunteng.entities.TkTriggerEntity; |
5 | 5 | |
6 | 6 | import java.util.List; | ... | ... |
... | ... | @@ -2,7 +2,7 @@ |
2 | 2 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
3 | 3 | |
4 | 4 | <mapper namespace="org.thingsboard.server.dao.yunteng.mapper.DoActionMapper"> |
5 | - <resultMap id="actionDTO" type="org.thingsboard.server.common.data.yunteng.dto.DoActionDTO"> | |
5 | + <resultMap id="actionDTO" type="org.thingsboard.server.common.data.yunteng.dto.scene.DoActionDTO"> | |
6 | 6 | <result property="id" column="id"/> |
7 | 7 | <result property="deviceId" column="device_id" |
8 | 8 | typeHandler="org.thingsboard.server.dao.yunteng.mapper.ListStringTypeHandler"/> | ... | ... |
... | ... | @@ -2,7 +2,7 @@ |
2 | 2 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
3 | 3 | |
4 | 4 | <mapper namespace="org.thingsboard.server.dao.yunteng.mapper.DoConditionMapper"> |
5 | - <resultMap id="conditionDTO" type="org.thingsboard.server.common.data.yunteng.dto.DoConditionDTO"> | |
5 | + <resultMap id="conditionDTO" type="org.thingsboard.server.common.data.yunteng.dto.scene.DoConditionDTO"> | |
6 | 6 | <result property="id" column="id"/> |
7 | 7 | <result property="entityId" column="entity_id" |
8 | 8 | typeHandler="org.thingsboard.server.dao.yunteng.mapper.ListStringTypeHandler"/> | ... | ... |
... | ... | @@ -2,7 +2,7 @@ |
2 | 2 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
3 | 3 | |
4 | 4 | <mapper namespace="org.thingsboard.server.dao.yunteng.mapper.SceneLinkageMapper"> |
5 | - <resultMap id="sceneLinkageMap" type="org.thingsboard.server.common.data.yunteng.dto.SceneLinkageDTO"> | |
5 | + <resultMap id="sceneLinkageMap" type="org.thingsboard.server.common.data.yunteng.dto.scene.SceneLinkageDTO"> | |
6 | 6 | <result property="id" column="id"/> |
7 | 7 | <result property="name" column="name"/> |
8 | 8 | <result property="organizationId" column="organization_id"/> | ... | ... |
... | ... | @@ -2,7 +2,7 @@ |
2 | 2 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
3 | 3 | |
4 | 4 | <mapper namespace="org.thingsboard.server.dao.yunteng.mapper.TriggerMapper"> |
5 | - <resultMap id="triggerDTO" type="org.thingsboard.server.common.data.yunteng.dto.TriggerDTO"> | |
5 | + <resultMap id="triggerDTO" type="org.thingsboard.server.common.data.yunteng.dto.scene.TriggerDTO"> | |
6 | 6 | <result property="id" column="id"/> |
7 | 7 | <result property="entityId" column="entity_id" |
8 | 8 | typeHandler="org.thingsboard.server.dao.yunteng.mapper.ListStringTypeHandler"/> | ... | ... |
... | ... | @@ -21,7 +21,7 @@ import org.thingsboard.server.common.data.id.DeviceId; |
21 | 21 | import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants; |
22 | 22 | import org.thingsboard.server.common.data.yunteng.dto.ActionAlarmDTO; |
23 | 23 | import org.thingsboard.server.common.data.yunteng.dto.AlarmInfoDTO; |
24 | -import org.thingsboard.server.common.data.yunteng.dto.TriggerDTO; | |
24 | +import org.thingsboard.server.common.data.yunteng.dto.scene.TriggerDTO; | |
25 | 25 | import org.thingsboard.server.common.data.yunteng.enums.*; |
26 | 26 | import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil; |
27 | 27 | import org.thingsboard.server.common.data.yunteng.utils.SpringBeanUtils; |
... | ... | @@ -93,7 +93,7 @@ class ReactState { |
93 | 93 | * @throws ExecutionException |
94 | 94 | * @throws InterruptedException |
95 | 95 | */ |
96 | - public void process(TbContext ctx, TbMsg msg, String deviceId) | |
96 | + public void process(TbContext ctx, TbMsg msg, String prefixId,String deviceId) | |
97 | 97 | throws ExecutionException, InterruptedException { |
98 | 98 | |
99 | 99 | StringBuilder detail = new StringBuilder(); |
... | ... | @@ -126,7 +126,13 @@ class ReactState { |
126 | 126 | return false; |
127 | 127 | } |
128 | 128 | try { |
129 | - if (triggerState.process(ctx, msg)) { | |
129 | + boolean fresh = false; | |
130 | + if(trigger.getId().equals(prefixId)&&msg.getOriginator().getId().toString().equals(id)){ | |
131 | + fresh=true; | |
132 | + } | |
133 | + boolean result = triggerState.process(ctx, msg,fresh); | |
134 | + log.error(String.format("触发器【%s】刷新【%s】结果【%s】触发器设备【%s】数据设备【%s】数据内容【%s】",trigger.getId(),fresh,result,id,msg.getOriginator(),msg.getData())); | |
135 | + if (result) { | |
130 | 136 | detail.append( |
131 | 137 | triggerState.getAlarmDetails() == null |
132 | 138 | ? "" |
... | ... | @@ -150,41 +156,45 @@ class ReactState { |
150 | 156 | }); |
151 | 157 | |
152 | 158 | /** 执行条件的所有设备都满足才为true */ |
153 | - AtomicBoolean conditionMatched = new AtomicBoolean(false); | |
154 | - if (triggerMatched.get()) { | |
155 | - conditionMatched.set(true); | |
156 | - Optional.ofNullable(conditions) | |
157 | - .ifPresent( | |
158 | - t -> { | |
159 | - t.forEach( | |
160 | - condition -> { | |
161 | - ScopeEnum entityType = condition.getEntityType(); | |
162 | - List<String> conditionDevices = condition.getEntityId(); | |
163 | - String tkProjectId = condition.getDeviceProfileId(); | |
164 | - if (ScopeEnum.ALL.equals(entityType)) { | |
165 | - conditionDevices = | |
166 | - ytDeviceService.findTbDeviceIdsByDeviceProfileId( | |
167 | - tkProjectId, condition.getTenantId()); | |
168 | - } | |
169 | - conditionMatched.set( | |
170 | - !conditionDevices.stream() | |
171 | - .anyMatch( | |
172 | - id -> { | |
173 | - TriggerState conditionState = | |
174 | - getOrCreateConditionState(condition, tkProjectId, id); | |
175 | - try { | |
176 | - return !conditionState.process(ctx, msg); | |
177 | - } catch (ExecutionException e) { | |
178 | - throw new RuntimeException(e); | |
179 | - } catch (InterruptedException e) { | |
180 | - throw new RuntimeException(e); | |
181 | - } | |
182 | - })); | |
159 | + AtomicBoolean conditionMatched = new AtomicBoolean(true); | |
160 | + Optional.ofNullable(conditions) | |
161 | + .ifPresent( | |
162 | + t -> { | |
163 | + t.forEach( | |
164 | + condition -> { | |
165 | + ScopeEnum entityType = condition.getEntityType(); | |
166 | + List<String> conditionDevices = condition.getEntityId(); | |
167 | + String tkProjectId = condition.getDeviceProfileId(); | |
168 | + if (ScopeEnum.ALL.equals(entityType)) { | |
169 | + conditionDevices = | |
170 | + ytDeviceService.findTbDeviceIdsByDeviceProfileId( | |
171 | + tkProjectId, condition.getTenantId()); | |
172 | + } | |
173 | + conditionMatched.set( | |
174 | + !conditionDevices.stream() | |
175 | + .anyMatch( | |
176 | + id -> { | |
177 | + TriggerState conditionState = | |
178 | + getOrCreateConditionState(condition, tkProjectId, id); | |
179 | + try { | |
180 | + boolean fresh = false; | |
181 | + if(msg.getOriginator().getId().toString().equals(id)){ | |
182 | + fresh=true; | |
183 | + } | |
184 | + boolean result = conditionState.process(ctx, msg,fresh); | |
185 | + log.warn(String.format("执行器【%s】刷新【%s】结果【%s】执行器设备【%s】数据设备【%s】数据内容【%s】",condition.getId(),fresh,result,id,msg.getOriginator(),msg.getData())); | |
186 | + return !result; | |
187 | + } catch (ExecutionException e) { | |
188 | + throw new RuntimeException(e); | |
189 | + } catch (InterruptedException e) { | |
190 | + throw new RuntimeException(e); | |
191 | + } | |
192 | + })); | |
193 | + }); | |
183 | 194 | }); |
184 | - }); | |
185 | - } | |
186 | 195 | |
187 | 196 | if (triggerMatched.get() && conditionMatched.get()) { |
197 | + log.error(String.format("设备【%s】的消息内容【%s】触发动作",deviceId,msg.getData())); | |
188 | 198 | for (TkDoActionEntity item : actions) { |
189 | 199 | if (ActionTypeEnum.MSG_NOTIFY.equals(item.getOutTarget())) { |
190 | 200 | noticeMsg(ctx, msg, item, deviceId, detail.toString(), msg.getTs()); |
... | ... | @@ -216,6 +226,7 @@ class ReactState { |
216 | 226 | || (trigger.getEntityType().equals(ScopeEnum.ALL) |
217 | 227 | && trigger.getDeviceProfileId().equals(profileId))) { |
218 | 228 | TriggerState state = createTriggerState(deviceId, trigger.getTriggerCondition()); |
229 | + log.error(String.format("新建设备【%s】的触发器",deviceId)); | |
219 | 230 | triggerState.put(cacheKey, state); |
220 | 231 | return state; |
221 | 232 | } |
... | ... | @@ -267,7 +278,7 @@ class ReactState { |
267 | 278 | || (condition.getEntityType().equals(ScopeEnum.ALL) |
268 | 279 | && condition.getDeviceProfileId().equals(profileId))) { |
269 | 280 | TriggerState state = createTriggerState(deviceId, condition.getTriggerCondition()); |
270 | - triggerState.put(cacheKey, state); | |
281 | + conditionState.put(cacheKey, state); | |
271 | 282 | return state; |
272 | 283 | } |
273 | 284 | return null; |
... | ... | @@ -397,7 +408,7 @@ class ReactState { |
397 | 408 | private void clearAlarm(TbContext ctx, TbMsg msg, String deviceId, String key) |
398 | 409 | throws ExecutionException, InterruptedException { |
399 | 410 | TriggerState clearState = getOrCreateClearState(deviceId, key); |
400 | - if (clearState != null && clearState.process(ctx, msg)) { | |
411 | + if (clearState != null && clearState.process(ctx, msg,true)) { | |
401 | 412 | ctx.getAlarmService() |
402 | 413 | .clearAlarmForResult( |
403 | 414 | ctx.getTenantId(), | ... | ... |
... | ... | @@ -25,6 +25,7 @@ import org.thingsboard.rule.engine.profile.SnapshotUpdate; |
25 | 25 | import org.thingsboard.rule.engine.yunteng.utils.TriggerRuleState; |
26 | 26 | import org.thingsboard.server.common.data.device.profile.AlarmConditionKeyType; |
27 | 27 | import org.thingsboard.server.common.data.plugin.ComponentType; |
28 | +import org.thingsboard.server.common.data.yunteng.dto.scene.RuleFilterDTO; | |
28 | 29 | import org.thingsboard.server.common.msg.TbMsg; |
29 | 30 | import org.thingsboard.server.common.msg.queue.PartitionChangeMsg; |
30 | 31 | |
... | ... | @@ -64,30 +65,30 @@ public class TbSceneReactNode implements TbNode { |
64 | 65 | ctx.tellSuccess(msg); |
65 | 66 | return; |
66 | 67 | } |
67 | - List<String> devScence = config.getScenes().get(deviceId); | |
68 | + List<RuleFilterDTO> devScence = config.getScenes().get(deviceId); | |
68 | 69 | if (devScence != null && !devScence.isEmpty()) { |
69 | 70 | devScence.stream() |
70 | 71 | .forEach( |
71 | 72 | t -> { |
72 | - ReactState react = getOrCreateReactState(ctx, config, t); | |
73 | + ReactState react = getOrCreateReactState(ctx, config, t.getScenId()); | |
73 | 74 | if (react != null) { |
74 | 75 | try { |
75 | - react.process(ctx, msg, deviceId); | |
76 | + react.process(ctx, msg,t.getRuleId(), deviceId); | |
76 | 77 | } catch (Exception e) { |
77 | 78 | ctx.tellFailure(msg, e); |
78 | 79 | } |
79 | 80 | } |
80 | 81 | }); |
81 | 82 | } |
82 | - List<String> projectScence = config.getProject().get(projectId); | |
83 | + List<RuleFilterDTO> projectScence = config.getProject().get(projectId); | |
83 | 84 | if (projectScence != null && !projectScence.isEmpty()) { |
84 | 85 | projectScence.stream() |
85 | 86 | .forEach( |
86 | 87 | t -> { |
87 | - ReactState react = getOrCreateReactState(ctx, config, t); | |
88 | + ReactState react = getOrCreateReactState(ctx, config, t.getScenId()); | |
88 | 89 | if (react != null) { |
89 | 90 | try { |
90 | - react.process(ctx, msg, deviceId); | |
91 | + react.process(ctx, msg,t.getRuleId(), deviceId); | |
91 | 92 | } catch (Exception e) { |
92 | 93 | ctx.tellFailure(msg, e); |
93 | 94 | } | ... | ... |
... | ... | @@ -6,16 +6,17 @@ import java.util.List; |
6 | 6 | import java.util.Map; |
7 | 7 | import lombok.Data; |
8 | 8 | import org.thingsboard.rule.engine.api.NodeConfiguration; |
9 | +import org.thingsboard.server.common.data.yunteng.dto.scene.RuleFilterDTO; | |
9 | 10 | |
10 | 11 | @Data |
11 | 12 | public class TbSceneReactNodeConfig implements NodeConfiguration<TbSceneReactNodeConfig> { |
12 | 13 | |
13 | 14 | /** 【TB设备配置ID,TK产品ID】产品信息 */ |
14 | 15 | private Map<String, String> profile; |
15 | - /** 【触发器的TK产品ID,场景】哪些产品会触发场景联动 */ | |
16 | - private Map<String, List<String>> project; | |
17 | - /** 【触发器的TB设备ID,场景】哪些设备会触发场景联动 */ | |
18 | - private Map<String, List<String>> scenes; | |
16 | + /** 【TK产品ID,场景】哪些产品会触发场景联动 */ | |
17 | + private Map<String, List<RuleFilterDTO>> project; | |
18 | + /** 【TB设备ID,场景】哪些设备会触发场景联动 */ | |
19 | + private Map<String, List<RuleFilterDTO>> scenes; | |
19 | 20 | /** 【TK场景ID,场景名称】场景联动信息 */ |
20 | 21 | private Map<String, String> names; |
21 | 22 | /** 【TK场景ID,组织ID】场景联动所属组织信息 */ |
... | ... | @@ -24,8 +25,8 @@ public class TbSceneReactNodeConfig implements NodeConfiguration<TbSceneReactNod |
24 | 25 | @Override |
25 | 26 | public TbSceneReactNodeConfig defaultConfiguration() { |
26 | 27 | TbSceneReactNodeConfig config = new TbSceneReactNodeConfig(); |
27 | - Map<String, List<String>> sceneMap = new HashMap<>(); | |
28 | - Map<String, List<String>> projectScenes = new HashMap<>(); | |
28 | + Map<String, List<RuleFilterDTO>> sceneMap = new HashMap<>(); | |
29 | + Map<String, List<RuleFilterDTO>> projectScenes = new HashMap<>(); | |
29 | 30 | |
30 | 31 | config.setScenes(sceneMap); |
31 | 32 | config.setProject(projectScenes); | ... | ... |
... | ... | @@ -73,8 +73,8 @@ class TriggerState { |
73 | 73 | this.alarmDetails = alarmDetails; |
74 | 74 | } |
75 | 75 | |
76 | - public boolean process(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException { | |
77 | - if (!msg.getOriginator().getId().toString().equals(originator)) { | |
76 | + public boolean process(TbContext ctx, TbMsg msg,boolean needFresh) throws ExecutionException, InterruptedException { | |
77 | + if (!needFresh) { | |
78 | 78 | return ruleMatched; |
79 | 79 | } |
80 | 80 | if (latestValues == null) { | ... | ... |