Search in sources :

Example 1 with DeviceProfileId

use of org.thingsboard.server.common.data.id.DeviceProfileId in project thingsboard by thingsboard.

the class DefaultTransportApiService method handle.

private ListenableFuture<TransportApiResponseMsg> handle(GetEntityProfileRequestMsg requestMsg) {
    EntityType entityType = EntityType.valueOf(requestMsg.getEntityType());
    UUID entityUuid = new UUID(requestMsg.getEntityIdMSB(), requestMsg.getEntityIdLSB());
    GetEntityProfileResponseMsg.Builder builder = GetEntityProfileResponseMsg.newBuilder();
    if (entityType.equals(EntityType.DEVICE_PROFILE)) {
        DeviceProfileId deviceProfileId = new DeviceProfileId(entityUuid);
        DeviceProfile deviceProfile = deviceProfileCache.find(deviceProfileId);
        builder.setData(ByteString.copyFrom(dataDecodingEncodingService.encode(deviceProfile)));
    } else if (entityType.equals(EntityType.TENANT)) {
        TenantId tenantId = TenantId.fromUUID(entityUuid);
        TenantProfile tenantProfile = tenantProfileCache.get(tenantId);
        ApiUsageState state = apiUsageStateService.getApiUsageState(tenantId);
        builder.setData(ByteString.copyFrom(dataDecodingEncodingService.encode(tenantProfile)));
        builder.setApiState(ByteString.copyFrom(dataDecodingEncodingService.encode(state)));
    } else {
        throw new RuntimeException("Invalid entity profile request: " + entityType);
    }
    return Futures.immediateFuture(TransportApiResponseMsg.newBuilder().setEntityProfileResponseMsg(builder).build());
}
Also used : EntityType(org.thingsboard.server.common.data.EntityType) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) TenantId(org.thingsboard.server.common.data.id.TenantId) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) TenantProfile(org.thingsboard.server.common.data.TenantProfile) UUID(java.util.UUID) GetEntityProfileResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.GetEntityProfileResponseMsg)

Example 2 with DeviceProfileId

use of org.thingsboard.server.common.data.id.DeviceProfileId in project thingsboard by thingsboard.

the class SnmpTransportContext method establishDeviceSession.

private void establishDeviceSession(Device device) {
    if (device == null)
        return;
    log.info("Establishing SNMP session for device {}", device.getId());
    DeviceProfileId deviceProfileId = device.getDeviceProfileId();
    DeviceProfile deviceProfile = deviceProfileCache.get(deviceProfileId);
    DeviceCredentials credentials = protoEntityService.getDeviceCredentialsByDeviceId(device.getId());
    if (credentials.getCredentialsType() != DeviceCredentialsType.ACCESS_TOKEN) {
        log.warn("[{}] Expected credentials type is {} but found {}", device.getId(), DeviceCredentialsType.ACCESS_TOKEN, credentials.getCredentialsType());
        return;
    }
    SnmpDeviceProfileTransportConfiguration profileTransportConfiguration = (SnmpDeviceProfileTransportConfiguration) deviceProfile.getProfileData().getTransportConfiguration();
    SnmpDeviceTransportConfiguration deviceTransportConfiguration = (SnmpDeviceTransportConfiguration) device.getDeviceData().getTransportConfiguration();
    DeviceSessionContext deviceSessionContext;
    try {
        deviceSessionContext = new DeviceSessionContext(device, deviceProfile, credentials.getCredentialsId(), profileTransportConfiguration, deviceTransportConfiguration, this);
        registerSessionMsgListener(deviceSessionContext);
    } catch (Exception e) {
        log.error("Failed to establish session for SNMP device {}: {}", device.getId(), e.toString());
        return;
    }
    sessions.put(device.getId(), deviceSessionContext);
    snmpTransportService.createQueryingTasks(deviceSessionContext);
    log.info("Established SNMP device session for device {}", device.getId());
}
Also used : SnmpDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.SnmpDeviceProfileTransportConfiguration) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) SnmpDeviceTransportConfiguration(org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) DeviceSessionContext(org.thingsboard.server.transport.snmp.session.DeviceSessionContext) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Example 3 with DeviceProfileId

