Commit 702a09132901836b9edea9e238c5b2ed9af5a7a1

Authored by ShvaykaD
Committed by Andrew Shvayka
1 parent 8bd617e3

fix/AlarmActionEvents

... ... @@ -252,26 +252,26 @@ class DefaultTbContext implements TbContext {
252 252 }
253 253
254 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 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 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 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 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 124 long ackTs = System.currentTimeMillis();
125 125 alarmService.ackAlarm(getCurrentUser().getTenantId(), alarmId, ackTs).get();
126 126 alarm.setAckTs(ackTs);
  127 + alarm.setStatus(alarm.getStatus().isCleared() ? AlarmStatus.CLEARED_ACK : AlarmStatus.ACTIVE_ACK);
127 128 logEntityAction(alarmId, alarm, getCurrentUser().getCustomerId(), ActionType.ALARM_ACK, null);
128 129 } catch (Exception e) {
129 130 throw handleException(e);
... ... @@ -141,6 +142,7 @@ public class AlarmController extends BaseController {
141 142 long clearTs = System.currentTimeMillis();
142 143 alarmService.clearAlarm(getCurrentUser().getTenantId(), alarmId, null, clearTs).get();
143 144 alarm.setClearTs(clearTs);
  145 + alarm.setStatus(alarm.getStatus().isAck() ? AlarmStatus.CLEARED_ACK : AlarmStatus.CLEARED_UNACK);
144 146 logEntityAction(alarmId, alarm, getCurrentUser().getCustomerId(), ActionType.ALARM_CLEAR, null);
145 147 } catch (Exception e) {
146 148 throw handleException(e);
... ...
... ... @@ -141,7 +141,7 @@ public interface TbContext {
141 141 TbMsg assetCreatedMsg(Asset asset, RuleNodeId ruleNodeId);
142 142
143 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 25 import org.thingsboard.rule.engine.api.TbNode;
26 26 import org.thingsboard.rule.engine.api.TbNodeConfiguration;
27 27 import org.thingsboard.rule.engine.api.TbNodeException;
  28 +import org.thingsboard.server.common.data.DataConstants;
28 29 import org.thingsboard.server.common.data.alarm.Alarm;
29 30 import org.thingsboard.server.common.msg.TbMsg;
30 31 import org.thingsboard.server.common.msg.TbMsgMetaData;
... ... @@ -61,13 +62,11 @@ public abstract class TbAbstractAlarmNode<C extends TbAbstractAlarmNodeConfigura
61 62 if (alarmResult.alarm == null) {
62 63 ctx.tellNext(msg, "False");
63 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 66 } else if (alarmResult.isUpdated) {
68   - ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), "Updated");
  67 + tellNext(ctx, msg, alarmResult, DataConstants.ENTITY_UPDATED, "Updated");
69 68 } else if (alarmResult.isCleared) {
70   - ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), "Cleared");
  69 + tellNext(ctx, msg, alarmResult, DataConstants.ALARM_CLEAR, "Cleared");
71 70 } else {
72 71 ctx.tellSuccess(msg);
73 72 }
... ... @@ -126,4 +125,10 @@ public abstract class TbAbstractAlarmNode<C extends TbAbstractAlarmNodeConfigura
126 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 }
... ...