Browse Source

implemented device actor delete msg and fixed assign to tenant

pull/9616/head
YevhenBondarenko 3 years ago
parent
commit
316a09854c
  1. 3
      application/src/main/java/org/thingsboard/server/actors/device/DeviceActor.java
  2. 14
      application/src/main/java/org/thingsboard/server/actors/tenant/TenantActor.java
  3. 3
      application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java
  4. 2
      application/src/main/java/org/thingsboard/server/service/entitiy/device/DefaultTbDeviceService.java
  5. 15
      application/src/main/java/org/thingsboard/server/service/queue/DefaultTbClusterService.java
  6. 22
      application/src/main/java/org/thingsboard/server/service/queue/ProtoUtils.java
  7. 4
      common/cluster-api/src/main/java/org/thingsboard/server/cluster/TbClusterService.java
  8. 8
      common/cluster-api/src/main/proto/queue.proto
  9. 2
      common/message/src/main/java/org/thingsboard/server/common/msg/MsgType.java
  10. 36
      common/message/src/main/java/org/thingsboard/server/common/msg/rule/engine/DeviceDeleteMsg.java

3
application/src/main/java/org/thingsboard/server/actors/device/DeviceActor.java

@ -64,6 +64,9 @@ public class DeviceActor extends ContextAwareActor {
case DEVICE_ATTRIBUTES_UPDATE_TO_DEVICE_ACTOR_MSG:
processor.processAttributesUpdate((DeviceAttributesEventNotificationMsg) msg);
break;
case DEVICE_DELETE_TO_DEVICE_ACTOR_MSG:
ctx.stop(ctx.getSelf());
break;
case DEVICE_CREDENTIALS_UPDATE_TO_DEVICE_ACTOR_MSG:
processor.processCredentialsUpdate(msg);
break;

14
application/src/main/java/org/thingsboard/server/actors/tenant/TenantActor.java

@ -53,10 +53,13 @@ import org.thingsboard.server.common.msg.queue.PartitionChangeMsg;
import org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg;
import org.thingsboard.server.common.msg.queue.RuleEngineException;
import org.thingsboard.server.common.msg.queue.ServiceType;
import org.thingsboard.server.common.msg.rule.engine.DeviceDeleteMsg;
import org.thingsboard.server.service.edge.rpc.EdgeRpcService;
import org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Slf4j
public class TenantActor extends RuleChainManagerActor {
@ -65,8 +68,11 @@ public class TenantActor extends RuleChainManagerActor {
private boolean isCore;
private ApiUsageState apiUsageState;
private Set<DeviceId> deletedDevices;
private TenantActor(ActorSystemContext systemContext, TenantId tenantId) {
super(systemContext, tenantId);
this.deletedDevices = new HashSet<>();
}
boolean cantFindTenant = false;
@ -221,6 +227,10 @@ public class TenantActor extends RuleChainManagerActor {
if (!isCore) {
log.warn("RECEIVED INVALID MESSAGE: {}", msg);
}
if (deletedDevices.contains(msg.getDeviceId())) {
log.warn("RECEIVED MESSAGE FOR DELETED DEVICE: {}", msg);
return;
}
TbActorRef deviceActor = getOrCreateDeviceActor(msg.getDeviceId());
if (priority) {
deviceActor.tellWithHighPriority(msg);
@ -249,6 +259,10 @@ public class TenantActor extends RuleChainManagerActor {
Edge edge = systemContext.getEdgeService().findEdgeById(tenantId, edgeId);
edgeRpcService.updateEdge(tenantId, edge);
}
} else if (msg.getEntityId().getEntityType() == EntityType.DEVICE && ComponentLifecycleEvent.DELETED == msg.getEvent()) {
DeviceId deviceId = (DeviceId) msg.getEntityId();
onToDeviceActorMsg(new DeviceDeleteMsg(tenantId, deviceId), true);
deletedDevices.add(deviceId);
} else if (isRuleEngine) {
TbActorRef target = getEntityActorRef(msg.getEntityId());
if (target != null) {

3
application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java

@ -112,7 +112,7 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS
public void notifyDeleteDevice(TenantId tenantId, DeviceId deviceId, CustomerId customerId, Device device,
User user, Object... additionalInfo) {
gatewayNotificationsService.onDeviceDeleted(device);
tbClusterService.onDeviceDeleted(device, null);
tbClusterService.onDeviceDeleted(tenantId, device, null);
logEntityAction(tenantId, deviceId, device, customerId, ActionType.DELETED, user, additionalInfo);
}
@ -126,6 +126,7 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS
@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);
}

2
application/src/main/java/org/thingsboard/server/service/entitiy/device/DefaultTbDeviceService.java

@ -232,6 +232,8 @@ public class DefaultTbDeviceService extends AbstractTbEntityService implements T
Tenant tenant = tenantService.findTenantById(tenantId);
Device assignedDevice = deviceService.assignDeviceToTenant(newTenantId, device);
notificationEntityService.notifyAssignDeviceToTenant(tenantId, newTenantId, deviceId,
assignedDevice.getCustomerId(), assignedDevice, tenant, user, newTenantId.toString(), newTenant.getName());

15
application/src/main/java/org/thingsboard/server/service/queue/DefaultTbClusterService.java

@ -308,10 +308,17 @@ public class DefaultTbClusterService implements TbClusterService {
}
@Override
public void onDeviceDeleted(Device device, TbQueueCallback callback) {
broadcastEntityDeleteToTransport(device.getTenantId(), device.getId(), device.getName(), callback);
sendDeviceStateServiceEvent(device.getTenantId(), device.getId(), false, false, true);
broadcastEntityStateChangeEvent(device.getTenantId(), device.getId(), ComponentLifecycleEvent.DELETED);
public void onDeviceDeleted(TenantId tenantId, Device device, TbQueueCallback callback) {
DeviceId deviceId = device.getId();
broadcastEntityDeleteToTransport(tenantId, deviceId, device.getName(), callback);
sendDeviceStateServiceEvent(tenantId, deviceId, false, false, true);
broadcastEntityStateChangeEvent(tenantId, deviceId, ComponentLifecycleEvent.DELETED);
}
@Override
public void onDeviceAssignedToTenant(TenantId oldTenantId, Device device) {
onDeviceDeleted(oldTenantId, device, null);
sendDeviceStateServiceEvent(device.getTenantId(), device.getId(), true, false, false);
}
@Override

22
application/src/main/java/org/thingsboard/server/service/queue/ProtoUtils.java

@ -44,6 +44,7 @@ import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponseActorMsg;
import org.thingsboard.server.common.msg.rpc.RemoveRpcActorMsg;
import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest;
import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequestActorMsg;
import org.thingsboard.server.common.msg.rule.engine.DeviceDeleteMsg;
import org.thingsboard.server.common.msg.rule.engine.DeviceAttributesEventNotificationMsg;
import org.thingsboard.server.common.msg.rule.engine.DeviceCredentialsUpdateNotificationMsg;
import org.thingsboard.server.common.msg.rule.engine.DeviceEdgeUpdateMsg;
@ -377,6 +378,21 @@ public class ProtoUtils {
);
}
private static TransportProtos.DeviceDeletedMsgProto toProto(DeviceDeleteMsg msg) {
return TransportProtos.DeviceDeletedMsgProto.newBuilder()
.setTenantIdMSB(msg.getTenantId().getId().getMostSignificantBits())
.setTenantIdLSB(msg.getTenantId().getId().getLeastSignificantBits())
.setDeviceIdMSB(msg.getDeviceId().getId().getMostSignificantBits())
.setDeviceIdLSB(msg.getDeviceId().getId().getLeastSignificantBits())
.build();
}
private static DeviceDeleteMsg fromProto(TransportProtos.DeviceDeletedMsgProto proto) {
return new DeviceDeleteMsg(
TenantId.fromUUID(new UUID(proto.getTenantIdMSB(), proto.getTenantIdLSB())),
new DeviceId(new UUID(proto.getDeviceIdMSB(), proto.getDeviceIdLSB())));
}
public static TransportProtos.ToDeviceActorNotificationMsgProto toProto(ToDeviceActorNotificationMsg msg) {
if (msg instanceof DeviceEdgeUpdateMsg) {
DeviceEdgeUpdateMsg updateMsg = (DeviceEdgeUpdateMsg) msg;
@ -406,6 +422,10 @@ public class ProtoUtils {
RemoveRpcActorMsg updateMsg = (RemoveRpcActorMsg) msg;
TransportProtos.RemoveRpcActorMsgProto proto = toProto(updateMsg);
return TransportProtos.ToDeviceActorNotificationMsgProto.newBuilder().setRemoveRpcActorMsg(proto).build();
} else if (msg instanceof DeviceDeleteMsg) {
DeviceDeleteMsg updateMsg = (DeviceDeleteMsg) msg;
TransportProtos.DeviceDeletedMsgProto proto = toProto(updateMsg);
return TransportProtos.ToDeviceActorNotificationMsgProto.newBuilder().setDeviceAssignToTenantMsgProto(proto).build();
}
return null;
}
@ -425,6 +445,8 @@ public class ProtoUtils {
return fromProto(proto.getFromDeviceRpcResponseMsg());
} else if (proto.hasRemoveRpcActorMsg()) {
return fromProto(proto.getRemoveRpcActorMsg());
} else if (proto.hasDeviceAssignToTenantMsgProto()) {
return fromProto(proto.getDeviceAssignToTenantMsgProto());
}
return null;
}

4
common/cluster-api/src/main/java/org/thingsboard/server/cluster/TbClusterService.java

@ -83,7 +83,9 @@ public interface TbClusterService extends TbQueueClusterService {
void onDeviceUpdated(Device device, Device old);
void onDeviceDeleted(Device device, TbQueueCallback callback);
void onDeviceDeleted(TenantId tenantId, Device device, TbQueueCallback callback);
void onDeviceAssignedToTenant(TenantId oldTenantId, Device device);
void onResourceChange(TbResource resource, TbQueueCallback callback);

8
common/cluster-api/src/main/proto/queue.proto

@ -1004,6 +1004,13 @@ message RemoveRpcActorMsgProto {
int64 deviceIdLSB = 6;
}
message DeviceDeletedMsgProto {
int64 tenantIdMSB = 1;
int64 tenantIdLSB = 2;
int64 deviceIdMSB = 3;
int64 deviceIdLSB = 4;
}
message ToDeviceActorNotificationMsgProto {
DeviceEdgeUpdateMsgProto deviceEdgeUpdateMsg = 1;
DeviceNameOrTypeUpdateMsgProto deviceNameOrTypeMsg = 2;
@ -1012,6 +1019,7 @@ message ToDeviceActorNotificationMsgProto {
ToDeviceRpcRequestActorMsgProto toDeviceRpcRequestMsg = 5;
FromDeviceRpcResponseActorMsgProto fromDeviceRpcResponseMsg = 6;
RemoveRpcActorMsgProto removeRpcActorMsg = 7;
DeviceDeletedMsgProto deviceAssignToTenantMsgProto = 8;
}
/**

2
common/message/src/main/java/org/thingsboard/server/common/msg/MsgType.java

@ -93,6 +93,8 @@ public enum MsgType {
DEVICE_NAME_OR_TYPE_UPDATE_TO_DEVICE_ACTOR_MSG,
DEVICE_DELETE_TO_DEVICE_ACTOR_MSG,
DEVICE_EDGE_UPDATE_TO_DEVICE_ACTOR_MSG,
DEVICE_RPC_REQUEST_TO_DEVICE_ACTOR_MSG,

36
common/message/src/main/java/org/thingsboard/server/common/msg/rule/engine/DeviceDeleteMsg.java

@ -0,0 +1,36 @@
/**
* Copyright © 2016-2023 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.common.msg.rule.engine;
import lombok.Data;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.msg.MsgType;
import org.thingsboard.server.common.msg.ToDeviceActorNotificationMsg;
@Data
public class DeviceDeleteMsg implements ToDeviceActorNotificationMsg {
private static final long serialVersionUID = 4679029228395462172L;
private final TenantId tenantId;
private final DeviceId deviceId;
@Override
public MsgType getMsgType() {
return MsgType.DEVICE_DELETE_TO_DEVICE_ACTOR_MSG;
}
}
Loading…
Cancel
Save