use of org.thingsboard.server.common.data.id.DeviceProfileId in project thingsboard by thingsboard.

the class ProtoTransportEntityService method getDeviceById.

public Device getDeviceById(DeviceId id) {
    TransportProtos.GetDeviceResponseMsg deviceProto = transportService.getDevice(TransportProtos.GetDeviceRequestMsg.newBuilder().setDeviceIdMSB(id.getId().getMostSignificantBits()).setDeviceIdLSB(id.getId().getLeastSignificantBits()).build());
    if (deviceProto == null) {
        return null;
    }
    DeviceProfileId deviceProfileId = new DeviceProfileId(new UUID(deviceProto.getDeviceProfileIdMSB(), deviceProto.getDeviceProfileIdLSB()));
    Device device = new Device();
    device.setId(id);
    device.setDeviceProfileId(deviceProfileId);
    DeviceTransportConfiguration deviceTransportConfiguration = (DeviceTransportConfiguration) dataDecodingEncodingService.decode(deviceProto.getDeviceTransportConfiguration().toByteArray()).orElseThrow(() -> new IllegalStateException("Can't find device transport configuration"));
    DeviceData deviceData = new DeviceData();
    deviceData.setTransportConfiguration(deviceTransportConfiguration);
    device.setDeviceData(deviceData);
    return device;
}
Also used : DeviceTransportConfiguration(org.thingsboard.server.common.data.device.data.DeviceTransportConfiguration) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) Device(org.thingsboard.server.common.data.Device) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) DeviceData(org.thingsboard.server.common.data.device.data.DeviceData) UUID(java.util.UUID)

Example 4 with DeviceProfileId

use of org.thingsboard.server.common.data.id.DeviceProfileId in project thingsboard by thingsboard.

the class DefaultTbClusterService method pushMsgToRuleEngine.

@Override
public void pushMsgToRuleEngine(TenantId tenantId, EntityId entityId, TbMsg tbMsg, TbQueueCallback callback) {
    if (tenantId.isNullUid()) {
        if (entityId.getEntityType().equals(EntityType.TENANT)) {
            tenantId = TenantId.fromUUID(entityId.getId());
        } else {
            log.warn("[{}][{}] Received invalid message: {}", tenantId, entityId, tbMsg);
            return;
        }
    } else {
        if (entityId.getEntityType().equals(EntityType.DEVICE)) {
            tbMsg = transformMsg(tbMsg, deviceProfileCache.get(tenantId, new DeviceId(entityId.getId())));
        } else if (entityId.getEntityType().equals(EntityType.DEVICE_PROFILE)) {
            tbMsg = transformMsg(tbMsg, deviceProfileCache.get(tenantId, new DeviceProfileId(entityId.getId())));
        }
    }
    TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_RULE_ENGINE, tbMsg.getQueueName(), tenantId, entityId);
    log.trace("PUSHING msg: {} to:{}", tbMsg, tpi);
    ToRuleEngineMsg msg = ToRuleEngineMsg.newBuilder().setTenantIdMSB(tenantId.getId().getMostSignificantBits()).setTenantIdLSB(tenantId.getId().getLeastSignificantBits()).setTbMsg(TbMsg.toByteString(tbMsg)).build();
    producerProvider.getRuleEngineMsgProducer().send(tpi, new TbProtoQueueMsg<>(tbMsg.getId(), msg), callback);
    toRuleEngineMsgs.incrementAndGet();
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ToRuleEngineMsg(org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg)

Example 5 with DeviceProfileId

use of org.thingsboard.server.common.data.id.DeviceProfileId in project thingsboard by thingsboard.

