Merge pull request #6533 from thingsboard/refactoring_01_AlarmController
[3.4] Refactoring Alarm controller
This commit is contained in:
commit
e055a7f16e
@ -17,6 +17,7 @@ package org.thingsboard.server.controller;
|
||||
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
@ -29,29 +30,24 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.thingsboard.server.common.data.EntityType;
|
||||
import org.thingsboard.server.common.data.alarm.Alarm;
|
||||
import org.thingsboard.server.common.data.alarm.AlarmInfo;
|
||||
import org.thingsboard.server.common.data.alarm.AlarmQuery;
|
||||
import org.thingsboard.server.common.data.alarm.AlarmSearchStatus;
|
||||
import org.thingsboard.server.common.data.alarm.AlarmSeverity;
|
||||
import org.thingsboard.server.common.data.alarm.AlarmStatus;
|
||||
import org.thingsboard.server.common.data.audit.ActionType;
|
||||
import org.thingsboard.server.common.data.edge.EdgeEventActionType;
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
|
||||
import org.thingsboard.server.common.data.id.AlarmId;
|
||||
import org.thingsboard.server.common.data.id.EdgeId;
|
||||
import org.thingsboard.server.common.data.id.EntityId;
|
||||
import org.thingsboard.server.common.data.id.EntityIdFactory;
|
||||
import org.thingsboard.server.common.data.page.PageData;
|
||||
import org.thingsboard.server.common.data.page.TimePageLink;
|
||||
import org.thingsboard.server.queue.util.TbCoreComponent;
|
||||
import org.thingsboard.server.service.entitiy.alarm.TbAlarmService;
|
||||
import org.thingsboard.server.service.security.permission.Operation;
|
||||
import org.thingsboard.server.service.security.permission.Resource;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.thingsboard.server.controller.ControllerConstants.ALARM_ID_PARAM_DESCRIPTION;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.ALARM_INFO_DESCRIPTION;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.ALARM_SORT_PROPERTY_ALLOWABLE_VALUES;
|
||||
@ -70,9 +66,12 @@ import static org.thingsboard.server.controller.ControllerConstants.UUID_WIKI_LI
|
||||
|
||||
@RestController
|
||||
@TbCoreComponent
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api")
|
||||
public class AlarmController extends BaseController {
|
||||
|
||||
private final TbAlarmService tbAlarmService;
|
||||
|
||||
public static final String ALARM_ID = "alarmId";
|
||||
private static final String ALARM_SECURITY_CHECK = "If the user has the authority of 'Tenant Administrator', the server checks that the originator of alarm is owned by the same tenant. " +
|
||||
"If the user has the authority of 'Customer User', the server checks that the originator of alarm belongs to the customer. ";
|
||||
@ -133,24 +132,9 @@ public class AlarmController extends BaseController {
|
||||
@RequestMapping(value = "/alarm", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public Alarm saveAlarm(@ApiParam(value = "A JSON value representing the alarm.") @RequestBody Alarm alarm) throws ThingsboardException {
|
||||
try {
|
||||
alarm.setTenantId(getCurrentUser().getTenantId());
|
||||
|
||||
checkEntity(alarm.getId(), alarm, Resource.ALARM);
|
||||
|
||||
Alarm savedAlarm = checkNotNull(alarmService.createOrUpdateAlarm(alarm));
|
||||
logEntityAction(savedAlarm.getOriginator(), savedAlarm,
|
||||
getCurrentUser().getCustomerId(),
|
||||
alarm.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null);
|
||||
|
||||
sendEntityNotificationMsg(getTenantId(), savedAlarm.getId(), alarm.getId() == null ? EdgeEventActionType.ADDED : EdgeEventActionType.UPDATED);
|
||||
|
||||
return savedAlarm;
|
||||
} catch (Exception e) {
|
||||
logEntityAction(emptyId(EntityType.ALARM), alarm,
|
||||
null, alarm.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e);
|
||||
throw handleException(e);
|
||||
}
|
||||
return tbAlarmService.save(alarm, getCurrentUser());
|
||||
}
|
||||
|
||||
@ApiOperation(value = "Delete Alarm (deleteAlarm)",
|
||||
@ -163,16 +147,7 @@ public class AlarmController extends BaseController {
|
||||
try {
|
||||
AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
|
||||
Alarm alarm = checkAlarmId(alarmId, Operation.WRITE);
|
||||
|
||||
List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), alarm.getOriginator());
|
||||
|
||||
logEntityAction(alarm.getOriginator(), alarm,
|
||||
getCurrentUser().getCustomerId(),
|
||||
ActionType.ALARM_DELETE, null);
|
||||
|
||||
sendAlarmDeleteNotificationMsg(getTenantId(), alarmId, relatedEdgeIds, alarm);
|
||||
|
||||
return alarmService.deleteAlarm(getTenantId(), alarmId);
|
||||
return tbAlarmService.delete(alarm, getCurrentUser());
|
||||
} catch (Exception e) {
|
||||
throw handleException(e);
|
||||
}
|
||||
@ -187,19 +162,9 @@ public class AlarmController extends BaseController {
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
public void ackAlarm(@ApiParam(value = ALARM_ID_PARAM_DESCRIPTION) @PathVariable(ALARM_ID) String strAlarmId) throws ThingsboardException {
|
||||
checkParameter(ALARM_ID, strAlarmId);
|
||||
try {
|
||||
AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
|
||||
Alarm alarm = checkAlarmId(alarmId, Operation.WRITE);
|
||||
long ackTs = System.currentTimeMillis();
|
||||
alarmService.ackAlarm(getCurrentUser().getTenantId(), alarmId, ackTs).get();
|
||||
alarm.setAckTs(ackTs);
|
||||
alarm.setStatus(alarm.getStatus().isCleared() ? AlarmStatus.CLEARED_ACK : AlarmStatus.ACTIVE_ACK);
|
||||
logEntityAction(alarm.getOriginator(), alarm, getCurrentUser().getCustomerId(), ActionType.ALARM_ACK, null);
|
||||
|
||||
sendEntityNotificationMsg(getTenantId(), alarmId, EdgeEventActionType.ALARM_ACK);
|
||||
} catch (Exception e) {
|
||||
throw handleException(e);
|
||||
}
|
||||
tbAlarmService.ack(alarm, getCurrentUser());
|
||||
}
|
||||
|
||||
@ApiOperation(value = "Clear Alarm (clearAlarm)",
|
||||
@ -211,19 +176,9 @@ public class AlarmController extends BaseController {
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
public void clearAlarm(@ApiParam(value = ALARM_ID_PARAM_DESCRIPTION) @PathVariable(ALARM_ID) String strAlarmId) throws ThingsboardException {
|
||||
checkParameter(ALARM_ID, strAlarmId);
|
||||
try {
|
||||
AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
|
||||
Alarm alarm = checkAlarmId(alarmId, Operation.WRITE);
|
||||
long clearTs = System.currentTimeMillis();
|
||||
alarmService.clearAlarm(getCurrentUser().getTenantId(), alarmId, null, clearTs).get();
|
||||
alarm.setClearTs(clearTs);
|
||||
alarm.setStatus(alarm.getStatus().isAck() ? AlarmStatus.CLEARED_ACK : AlarmStatus.CLEARED_UNACK);
|
||||
logEntityAction(alarm.getOriginator(), alarm, getCurrentUser().getCustomerId(), ActionType.ALARM_CLEAR, null);
|
||||
|
||||
sendEntityNotificationMsg(getTenantId(), alarmId, EdgeEventActionType.ALARM_CLEAR);
|
||||
} catch (Exception e) {
|
||||
throw handleException(e);
|
||||
}
|
||||
tbAlarmService.clear(alarm, getCurrentUser());
|
||||
}
|
||||
|
||||
@ApiOperation(value = "Get Alarms (getAlarms)",
|
||||
@ -276,6 +231,7 @@ public class AlarmController extends BaseController {
|
||||
throw handleException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiOperation(value = "Get All Alarms (getAllAlarms)",
|
||||
notes = "Returns a page of alarms that belongs to the current user owner. " +
|
||||
"If the user has the authority of 'Tenant Administrator', the server returns alarms that belongs to the tenant of current user. " +
|
||||
|
||||
@ -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