Commit 3b347a99494a81fe37fff6b70a7c2881001d8f5a

Authored by xp.Huang
2 parents c201e6cc e8c4ac06

Merge branch 'ljl0224' into 'master'

refactor: 场景联动的节点信息同步方式调整

See merge request huang/thingsboard3.3.2!48
... ... @@ -32,6 +32,7 @@ import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum;
32 32 import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
33 33 import org.thingsboard.server.controller.BaseController;
34 34 import org.thingsboard.server.dao.yunteng.service.ConvertConfigService;
  35 +import org.thingsboard.server.dao.yunteng.service.SceneLinkageService;
35 36 import org.thingsboard.server.dao.yunteng.service.YtRuleChainService;
36 37
37 38 import java.util.*;
... ... @@ -55,6 +56,7 @@ public class YtConvertDataToController extends BaseController {
55 56 private ActorSystemContext actorContext;
56 57
57 58 private final ConvertConfigService convertConfigService;
  59 + private final SceneLinkageService sceneLinkageService;
58 60
59 61 @GetMapping(params = {PAGE_SIZE, PAGE})
60 62 @ApiOperation("分页查询")
... ... @@ -181,8 +183,8 @@ public class YtConvertDataToController extends BaseController {
181 183 @Validated(UpdateGroup.class) @RequestBody ConvertReqDTO convertReqDTO)
182 184 throws ThingsboardException {
183 185 //TODO 通过接口获取JsonNode
184   - JsonNode configuration = convertReqDTO.getConfiguration();
185 186 int status = -1;
  187 + JsonNode configuration = sceneLinkageService.getRuleNodeConfig(convertReqDTO.getId(),getCurrentUser().getCurrentTenantId(),convertReqDTO.getStatus());
186 188 boolean noValue = configuration == null;
187 189 if (noValue && convertReqDTO.getStatus() == FastIotConstants.StateValue.DISABLE) {
188 190 status = FastIotConstants.StateValue.DISABLE;
... ...
... ... @@ -66,7 +66,7 @@ public final class ModelConstants {
66 66 /** 执行条件表 */
67 67 public static final String IOTFS_DO_CONDITION_TABLE_NAME = "iotfs_do_condition";
68 68 /** 条件动作表 */
69   - public static final String IOTFS_DO_ACTION_TABLE_NAME = "iotfs_do_action_ljl";
  69 + public static final String IOTFS_DO_ACTION_TABLE_NAME = "iotfs_do_action";
70 70 /** 中国城镇区街 */
71 71 public static final String SYS_AREA_TABLE_NAME = "sys_area";
72 72 /** 数据流转配置表 */
... ...
... ... @@ -22,9 +22,8 @@ public class DoAction extends TenantBaseEntity {
22 22 @TableField(typeHandler = EnumTypeHandler.class)
23 23 private ActionTypeEnum outTarget;
24 24 /**
25   - * 下发指令
  25 + * 场景联动内容
26 26 */
27   - private String command;
28 27 private String doContext;
29 28
30 29 /**
... ...
... ... @@ -12,7 +12,7 @@ import org.thingsboard.server.common.data.yunteng.enums.TriggerTypeEnum;
12 12
13 13 /** @Description 执行条件实体表 @Author cxy @Date 2021/11/24 17:16 */
14 14 @Data
15   -@TableName(value = ModelConstants.Table.IOTFS_DO_CONDITION_TABLE_NAME)
  15 +@TableName(value = ModelConstants.Table.IOTFS_DO_CONDITION_TABLE_NAME,autoResultMap=true)
16 16 @EqualsAndHashCode(callSuper = true)
17 17 public class DoCondition extends TenantBaseEntity {
18 18 private static final long serialVersionUID = 2827674377416477646L;
... ...
... ... @@ -11,7 +11,7 @@ import org.thingsboard.server.common.data.yunteng.enums.TriggerTypeEnum;
11 11
12 12 /** @Description 触发器实体表 @Author cxy @Date 2021/11/24 17:06 */
13 13 @Data
14   -@TableName(value = ModelConstants.Table.IOTFS_TRIGGER_TABLE_NAME)
  14 +@TableName(value = ModelConstants.Table.IOTFS_TRIGGER_TABLE_NAME,autoResultMap=true)
15 15 @EqualsAndHashCode(callSuper = true)
16 16 public class Trigger extends TenantBaseEntity {
17 17
... ...
... ... @@ -43,8 +43,6 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap
43 43 private final DoConditionService doConditionService;
44 44 private final DoActionService doActionService;
45 45 private final UserMapper userMapper;
46   - private final ConvertConfigMapper ruleEngineMapper;
47   - private final ConvertConfigService ruleEngineService;
48 46 private final UserOrganizationMappingService userOrganizationMappingService;
49 47 /**
50 48 * 增加场景联动,触发器可以多个,执行条件可以多个,执行动作可以多个
... ... @@ -169,7 +167,7 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap
169 167 continue;
170 168 }
171 169 if (!tbDeviceIds.contains(deviceId)) {
172   - throw new DataValidationException("触发器中存在不属于场景联动所在组织的设备");
  170 + throw new DataValidationException("in trigger ,this device not belong this org");
173 171 }
174 172 }
175 173 }else{
... ... @@ -186,10 +184,13 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap
186 184 // 如果获取的触发器不为空,进行添加操作
187 185 List<Trigger> triggers =
188 186 triggerDTOS.stream()
189   - .map(triggerDTO -> triggerDTO.getEntity(Trigger.class))
  187 + .map(triggerDTO -> {
  188 + triggerDTO.setTenantId(sceneLinkage.getTenantId());
  189 + triggerDTO.setSceneLinkageId(sceneLinkage.getId());
  190 + return triggerDTO.getEntity(Trigger.class);
  191 + })
190 192 .collect(Collectors.toList());
191 193 triggerService.insertBatch(triggers, 1000);
192   - freshRuleChain(sceneLinkage);
193 194 }
194 195 /**
195 196 * 修改执行动作
... ... @@ -206,7 +207,7 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap
206 207 continue;
207 208 }
208 209 if (!tbDeviceIds.contains(deviceId)) {
209   - throw new DataValidationException("动作中存在不属于场景联动所在组织的设备");
  210 + throw new DataValidationException("in action ,this device not belong this org ");
210 211 }
211 212 }
212 213 }
... ... @@ -220,7 +221,11 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap
220 221
221 222 List<DoAction> collectA =
222 223 sceneLinkageDTO.getDoActions().stream()
223   - .map(doActionDTO -> doActionDTO.getEntity(DoAction.class))
  224 + .map(doActionDTO -> {
  225 + doActionDTO.setTenantId(sceneLinkageDTO.getTenantId());
  226 + doActionDTO.setSceneLinkageId(sceneLinkageDTO.getId());
  227 + return doActionDTO.getEntity(DoAction.class);
  228 + })
224 229 .collect(Collectors.toList());
225 230 doActionService.insertBatch(collectA, 1000);
226 231
... ... @@ -243,7 +248,7 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap
243 248 continue;
244 249 }
245 250 if (!tbDeviceIds.contains(deviceId)) {
246   - throw new DataValidationException("执行条件中存在不属于场景联动所在组织的设备");
  251 + throw new DataValidationException("in condition ,this device not belong this org");
247 252 }
248 253 }
249 254 }
... ... @@ -257,7 +262,12 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap
257 262 // 4.批量新增执行条件
258 263 List<DoCondition> collectC =
259 264 sceneLinkageDTO.getDoConditions().stream()
260   - .map(doConditionDTO -> doConditionDTO.getEntity(DoCondition.class))
  265 + .map(doConditionDTO -> {
  266 +
  267 + doConditionDTO.setTenantId(sceneLinkageDTO.getTenantId());
  268 + doConditionDTO.setSceneLinkageId(sceneLinkageDTO.getId());
  269 + return doConditionDTO.getEntity(DoCondition.class);}
  270 + )
261 271 .collect(Collectors.toList());
262 272 doConditionService.insertBatch(collectC, 1000);
263 273 }
... ... @@ -367,55 +377,61 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap
367 377 }
368 378
369 379 /**
370   - *
371   - * @param scene 场景联动信息
  380 + * 场景联动节点配置信息
  381 + * @param sceneId 场景联动主键
  382 + * @param tenantId 租户主键
  383 + * @param state 是否禁用场景联动,true标识禁用,false标识启用。
372 384 * @return
373 385 */
374   - private boolean freshRuleChain(SceneLinkageDTO scene){
  386 + @Override
  387 + public JsonNode getRuleNodeConfig(String sceneId,String tenantId,Integer state){
375 388 String ruleTyp = "org.thingsboard.rule.engine.filter.TbSceneReactNode";
376   - ConvertConfig rule = ruleEngineMapper.selectOne(
377   - new QueryWrapper<ConvertConfig>()
  389 + List<SceneLinkage> runningScenes = baseMapper.selectList(
  390 + new QueryWrapper<SceneLinkage>()
378 391 .lambda()
379   - .eq(ConvertConfig::getTenantId,scene.getTenantId())
380   - .eq(ConvertConfig::getType,ruleTyp)
  392 + .eq(SceneLinkage::getTenantId,tenantId)
  393 + .eq(SceneLinkage::getStatus,FastIotConstants.StateValue.ENABLE)
381 394 );
382   - ConvertConfigDTO ruleEngine = new ConvertConfigDTO();
383   - Map<String,List<String>> devices;
384   - if(rule == null){
385   - devices = new HashMap<>();
386   - ruleEngine.setName(scene.getName());
387   - }else{
388   - devices = JacksonUtil.convertValue(rule.getConfiguration(),new TypeReference<Map<String,Map<String, List<String>>>>(){}).get("scenes");
389   - ruleEngine.setId(rule.getId());
  395 + Set<String> enableIds = new HashSet<>();
  396 + enableIds.add(sceneId);
  397 + for(SceneLinkage item : runningScenes){
  398 + enableIds.add(item.getId());
390 399 }
391   - List<String> scenes;
392   - for(TriggerDTO item:scene.getTriggers()){
  400 + if(state == FastIotConstants.StateValue.DISABLE ){
  401 + enableIds.remove(sceneId);
  402 + }
  403 +
  404 + List<Trigger> triggers =triggerMapper.selectList(
  405 + new QueryWrapper<Trigger>()
  406 + .lambda()
  407 + .eq(Trigger::getTenantId,tenantId)
  408 + .eq(Trigger::getTriggerType,TriggerTypeEnum.DEVICE_TRIGGER)
  409 + .in(Trigger::getSceneLinkageId,enableIds)
  410 + );
  411 +
  412 + Map<String,List<String>> devices = new HashMap<>();
  413 + for(Trigger item: triggers){
393 414 String deviceId = item.getEntityId();
394   - if(devices.containsKey(deviceId)){
395   - scenes = devices.get(deviceId);
396   - }else{
397   - scenes = new ArrayList<>();
  415 + List<String> scenes = devices.computeIfAbsent(deviceId,k -> new ArrayList<String>());
  416 + String scenId = item.getSceneLinkageId();
  417 + if(!scenes.contains(scenId)){
  418 + scenes.add(sceneId);
398 419 }
399   -
400   - if(!scenes.contains(scene.getId())){
401   - scenes.add(scene.getId());
  420 + if(scenes.isEmpty()){
  421 + devices.remove(deviceId);
  422 + }else{
  423 + devices.put(deviceId,scenes);
402 424 }
403   - devices.put(deviceId,scenes);
404   -
  425 + }
  426 + if(devices.isEmpty()){
  427 + return null;
405 428 }
406 429
407 430 Map<String,Map> engineConfig = new HashMap<>();
408 431 engineConfig.put("scenes",devices);
409 432
410 433
411   -
412   - ruleEngine.setTenantId(scene.getTenantId());
413   - ruleEngine.setNodeType(FastIotConstants.SCENE_REACT);
414   - ruleEngine.setType(ruleTyp);
415   - ruleEngine.setConfiguration(JacksonUtil.convertValue(engineConfig, JsonNode.class));
416   -
417   - ruleEngineService.createOrUpdate(ruleEngine);
418   - return false;
  434 + return JacksonUtil.convertValue(engineConfig, JsonNode.class);
419 435 }
420 436 private List<String> getQueryOrganizationIds(String tenantId,List<String> organizationIds){
421 437 // 查询该组织的所有子类
... ...
1 1 package org.thingsboard.server.dao.yunteng.service;
  2 +import com.fasterxml.jackson.databind.JsonNode;
2 3 import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO;
3 4 import org.thingsboard.server.common.data.yunteng.dto.SceneLinkageDTO;
4 5 import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
... ... @@ -68,4 +69,13 @@ public interface SceneLinkageService extends BaseService<SceneLinkage>{
68 69 */
69 70 List<DeviceDTO> findDeviceList(String organizationId,boolean isTenantAdmin,String tenantId,String currentUserId);
70 71
  72 + /**
  73 + * 获取租户的场景联动节点配置信息
  74 + * @param sceneId 场景联动主键
  75 + * @param tenantId 租户主键
  76 + * @param state 是否启动
  77 + * @return
  78 + */
  79 + JsonNode getRuleNodeConfig(String sceneId,String tenantId,Integer state);
  80 +
71 81 }
... ...