103 changed files with 996 additions and 928 deletions
@ -0,0 +1,79 @@ |
|||
/** |
|||
* Copyright © 2016-2024 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.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.HasName; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.audit.ActionType; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.relation.EntityRelation; |
|||
import org.thingsboard.server.service.action.EntityActionService; |
|||
|
|||
@Slf4j |
|||
@Service |
|||
@RequiredArgsConstructor |
|||
public class DefaultTbLogEntityActionService implements TbLogEntityActionService { |
|||
|
|||
private final EntityActionService entityActionService; |
|||
|
|||
@Override |
|||
public <I extends EntityId> void logEntityAction(TenantId tenantId, I entityId, ActionType actionType, |
|||
User user, Exception e, Object... additionalInfo) { |
|||
logEntityAction(tenantId, entityId, null, null, actionType, user, e, additionalInfo); |
|||
} |
|||
|
|||
@Override |
|||
public <E extends HasName, I extends EntityId> void logEntityAction(TenantId tenantId, I entityId, E entity, |
|||
ActionType actionType, User user, Object... additionalInfo) { |
|||
logEntityAction(tenantId, entityId, entity, null, actionType, user, null, additionalInfo); |
|||
} |
|||
|
|||
@Override |
|||
public <E extends HasName, I extends EntityId> void logEntityAction(TenantId tenantId, I entityId, E entity, |
|||
ActionType actionType, User user, Exception e, |
|||
Object... additionalInfo) { |
|||
logEntityAction(tenantId, entityId, entity, null, actionType, user, e, additionalInfo); |
|||
} |
|||
|
|||
@Override |
|||
public <E extends HasName, I extends EntityId> void logEntityAction(TenantId tenantId, I entityId, E entity, CustomerId customerId, |
|||
ActionType actionType, User user, Object... additionalInfo) { |
|||
logEntityAction(tenantId, entityId, entity, customerId, actionType, user, null, additionalInfo); |
|||
} |
|||
|
|||
@Override |
|||
public <E extends HasName, I extends EntityId> void logEntityAction(TenantId tenantId, I entityId, E entity, |
|||
CustomerId customerId, ActionType actionType, |
|||
User user, Exception e, Object... additionalInfo) { |
|||
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); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void logEntityRelationAction(TenantId tenantId, CustomerId customerId, EntityRelation relation, User user, |
|||
ActionType actionType, Exception e, Object... additionalInfo) { |
|||
logEntityAction(tenantId, relation.getFrom(), null, customerId, actionType, user, e, additionalInfo); |
|||
logEntityAction(tenantId, relation.getTo(), null, customerId, actionType, user, e, additionalInfo); |
|||
} |
|||
} |
|||
@ -1,212 +0,0 @@ |
|||
/** |
|||
* Copyright © 2016-2024 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.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.msg.rule.engine.DeviceCredentialsUpdateNotificationMsg; |
|||
import org.thingsboard.server.cluster.TbClusterService; |
|||
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.User; |
|||
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.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.EntityId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.msg.TbMsgType; |
|||
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
|||
import org.thingsboard.server.common.data.relation.EntityRelation; |
|||
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.service.action.EntityActionService; |
|||
import org.thingsboard.server.service.gateway_device.GatewayNotificationsService; |
|||
|
|||
@Slf4j |
|||
@Service |
|||
@RequiredArgsConstructor |
|||
public class DefaultTbNotificationEntityService implements TbNotificationEntityService { |
|||
|
|||
private final EntityActionService entityActionService; |
|||
private final TbClusterService tbClusterService; |
|||
private final GatewayNotificationsService gatewayNotificationsService; |
|||
|
|||
@Override |
|||
public <I extends EntityId> void logEntityAction(TenantId tenantId, I entityId, ActionType actionType, |
|||
User user, Exception e, Object... additionalInfo) { |
|||
logEntityAction(tenantId, entityId, null, null, actionType, user, e, additionalInfo); |
|||
} |
|||
|
|||
@Override |
|||
public <E extends HasName, I extends EntityId> void logEntityAction(TenantId tenantId, I entityId, E entity, |
|||
ActionType actionType, User user, Object... additionalInfo) { |
|||
logEntityAction(tenantId, entityId, entity, null, actionType, user, null, additionalInfo); |
|||
} |
|||
|
|||
@Override |
|||
public <E extends HasName, I extends EntityId> void logEntityAction(TenantId tenantId, I entityId, E entity, |
|||
ActionType actionType, User user, Exception e, |
|||
Object... additionalInfo) { |
|||
logEntityAction(tenantId, entityId, entity, null, actionType, user, e, additionalInfo); |
|||
} |
|||
|
|||
@Override |
|||
public <E extends HasName, I extends EntityId> void logEntityAction(TenantId tenantId, I entityId, E entity, CustomerId customerId, |
|||
ActionType actionType, User user, Object... additionalInfo) { |
|||
logEntityAction(tenantId, entityId, entity, customerId, actionType, user, null, additionalInfo); |
|||
} |
|||
|
|||
@Override |
|||
public <E extends HasName, I extends EntityId> void logEntityAction(TenantId tenantId, I entityId, E entity, |
|||
CustomerId customerId, ActionType actionType, |
|||
User user, Exception e, Object... additionalInfo) { |
|||
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); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void notifyCreateOrUpdateTenant(Tenant tenant, ComponentLifecycleEvent event) { |
|||
tbClusterService.onTenantChange(tenant, null); |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenant.getId(), tenant.getId(), event); |
|||
} |
|||
|
|||
@Override |
|||
public void notifyDeleteTenant(Tenant tenant) { |
|||
tbClusterService.onTenantDelete(tenant, null); |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenant.getId(), tenant.getId(), ComponentLifecycleEvent.DELETED); |
|||
} |
|||
|
|||
@Override |
|||
public void notifyCreateOrUpdateDevice(TenantId tenantId, DeviceId deviceId, CustomerId customerId, |
|||
Device device, Device oldDevice, ActionType actionType, |
|||
User user, Object... additionalInfo) { |
|||
tbClusterService.onDeviceUpdated(device, oldDevice); |
|||
logEntityAction(tenantId, deviceId, device, customerId, actionType, user, additionalInfo); |
|||
} |
|||
|
|||
@Override |
|||
public void notifyDeleteDevice(TenantId tenantId, DeviceId deviceId, CustomerId customerId, Device device, |
|||
User user, Object... additionalInfo) { |
|||
gatewayNotificationsService.onDeviceDeleted(device); |
|||
tbClusterService.onDeviceDeleted(tenantId, device, null); |
|||
logEntityAction(tenantId, deviceId, device, customerId, ActionType.DELETED, user, additionalInfo); |
|||
} |
|||
|
|||
@Override |
|||
public void notifyUpdateDeviceCredentials(TenantId tenantId, DeviceId deviceId, CustomerId customerId, Device device, |
|||
DeviceCredentials deviceCredentials, User user) { |
|||
tbClusterService.pushMsgToCore(new DeviceCredentialsUpdateNotificationMsg(tenantId, deviceCredentials.getDeviceId(), deviceCredentials), null); |
|||
logEntityAction(tenantId, deviceId, device, customerId, ActionType.CREDENTIALS_UPDATED, user, deviceCredentials); |
|||
} |
|||
|
|||
@Override |
|||
public void notifyAssignDeviceToTenant(TenantId tenantId, TenantId newTenantId, DeviceId deviceId, CustomerId customerId, |
|||
Device device, Tenant tenant, User user, Object... additionalInfo) { |
|||
tbClusterService.onDeviceAssignedToTenant(tenantId, device); |
|||
logEntityAction(tenantId, deviceId, device, customerId, ActionType.ASSIGNED_TO_TENANT, user, additionalInfo); |
|||
pushAssignedFromNotification(tenant, newTenantId, device); |
|||
} |
|||
|
|||
@Override |
|||
public void notifyCreateOrUpdateOrDeleteEdge(TenantId tenantId, EdgeId edgeId, CustomerId customerId, Edge edge, |
|||
ActionType actionType, User user, Object... additionalInfo) { |
|||
ComponentLifecycleEvent lifecycleEvent; |
|||
switch (actionType) { |
|||
case ADDED: |
|||
lifecycleEvent = ComponentLifecycleEvent.CREATED; |
|||
break; |
|||
case UPDATED: |
|||
lifecycleEvent = ComponentLifecycleEvent.UPDATED; |
|||
break; |
|||
case DELETED: |
|||
lifecycleEvent = ComponentLifecycleEvent.DELETED; |
|||
break; |
|||
default: |
|||
throw new IllegalArgumentException("Unknown actionType: " + actionType); |
|||
} |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenantId, edgeId, lifecycleEvent); |
|||
logEntityAction(tenantId, edgeId, edge, customerId, actionType, user, additionalInfo); |
|||
} |
|||
|
|||
@Override |
|||
public void logEntityRelationAction(TenantId tenantId, CustomerId customerId, EntityRelation relation, User user, |
|||
ActionType actionType, Exception e, Object... additionalInfo) { |
|||
logEntityAction(tenantId, relation.getFrom(), null, customerId, actionType, user, e, additionalInfo); |
|||
logEntityAction(tenantId, relation.getTo(), null, customerId, actionType, user, e, additionalInfo); |
|||
} |
|||
|
|||
private void pushAssignedFromNotification(Tenant currentTenant, TenantId newTenantId, Device assignedDevice) { |
|||
String data = JacksonUtil.toString(JacksonUtil.valueToTree(assignedDevice)); |
|||
if (data != null) { |
|||
TbMsg tbMsg = TbMsg.newMsg(TbMsgType.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; |
|||
} |
|||
|
|||
public static 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 ALARM_ASSIGNED: |
|||
return EdgeEventActionType.ALARM_ASSIGNED; |
|||
case ALARM_UNASSIGNED: |
|||
return EdgeEventActionType.ALARM_UNASSIGNED; |
|||
case DELETED: |
|||
return EdgeEventActionType.DELETED; |
|||
case RELATION_ADD_OR_UPDATE: |
|||
return EdgeEventActionType.RELATION_ADD_OR_UPDATE; |
|||
case RELATION_DELETED: |
|||
return EdgeEventActionType.RELATION_DELETED; |
|||
case ASSIGNED_TO_CUSTOMER: |
|||
return EdgeEventActionType.ASSIGNED_TO_CUSTOMER; |
|||
case UNASSIGNED_FROM_CUSTOMER: |
|||
return EdgeEventActionType.UNASSIGNED_FROM_CUSTOMER; |
|||
case ASSIGNED_TO_EDGE: |
|||
return EdgeEventActionType.ASSIGNED_TO_EDGE; |
|||
case UNASSIGNED_FROM_EDGE: |
|||
return EdgeEventActionType.UNASSIGNED_FROM_EDGE; |
|||
case CREDENTIALS_UPDATED: |
|||
return EdgeEventActionType.CREDENTIALS_UPDATED; |
|||
default: |
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,266 @@ |
|||
/** |
|||
* Copyright © 2016-2024 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.type.TypeReference; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.transaction.event.TransactionalEventListener; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.cluster.TbClusterService; |
|||
import org.thingsboard.server.common.data.ApiUsageState; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.DeviceProfile; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.TbResource; |
|||
import org.thingsboard.server.common.data.TbResourceInfo; |
|||
import org.thingsboard.server.common.data.Tenant; |
|||
import org.thingsboard.server.common.data.TenantProfile; |
|||
import org.thingsboard.server.common.data.audit.ActionType; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.EdgeId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.RuleChainId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.msg.TbMsgType; |
|||
import org.thingsboard.server.common.data.notification.NotificationRequest; |
|||
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
|||
import org.thingsboard.server.common.data.rule.RuleChain; |
|||
import org.thingsboard.server.common.data.rule.RuleChainType; |
|||
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.common.msg.rule.engine.DeviceCredentialsUpdateNotificationMsg; |
|||
import org.thingsboard.server.dao.eventsourcing.ActionEntityEvent; |
|||
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent; |
|||
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent; |
|||
|
|||
import javax.annotation.PostConstruct; |
|||
import java.util.Set; |
|||
|
|||
@Component |
|||
@RequiredArgsConstructor |
|||
@Slf4j |
|||
public class EntityStateSourcingListener { |
|||
|
|||
private final TbClusterService tbClusterService; |
|||
|
|||
@PostConstruct |
|||
public void init() { |
|||
log.info("EntityStateSourcingListener initiated"); |
|||
} |
|||
|
|||
@TransactionalEventListener(fallbackExecution = true) |
|||
public void handleEvent(SaveEntityEvent<?> event) { |
|||
log.trace("[{}] SaveEntityEvent called: {}", event.getTenantId(), event); |
|||
TenantId tenantId = event.getTenantId(); |
|||
EntityId entityId = event.getEntityId(); |
|||
EntityType entityType = entityId.getEntityType(); |
|||
boolean isCreated = event.getAdded() != null && event.getAdded(); |
|||
ComponentLifecycleEvent lifecycleEvent = isCreated ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED; |
|||
|
|||
switch (entityType) { |
|||
case ASSET: |
|||
case ASSET_PROFILE: |
|||
case ENTITY_VIEW: |
|||
case NOTIFICATION_RULE: |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenantId, entityId, lifecycleEvent); |
|||
break; |
|||
case RULE_CHAIN: |
|||
RuleChain ruleChain = (RuleChain) event.getEntity(); |
|||
if (RuleChainType.CORE.equals(ruleChain.getType())) { |
|||
tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), ruleChain.getId(), lifecycleEvent); |
|||
} |
|||
break; |
|||
case TENANT: |
|||
Tenant tenant = (Tenant) event.getEntity(); |
|||
onTenantUpdate(tenant, lifecycleEvent); |
|||
break; |
|||
case TENANT_PROFILE: |
|||
TenantProfile tenantProfile = (TenantProfile) event.getEntity(); |
|||
onTenantProfileUpdate(tenantProfile, lifecycleEvent); |
|||
break; |
|||
case DEVICE: |
|||
onDeviceUpdate(event.getEntity(), event.getOldEntity()); |
|||
break; |
|||
case DEVICE_PROFILE: |
|||
DeviceProfile deviceProfile = (DeviceProfile) event.getEntity(); |
|||
onDeviceProfileUpdate(deviceProfile, event.getOldEntity(), isCreated); |
|||
break; |
|||
case EDGE: |
|||
handleEdgeEvent(tenantId, entityId, event.getEntity(), lifecycleEvent); |
|||
break; |
|||
case TB_RESOURCE: |
|||
TbResource tbResource = (TbResource) event.getEntity(); |
|||
tbClusterService.onResourceChange(tbResource, null); |
|||
break; |
|||
case API_USAGE_STATE: |
|||
ApiUsageState apiUsageState = (ApiUsageState) event.getEntity(); |
|||
tbClusterService.onApiStateChange(apiUsageState, null); |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
@TransactionalEventListener(fallbackExecution = true) |
|||
public void handleEvent(DeleteEntityEvent<?> event) { |
|||
log.trace("[{}] DeleteEntityEvent called: {}", event.getTenantId(), event); |
|||
TenantId tenantId = event.getTenantId(); |
|||
EntityId entityId = event.getEntityId(); |
|||
EntityType entityType = entityId.getEntityType(); |
|||
|
|||
switch (entityType) { |
|||
case ASSET: |
|||
case ASSET_PROFILE: |
|||
case ENTITY_VIEW: |
|||
case CUSTOMER: |
|||
case EDGE: |
|||
case NOTIFICATION_RULE: |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenantId, entityId, ComponentLifecycleEvent.DELETED); |
|||
break; |
|||
case NOTIFICATION_REQUEST: |
|||
NotificationRequest request = (NotificationRequest) event.getEntity(); |
|||
if (request.isScheduled()) { |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenantId, entityId, ComponentLifecycleEvent.DELETED); |
|||
} |
|||
break; |
|||
case RULE_CHAIN: |
|||
RuleChain ruleChain = (RuleChain) event.getEntity(); |
|||
if (RuleChainType.CORE.equals(ruleChain.getType())) { |
|||
Set<RuleChainId> referencingRuleChainIds = JacksonUtil.fromString(event.getBody(), new TypeReference<>() {}); |
|||
if (referencingRuleChainIds != null) { |
|||
referencingRuleChainIds.forEach(referencingRuleChainId -> |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenantId, referencingRuleChainId, ComponentLifecycleEvent.UPDATED)); |
|||
} |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenantId, ruleChain.getId(), ComponentLifecycleEvent.DELETED); |
|||
} |
|||
break; |
|||
case TENANT: |
|||
Tenant tenant = (Tenant) event.getEntity(); |
|||
onTenantDeleted(tenant); |
|||
break; |
|||
case TENANT_PROFILE: |
|||
TenantProfile tenantProfile = (TenantProfile) event.getEntity(); |
|||
tbClusterService.onTenantProfileDelete(tenantProfile, null); |
|||
break; |
|||
case DEVICE: |
|||
Device device = (Device) event.getEntity(); |
|||
tbClusterService.onDeviceDeleted(tenantId, device, null); |
|||
break; |
|||
case DEVICE_PROFILE: |
|||
DeviceProfile deviceProfile = (DeviceProfile) event.getEntity(); |
|||
onDeviceProfileDelete(event.getTenantId(), event.getEntityId(), deviceProfile); |
|||
break; |
|||
case TB_RESOURCE: |
|||
TbResourceInfo tbResource = (TbResourceInfo) event.getEntity(); |
|||
tbClusterService.onResourceDeleted(tbResource, null); |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
@TransactionalEventListener(fallbackExecution = true) |
|||
public void handleEvent(ActionEntityEvent<?> event) { |
|||
log.trace("[{}] ActionEntityEvent called: {}", event.getTenantId(), event); |
|||
if (ActionType.CREDENTIALS_UPDATED.equals(event.getActionType()) && |
|||
EntityType.DEVICE.equals(event.getEntityId().getEntityType()) |
|||
&& event.getEntity() instanceof DeviceCredentials) { |
|||
tbClusterService.pushMsgToCore(new DeviceCredentialsUpdateNotificationMsg(event.getTenantId(), |
|||
(DeviceId) event.getEntityId(), (DeviceCredentials) event.getEntity()), null); |
|||
} else if (ActionType.ASSIGNED_TO_TENANT.equals(event.getActionType()) && event.getEntity() instanceof Device) { |
|||
Device device = (Device) event.getEntity(); |
|||
Tenant tenant = JacksonUtil.fromString(event.getBody(), Tenant.class); |
|||
if (tenant != null) { |
|||
tbClusterService.onDeviceAssignedToTenant(tenant.getId(), device); |
|||
} |
|||
pushAssignedFromNotification(tenant, event.getTenantId(), device); |
|||
} |
|||
} |
|||
|
|||
private void onTenantUpdate(Tenant tenant, ComponentLifecycleEvent lifecycleEvent) { |
|||
tbClusterService.onTenantChange(tenant, null); |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenant.getId(), tenant.getId(), lifecycleEvent); |
|||
} |
|||
|
|||
private void onTenantDeleted(Tenant tenant) { |
|||
tbClusterService.onTenantDelete(tenant, null); |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenant.getId(), tenant.getId(), ComponentLifecycleEvent.DELETED); |
|||
} |
|||
|
|||
private void onTenantProfileUpdate(TenantProfile tenantProfile, ComponentLifecycleEvent lifecycleEvent) { |
|||
tbClusterService.onTenantProfileChange(tenantProfile, null); |
|||
tbClusterService.broadcastEntityStateChangeEvent(TenantId.SYS_TENANT_ID, tenantProfile.getId(), lifecycleEvent); |
|||
} |
|||
|
|||
private void onDeviceProfileUpdate(DeviceProfile deviceProfile, Object oldEntity, boolean isCreated) { |
|||
DeviceProfile oldDeviceProfile = null; |
|||
if (!isCreated) { |
|||
oldDeviceProfile = getOldDeviceProfile(oldEntity); |
|||
} |
|||
tbClusterService.onDeviceProfileChange(deviceProfile, oldDeviceProfile, null); |
|||
} |
|||
|
|||
private DeviceProfile getOldDeviceProfile(Object oldEntity) { |
|||
if (oldEntity instanceof DeviceProfile) { |
|||
return (DeviceProfile) oldEntity; |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
private void onDeviceProfileDelete(TenantId tenantId, EntityId entityId, DeviceProfile deviceProfile) { |
|||
tbClusterService.onDeviceProfileDelete(deviceProfile, null); |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenantId, entityId, ComponentLifecycleEvent.DELETED); |
|||
} |
|||
|
|||
private void onDeviceUpdate(Object entity, Object oldEntity) { |
|||
Device device = (Device) entity; |
|||
Device oldDevice = null; |
|||
if (oldEntity instanceof Device) { |
|||
oldDevice = (Device) oldEntity; |
|||
} |
|||
tbClusterService.onDeviceUpdated(device, oldDevice); |
|||
} |
|||
|
|||
private void handleEdgeEvent(TenantId tenantId, EntityId entityId, Object entity, ComponentLifecycleEvent lifecycleEvent) { |
|||
if (entity instanceof Edge) { |
|||
tbClusterService.broadcastEntityStateChangeEvent(tenantId, entityId, lifecycleEvent); |
|||
} else if (entity instanceof EdgeEvent) { |
|||
tbClusterService.onEdgeEventUpdate(tenantId, (EdgeId) entityId); |
|||
} |
|||
} |
|||
|
|||
private void pushAssignedFromNotification(Tenant currentTenant, TenantId newTenantId, Device assignedDevice) { |
|||
String data = JacksonUtil.toString(JacksonUtil.valueToTree(assignedDevice)); |
|||
if (data != null) { |
|||
TbMsg tbMsg = TbMsg.newMsg(TbMsgType.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; |
|||
} |
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue