Commit 711d6b9149f0f55b2c32db7187e8096dfde3cd63

Authored by xp.Huang
1 parent 62dea8f7

fix: 调整场景联动触发判断逻辑及告警详情值

... ... @@ -302,10 +302,13 @@ public class TkNoticeServiceImpl implements TkNoticeService {
302 302 JsonNode jsonNode = objectMapper.readTree(alarmInfo.getDetails());
303 303 Iterator<String> fieldNames = jsonNode.fieldNames();
304 304 while (fieldNames.hasNext()) {
305   - String fieldName = fieldNames.next();
306   - JsonNode data = jsonNode.get(fieldName);
307   - String realValue = JacksonUtil.convertValue(data.path(FastIotConstants.Scene.TRIGGER_DATA).path("realValue"), String.class);
308   - params.put("triggerValue", realValue);
  305 + String fieldName = fieldNames.next();
  306 + JsonNode data = jsonNode.get(fieldName);
  307 + if(null !=data.path(FastIotConstants.Scene.TRIGGER_DATA)){
  308 + String realValue = JacksonUtil.convertValue(data.path(FastIotConstants.Scene.TRIGGER_DATA).path("realValue"), String.class);
  309 + params.put("triggerValue", realValue);
  310 + break;
  311 + }
309 312 }
310 313 } catch (Exception e) {
311 314 log.error(String.format("转换异常【s%】", alarmInfo.getDetails()));
... ...
... ... @@ -70,9 +70,9 @@ class ReactState {
70 70 private final CacheManager cacheManager;
71 71 private final Cache cache;
72 72 private TkDeviceService ytDeviceService;
73   - private String triggerDevicesKey = "triggerDevices";
  73 + private String triggerDevicesKey;
74 74
75   - private String currentAlarmsKey = "currentAlarms";
  75 + private String currentAlarmsKey;
76 76
77 77 ReactState(String reactId, TbContext ctx, TbSceneReactNodeConfig config) {
78 78 this.reactId = reactId;
... ... @@ -95,10 +95,12 @@ class ReactState {
95 95 this.noticeService = SpringBeanUtils.getBean(TkNoticeService.class);
96 96 this.ytDeviceService = SpringBeanUtils.getBean(TkDeviceService.class);
97 97 this.cache = cacheManager.getCache(FastIotConstants.CacheConfigKey.SCENE_REACT);
  98 + this.triggerDevicesKey = reactId+ "_triggerDevices";
  99 + this.currentAlarmsKey = reactId+ "_currentAlarms";
98 100 this.triggerDevices = null == cache.get(triggerDevicesKey)?
99   - new ConcurrentHashMap<>():cache.get("triggerDevices",ConcurrentHashMap.class);
  101 + new ConcurrentHashMap<>():cache.get(triggerDevicesKey,ConcurrentHashMap.class);
100 102 this.currentAlarms = null == cache.get(currentAlarmsKey)?
101   - new ConcurrentHashMap<>():cache.get("triggerDevices",ConcurrentHashMap.class);
  103 + new ConcurrentHashMap<>():cache.get(currentAlarmsKey,ConcurrentHashMap.class);
102 104 }
103 105
104 106 /**
... ... @@ -133,7 +135,7 @@ class ReactState {
133 135 }else{
134 136 conditionMatched.set(true);
135 137 }
136   - cache.put(triggerDevicesKey,triggerDevices);
  138 +
137 139 String msgData = msg.getData();
138 140 if (allowExecuteCondition && conditionMatched.get()) {
139 141 log.debug(String.format("设备【%s】的消息内容【%s】触发动作", deviceId, msgData));
... ... @@ -213,6 +215,17 @@ class ReactState {
213 215 fresh = true;
214 216 }
215 217 triggerResult = triggerState.process(ctx, msg,fresh);
  218 + if(fresh){
  219 + String triggerKey = deviceId + "_" + matchKey.get();
  220 + if(triggerResult){
  221 + //放入满足的触发器信息,设备ID+触发关键字
  222 + triggerDevices.put(triggerKey,
  223 + new TriggerData(msg,detail));
  224 + cache.put(triggerDevicesKey,triggerDevices);
  225 + }else{
  226 + triggerDevices.remove(triggerKey);
  227 + }
  228 + }
216 229 ObjectNode result = triggerState.getRuleState().getDetailInform();
217 230 log.debug(
218 231 String.format(
... ... @@ -222,7 +235,7 @@ class ReactState {
222 235 devId,
223 236 msg.getOriginator(),
224 237 msg.getData()));
225   - if (!result.isEmpty() && triggerResult) {
  238 + if (!result.isEmpty() && triggerResult && fresh) {
226 239 ObjectNode triggerData = JacksonUtil.newObjectNode();
227 240 triggerData.put(FastIotConstants.Scene.TRIGGER_DATA,result);
228 241 detail.set(devId,triggerData);
... ... @@ -243,9 +256,6 @@ class ReactState {
243 256 });
244 257 if(matched){
245 258 triggerMatched.set(matched);
246   - //放入满足的触发器信息,设备ID+触发关键字
247   - triggerDevices.put(deviceId + "_" + matchKey.get(),
248   - new TriggerData(msg,detail));
249 259 }
250 260 });
251 261 });
... ...