Commit 77d50fd47f88bd153c9723540486e61d91edf69e
Merge branch '20220319' into 'master'
fix: 场景联动不存在激活时500异常 See merge request huang/thingsboard3.3.2!70
Showing
2 changed files
with
40 additions
and
17 deletions
... | ... | @@ -56,6 +56,7 @@ public enum ErrorMessage { |
56 | 56 | STORE_FILE_FAILED(400037,"文件存储失败"), |
57 | 57 | NOT_BELONG_CURRENT_TENANT(400038,"不属于当前租户"), |
58 | 58 | DEVICE_LOSED(400039,"设备相关参数丢失"), |
59 | + SCENE_REACT_NOT_EXTIED(400040,"场景联动不存在"), | |
59 | 60 | HAVE_NO_PERMISSION(500002,"没有修改权限"); |
60 | 61 | private final int code; |
61 | 62 | private String message; | ... | ... |
... | ... | @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.JsonNode; |
7 | 7 | import com.fasterxml.jackson.databind.node.ObjectNode; |
8 | 8 | import lombok.RequiredArgsConstructor; |
9 | 9 | import org.apache.commons.lang3.StringUtils; |
10 | +import org.springframework.scheduling.annotation.Async; | |
10 | 11 | import org.springframework.stereotype.Service; |
11 | 12 | import org.springframework.transaction.annotation.Transactional; |
12 | 13 | import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants; |
... | ... | @@ -366,6 +367,9 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap |
366 | 367 | sceneInform.put(item.getId(), item.getName()); |
367 | 368 | } |
368 | 369 | SceneLinkage self = baseMapper.selectById(currentSceneId); |
370 | + if (self == null) { | |
371 | + throw new YtDataValidationException(ErrorMessage.SCENE_REACT_NOT_EXTIED.getMessage()); | |
372 | + } | |
369 | 373 | if (state == FastIotConstants.StateValue.DISABLE) { |
370 | 374 | enableIds.remove(currentSceneId); |
371 | 375 | sceneInform.remove(currentSceneId); |
... | ... | @@ -394,33 +398,18 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap |
394 | 398 | String scenId = trigger.getSceneLinkageId(); |
395 | 399 | List<String> devices = trigger.getEntityId(); |
396 | 400 | if (ScopeEnum.ALL.equals(trigger.getEntityType()) && currentSceneId.equals(scenId)) { |
397 | - trigger.setEntityId(allDevices); | |
398 | - triggerMapper.updateById(trigger); | |
399 | 401 | devices = allDevices; |
400 | 402 | } |
401 | 403 | deviceSceneMap(matchedDevices, devices, scenId); |
402 | 404 | }); |
403 | 405 | |
404 | - List<DoCondition> conditions = doConditionMapper.selectList(new QueryWrapper<DoCondition>().lambda().eq(DoCondition::getSceneLinkageId, currentSceneId).eq(DoCondition::getEntityType, ScopeEnum.ALL)); | |
405 | - conditions.forEach(item -> { | |
406 | - if (currentSceneId.equals(item.getSceneLinkageId())) { | |
407 | - item.setEntityId(allDevices); | |
408 | - doConditionMapper.updateById(item); | |
409 | - } | |
410 | - }); | |
411 | - | |
412 | - List<DoAction> actions = doActionMapper.selectList(new QueryWrapper<DoAction>().lambda().eq(DoAction::getSceneLinkageId, currentSceneId).eq(DoAction::getEntityType, ScopeEnum.ALL)); | |
413 | - actions.forEach(item -> { | |
414 | - if (currentSceneId.equals(item.getSceneLinkageId())) { | |
415 | - item.setDeviceId(allDevices); | |
416 | - doActionMapper.updateById(item); | |
417 | - } | |
418 | - }); | |
419 | 406 | |
420 | 407 | if (matchedDevices.isEmpty()) { |
421 | 408 | return null; |
422 | 409 | } |
423 | 410 | |
411 | + freshEntityIds(currentSceneId, allDevices, triggers); | |
412 | + | |
424 | 413 | Map<String, Map> engineConfig = new HashMap<>(); |
425 | 414 | engineConfig.put("scenes", matchedDevices); |
426 | 415 | engineConfig.put("names", sceneInform); |
... | ... | @@ -430,6 +419,39 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap |
430 | 419 | } |
431 | 420 | |
432 | 421 | /** |
422 | + * 异步刷新场景联动的设备ID | |
423 | + * | |
424 | + * @param sceneId | |
425 | + * @param allDevices | |
426 | + * @param triggers | |
427 | + */ | |
428 | + @Async | |
429 | + public void freshEntityIds(String sceneId, List<String> allDevices, List<Trigger> triggers) { | |
430 | + triggers.forEach(trigger -> { | |
431 | + if (ScopeEnum.ALL.equals(trigger.getEntityType()) && sceneId.equals(trigger.getSceneLinkageId())) { | |
432 | + trigger.setEntityId(allDevices); | |
433 | + triggerMapper.updateById(trigger); | |
434 | + } | |
435 | + }); | |
436 | + | |
437 | + List<DoCondition> conditions = doConditionMapper.selectList(new QueryWrapper<DoCondition>().lambda().eq(DoCondition::getSceneLinkageId, sceneId).eq(DoCondition::getEntityType, ScopeEnum.ALL)); | |
438 | + conditions.forEach(item -> { | |
439 | + if (sceneId.equals(item.getSceneLinkageId())) { | |
440 | + item.setEntityId(allDevices); | |
441 | + doConditionMapper.updateById(item); | |
442 | + } | |
443 | + }); | |
444 | + | |
445 | + List<DoAction> actions = doActionMapper.selectList(new QueryWrapper<DoAction>().lambda().eq(DoAction::getSceneLinkageId, sceneId).eq(DoAction::getEntityType, ScopeEnum.ALL)); | |
446 | + actions.forEach(item -> { | |
447 | + if (sceneId.equals(item.getSceneLinkageId())) { | |
448 | + item.setDeviceId(allDevices); | |
449 | + doActionMapper.updateById(item); | |
450 | + } | |
451 | + }); | |
452 | + } | |
453 | + | |
454 | + /** | |
433 | 455 | * 设备与场景联动的映射集合 |
434 | 456 | * |
435 | 457 | * @param resultMap 缓存设备和场景联动映射结果的集合 | ... | ... |