Commit 5274de80a94166a74c3e975ad3d932cce5a9145c

Authored by Andrew Shvayka
Committed by GitHub
2 parents 7d64e196 1c7b008d

Merge pull request #302 from thingsboard/feature/rpc-rule

Alarm Processor improvement to trigger alarm on each incoming message
@@ -41,6 +41,7 @@ import javax.script.Bindings; @@ -41,6 +41,7 @@ import javax.script.Bindings;
41 import javax.script.ScriptException; 41 import javax.script.ScriptException;
42 import java.io.IOException; 42 import java.io.IOException;
43 import java.util.List; 43 import java.util.List;
  44 +import java.util.concurrent.ExecutionException;
44 45
45 /** 46 /**
46 * @author Andrew Shvayka 47 * @author Andrew Shvayka
@@ -131,6 +132,16 @@ public class AlarmProcessor implements RuleProcessor<AlarmProcessorConfiguration @@ -131,6 +132,16 @@ public class AlarmProcessor implements RuleProcessor<AlarmProcessorConfiguration
131 Alarm existing = null; 132 Alarm existing = null;
132 if (isActiveAlarm) { 133 if (isActiveAlarm) {
133 Alarm alarm = buildAlarm(ctx, msg); 134 Alarm alarm = buildAlarm(ctx, msg);
  135 + if (configuration.isNewAlarmFlag()) {
  136 + Optional<Alarm> oldAlarmOpt = ctx.findLatestAlarm(alarm.getOriginator(), alarm.getType());
  137 + if (oldAlarmOpt.isPresent() && !oldAlarmOpt.get().getStatus().isCleared()) {
  138 + try {
  139 + ctx.clearAlarm(oldAlarmOpt.get().getId(), oldAlarmOpt.get().getEndTs()).get();
  140 + } catch (Exception e) {
  141 + throw new RuleException("Failed to clear old alarm", e);
  142 + }
  143 + }
  144 + }
134 existing = ctx.createOrUpdateAlarm(alarm); 145 existing = ctx.createOrUpdateAlarm(alarm);
135 if (existing.getStartTs() == alarm.getStartTs()) { 146 if (existing.getStartTs() == alarm.getStartTs()) {
136 log.debug("[{}][{}] New Active Alarm detected", ctx.getRuleId(), existing.getId()); 147 log.debug("[{}][{}] New Active Alarm detected", ctx.getRuleId(), existing.getId());
@@ -140,7 +151,7 @@ public class AlarmProcessor implements RuleProcessor<AlarmProcessorConfiguration @@ -140,7 +151,7 @@ public class AlarmProcessor implements RuleProcessor<AlarmProcessorConfiguration
140 log.debug("[{}][{}] Existing Active Alarm detected", ctx.getRuleId(), existing.getId()); 151 log.debug("[{}][{}] Existing Active Alarm detected", ctx.getRuleId(), existing.getId());
141 md.put(IS_EXISTING_ALARM, Boolean.TRUE); 152 md.put(IS_EXISTING_ALARM, Boolean.TRUE);
142 } 153 }
143 - } else if (isClearedAlarm) { 154 + } else {
144 String alarmType = VelocityUtils.merge(alarmTypeTemplate, context); 155 String alarmType = VelocityUtils.merge(alarmTypeTemplate, context);
145 Optional<Alarm> alarm = ctx.findLatestAlarm(ctx.getDeviceMetaData().getDeviceId(), alarmType); 156 Optional<Alarm> alarm = ctx.findLatestAlarm(ctx.getDeviceMetaData().getDeviceId(), alarmType);
146 if (alarm.isPresent()) { 157 if (alarm.isPresent()) {
@@ -32,6 +32,7 @@ public class AlarmProcessorConfiguration { @@ -32,6 +32,7 @@ public class AlarmProcessorConfiguration {
32 private String alarmSeverity; 32 private String alarmSeverity;
33 private String alarmStatus; 33 private String alarmStatus;
34 private boolean alarmPropagateFlag; 34 private boolean alarmPropagateFlag;
  35 + private boolean newAlarmFlag;
35 36
36 private String alarmDetailsTemplate; 37 private String alarmDetailsTemplate;
37 38
@@ -30,6 +30,11 @@ @@ -30,6 +30,11 @@
30 "type": "boolean", 30 "type": "boolean",
31 "default": true 31 "default": true
32 }, 32 },
  33 + "newAlarmFlag": {
  34 + "title": "New Alarm on each event",
  35 + "type": "boolean",
  36 + "default": false
  37 + },
33 "alarmDetailsTemplate": { 38 "alarmDetailsTemplate": {
34 "title": "Alarm details (JSON)", 39 "title": "Alarm details (JSON)",
35 "type": "string", 40 "type": "string",
@@ -106,6 +111,7 @@ @@ -106,6 +111,7 @@
106 }, 111 },
107 "alarmTypeTemplate", 112 "alarmTypeTemplate",
108 "alarmPropagateFlag", 113 "alarmPropagateFlag",
  114 + "newAlarmFlag",
109 { 115 {
110 "key": "alarmDetailsTemplate", 116 "key": "alarmDetailsTemplate",
111 "type": "textarea", 117 "type": "textarea",