Commit 1297fef550e8789902b49e1c55d2b4e6f919fd18

Authored by Volodymyr Babak
1 parent 0ce509e8

Fixed alarm delete functionality on edge

... ... @@ -38,6 +38,7 @@ import org.thingsboard.server.common.data.edge.EdgeEventActionType;
38 38 import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
39 39 import org.thingsboard.server.common.data.exception.ThingsboardException;
40 40 import org.thingsboard.server.common.data.id.AlarmId;
  41 +import org.thingsboard.server.common.data.id.EdgeId;
41 42 import org.thingsboard.server.common.data.id.EntityId;
42 43 import org.thingsboard.server.common.data.id.EntityIdFactory;
43 44 import org.thingsboard.server.common.data.page.PageData;
... ... @@ -46,6 +47,8 @@ import org.thingsboard.server.queue.util.TbCoreComponent;
46 47 import org.thingsboard.server.service.security.permission.Operation;
47 48 import org.thingsboard.server.service.security.permission.Resource;
48 49
  50 +import java.util.List;
  51 +
49 52 @RestController
50 53 @TbCoreComponent
51 54 @RequestMapping("/api")
... ... @@ -112,10 +115,13 @@ public class AlarmController extends BaseController {
112 115 AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
113 116 Alarm alarm = checkAlarmId(alarmId, Operation.WRITE);
114 117
  118 + List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), alarm.getOriginator());
  119 +
115 120 logEntityAction(alarm.getOriginator(), alarm,
116 121 getCurrentUser().getCustomerId(),
117 122 ActionType.ALARM_DELETE, null);
118   - sendEntityNotificationMsg(getTenantId(), alarmId, EdgeEventActionType.DELETED);
  123 +
  124 + sendAlarmDeleteNotificationMsg(getTenantId(), alarmId, relatedEdgeIds, alarm);
