Commit e30ec49d2bfc9a2e6c35e7c88370e8f779361d9f
Committed by
GitHub
1 parent
0a2477da
[3.3] [PROD-806] Implement alarm condition metadata displaying (#4185)
* Implement alarm condition metadata displaying * Refactor
Showing
3 changed files
with
104 additions
and
4 deletions
... | ... | @@ -33,6 +33,8 @@ public class DataConstants { |
33 | 33 | public static final String IS_EXISTING_ALARM = "isExistingAlarm"; |
34 | 34 | public static final String IS_SEVERITY_UPDATED_ALARM = "isSeverityUpdated"; |
35 | 35 | public static final String IS_CLEARED_ALARM = "isClearedAlarm"; |
36 | + public static final String ALARM_CONDITION_REPEATS = "alarmConditionRepeats"; | |
37 | + public static final String ALARM_CONDITION_DURATION = "alarmConditionDuration"; | |
36 | 38 | |
37 | 39 | public static final String[] allScopes() { |
38 | 40 | return new String[]{CLIENT_SCOPE, SHARED_SCOPE, SERVER_SCOPE}; | ... | ... |
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/AlarmState.java
... | ... | @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; |
20 | 20 | import lombok.Data; |
21 | 21 | import lombok.extern.slf4j.Slf4j; |
22 | 22 | import org.apache.commons.lang3.StringUtils; |
23 | +import org.thingsboard.common.util.JacksonUtil; | |
23 | 24 | import org.thingsboard.rule.engine.action.TbAlarmResult; |
24 | 25 | import org.thingsboard.rule.engine.api.TbContext; |
25 | 26 | import org.thingsboard.rule.engine.profile.state.PersistedAlarmRuleState; |
... | ... | @@ -28,6 +29,7 @@ import org.thingsboard.server.common.data.DataConstants; |
28 | 29 | import org.thingsboard.server.common.data.alarm.Alarm; |
29 | 30 | import org.thingsboard.server.common.data.alarm.AlarmSeverity; |
30 | 31 | import org.thingsboard.server.common.data.alarm.AlarmStatus; |
32 | +import org.thingsboard.server.common.data.device.profile.AlarmConditionSpecType; | |
31 | 33 | import org.thingsboard.server.common.data.device.profile.AlarmConditionKeyType; |
32 | 34 | import org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm; |
33 | 35 | import org.thingsboard.server.common.data.id.EntityId; |
... | ... | @@ -36,7 +38,6 @@ import org.thingsboard.server.common.data.query.KeyFilter; |
36 | 38 | import org.thingsboard.server.common.msg.TbMsg; |
37 | 39 | import org.thingsboard.server.common.msg.TbMsgMetaData; |
38 | 40 | import org.thingsboard.server.common.msg.queue.ServiceQueue; |
39 | -import org.thingsboard.common.util.JacksonUtil; | |
40 | 41 | |
41 | 42 | import java.util.ArrayList; |
42 | 43 | import java.util.Comparator; |
... | ... | @@ -101,7 +102,7 @@ class AlarmState { |
101 | 102 | if (resultState != null) { |
102 | 103 | TbAlarmResult result = calculateAlarmResult(ctx, resultState); |
103 | 104 | if (result != null) { |
104 | - pushMsg(ctx, result); | |
105 | + pushMsg(ctx, result, resultState); | |
105 | 106 | } |
106 | 107 | stateUpdate = clearAlarmState(stateUpdate, clearState); |
107 | 108 | } else if (currentAlarm != null && clearState != null) { |
... | ... | @@ -116,7 +117,7 @@ class AlarmState { |
116 | 117 | stateUpdate = clearAlarmState(stateUpdate, state); |
117 | 118 | } |
118 | 119 | ctx.getAlarmService().clearAlarm(ctx.getTenantId(), currentAlarm.getId(), createDetails(clearState), System.currentTimeMillis()); |
119 | - pushMsg(ctx, new TbAlarmResult(false, false, true, currentAlarm)); | |
120 | + pushMsg(ctx, new TbAlarmResult(false, false, true, currentAlarm), clearState); | |
120 | 121 | currentAlarm = null; |
121 | 122 | } else if (AlarmEvalResult.FALSE.equals(evalResult)) { |
122 | 123 | stateUpdate = clearAlarmState(stateUpdate, clearState); |
... | ... | @@ -155,7 +156,7 @@ class AlarmState { |
155 | 156 | } |
156 | 157 | } |
157 | 158 | |
158 | - public void pushMsg(TbContext ctx, TbAlarmResult alarmResult) { | |
159 | + public void pushMsg(TbContext ctx, TbAlarmResult alarmResult, AlarmRuleState ruleState) { | |
159 | 160 | JsonNode jsonNodes = JacksonUtil.valueToTree(alarmResult.getAlarm()); |
160 | 161 | String data = jsonNodes.toString(); |
161 | 162 | TbMsgMetaData metaData = lastMsgMetaData != null ? lastMsgMetaData.copy() : new TbMsgMetaData(); |
... | ... | @@ -174,10 +175,20 @@ class AlarmState { |
174 | 175 | relationType = "Alarm Cleared"; |
175 | 176 | metaData.putValue(DataConstants.IS_CLEARED_ALARM, Boolean.TRUE.toString()); |
176 | 177 | } |
178 | + setAlarmConditionMetadata(ruleState, metaData); | |
177 | 179 | TbMsg newMsg = ctx.newMsg(lastMsgQueueName != null ? lastMsgQueueName : ServiceQueue.MAIN, "ALARM", originator, metaData, data); |
178 | 180 | ctx.tellNext(newMsg, relationType); |
179 | 181 | } |
180 | 182 | |
183 | + protected void setAlarmConditionMetadata(AlarmRuleState ruleState, TbMsgMetaData metaData) { | |
184 | + if (ruleState.getSpec().getType() == AlarmConditionSpecType.REPEATING) { | |
185 | + metaData.putValue(DataConstants.ALARM_CONDITION_REPEATS, String.valueOf(ruleState.getState().getEventCount())); | |
186 | + } | |
187 | + if (ruleState.getSpec().getType() == AlarmConditionSpecType.DURATION) { | |
188 | + metaData.putValue(DataConstants.ALARM_CONDITION_DURATION, String.valueOf(ruleState.getState().getDuration())); | |
189 | + } | |
190 | + } | |
191 | + | |
181 | 192 | public void updateState(DeviceProfileAlarm alarm, PersistedAlarmState alarmState) { |
182 | 193 | this.alarmDefinition = alarm; |
183 | 194 | this.createRulesSortedBySeverityDesc = new ArrayList<>(); | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2021 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.rule.engine.profile; | |
17 | + | |
18 | +import org.junit.Test; | |
19 | +import org.thingsboard.server.common.data.DataConstants; | |
20 | +import org.thingsboard.server.common.data.device.profile.AlarmCondition; | |
21 | +import org.thingsboard.server.common.data.device.profile.AlarmConditionSpec; | |
22 | +import org.thingsboard.server.common.data.device.profile.AlarmConditionSpecType; | |
23 | +import org.thingsboard.server.common.data.device.profile.AlarmRule; | |
24 | +import org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm; | |
25 | +import org.thingsboard.server.common.data.device.profile.DurationAlarmConditionSpec; | |
26 | +import org.thingsboard.server.common.data.device.profile.RepeatingAlarmConditionSpec; | |
27 | +import org.thingsboard.server.common.msg.TbMsgMetaData; | |
28 | + | |
29 | +import java.util.concurrent.TimeUnit; | |
30 | + | |
31 | +import static org.junit.Assert.assertEquals; | |
32 | +import static org.junit.Assert.assertNotNull; | |
33 | +import static org.junit.Assert.assertNull; | |
34 | +import static org.mockito.Mockito.mock; | |
35 | + | |
36 | +public class AlarmStateTest { | |
37 | + | |
38 | + @Test | |
39 | + public void testSetAlarmConditionMetadata_repeatingCondition() { | |
40 | + AlarmRuleState ruleState = createMockAlarmRuleState(new RepeatingAlarmConditionSpec()); | |
41 | + int eventCount = 3; | |
42 | + ruleState.getState().setEventCount(eventCount); | |
43 | + | |
44 | + AlarmState alarmState = createMockAlarmState(); | |
45 | + TbMsgMetaData metaData = new TbMsgMetaData(); | |
46 | + | |
47 | + alarmState.setAlarmConditionMetadata(ruleState, metaData); | |
48 | + | |
49 | + assertEquals(AlarmConditionSpecType.REPEATING, ruleState.getSpec().getType()); | |
50 | + assertNotNull(metaData.getValue(DataConstants.ALARM_CONDITION_REPEATS)); | |
51 | + assertNull(metaData.getValue(DataConstants.ALARM_CONDITION_DURATION)); | |
52 | + assertEquals(String.valueOf(eventCount), metaData.getValue(DataConstants.ALARM_CONDITION_REPEATS)); | |
53 | + } | |
54 | + | |
55 | + @Test | |
56 | + public void testSetAlarmConditionMetadata_durationCondition() { | |
57 | + DurationAlarmConditionSpec spec = new DurationAlarmConditionSpec(); | |
58 | + spec.setUnit(TimeUnit.SECONDS); | |
59 | + AlarmRuleState ruleState = createMockAlarmRuleState(spec); | |
60 | + int duration = 12; | |
61 | + ruleState.getState().setDuration(duration); | |
62 | + | |
63 | + AlarmState alarmState = createMockAlarmState(); | |
64 | + TbMsgMetaData metaData = new TbMsgMetaData(); | |
65 | + | |
66 | + alarmState.setAlarmConditionMetadata(ruleState, metaData); | |
67 | + | |
68 | + assertEquals(AlarmConditionSpecType.DURATION, ruleState.getSpec().getType()); | |
69 | + assertNotNull(metaData.getValue(DataConstants.ALARM_CONDITION_DURATION)); | |
70 | + assertNull(metaData.getValue(DataConstants.ALARM_CONDITION_REPEATS)); | |
71 | + assertEquals(String.valueOf(duration), metaData.getValue(DataConstants.ALARM_CONDITION_DURATION)); | |
72 | + } | |
73 | + | |
74 | + private AlarmRuleState createMockAlarmRuleState(AlarmConditionSpec spec) { | |
75 | + AlarmCondition alarmCondition = new AlarmCondition(); | |
76 | + alarmCondition.setSpec(spec); | |
77 | + | |
78 | + AlarmRule alarmRule = new AlarmRule(); | |
79 | + alarmRule.setCondition(alarmCondition); | |
80 | + | |
81 | + return new AlarmRuleState(null, alarmRule, null, null, null); | |
82 | + } | |
83 | + | |
84 | + private AlarmState createMockAlarmState() { | |
85 | + return new AlarmState(null, null, mock(DeviceProfileAlarm.class), null, null); | |
86 | + } | |
87 | +} | ... | ... |