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 41 import javax.script.ScriptException;
42 42 import java.io.IOException;
43 43 import java.util.List;
  44 +import java.util.concurrent.ExecutionException;
44 45
45 46 /**
46 47 * @author Andrew Shvayka
... ... @@ -131,6 +132,16 @@ public class AlarmProcessor implements RuleProcessor<AlarmProcessorConfiguration
131 132 Alarm existing = null;
132 133 if (isActiveAlarm) {
133 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 145 existing = ctx.createOrUpdateAlarm(alarm);
135 146 if (existing.getStartTs() == alarm.getStartTs()) {
136 147 log.debug("[{}][{}] New Active Alarm detected", ctx.getRuleId(), existing.getId());
... ... @@ -140,7 +151,7 @@ public class AlarmProcessor implements RuleProcessor<AlarmProcessorConfiguration
140 151 log.debug("[{}][{}] Existing Active Alarm detected", ctx.getRuleId(), existing.getId());
141 152 md.put(IS_EXISTING_ALARM, Boolean.TRUE);
142 153 }
143   - } else if (isClearedAlarm) {
  154 + } else {
144 155 String alarmType = VelocityUtils.merge(alarmTypeTemplate, context);
145 156 Optional<Alarm> alarm = ctx.findLatestAlarm(ctx.getDeviceMetaData().getDeviceId(), alarmType);
146 157 if (alarm.isPresent()) {
... ...
... ... @@ -32,6 +32,7 @@ public class AlarmProcessorConfiguration {
32 32 private String alarmSeverity;
33 33 private String alarmStatus;
34 34 private boolean alarmPropagateFlag;
  35 + private boolean newAlarmFlag;
35 36
36 37 private String alarmDetailsTemplate;
37 38
... ...
... ... @@ -30,6 +30,11 @@
30 30 "type": "boolean",
31 31 "default": true
32 32 },
  33 + "newAlarmFlag": {
  34 + "title": "New Alarm on each event",
  35 + "type": "boolean",
  36 + "default": false
  37 + },
33 38 "alarmDetailsTemplate": {
34 39 "title": "Alarm details (JSON)",
35 40 "type": "string",
... ... @@ -106,6 +111,7 @@
106 111 },
107 112 "alarmTypeTemplate",
108 113 "alarmPropagateFlag",
  114 + "newAlarmFlag",
109 115 {
110 116 "key": "alarmDetailsTemplate",
111 117 "type": "textarea",
... ...