Commit 702a09132901836b9edea9e238c5b2ed9af5a7a1

Authored by ShvaykaD
Committed by Andrew Shvayka
1 parent 8bd617e3

fix/AlarmActionEvents

@@ -252,26 +252,26 @@ class DefaultTbContext implements TbContext { @@ -252,26 +252,26 @@ class DefaultTbContext implements TbContext {
252 } 252 }
253 253
254 public TbMsg customerCreatedMsg(Customer customer, RuleNodeId ruleNodeId) { 254 public TbMsg customerCreatedMsg(Customer customer, RuleNodeId ruleNodeId) {
255 - return entityCreatedMsg(customer, customer.getId(), ruleNodeId); 255 + return entityActionMsg(customer, customer.getId(), ruleNodeId, DataConstants.ENTITY_CREATED);
256 } 256 }
257 257
258 public TbMsg deviceCreatedMsg(Device device, RuleNodeId ruleNodeId) { 258 public TbMsg deviceCreatedMsg(Device device, RuleNodeId ruleNodeId) {
259 - return entityCreatedMsg(device, device.getId(), ruleNodeId); 259 + return entityActionMsg(device, device.getId(), ruleNodeId, DataConstants.ENTITY_CREATED);
260 } 260 }
261 261
262 public TbMsg assetCreatedMsg(Asset asset, RuleNodeId ruleNodeId) { 262 public TbMsg assetCreatedMsg(Asset asset, RuleNodeId ruleNodeId) {
263 - return entityCreatedMsg(asset, asset.getId(), ruleNodeId); 263 + return entityActionMsg(asset, asset.getId(), ruleNodeId, DataConstants.ENTITY_CREATED);
264 } 264 }
265 265
266 - public TbMsg alarmCreatedMsg(Alarm alarm, RuleNodeId ruleNodeId) {  
267 - return entityCreatedMsg(alarm, alarm.getId(), ruleNodeId); 266 + public TbMsg alarmActionMsg(Alarm alarm, RuleNodeId ruleNodeId, String action) {
  267 + return entityActionMsg(alarm, alarm.getId(), ruleNodeId, action);
268 } 268 }
269 269
270 - public <E, I extends EntityId> TbMsg entityCreatedMsg(E entity, I id, RuleNodeId ruleNodeId) { 270 + public <E, I extends EntityId> TbMsg entityActionMsg(E entity, I id, RuleNodeId ruleNodeId, String action) {
271 try { 271 try {
272 - return TbMsg.newMsg(DataConstants.ENTITY_CREATED, id, getActionMetaData(ruleNodeId), mapper.writeValueAsString(mapper.valueToTree(entity))); 272 + return TbMsg.newMsg(action, id, getActionMetaData(ruleNodeId), mapper.writeValueAsString(mapper.valueToTree(entity)));
273 } catch (JsonProcessingException | IllegalArgumentException e) { 273 } catch (JsonProcessingException | IllegalArgumentException e) {
274 - throw new RuntimeException("Failed to process " + id.getEntityType().name().toLowerCase() + " created msg: " + e); 274 + throw new RuntimeException("Failed to process " + id.getEntityType().name().toLowerCase() + " " + action + " msg: " + e);
275 } 275 }
276 } 276 }
277 277
@@ -124,6 +124,7 @@ public class AlarmController extends BaseController { @@ -124,6 +124,7 @@ public class AlarmController extends BaseController {
124 long ackTs = System.currentTimeMillis(); 124 long ackTs = System.currentTimeMillis();
125 alarmService.ackAlarm(getCurrentUser().getTenantId(), alarmId, ackTs).get(); 125 alarmService.ackAlarm(getCurrentUser().getTenantId(), alarmId, ackTs).get();
126 alarm.setAckTs(ackTs); 126 alarm.setAckTs(ackTs);
  127 + alarm.setStatus(alarm.getStatus().isCleared() ? AlarmStatus.CLEARED_ACK : AlarmStatus.ACTIVE_ACK);
127 logEntityAction(alarmId, alarm, getCurrentUser().getCustomerId(), ActionType.ALARM_ACK, null); 128 logEntityAction(alarmId, alarm, getCurrentUser().getCustomerId(), ActionType.ALARM_ACK, null);
128 } catch (Exception e) { 129 } catch (Exception e) {
129 throw handleException(e); 130 throw handleException(e);
@@ -141,6 +142,7 @@ public class AlarmController extends BaseController { @@ -141,6 +142,7 @@ public class AlarmController extends BaseController {
141 long clearTs = System.currentTimeMillis(); 142 long clearTs = System.currentTimeMillis();
142 alarmService.clearAlarm(getCurrentUser().getTenantId(), alarmId, null, clearTs).get(); 143 alarmService.clearAlarm(getCurrentUser().getTenantId(), alarmId, null, clearTs).get();
143 alarm.setClearTs(clearTs); 144 alarm.setClearTs(clearTs);
  145 + alarm.setStatus(alarm.getStatus().isAck() ? AlarmStatus.CLEARED_ACK : AlarmStatus.CLEARED_UNACK);
144 logEntityAction(alarmId, alarm, getCurrentUser().getCustomerId(), ActionType.ALARM_CLEAR, null); 146 logEntityAction(alarmId, alarm, getCurrentUser().getCustomerId(), ActionType.ALARM_CLEAR, null);
145 } catch (Exception e) { 147 } catch (Exception e) {
146 throw handleException(e); 148 throw handleException(e);
@@ -141,7 +141,7 @@ public interface TbContext { @@ -141,7 +141,7 @@ public interface TbContext {
141 TbMsg assetCreatedMsg(Asset asset, RuleNodeId ruleNodeId); 141 TbMsg assetCreatedMsg(Asset asset, RuleNodeId ruleNodeId);
142 142
143 // TODO: Does this changes the message? 143 // TODO: Does this changes the message?
144 - TbMsg alarmCreatedMsg(Alarm alarm, RuleNodeId ruleNodeId); 144 + TbMsg alarmActionMsg(Alarm alarm, RuleNodeId ruleNodeId, String action);
145 145
146 /* 146 /*
147 * 147 *
@@ -25,6 +25,7 @@ import org.thingsboard.rule.engine.api.TbContext; @@ -25,6 +25,7 @@ import org.thingsboard.rule.engine.api.TbContext;
25 import org.thingsboard.rule.engine.api.TbNode; 25 import org.thingsboard.rule.engine.api.TbNode;
26 import org.thingsboard.rule.engine.api.TbNodeConfiguration; 26 import org.thingsboard.rule.engine.api.TbNodeConfiguration;
27 import org.thingsboard.rule.engine.api.TbNodeException; 27 import org.thingsboard.rule.engine.api.TbNodeException;
  28 +import org.thingsboard.server.common.data.DataConstants;
28 import org.thingsboard.server.common.data.alarm.Alarm; 29 import org.thingsboard.server.common.data.alarm.Alarm;
29 import org.thingsboard.server.common.msg.TbMsg; 30 import org.thingsboard.server.common.msg.TbMsg;
30 import org.thingsboard.server.common.msg.TbMsgMetaData; 31 import org.thingsboard.server.common.msg.TbMsgMetaData;
@@ -61,13 +62,11 @@ public abstract class TbAbstractAlarmNode<C extends TbAbstractAlarmNodeConfigura @@ -61,13 +62,11 @@ public abstract class TbAbstractAlarmNode<C extends TbAbstractAlarmNodeConfigura
61 if (alarmResult.alarm == null) { 62 if (alarmResult.alarm == null) {
62 ctx.tellNext(msg, "False"); 63 ctx.tellNext(msg, "False");
63 } else if (alarmResult.isCreated) { 64 } else if (alarmResult.isCreated) {
64 - ctx.enqueue(ctx.alarmCreatedMsg(alarmResult.alarm, ctx.getSelfId()),  
65 - () -> ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), "Created"),  
66 - throwable -> ctx.tellFailure(toAlarmMsg(ctx, alarmResult, msg), throwable)); 65 + tellNext(ctx, msg, alarmResult, DataConstants.ENTITY_CREATED, "Created");
67 } else if (alarmResult.isUpdated) { 66 } else if (alarmResult.isUpdated) {
68 - ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), "Updated"); 67 + tellNext(ctx, msg, alarmResult, DataConstants.ENTITY_UPDATED, "Updated");
69 } else if (alarmResult.isCleared) { 68 } else if (alarmResult.isCleared) {
70 - ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), "Cleared"); 69 + tellNext(ctx, msg, alarmResult, DataConstants.ALARM_CLEAR, "Cleared");
71 } else { 70 } else {
72 ctx.tellSuccess(msg); 71 ctx.tellSuccess(msg);
73 } 72 }
@@ -126,4 +125,10 @@ public abstract class TbAbstractAlarmNode<C extends TbAbstractAlarmNodeConfigura @@ -126,4 +125,10 @@ public abstract class TbAbstractAlarmNode<C extends TbAbstractAlarmNodeConfigura
126 this.alarm = alarm; 125 this.alarm = alarm;
127 } 126 }
128 } 127 }
  128 +
  129 + private void tellNext(TbContext ctx, TbMsg msg, AlarmResult alarmResult, String alarmAction, String alarmResultMsgType) {
  130 + ctx.enqueue(ctx.alarmActionMsg(alarmResult.alarm, ctx.getSelfId(), alarmAction),
  131 + () -> ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), alarmResultMsgType),
  132 + throwable -> ctx.tellFailure(toAlarmMsg(ctx, alarmResult, msg), throwable));
  133 + }
129 } 134 }