Commit fc1c8dba9517b75118b6120d9215bedd4997679c
Merge branch '20220322' into 'master'
20220322 See merge request huang/thingsboard3.3.2!71
Showing
5 changed files
with
52 additions
and
14 deletions
@@ -102,7 +102,7 @@ public class YtSceneLinkageController extends BaseController { | @@ -102,7 +102,7 @@ public class YtSceneLinkageController extends BaseController { | ||
102 | 102 | ||
103 | 103 | ||
104 | 104 | ||
105 | - @ApiOperation(value = "通过场景id获取所属组织的设备集合") | 105 | + @ApiOperation(value = "组织的设备集合") |
106 | @GetMapping("/device") | 106 | @GetMapping("/device") |
107 | public ResponseEntity<Map> findDeviceList(@RequestParam String organizationId) | 107 | public ResponseEntity<Map> findDeviceList(@RequestParam String organizationId) |
108 | throws ThingsboardException { | 108 | throws ThingsboardException { |
@@ -23,6 +23,6 @@ public class ActionAlarmDTO extends TenantDTO { | @@ -23,6 +23,6 @@ public class ActionAlarmDTO extends TenantDTO { | ||
23 | private AlarmSeverity alarmLevel; | 23 | private AlarmSeverity alarmLevel; |
24 | 24 | ||
25 | 25 | ||
26 | - private TriggerDTO clearRule; | 26 | + private List<TriggerDTO> clearRule; |
27 | 27 | ||
28 | } | 28 | } |
@@ -74,6 +74,7 @@ public class DeviceDTO extends TenantDTO { | @@ -74,6 +74,7 @@ public class DeviceDTO extends TenantDTO { | ||
74 | private Long lastOnlineTime; | 74 | private Long lastOnlineTime; |
75 | private Long lastOfflineTime; | 75 | private Long lastOfflineTime; |
76 | private String description; | 76 | private String description; |
77 | + private boolean enable; | ||
77 | 78 | ||
78 | public void setDeviceState(boolean deviceState) { | 79 | public void setDeviceState(boolean deviceState) { |
79 | if (deviceState) { | 80 | if (deviceState) { |
@@ -5,11 +5,15 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | @@ -5,11 +5,15 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||
5 | import com.baomidou.mybatisplus.core.metadata.IPage; | 5 | import com.baomidou.mybatisplus.core.metadata.IPage; |
6 | import com.fasterxml.jackson.databind.JsonNode; | 6 | import com.fasterxml.jackson.databind.JsonNode; |
7 | import com.fasterxml.jackson.databind.node.ObjectNode; | 7 | import com.fasterxml.jackson.databind.node.ObjectNode; |
8 | +import com.google.common.util.concurrent.MoreExecutors; | ||
8 | import lombok.RequiredArgsConstructor; | 9 | import lombok.RequiredArgsConstructor; |
9 | import org.apache.commons.lang3.StringUtils; | 10 | import org.apache.commons.lang3.StringUtils; |
10 | import org.springframework.scheduling.annotation.Async; | 11 | import org.springframework.scheduling.annotation.Async; |
12 | +import org.springframework.scheduling.annotation.EnableAsync; | ||
11 | import org.springframework.stereotype.Service; | 13 | import org.springframework.stereotype.Service; |
12 | import org.springframework.transaction.annotation.Transactional; | 14 | import org.springframework.transaction.annotation.Transactional; |
15 | +import org.thingsboard.common.util.ThingsBoardThreadFactory; | ||
16 | +import org.thingsboard.server.common.data.id.EntityId; | ||
13 | import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants; | 17 | import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants; |
14 | import org.thingsboard.server.common.data.yunteng.core.exception.YtDataValidationException; | 18 | import org.thingsboard.server.common.data.yunteng.core.exception.YtDataValidationException; |
15 | import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; | 19 | import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; |
@@ -25,6 +29,10 @@ import org.thingsboard.server.dao.yunteng.mapper.*; | @@ -25,6 +29,10 @@ import org.thingsboard.server.dao.yunteng.mapper.*; | ||
25 | import org.thingsboard.server.dao.yunteng.service.*; | 29 | import org.thingsboard.server.dao.yunteng.service.*; |
26 | 30 | ||
27 | import java.util.*; | 31 | import java.util.*; |
32 | +import java.util.concurrent.ExecutorService; | ||
33 | +import java.util.concurrent.Executors; | ||
34 | +import java.util.concurrent.ScheduledExecutorService; | ||
35 | +import java.util.concurrent.TimeUnit; | ||
28 | import java.util.stream.Collectors; | 36 | import java.util.stream.Collectors; |
29 | 37 | ||
30 | /** | 38 | /** |
@@ -43,7 +51,6 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap | @@ -43,7 +51,6 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap | ||
43 | private final TriggerService triggerService; | 51 | private final TriggerService triggerService; |
44 | private final DoConditionService doConditionService; | 52 | private final DoConditionService doConditionService; |
45 | private final DoActionService doActionService; | 53 | private final DoActionService doActionService; |
46 | - | ||
47 | /** | 54 | /** |
48 | * 增加场景联动,触发器可以多个,执行条件可以多个,执行动作可以多个 | 55 | * 增加场景联动,触发器可以多个,执行条件可以多个,执行动作可以多个 |
49 | * | 56 | * |
@@ -339,10 +346,21 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap | @@ -339,10 +346,21 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap | ||
339 | 346 | ||
340 | 347 | ||
341 | List<String> customerDevices = deviceMapper.findDeviceIdsByCustomerId(customerId); | 348 | List<String> customerDevices = deviceMapper.findDeviceIdsByCustomerId(customerId); |
342 | - if (customerDevices == null || customerDevices.isEmpty()) { | 349 | + if (!EntityId.NULL_UUID.toString().equals(customerId) && (customerDevices == null || customerDevices.isEmpty())) { |
343 | return null; | 350 | return null; |
344 | } | 351 | } |
345 | - List<DeviceDTO> result = orgDevices.stream().filter(f -> customerDevices.contains(f.getTbDeviceId())).map(device -> device.getDTO(DeviceDTO.class)).collect(Collectors.toList()); | 352 | + List<DeviceDTO> result = orgDevices.stream() |
353 | +// .filter(f -> customerDevices.contains(f.getTbDeviceId())) | ||
354 | + .map(device -> { | ||
355 | + DeviceDTO dto = device.getDTO(DeviceDTO.class); | ||
356 | + if(customerDevices != null && !customerDevices.isEmpty() && customerDevices.contains(device.getTbDeviceId())){ | ||
357 | + dto.setEnable(true); | ||
358 | + }else{ | ||
359 | + dto.setEnable(false); | ||
360 | + } | ||
361 | + return dto; | ||
362 | + }) | ||
363 | + .collect(Collectors.toList()); | ||
346 | 364 | ||
347 | 365 | ||
348 | return result; | 366 | return result; |
@@ -408,7 +426,9 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap | @@ -408,7 +426,9 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap | ||
408 | return null; | 426 | return null; |
409 | } | 427 | } |
410 | 428 | ||
411 | - freshEntityIds(currentSceneId, allDevices, triggers); | 429 | + Executors.newScheduledThreadPool(1).schedule(()->{ |
430 | + freshEntityIds(currentSceneId, allDevices, triggers); | ||
431 | + },1, TimeUnit.SECONDS); | ||
412 | 432 | ||
413 | Map<String, Map> engineConfig = new HashMap<>(); | 433 | Map<String, Map> engineConfig = new HashMap<>(); |
414 | engineConfig.put("scenes", matchedDevices); | 434 | engineConfig.put("scenes", matchedDevices); |
@@ -425,7 +445,6 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap | @@ -425,7 +445,6 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap | ||
425 | * @param allDevices | 445 | * @param allDevices |
426 | * @param triggers | 446 | * @param triggers |
427 | */ | 447 | */ |
428 | - @Async | ||
429 | public void freshEntityIds(String sceneId, List<String> allDevices, List<Trigger> triggers) { | 448 | public void freshEntityIds(String sceneId, List<String> allDevices, List<Trigger> triggers) { |
430 | triggers.forEach(trigger -> { | 449 | triggers.forEach(trigger -> { |
431 | if (ScopeEnum.ALL.equals(trigger.getEntityType()) && sceneId.equals(trigger.getSceneLinkageId())) { | 450 | if (ScopeEnum.ALL.equals(trigger.getEntityType()) && sceneId.equals(trigger.getSceneLinkageId())) { |
@@ -122,6 +122,9 @@ class ReactState { | @@ -122,6 +122,9 @@ class ReactState { | ||
122 | matched = false; | 122 | matched = false; |
123 | for (TriggerDTO trigger : triggers) { | 123 | for (TriggerDTO trigger : triggers) { |
124 | TriggerState triggerState = getOrCreateTriggerState(trigger, deviceId); | 124 | TriggerState triggerState = getOrCreateTriggerState(trigger, deviceId); |
125 | + if(triggerState == null){ | ||
126 | + continue; | ||
127 | + } | ||
125 | matched = triggerState.process(ctx, msg); | 128 | matched = triggerState.process(ctx, msg); |
126 | if (matched) { | 129 | if (matched) { |
127 | detail.append(triggerState.getAlarmDetails()); | 130 | detail.append(triggerState.getAlarmDetails()); |
@@ -130,7 +133,9 @@ class ReactState { | @@ -130,7 +133,9 @@ class ReactState { | ||
130 | } | 133 | } |
131 | } else if (currentAlarms.containsKey(deviceId) && this.alarmAction != null) { | 134 | } else if (currentAlarms.containsKey(deviceId) && this.alarmAction != null) { |
132 | //清除设备告警 | 135 | //清除设备告警 |
133 | - clearAlarm(ctx, msg, deviceId); | 136 | + for (AlarmConditionFilterKey entityKey : triggerState.getEntityKeys()) { |
137 | + clearAlarm(ctx, msg, deviceId, entityKey.getKey()); | ||
138 | + } | ||
134 | } | 139 | } |
135 | } | 140 | } |
136 | } | 141 | } |
@@ -189,15 +194,28 @@ class ReactState { | @@ -189,15 +194,28 @@ class ReactState { | ||
189 | 194 | ||
190 | } | 195 | } |
191 | 196 | ||
192 | - protected TriggerState getOrCreateClearState(String deviceId) { | 197 | + protected TriggerState getOrCreateClearState(String deviceId,String key) { |
198 | + String cacheKey = deviceId+key; | ||
193 | if (triggerState.containsKey(deviceId)) { | 199 | if (triggerState.containsKey(deviceId)) { |
194 | - return triggerState.get(deviceId); | 200 | + return triggerState.get(cacheKey); |
195 | } | 201 | } |
196 | if (alarmAction.getDeviceId().contains(deviceId)) { | 202 | if (alarmAction.getDeviceId().contains(deviceId)) { |
203 | + TriggerState state = null; | ||
197 | ActionAlarmDTO alarm = JacksonUtil.convertValue(alarmAction.getDoContext(), ActionAlarmDTO.class); | 204 | ActionAlarmDTO alarm = JacksonUtil.convertValue(alarmAction.getDoContext(), ActionAlarmDTO.class); |
198 | if (alarm != null && alarm.getClearRule() != null) { | 205 | if (alarm != null && alarm.getClearRule() != null) { |
199 | - TriggerState state = createTriggerState(deviceId, alarm.getClearRule().getTriggerCondition()); | ||
200 | - triggerState.put(deviceId, state); | 206 | + for(TriggerDTO rule:alarm.getClearRule()){ |
207 | + for (AlarmConditionFilter filter : rule.getTriggerCondition().getCondition().getCondition()) { | ||
208 | + String tempKey =filter.getKey().getKey(); | ||
209 | + if(key.equals(tempKey)){ | ||
210 | + state = createTriggerState(deviceId, rule.getTriggerCondition()); | ||
211 | + triggerState.put(cacheKey, state); | ||
212 | + break; | ||
213 | + } | ||
214 | + } | ||
215 | + if(state != null){ | ||
216 | + break; | ||
217 | + } | ||
218 | + } | ||
201 | return state; | 219 | return state; |
202 | } | 220 | } |
203 | } | 221 | } |
@@ -317,8 +335,8 @@ class ReactState { | @@ -317,8 +335,8 @@ class ReactState { | ||
317 | noticeService.alert(action.getAlarmProfileId(), formData); | 335 | noticeService.alert(action.getAlarmProfileId(), formData); |
318 | } | 336 | } |
319 | 337 | ||
320 | - private void clearAlarm(TbContext ctx, TbMsg msg, String deviceId) throws ExecutionException, InterruptedException { | ||
321 | - TriggerState clearState = getOrCreateClearState(deviceId); | 338 | + private void clearAlarm(TbContext ctx, TbMsg msg, String deviceId,String key) throws ExecutionException, InterruptedException { |
339 | + TriggerState clearState = getOrCreateClearState(deviceId,key); | ||
322 | if (clearState.process(ctx, msg)) { | 340 | if (clearState.process(ctx, msg)) { |
323 | ctx.getAlarmService().clearAlarmForResult(ctx.getTenantId(), currentAlarms.get(deviceId).getId(), null, System.currentTimeMillis()); | 341 | ctx.getAlarmService().clearAlarmForResult(ctx.getTenantId(), currentAlarms.get(deviceId).getId(), null, System.currentTimeMillis()); |
324 | ytDeviceService.freshAlarmStatus(new DeviceId(UUID.fromString(deviceId)), 0); | 342 | ytDeviceService.freshAlarmStatus(new DeviceId(UUID.fromString(deviceId)), 0); |