119 125
120 126 return alarmService.deleteAlarm(getTenantId(), alarmId);
121 127 } catch (Exception e) {
... ...
... ... @@ -852,13 +852,25 @@ public abstract class BaseController {
852 852 }
853 853
854 854 protected void sendDeleteNotificationMsg(TenantId tenantId, EntityId entityId, List<EdgeId> edgeIds) {
  855 + sendDeleteNotificationMsg(tenantId, entityId, edgeIds, null);
  856 + }
  857 +
  858 + protected void sendDeleteNotificationMsg(TenantId tenantId, EntityId entityId, List<EdgeId> edgeIds, String body) {
855 859 if (edgeIds != null && !edgeIds.isEmpty()) {
856 860 for (EdgeId edgeId : edgeIds) {
857   - sendNotificationMsgToEdgeService(tenantId, edgeId, entityId, null, null, EdgeEventActionType.DELETED);
  861 + sendNotificationMsgToEdgeService(tenantId, edgeId, entityId, body, null, EdgeEventActionType.DELETED);
858 862 }
859 863 }
860 864 }
861 865
  866 + protected void sendAlarmDeleteNotificationMsg(TenantId tenantId, EntityId entityId, List<EdgeId> edgeIds, Alarm alarm) {
  867 + try {
  868 + sendDeleteNotificationMsg(tenantId, entityId, edgeIds, json.writeValueAsString(alarm));
  869 + } catch (Exception e) {
  870 + log.warn("Failed to push delete alarm msg to core: {}", alarm, e);
  871 + }
  872 + }
  873 +
862 874 protected void sendEntityAssignToCustomerNotificationMsg(TenantId tenantId, EntityId entityId, CustomerId customerId, EdgeEventActionType action) {
863 875 try {
864 876 sendNotificationMsgToEdgeService(tenantId, null, entityId, json.writeValueAsString(customerId), null, action);
... ...
... ... @@ -121,6 +121,7 @@ public class DefaultEdgeNotificationService implements EdgeNotificationService {
121 121
122 122 @Override
123 123 public void pushNotificationToEdge(TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg, TbCallback callback) {
  124 + log.trace("Pushing notification to edge {}", edgeNotificationMsg);
124 125 try {
125 126 TenantId tenantId = new TenantId(new UUID(edgeNotificationMsg.getTenantIdMSB(), edgeNotificationMsg.getTenantIdLSB()));
126 127 EdgeEventType type = EdgeEventType.valueOf(edgeNotificationMsg.getType());
... ...
... ... @@ -527,7 +527,7 @@ public final class EdgeGrpcSession implements Closeable {
527 527 case RULE_CHAIN_METADATA:
528 528 return ctx.getRuleChainProcessor().processRuleChainMetadataToEdge(edgeEvent, msgType);
529 529 case ALARM:
530   - return ctx.getAlarmProcessor().processAlarmToEdge(edge, edgeEvent, msgType);
  530 + return ctx.getAlarmProcessor().processAlarmToEdge(edge, edgeEvent, msgType, action);
531 531 case USER:
532 532 return ctx.getUserProcessor().processUserToEdge(edge, edgeEvent, msgType, action);
533 533 case RELATION:
... ...
... ... @@ -15,6 +15,7 @@
15 15 */
16 16 package org.thingsboard.server.service.edge.rpc.processor;
17 17
  18 +import com.fasterxml.jackson.core.JsonProcessingException;
18 19 import com.google.common.util.concurrent.FutureCallback;
19 20 import com.google.common.util.concurrent.Futures;
20 21 import com.google.common.util.concurrent.ListenableFuture;
... ... @@ -114,59 +115,84 @@ public class AlarmEdgeProcessor extends BaseEdgeProcessor {
114 115 }
115 116 }
116 117
117   - public DownlinkMsg processAlarmToEdge(Edge edge, EdgeEvent edgeEvent, UpdateMsgType msgType) {
  118 + public DownlinkMsg processAlarmToEdge(Edge edge, EdgeEvent edgeEvent, UpdateMsgType msgType, EdgeEventActionType action) {
  119 + AlarmId alarmId = new AlarmId(edgeEvent.getEntityId());
118 120 DownlinkMsg downlinkMsg = null;
119   - try {
120   - AlarmId alarmId = new AlarmId(edgeEvent.getEntityId());
121   - Alarm alarm = alarmService.findAlarmByIdAsync(edgeEvent.getTenantId(), alarmId).get();
122   - if (alarm != null) {
  121 + switch (action) {
  122 + case ADDED:
  123 + case UPDATED:
  124 + case ALARM_ACK:
  125 + case ALARM_CLEAR:
  126 + try {
  127 + Alarm alarm = alarmService.findAlarmByIdAsync(edgeEvent.getTenantId(), alarmId).get();
  128 + if (alarm != null) {
  129 + downlinkMsg = DownlinkMsg.newBuilder()
  130 + .setDownlinkMsgId(EdgeUtils.nextPositiveInt())
  131 + .addAlarmUpdateMsg(alarmMsgConstructor.constructAlarmUpdatedMsg(edge.getTenantId(), msgType, alarm))
  132 + .build();
  133 + }
  134 + } catch (Exception e) {
  135 + log.error("Can't process alarm msg [{}] [{}]", edgeEvent, msgType, e);
  136 + }
  137 + break;
  138 + case DELETED:
  139 + Alarm alarm = mapper.convertValue(edgeEvent.getBody(), Alarm.class);
  140 + AlarmUpdateMsg alarmUpdateMsg =
  141 + alarmMsgConstructor.constructAlarmUpdatedMsg(edge.getTenantId(), msgType, alarm);
123 142 downlinkMsg = DownlinkMsg.newBuilder()
124 143 .setDownlinkMsgId(EdgeUtils.nextPositiveInt())
125   - .addAlarmUpdateMsg(alarmMsgConstructor.constructAlarmUpdatedMsg(edge.getTenantId(), msgType, alarm))
  144 + .addAlarmUpdateMsg(alarmUpdateMsg)
126 145 .build();
127   - }
128   - } catch (Exception e) {
129   - log.error("Can't process alarm msg [{}] [{}]", edgeEvent, msgType, e);
  146 + break;
130 147 }
131 148 return downlinkMsg;
132 149 }
133 150
134   - public void processAlarmNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) {
  151 + public void processAlarmNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) throws JsonProcessingException {
  152 + EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction());
135 153 AlarmId alarmId = new AlarmId(new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB()));
136   - ListenableFuture<Alarm> alarmFuture = alarmService.findAlarmByIdAsync(tenantId, alarmId);
137   - Futures.addCallback(alarmFuture, new FutureCallback<Alarm>() {
138   - @Override
139   - public void onSuccess(@Nullable Alarm alarm) {
140   - if (alarm != null) {
141   - EdgeEventType type = EdgeUtils.getEdgeEventTypeByEntityType(alarm.getOriginator().getEntityType());
142   - if (type != null) {
143   - PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
144   - PageData<EdgeId> pageData;
145   - do {
146   - pageData = edgeService.findRelatedEdgeIdsByEntityId(tenantId, alarm.getOriginator(), pageLink);
147   - if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
148   - for (EdgeId edgeId : pageData.getData()) {
149   - saveEdgeEvent(tenantId,
150   - edgeId,
151   - EdgeEventType.ALARM,
152   - EdgeEventActionType.valueOf(edgeNotificationMsg.getAction()),
153   - alarmId,
154   - null);
155   - }
156   - if (pageData.hasNext()) {
157   - pageLink = pageLink.nextPageLink();
158   - }
  154 + switch (actionType) {
  155 + case DELETED:
  156 + EdgeId edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()));
  157 + Alarm alarm = mapper.readValue(edgeNotificationMsg.getBody(), Alarm.class);
  158 + saveEdgeEvent(tenantId, edgeId, EdgeEventType.ALARM, actionType, alarmId, mapper.valueToTree(alarm));
  159 + break;
  160 + default:
  161 + ListenableFuture<Alarm> alarmFuture = alarmService.findAlarmByIdAsync(tenantId, alarmId);
  162 + Futures.addCallback(alarmFuture, new FutureCallback<Alarm>() {
  163 + @Override
  164 + public void onSuccess(@Nullable Alarm alarm) {
  165 + if (alarm != null) {
  166 + EdgeEventType type = EdgeUtils.getEdgeEventTypeByEntityType(alarm.getOriginator().getEntityType());
  167 + if (type != null) {
  168 + PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
  169 + PageData<EdgeId> pageData;
  170 + do {
  171 + pageData = edgeService.findRelatedEdgeIdsByEntityId(tenantId, alarm.getOriginator(), pageLink);
  172 + if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
  173 + for (EdgeId edgeId : pageData.getData()) {
  174 + saveEdgeEvent(tenantId,
  175 + edgeId,
  176 + EdgeEventType.ALARM,
  177 + EdgeEventActionType.valueOf(edgeNotificationMsg.getAction()),
  178 + alarmId,
  179 + null);
  180 + }
  181 + if (pageData.hasNext()) {
  182 + pageLink = pageLink.nextPageLink();
  183 + }
  184 + }
  185 + } while (pageData != null && pageData.hasNext());
159 186 }
160   - } while (pageData != null && pageData.hasNext());
  187 + }
161 188 }
162   - }
163   - }
164 189
165   - @Override
166   - public void onFailure(Throwable t) {
167   - log.warn("[{}] can't find alarm by id [{}] {}", tenantId.getId(), alarmId.getId(), t);
168   - }
169   - }, dbCallbackExecutorService);
  190 + @Override
  191 + public void onFailure(Throwable t) {
  192 + log.warn("[{}] can't find alarm by id [{}] {}", tenantId.getId(), alarmId.getId(), t);
  193 + }
  194 + }, dbCallbackExecutorService);
  195 + }
170 196 }
171 197
172 198 }
... ...
... ... @@ -337,7 +337,7 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
337 337 addTimePageLinkToParam(params, pageLink);
338 338
339 339 return restTemplate.exchange(
340   - baseURL + urlSecondPart + getTimeUrlParams(pageLink),
  340 + baseURL + urlSecondPart + "&" + getTimeUrlParams(pageLink),
341 341 HttpMethod.GET,
342 342 HttpEntity.EMPTY,
343 343 new ParameterizedTypeReference<PageData<AlarmInfo>>() {
... ...