Commit 67e8bf8c2fe2600f3a7d730f0ef52e268b7b65d2

Authored by ShvaykaD
Committed by Andrew Shvayka
1 parent 55f26ebf

push ENTITY_UPDATED events on ALARM updates,clears

... ... @@ -257,26 +257,26 @@ class DefaultTbContext implements TbContext {
257 257 }
258 258
259 259 public TbMsg customerCreatedMsg(Customer customer, RuleNodeId ruleNodeId) {
260   - return entityCreatedMsg(customer, customer.getId(), ruleNodeId);
  260 + return entityActionMsg(customer, customer.getId(), ruleNodeId, DataConstants.ENTITY_CREATED);
261 261 }
262 262
263 263 public TbMsg deviceCreatedMsg(Device device, RuleNodeId ruleNodeId) {
264   - return entityCreatedMsg(device, device.getId(), ruleNodeId);
  264 + return entityActionMsg(device, device.getId(), ruleNodeId, DataConstants.ENTITY_CREATED);
265 265 }
266 266
267 267 public TbMsg assetCreatedMsg(Asset asset, RuleNodeId ruleNodeId) {
268   - return entityCreatedMsg(asset, asset.getId(), ruleNodeId);
  268 + return entityActionMsg(asset, asset.getId(), ruleNodeId, DataConstants.ENTITY_CREATED);
269 269 }
270 270
271   - public TbMsg alarmCreatedMsg(Alarm alarm, RuleNodeId ruleNodeId) {
272   - return entityCreatedMsg(alarm, alarm.getId(), ruleNodeId);
  271 + public TbMsg alarmActionMsg(Alarm alarm, RuleNodeId ruleNodeId, String action) {
  272 + return entityActionMsg(alarm, alarm.getId(), ruleNodeId, action);
273 273 }
274 274
275   - public <E, I extends EntityId> TbMsg entityCreatedMsg(E entity, I id, RuleNodeId ruleNodeId) {
  275 + public <E, I extends EntityId> TbMsg entityActionMsg(E entity, I id, RuleNodeId ruleNodeId, String action) {
276 276 try {
277   - return TbMsg.newMsg(DataConstants.ENTITY_CREATED, id, getActionMetaData(ruleNodeId), mapper.writeValueAsString(mapper.valueToTree(entity)));
  277 + return TbMsg.newMsg(action, id, getActionMetaData(ruleNodeId), mapper.writeValueAsString(mapper.valueToTree(entity)));
278 278 } catch (JsonProcessingException | IllegalArgumentException e) {
279   - throw new RuntimeException("Failed to process " + id.getEntityType().name().toLowerCase() + " created msg: " + e);
  279 + throw new RuntimeException("Failed to process " + id.getEntityType().name().toLowerCase() + " " + action + " msg: " + e);
280 280 }
281 281 }
282 282
... ...
... ... @@ -144,7 +144,7 @@ public interface TbContext {
144 144 TbMsg assetCreatedMsg(Asset asset, RuleNodeId ruleNodeId);
145 145
146 146 // TODO: Does this changes the message?
147   - TbMsg alarmCreatedMsg(Alarm alarm, RuleNodeId ruleNodeId);
  147 + TbMsg alarmActionMsg(Alarm alarm, RuleNodeId ruleNodeId, String action);
148 148
149 149 /*
150 150 *
... ...
... ... @@ -58,13 +58,11 @@ public abstract class TbAbstractAlarmNode<C extends TbAbstractAlarmNodeConfigura
58 58 if (alarmResult.alarm == null) {
59 59 ctx.tellNext(msg, "False");
60 60 } else if (alarmResult.isCreated) {
61   - ctx.enqueue(ctx.alarmCreatedMsg(alarmResult.alarm, ctx.getSelfId()),
62   - () -> ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), "Created"),
63   - throwable -> ctx.tellFailure(toAlarmMsg(ctx, alarmResult, msg), throwable));
  61 + tellNext(ctx, msg, alarmResult, DataConstants.ENTITY_CREATED, "Created");
64 62 } else if (alarmResult.isUpdated) {
65   - ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), "Updated");
  63 + tellNext(ctx, msg, alarmResult, DataConstants.ENTITY_UPDATED, "Updated");
66 64 } else if (alarmResult.isCleared) {
67   - ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), "Cleared");
  65 + tellNext(ctx, msg, alarmResult, DataConstants.ENTITY_UPDATED, "Cleared");
68 66 } else {
69 67 ctx.tellSuccess(msg);
70 68 }
... ... @@ -109,4 +107,10 @@ public abstract class TbAbstractAlarmNode<C extends TbAbstractAlarmNodeConfigura
109 107 }
110 108 }
111 109
  110 + private void tellNext(TbContext ctx, TbMsg msg, TbAlarmResult alarmResult, String entityAction, String alarmAction) {
  111 + ctx.enqueue(ctx.alarmActionMsg(alarmResult.alarm, ctx.getSelfId(), entityAction),
  112 + () -> ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), alarmAction),
  113 + throwable -> ctx.tellFailure(toAlarmMsg(ctx, alarmResult, msg), throwable));
  114 + }
  115 +
112 116 }
... ...