9 changed files with 668 additions and 271 deletions
@ -0,0 +1,243 @@ |
|||
/** |
|||
* 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; |
|||
|
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import lombok.Getter; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.thingsboard.common.util.DonAsynchron; |
|||
import org.thingsboard.server.cluster.TbClusterService; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.HasName; |
|||
import org.thingsboard.server.common.data.User; |
|||
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.audit.ActionType; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|||
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.CustomerId; |
|||
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.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageDataIterableByTenantIdEntityId; |
|||
import org.thingsboard.server.common.data.page.TimePageLink; |
|||
import org.thingsboard.server.common.msg.queue.TbCallback; |
|||
import org.thingsboard.server.dao.alarm.AlarmOperationResult; |
|||
import org.thingsboard.server.dao.alarm.AlarmService; |
|||
import org.thingsboard.server.dao.customer.CustomerService; |
|||
import org.thingsboard.server.dao.device.DeviceCredentialsService; |
|||
import org.thingsboard.server.dao.device.DeviceService; |
|||
import org.thingsboard.server.dao.edge.EdgeService; |
|||
import org.thingsboard.server.dao.exception.DataValidationException; |
|||
import org.thingsboard.server.dao.exception.IncorrectParameterException; |
|||
import org.thingsboard.server.dao.model.ModelConstants; |
|||
import org.thingsboard.server.dao.tenant.TenantService; |
|||
import org.thingsboard.server.service.action.EntityActionService; |
|||
import org.thingsboard.server.service.executors.DbCallbackExecutorService; |
|||
|
|||
import javax.mail.MessagingException; |
|||
import java.util.ArrayList; |
|||
import java.util.Collections; |
|||
import java.util.List; |
|||
import java.util.Optional; |
|||
import java.util.stream.Collectors; |
|||
|
|||
@Slf4j |
|||
public abstract class AbstractTbEntityService { |
|||
|
|||
protected static final int DEFAULT_PAGE_SIZE = 1000; |
|||
|
|||
private static final ObjectMapper json = new ObjectMapper(); |
|||
|
|||
@Value("${server.log_controller_error_stack_trace}") |
|||
@Getter |
|||
private boolean logControllerErrorStackTrace; |
|||
@Value("${edges.enabled}") |
|||
@Getter |
|||
protected boolean edgesEnabled; |
|||
|
|||
@Autowired |
|||
protected DbCallbackExecutorService dbExecutor; |
|||
@Autowired(required = false) |
|||
protected EdgeService edgeService; |
|||
@Autowired |
|||
protected AlarmService alarmService; |
|||
@Autowired |
|||
protected EntityActionService entityActionService; |
|||
@Autowired |
|||
protected TbClusterService tbClusterService; |
|||
@Autowired |
|||
protected DeviceService deviceService; |
|||
@Autowired |
|||
protected DeviceCredentialsService deviceCredentialsService; |
|||
@Autowired |
|||
protected TenantService tenantService; |
|||
@Autowired |
|||
protected CustomerService customerService; |
|||
|
|||
protected void removeAlarmsByEntityId(TenantId tenantId, EntityId entityId, TbCallback callback) { |
|||
ListenableFuture<PageData<AlarmInfo>> alarmsFuture = |
|||
alarmService.findAlarms(tenantId, new AlarmQuery(entityId, new TimePageLink(Integer.MAX_VALUE), null, null, false)); |
|||
|
|||
ListenableFuture<List<AlarmId>> alarmIdsFuture = Futures.transform(alarmsFuture, page -> |
|||
page.getData().stream().map(AlarmInfo::getId).collect(Collectors.toList()), dbExecutor); |
|||
|
|||
ListenableFuture<List<AlarmOperationResult>> resultFuture = Futures.transform(alarmIdsFuture, ids -> |
|||
ids.stream().map(alarmId -> alarmService.deleteAlarm(tenantId, alarmId)).collect(Collectors.toList()), |
|||
dbExecutor); |
|||
|
|||
DonAsynchron.withCallback(resultFuture, result -> callback.onSuccess(), callback::onFailure, dbExecutor); |
|||
} |
|||
|
|||
protected <E extends HasName, I extends EntityId> void logEntityAction(User user, TenantId tenantId, I entityId, E entity, CustomerId customerId, |
|||
ActionType actionType, Exception e, Object... additionalInfo) throws ThingsboardException { |
|||
if (user != null) { |
|||
entityActionService.logEntityAction(user, entityId, entity, customerId, actionType, e, additionalInfo); |
|||
} else if (e == null) { |
|||
entityActionService.pushEntityActionToRuleEngine(entityId, entity, tenantId, customerId, actionType, null, additionalInfo); |
|||
} |
|||
} |
|||
|
|||
protected <T> T checkNotNull(T reference) throws ThingsboardException { |
|||
return checkNotNull(reference, "Requested item wasn't found!"); |
|||
} |
|||
|
|||
protected <T> T checkNotNull(T reference, String notFoundMessage) throws ThingsboardException { |
|||
if (reference == null) { |
|||
throw new ThingsboardException(notFoundMessage, ThingsboardErrorCode.ITEM_NOT_FOUND); |
|||
} |
|||
return reference; |
|||
} |
|||
|
|||
protected <T> T checkNotNull(Optional<T> reference) throws ThingsboardException { |
|||
return checkNotNull(reference, "Requested item wasn't found!"); |
|||
} |
|||
|
|||
protected <T> T checkNotNull(Optional<T> reference, String notFoundMessage) throws ThingsboardException { |
|||
if (reference.isPresent()) { |
|||
return reference.get(); |
|||
} else { |
|||
throw new ThingsboardException(notFoundMessage, ThingsboardErrorCode.ITEM_NOT_FOUND); |
|||
} |
|||
} |
|||
|
|||
protected ThingsboardException handleException(Exception exception) { |
|||
return handleException(exception, true); |
|||
} |
|||
|
|||
protected ThingsboardException handleException(Exception exception, boolean logException) { |
|||
if (logException && logControllerErrorStackTrace) { |
|||
log.error("Error [{}]", exception.getMessage(), exception); |
|||
} |
|||
|
|||
String cause = ""; |
|||
if (exception.getCause() != null) { |
|||
cause = exception.getCause().getClass().getCanonicalName(); |
|||
} |
|||
|
|||
if (exception instanceof ThingsboardException) { |
|||
return (ThingsboardException) exception; |
|||
} else if (exception instanceof IllegalArgumentException || exception instanceof IncorrectParameterException |
|||
|| exception instanceof DataValidationException || cause.contains("IncorrectParameterException")) { |
|||
return new ThingsboardException(exception.getMessage(), ThingsboardErrorCode.BAD_REQUEST_PARAMS); |
|||
} else if (exception instanceof MessagingException) { |
|||
return new ThingsboardException("Unable to send mail: " + exception.getMessage(), ThingsboardErrorCode.GENERAL); |
|||
} else { |
|||
return new ThingsboardException(exception.getMessage(), exception, ThingsboardErrorCode.GENERAL); |
|||
} |
|||
} |
|||
|
|||
protected void sendEntityAssignToCustomerNotificationMsg(TenantId tenantId, EntityId entityId, CustomerId customerId, EdgeEventActionType action) { |
|||
try { |
|||
sendNotificationMsgToEdgeService(tenantId, null, entityId, json.writeValueAsString(customerId), null, action); |
|||
} catch (Exception e) { |
|||
log.warn("Failed to push assign/unassign to/from customer to core: {}", customerId, e); |
|||
} |
|||
} |
|||
|
|||
protected void sendDeleteNotificationMsg(TenantId tenantId, EntityId entityId, List<EdgeId> edgeIds) { |
|||
sendDeleteNotificationMsg(tenantId, entityId, edgeIds, null); |
|||
} |
|||
|
|||
protected void sendDeleteNotificationMsg(TenantId tenantId, EntityId entityId, List<EdgeId> edgeIds, String body) { |
|||
if (edgeIds != null && !edgeIds.isEmpty()) { |
|||
for (EdgeId edgeId : edgeIds) { |
|||
sendNotificationMsgToEdgeService(tenantId, edgeId, entityId, body, null, EdgeEventActionType.DELETED); |
|||
} |
|||
} |
|||
} |
|||
|
|||
protected void sendEntityNotificationMsg(TenantId tenantId, EntityId entityId, EdgeEventActionType action) { |
|||
sendNotificationMsgToEdgeService(tenantId, null, entityId, null, null, action); |
|||
} |
|||
|
|||
protected void sendEntityAssignToEdgeNotificationMsg(TenantId tenantId, EdgeId edgeId, EntityId entityId, EdgeEventActionType action) { |
|||
sendNotificationMsgToEdgeService(tenantId, edgeId, entityId, null, null, action); |
|||
} |
|||
|
|||
private void sendNotificationMsgToEdgeService(TenantId tenantId, EdgeId edgeId, EntityId entityId, String body, EdgeEventType type, EdgeEventActionType action) { |
|||
tbClusterService.sendNotificationMsgToEdgeService(tenantId, edgeId, entityId, body, type, action); |
|||
} |
|||
|
|||
protected void sendAlarmDeleteNotificationMsg(TenantId tenantId, EntityId entityId, List<EdgeId> edgeIds, Alarm alarm) { |
|||
try { |
|||
sendDeleteNotificationMsg(tenantId, entityId, edgeIds, json.writeValueAsString(alarm)); |
|||
} catch (Exception e) { |
|||
log.warn("Failed to push delete alarm msg to core: {}", alarm, e); |
|||
} |
|||
} |
|||
|
|||
@SuppressWarnings("unchecked") |
|||
protected <I extends EntityId> I emptyId(EntityType entityType) { |
|||
return (I) EntityIdFactory.getByTypeAndUuid(entityType, ModelConstants.NULL_UUID); |
|||
} |
|||
|
|||
protected List<EdgeId> findRelatedEdgeIds(TenantId tenantId, EntityId entityId) { |
|||
if (!edgesEnabled) { |
|||
return null; |
|||
} |
|||
if (EntityType.EDGE.equals(entityId.getEntityType())) { |
|||
return Collections.singletonList(new EdgeId(entityId.getId())); |
|||
} |
|||
PageDataIterableByTenantIdEntityId<EdgeId> relatedEdgeIdsIterator = |
|||
new PageDataIterableByTenantIdEntityId<>(edgeService::findRelatedEdgeIdsByEntityId, tenantId, entityId, DEFAULT_PAGE_SIZE); |
|||
List<EdgeId> result = new ArrayList<>(); |
|||
for (EdgeId edgeId : relatedEdgeIdsIterator) { |
|||
result.add(edgeId); |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
protected <E extends HasName> String entityToStr(E entity) { |
|||
try { |
|||
return json.writeValueAsString(json.valueToTree(entity)); |
|||
} catch (JsonProcessingException e) { |
|||
log.warn("[{}] Failed to convert entity to string!", entity, e); |
|||
} |
|||
return null; |
|||
} |
|||
} |
|||
@ -0,0 +1,299 @@ |
|||
/** |
|||
* 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; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMsg; |
|||
import org.thingsboard.server.common.data.Customer; |
|||
import org.thingsboard.server.common.data.DataConstants; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.Tenant; |
|||
import org.thingsboard.server.common.data.audit.ActionType; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.EdgeId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.security.DeviceCredentials; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
import org.thingsboard.server.common.msg.TbMsgDataType; |
|||
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.gateway_device.GatewayNotificationsService; |
|||
import org.thingsboard.server.service.security.model.SecurityUser; |
|||
|
|||
import java.util.List; |
|||
|
|||
@AllArgsConstructor |
|||
@TbCoreComponent |
|||
@Service |
|||
@Slf4j |
|||
public class DefaultTbDeviceService extends AbstractTbEntityService implements TbDeviceService { |
|||
|
|||
private final GatewayNotificationsService gatewayNotificationsService; |
|||
|
|||
@Override |
|||
public Device save(SecurityUser user, TenantId tenantId, Device device, Device oldDevice, String accessToken) throws ThingsboardException { |
|||
boolean created = device.getId() == null; |
|||
try { |
|||
Device savedDevice = checkNotNull(deviceService.saveDeviceWithAccessToken(device, accessToken)); |
|||
tbClusterService.onDeviceUpdated(savedDevice, oldDevice); |
|||
|
|||
logEntityAction(user, tenantId, savedDevice.getId(), savedDevice, |
|||
savedDevice.getCustomerId(), |
|||
created ? ActionType.ADDED : ActionType.UPDATED, null); |
|||
|
|||
return savedDevice; |
|||
} catch (Exception e) { |
|||
logEntityAction(user, tenantId, emptyId(EntityType.DEVICE), device, |
|||
null, created ? ActionType.ADDED : ActionType.UPDATED, e); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public Device saveDeviceWithCredentials(SecurityUser user, TenantId tenantId, Device device, DeviceCredentials credentials) throws ThingsboardException { |
|||
boolean created = device.getId() == null; |
|||
try { |
|||
Device savedDevice = checkNotNull(deviceService.saveDeviceWithCredentials(device, credentials)); |
|||
tbClusterService.onDeviceUpdated(savedDevice, device); |
|||
logEntityAction(user, tenantId, savedDevice.getId(), savedDevice, |
|||
savedDevice.getCustomerId(), |
|||
created ? ActionType.ADDED : ActionType.UPDATED, null); |
|||
return savedDevice; |
|||
} catch (Exception e) { |
|||
logEntityAction(user, tenantId, emptyId(EntityType.DEVICE), device, |
|||
null, created ? ActionType.ADDED : ActionType.UPDATED, e); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void deleteDevice(SecurityUser user, TenantId tenantId, DeviceId deviceId) throws ThingsboardException { |
|||
try { |
|||
Device device = deviceService.findDeviceById(tenantId, deviceId); |
|||
List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(tenantId, deviceId); |
|||
|
|||
deviceService.deleteDevice(tenantId, deviceId); |
|||
|
|||
gatewayNotificationsService.onDeviceDeleted(device); |
|||
tbClusterService.onDeviceDeleted(device, null); |
|||
|
|||
logEntityAction(user, tenantId, deviceId, device, |
|||
device.getCustomerId(), |
|||
ActionType.DELETED, null, deviceId.toString()); |
|||
|
|||
sendDeleteNotificationMsg(tenantId, deviceId, relatedEdgeIds); |
|||
} catch (Exception e) { |
|||
logEntityAction(user, tenantId, emptyId(EntityType.DEVICE), |
|||
null, |
|||
null, |
|||
ActionType.DELETED, e, deviceId.toString()); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public Device assignDeviceToCustomer(SecurityUser user, TenantId tenantId, DeviceId deviceId, CustomerId customerId) throws ThingsboardException { |
|||
try { |
|||
Device savedDevice = checkNotNull(deviceService.assignDeviceToCustomer(user.getTenantId(), deviceId, customerId)); |
|||
|
|||
Customer customer = customerService.findCustomerById(user.getTenantId(), customerId); |
|||
|
|||
logEntityAction(user, tenantId, deviceId, savedDevice, |
|||
savedDevice.getCustomerId(), |
|||
ActionType.ASSIGNED_TO_CUSTOMER, null, deviceId.toString(), customerId.toString(), customer.getName()); |
|||
|
|||
sendEntityAssignToCustomerNotificationMsg(savedDevice.getTenantId(), savedDevice.getId(), |
|||
customerId, EdgeEventActionType.ASSIGNED_TO_CUSTOMER); |
|||
|
|||
return savedDevice; |
|||
} catch (Exception e) { |
|||
logEntityAction(user, tenantId, emptyId(EntityType.DEVICE), null, |
|||
null, |
|||
ActionType.ASSIGNED_TO_CUSTOMER, e, deviceId.toString(), customerId.toString()); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public Device unassignDeviceFromCustomer(SecurityUser user, TenantId tenantId, DeviceId deviceId) throws ThingsboardException { |
|||
try { |
|||
Device device = deviceService.findDeviceById(tenantId, deviceId); |
|||
Customer customer = customerService.findCustomerById(tenantId, device.getCustomerId()); |
|||
Device savedDevice = checkNotNull(deviceService.unassignDeviceFromCustomer(tenantId, deviceId)); |
|||
|
|||
logEntityAction(user, tenantId, deviceId, device, |
|||
device.getCustomerId(), |
|||
ActionType.UNASSIGNED_FROM_CUSTOMER, null, deviceId.toString(), customer.getId().toString(), customer.getName()); |
|||
|
|||
sendEntityAssignToCustomerNotificationMsg(savedDevice.getTenantId(), savedDevice.getId(), |
|||
customer.getId(), EdgeEventActionType.UNASSIGNED_FROM_CUSTOMER); |
|||
|
|||
return savedDevice; |
|||
} catch (Exception e) { |
|||
logEntityAction(user, tenantId, emptyId(EntityType.DEVICE), null, |
|||
null, |
|||
ActionType.UNASSIGNED_FROM_CUSTOMER, e, deviceId.toString()); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public Device assignDeviceToPublicCustomer(SecurityUser user, TenantId tenantId, DeviceId deviceId) throws ThingsboardException { |
|||
try { |
|||
Customer publicCustomer = customerService.findOrCreatePublicCustomer(tenantId); |
|||
Device savedDevice = checkNotNull(deviceService.assignDeviceToCustomer(tenantId, deviceId, publicCustomer.getId())); |
|||
|
|||
logEntityAction(user, tenantId, deviceId, savedDevice, |
|||
savedDevice.getCustomerId(), |
|||
ActionType.ASSIGNED_TO_CUSTOMER, null, deviceId.toString(), publicCustomer.getId().toString(), publicCustomer.getName()); |
|||
|
|||
return savedDevice; |
|||
} catch (Exception e) { |
|||
logEntityAction(user, tenantId, emptyId(EntityType.DEVICE), null, |
|||
null, |
|||
ActionType.ASSIGNED_TO_CUSTOMER, e, deviceId.toString()); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public DeviceCredentials getDeviceCredentialsByDeviceId(SecurityUser user, TenantId tenantId, DeviceId deviceId) throws ThingsboardException { |
|||
try { |
|||
Device device = deviceService.findDeviceById(tenantId, deviceId); |
|||
DeviceCredentials deviceCredentials = checkNotNull(deviceCredentialsService.findDeviceCredentialsByDeviceId(tenantId, deviceId)); |
|||
logEntityAction(user, tenantId, deviceId, device, |
|||
device.getCustomerId(), |
|||
ActionType.CREDENTIALS_READ, null, deviceId.toString()); |
|||
return deviceCredentials; |
|||
} catch (Exception e) { |
|||
logEntityAction(user, tenantId, emptyId(EntityType.DEVICE), null, |
|||
null, |
|||
ActionType.CREDENTIALS_READ, e, deviceId.toString()); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public DeviceCredentials updateDeviceCredentials(SecurityUser user, TenantId tenantId, DeviceCredentials deviceCredentials) throws ThingsboardException { |
|||
try { |
|||
DeviceId deviceId = deviceCredentials.getDeviceId(); |
|||
Device device = deviceService.findDeviceById(tenantId, deviceId); |
|||
DeviceCredentials result = checkNotNull(deviceCredentialsService.updateDeviceCredentials(tenantId, deviceCredentials)); |
|||
tbClusterService.pushMsgToCore(new DeviceCredentialsUpdateNotificationMsg(tenantId, deviceCredentials.getDeviceId(), result), null); |
|||
|
|||
sendEntityNotificationMsg(tenantId, device.getId(), EdgeEventActionType.CREDENTIALS_UPDATED); |
|||
|
|||
logEntityAction(user, tenantId, deviceId, device, |
|||
device.getCustomerId(), |
|||
ActionType.CREDENTIALS_UPDATED, null, deviceCredentials); |
|||
return result; |
|||
} catch (Exception e) { |
|||
logEntityAction(user, tenantId, emptyId(EntityType.DEVICE), null, |
|||
null, |
|||
ActionType.CREDENTIALS_UPDATED, e, deviceCredentials); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public Device assignDeviceToTenant(SecurityUser user, TenantId tenantId, TenantId newTenantId, DeviceId deviceId) throws ThingsboardException { |
|||
try { |
|||
Tenant newTenant = tenantService.findTenantById(newTenantId); |
|||
Device device = deviceService.findDeviceById(tenantId, deviceId); |
|||
|
|||
Device assignedDevice = deviceService.assignDeviceToTenant(newTenantId, device); |
|||
|
|||
logEntityAction(user, tenantId, deviceId, assignedDevice, |
|||
assignedDevice.getCustomerId(), |
|||
ActionType.ASSIGNED_TO_TENANT, null, newTenantId.toString(), newTenant.getName()); |
|||
|
|||
Tenant currentTenant = tenantService.findTenantById(tenantId); |
|||
pushAssignedFromNotification(currentTenant, newTenantId, assignedDevice); |
|||
|
|||
return assignedDevice; |
|||
} catch (Exception e) { |
|||
logEntityAction(user, tenantId, emptyId(EntityType.DEVICE), null, |
|||
null, |
|||
ActionType.ASSIGNED_TO_TENANT, e, newTenantId.toString()); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public Device assignDeviceToEdge(SecurityUser user, TenantId tenantId, DeviceId deviceId, EdgeId edgeId) throws ThingsboardException { |
|||
try { |
|||
Device savedDevice = checkNotNull(deviceService.assignDeviceToEdge(tenantId, deviceId, edgeId)); |
|||
Edge edge = edgeService.findEdgeById(tenantId, edgeId); |
|||
|
|||
logEntityAction(user, tenantId, deviceId, savedDevice, |
|||
savedDevice.getCustomerId(), |
|||
ActionType.ASSIGNED_TO_EDGE, null, deviceId.toString(), edgeId.toString(), edge.getName()); |
|||
|
|||
sendEntityAssignToEdgeNotificationMsg(tenantId, edgeId, savedDevice.getId(), EdgeEventActionType.ASSIGNED_TO_EDGE); |
|||
|
|||
return savedDevice; |
|||
} catch (Exception e) { |
|||
logEntityAction(user, tenantId, emptyId(EntityType.DEVICE), null, |
|||
null, |
|||
ActionType.ASSIGNED_TO_EDGE, e, deviceId.toString(), edgeId.toString()); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public Device unassignDeviceFromEdge(SecurityUser user, TenantId tenantId, DeviceId deviceId, EdgeId edgeId) throws ThingsboardException { |
|||
try { |
|||
Device device = deviceService.findDeviceById(tenantId, deviceId); |
|||
Device savedDevice = checkNotNull(deviceService.unassignDeviceFromEdge(tenantId, deviceId, edgeId)); |
|||
Edge edge = edgeService.findEdgeById(tenantId, edgeId); |
|||
|
|||
logEntityAction(user, tenantId, deviceId, device, |
|||
device.getCustomerId(), |
|||
ActionType.UNASSIGNED_FROM_EDGE, null, deviceId.toString(), edgeId.toString(), edge.getName()); |
|||
|
|||
sendEntityAssignToEdgeNotificationMsg(tenantId, edgeId, savedDevice.getId(), EdgeEventActionType.UNASSIGNED_FROM_EDGE); |
|||
|
|||
return savedDevice; |
|||
} catch (Exception e) { |
|||
logEntityAction(user, tenantId, emptyId(EntityType.DEVICE), null, |
|||
null, |
|||
ActionType.UNASSIGNED_FROM_EDGE, e, deviceId.toString(), edgeId.toString()); |
|||
throw handleException(e); |
|||
} |
|||
} |
|||
|
|||
private void pushAssignedFromNotification(Tenant currentTenant, TenantId newTenantId, Device assignedDevice) { |
|||
String data = entityToStr(assignedDevice); |
|||
if (data != null) { |
|||
TbMsg tbMsg = TbMsg.newMsg(DataConstants.ENTITY_ASSIGNED_FROM_TENANT, assignedDevice.getId(), assignedDevice.getCustomerId(), getMetaDataForAssignedFrom(currentTenant), TbMsgDataType.JSON, data); |
|||
tbClusterService.pushMsgToRuleEngine(newTenantId, assignedDevice.getId(), tbMsg, null); |
|||
} |
|||
} |
|||
|
|||
private TbMsgMetaData getMetaDataForAssignedFrom(Tenant tenant) { |
|||
TbMsgMetaData metaData = new TbMsgMetaData(); |
|||
metaData.putValue("assignedFromTenantId", tenant.getId().getId().toString()); |
|||
metaData.putValue("assignedFromTenantName", tenant.getName()); |
|||
return metaData; |
|||
} |
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
/** |
|||
* 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; |
|||
|
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.EdgeId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.security.DeviceCredentials; |
|||
import org.thingsboard.server.service.security.model.SecurityUser; |
|||
|
|||
public interface TbDeviceService { |
|||
|
|||
Device save(SecurityUser user, TenantId tenantId, Device device, Device oldDevice, String accessToken) throws ThingsboardException; |
|||
|
|||
Device saveDeviceWithCredentials(SecurityUser user, TenantId tenantId, Device device, DeviceCredentials deviceCredentials) throws ThingsboardException; |
|||
|
|||
void deleteDevice(SecurityUser user, TenantId tenantId, DeviceId deviceId) throws ThingsboardException; |
|||
|
|||
Device assignDeviceToCustomer(SecurityUser user, TenantId tenantId, DeviceId deviceId, CustomerId customerId) throws ThingsboardException; |
|||
|
|||
Device unassignDeviceFromCustomer(SecurityUser user, TenantId tenantId, DeviceId deviceId) throws ThingsboardException; |
|||
|
|||
Device assignDeviceToPublicCustomer(SecurityUser user, TenantId tenantId, DeviceId deviceId) throws ThingsboardException; |
|||
|
|||
DeviceCredentials getDeviceCredentialsByDeviceId(SecurityUser user, TenantId tenantId, DeviceId deviceId) throws ThingsboardException; |
|||
|
|||
DeviceCredentials updateDeviceCredentials(SecurityUser user, TenantId tenantId, DeviceCredentials deviceCredentials) throws ThingsboardException; |
|||
|
|||
Device assignDeviceToTenant(SecurityUser user, TenantId tenantId, TenantId newTenantId, DeviceId deviceId) throws ThingsboardException; |
|||
|
|||
Device assignDeviceToEdge(SecurityUser user, TenantId tenantId, DeviceId deviceId, EdgeId edgeId) throws ThingsboardException; |
|||
|
|||
Device unassignDeviceFromEdge(SecurityUser user, TenantId tenantId, DeviceId deviceId, EdgeId edgeId) throws ThingsboardException; |
|||
} |
|||
Loading…
Reference in new issue