refactoring: 01 - Alarm Common
This commit is contained in:
parent
22b633b8ab
commit
c6a8b9fbaf
@ -82,6 +82,7 @@ import java.util.stream.Collectors;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.CUSTOMER_AUTHORITY_PARAGRAPH;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.CUSTOMER_ID;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.CUSTOMER_ID_PARAM_DESCRIPTION;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_ID;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_ID_PARAM_DESCRIPTION;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_INFO_DESCRIPTION;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_NAME_DESCRIPTION;
|
||||
@ -102,6 +103,7 @@ import static org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_A
|
||||
import static org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_DESCRIPTION;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.SORT_PROPERTY_DESCRIPTION;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.TENANT_AUTHORITY_PARAGRAPH;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.TENANT_ID;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.TENANT_ID_PARAM_DESCRIPTION;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.UUID_WIKI_LINK;
|
||||
@ -114,9 +116,7 @@ import static org.thingsboard.server.controller.EdgeController.EDGE_ID;
|
||||
@Slf4j
|
||||
public class DeviceController extends BaseController {
|
||||
|
||||
protected static final String DEVICE_ID = "deviceId";
|
||||
protected static final String DEVICE_NAME = "deviceName";
|
||||
protected static final String TENANT_ID = "tenantId";
|
||||
|
||||
private final DeviceBulkImportService deviceBulkImportService;
|
||||
|
||||
|
||||
@ -26,6 +26,7 @@ import org.thingsboard.server.common.data.DataConstants;
|
||||
import org.thingsboard.server.common.data.Device;
|
||||
import org.thingsboard.server.common.data.HasName;
|
||||
import org.thingsboard.server.common.data.Tenant;
|
||||
import org.thingsboard.server.common.data.alarm.Alarm;
|
||||
import org.thingsboard.server.common.data.asset.Asset;
|
||||
import org.thingsboard.server.common.data.audit.ActionType;
|
||||
import org.thingsboard.server.common.data.edge.Edge;
|
||||
@ -189,6 +190,18 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyCreateOrUpdateAlarm(Alarm alarm, ActionType actionType, SecurityUser user, Object... additionalInfo) {
|
||||
logEntityAction(alarm.getTenantId(), alarm.getOriginator(), alarm, alarm.getCustomerId(), actionType, user, additionalInfo);
|
||||
sendEntityNotificationMsg(alarm.getTenantId(), alarm.getId(), edgeTypeByActionType (actionType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyDeleteAlarm(Alarm alarm, SecurityUser user, List<EdgeId> relatedEdgeIds) {
|
||||
logEntityAction(alarm.getTenantId(), alarm.getOriginator(), alarm, alarm.getCustomerId(), ActionType.ALARM_DELETE, user, null);
|
||||
sendAlarmDeleteNotificationMsg(alarm, relatedEdgeIds);
|
||||
}
|
||||
|
||||
private <E extends HasName, I extends EntityId> void logEntityAction(TenantId tenantId, I entityId, E entity, CustomerId customerId,
|
||||
ActionType actionType, SecurityUser user, Object... additionalInfo) {
|
||||
logEntityAction(tenantId, entityId, entity, customerId, actionType, user, null, additionalInfo);
|
||||
@ -219,6 +232,13 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS
|
||||
sendDeleteNotificationMsg(tenantId, entityId, edgeIds, null);
|
||||
}
|
||||
|
||||
protected void sendAlarmDeleteNotificationMsg(Alarm alarm, List<EdgeId> relatedEdgeIds) {
|
||||
try {
|
||||
sendDeleteNotificationMsg(alarm.getTenantId(), alarm.getId(), relatedEdgeIds, json.writeValueAsString(alarm));
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to push delete alarm msg to core: {}", alarm, e);
|
||||
}
|
||||
}
|
||||
private void sendDeleteNotificationMsg(TenantId tenantId, EntityId entityId, List<EdgeId> edgeIds, String body) {
|
||||
if (edgeIds != null && !edgeIds.isEmpty()) {
|
||||
for (EdgeId edgeId : edgeIds) {
|
||||
@ -259,4 +279,21 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS
|
||||
return null;
|
||||
}
|
||||
|
||||
private EdgeEventActionType edgeTypeByActionType (ActionType actionType) {
|
||||
switch (actionType) {
|
||||
case ADDED:
|
||||
return EdgeEventActionType.ADDED;
|
||||
case UPDATED:
|
||||
return EdgeEventActionType.UPDATED;
|
||||
case ALARM_ACK:
|
||||
return EdgeEventActionType.ALARM_ACK;
|
||||
case ALARM_CLEAR:
|
||||
return EdgeEventActionType.ALARM_CLEAR;
|
||||
case DELETED:
|
||||
return EdgeEventActionType.DELETED;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -18,6 +18,7 @@ package org.thingsboard.server.service.entitiy;
|
||||
import org.thingsboard.server.common.data.Device;
|
||||
import org.thingsboard.server.common.data.HasName;
|
||||
import org.thingsboard.server.common.data.Tenant;
|
||||
import org.thingsboard.server.common.data.alarm.Alarm;
|
||||
import org.thingsboard.server.common.data.asset.Asset;
|
||||
import org.thingsboard.server.common.data.audit.ActionType;
|
||||
import org.thingsboard.server.common.data.edge.Edge;
|
||||
@ -78,4 +79,8 @@ public interface TbNotificationEntityService {
|
||||
|
||||
void notifyEdge(TenantId tenantId, EdgeId edgeId, CustomerId customerId, Edge edge, ActionType actionType, SecurityUser user, Object... additionalInfo);
|
||||
|
||||
void notifyCreateOrUpdateAlarm(Alarm alarm,
|
||||
ActionType actionType, SecurityUser user, Object... additionalInfo);
|
||||
|
||||
void notifyDeleteAlarm(Alarm alarm, SecurityUser user, List<EdgeId> relatedEdgeIds);
|
||||
}
|
||||
|
||||
@ -0,0 +1,85 @@
|
||||
/**
|
||||
* Copyright © 2016-2022 The Thingsboard Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.service.entitiy.alarm;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thingsboard.server.common.data.EntityType;
|
||||
import org.thingsboard.server.common.data.alarm.Alarm;
|
||||
import org.thingsboard.server.common.data.alarm.AlarmStatus;
|
||||
import org.thingsboard.server.common.data.audit.ActionType;
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
|
||||
import org.thingsboard.server.common.data.id.EdgeId;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.queue.util.TbCoreComponent;
|
||||
import org.thingsboard.server.service.entitiy.AbstractTbEntityService;
|
||||
import org.thingsboard.server.service.security.model.SecurityUser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@TbCoreComponent
|
||||
@AllArgsConstructor
|
||||
public class DefaultTbAlarmService extends AbstractTbEntityService implements TbAlarmService {
|
||||
|
||||
@Override
|
||||
public Alarm save(Alarm alarm, SecurityUser user) throws ThingsboardException {
|
||||
ActionType actionType = alarm.getId() == null ? ActionType.ADDED : ActionType.UPDATED;
|
||||
TenantId tenantId = alarm.getTenantId();
|
||||
try {
|
||||
Alarm savedAlarm = checkNotNull(alarmService.createOrUpdateAlarm(alarm).getAlarm());
|
||||
notificationEntityService.notifyCreateOrUpdateAlarm(savedAlarm, actionType, user);
|
||||
return savedAlarm;
|
||||
} catch (Exception e) {
|
||||
notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.ALARM), alarm, null, actionType, user, e);
|
||||
throw handleException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ack(Alarm alarm, SecurityUser user) throws ThingsboardException {
|
||||
try {
|
||||
long ackTs = System.currentTimeMillis();
|
||||
alarmService.ackAlarm(user.getTenantId(), alarm.getId(), ackTs).get();
|
||||
alarm.setAckTs(ackTs);
|
||||
alarm.setStatus(alarm.getStatus().isCleared() ? AlarmStatus.CLEARED_ACK : AlarmStatus.ACTIVE_ACK);
|
||||
notificationEntityService.notifyCreateOrUpdateAlarm(alarm, ActionType.ALARM_ACK, user);
|
||||
} catch (Exception e) {
|
||||
throw handleException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear(Alarm alarm, SecurityUser user) throws ThingsboardException {
|
||||
try {
|
||||
long clearTs = System.currentTimeMillis();
|
||||
alarmService.clearAlarm(user.getTenantId(), alarm.getId(), null, clearTs).get();
|
||||
alarm.setClearTs(clearTs);
|
||||
alarm.setStatus(alarm.getStatus().isAck() ? AlarmStatus.CLEARED_ACK : AlarmStatus.CLEARED_UNACK);
|
||||
notificationEntityService.notifyCreateOrUpdateAlarm(alarm, ActionType.ALARM_CLEAR, user);
|
||||
} catch (Exception e) {
|
||||
throw handleException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean delete(Alarm alarm, SecurityUser user) throws ThingsboardException {
|
||||
List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(alarm.getTenantId(), alarm.getOriginator());
|
||||
notificationEntityService.notifyDeleteAlarm(alarm, user, relatedEdgeIds);
|
||||
return alarmService.deleteAlarm(alarm.getTenantId(), alarm.getId()).isSuccessful();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Copyright © 2016-2022 The Thingsboard Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.service.entitiy.alarm;
|
||||
|
||||
import org.thingsboard.server.common.data.alarm.Alarm;
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
|
||||
import org.thingsboard.server.service.security.model.SecurityUser;
|
||||
|
||||
public interface TbAlarmService {
|
||||
|
||||
Alarm save (Alarm alarm, SecurityUser user) throws ThingsboardException;
|
||||
|
||||
void ack (Alarm alarm, SecurityUser user) throws ThingsboardException;
|
||||
|
||||
void clear (Alarm alarm, SecurityUser user) throws ThingsboardException;
|
||||
|
||||
Boolean delete (Alarm alarm, SecurityUser user) throws ThingsboardException;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user