the class AbstractConsumerService method handleComponentLifecycleMsg.

protected void handleComponentLifecycleMsg(UUID id, ByteString nfMsg) {
    Optional<TbActorMsg> actorMsgOpt = encodingService.decode(nfMsg.toByteArray());
    if (actorMsgOpt.isPresent()) {
        TbActorMsg actorMsg = actorMsgOpt.get();
        if (actorMsg instanceof ComponentLifecycleMsg) {
            ComponentLifecycleMsg componentLifecycleMsg = (ComponentLifecycleMsg) actorMsg;
            log.info("[{}][{}][{}] Received Lifecycle event: {}", componentLifecycleMsg.getTenantId(), componentLifecycleMsg.getEntityId().getEntityType(), componentLifecycleMsg.getEntityId(), componentLifecycleMsg.getEvent());
            if (EntityType.TENANT_PROFILE.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                TenantProfileId tenantProfileId = new TenantProfileId(componentLifecycleMsg.getEntityId().getId());
                tenantProfileCache.evict(tenantProfileId);
                if (componentLifecycleMsg.getEvent().equals(ComponentLifecycleEvent.UPDATED)) {
                    apiUsageStateService.onTenantProfileUpdate(tenantProfileId);
                }
            } else if (EntityType.TENANT.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                tenantProfileCache.evict(componentLifecycleMsg.getTenantId());
                if (componentLifecycleMsg.getEvent().equals(ComponentLifecycleEvent.UPDATED)) {
                    apiUsageStateService.onTenantUpdate(componentLifecycleMsg.getTenantId());
                } else if (componentLifecycleMsg.getEvent().equals(ComponentLifecycleEvent.DELETED)) {
                    apiUsageStateService.onTenantDelete((TenantId) componentLifecycleMsg.getEntityId());
                }
            } else if (EntityType.DEVICE_PROFILE.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                deviceProfileCache.evict(componentLifecycleMsg.getTenantId(), new DeviceProfileId(componentLifecycleMsg.getEntityId().getId()));
            } else if (EntityType.DEVICE.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                deviceProfileCache.evict(componentLifecycleMsg.getTenantId(), new DeviceId(componentLifecycleMsg.getEntityId().getId()));
            } else if (EntityType.API_USAGE_STATE.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                apiUsageStateService.onApiUsageStateUpdate(componentLifecycleMsg.getTenantId());
            } else if (EntityType.CUSTOMER.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                if (componentLifecycleMsg.getEvent() == ComponentLifecycleEvent.DELETED) {
                    apiUsageStateService.onCustomerDelete((CustomerId) componentLifecycleMsg.getEntityId());
                }
            }
        }
        log.trace("[{}] Forwarding message to App Actor {}", id, actorMsg);
        actorContext.tellWithHighPriority(actorMsg);
    }
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) TenantProfileId(org.thingsboard.server.common.data.id.TenantProfileId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg)

Aggregations

DeviceProfileId (org.thingsboard.server.common.data.id.DeviceProfileId)33 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)12 UUID (java.util.UUID)9 DeviceId (org.thingsboard.server.common.data.id.DeviceId)9 ApiOperation (io.swagger.annotations.ApiOperation)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 Device (org.thingsboard.server.common.data.Device)8 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)8 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)7 CustomerId (org.thingsboard.server.common.data.id.CustomerId)6 OtaPackageId (org.thingsboard.server.common.data.id.OtaPackageId)4 TenantId (org.thingsboard.server.common.data.id.TenantId)4 IOException (java.io.IOException)3 EntityType (org.thingsboard.server.common.data.EntityType)3 PageLink (org.thingsboard.server.common.data.page.PageLink)3 TransportDeviceInfo (org.thingsboard.server.common.transport.auth.TransportDeviceInfo)3 ByteString (com.google.protobuf.ByteString)2 ExecutionException (java.util.concurrent.ExecutionException)2 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)2