Search in sources :

Example 11 with DeviceProfileId

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

the class AbstractDeviceEntity method toDevice.

protected Device toDevice() {
    Device device = new Device(new DeviceId(getUuid()));
    device.setCreatedTime(createdTime);
    if (tenantId != null) {
        device.setTenantId(TenantId.fromUUID(tenantId));
    }
    if (customerId != null) {
        device.setCustomerId(new CustomerId(customerId));
    }
    if (deviceProfileId != null) {
        device.setDeviceProfileId(new DeviceProfileId(deviceProfileId));
    }
    if (firmwareId != null) {
        device.setFirmwareId(new OtaPackageId(firmwareId));
    }
    if (softwareId != null) {
        device.setSoftwareId(new OtaPackageId(softwareId));
    }
    device.setDeviceData(JacksonUtil.convertValue(deviceData, DeviceData.class));
    device.setName(name);
    device.setType(type);
    device.setLabel(label);
    device.setAdditionalInfo(additionalInfo);
    return device;
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) Device(org.thingsboard.server.common.data.Device) DeviceId(org.thingsboard.server.common.data.id.DeviceId) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) DeviceData(org.thingsboard.server.common.data.device.data.DeviceData) CustomerId(org.thingsboard.server.common.data.id.CustomerId)

Example 12 with DeviceProfileId

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

the class DeviceEdgeProcessor method updateDevice.

private void updateDevice(TenantId tenantId, Edge edge, DeviceUpdateMsg deviceUpdateMsg) {
    DeviceId deviceId = new DeviceId(new UUID(deviceUpdateMsg.getIdMSB(), deviceUpdateMsg.getIdLSB()));
    Device device = deviceService.findDeviceById(tenantId, deviceId);
    if (device != null) {
        device.setName(deviceUpdateMsg.getName());
        device.setType(deviceUpdateMsg.getType());
        if (deviceUpdateMsg.hasLabel()) {
            device.setLabel(deviceUpdateMsg.getLabel());
        }
        if (deviceUpdateMsg.hasAdditionalInfo()) {
            device.setAdditionalInfo(JacksonUtil.toJsonNode(deviceUpdateMsg.getAdditionalInfo()));
        }
        if (deviceUpdateMsg.hasDeviceProfileIdMSB() && deviceUpdateMsg.hasDeviceProfileIdLSB()) {
            DeviceProfileId deviceProfileId = new DeviceProfileId(new UUID(deviceUpdateMsg.getDeviceProfileIdMSB(), deviceUpdateMsg.getDeviceProfileIdLSB()));
            device.setDeviceProfileId(deviceProfileId);
        }
        Device savedDevice = deviceService.saveDevice(device);
        tbClusterService.onDeviceUpdated(savedDevice, device);
        saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.DEVICE, EdgeEventActionType.CREDENTIALS_REQUEST, deviceId, null);
    } else {
        log.warn("[{}] can't find device [{}], edge [{}]", tenantId, deviceUpdateMsg, edge.getId());
    }
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) UUID(java.util.UUID)

Example 13 with DeviceProfileId

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

the class DefaultTransportService method sendToRuleEngine.

private void sendToRuleEngine(TenantId tenantId, DeviceId deviceId, CustomerId customerId, TransportProtos.SessionInfoProto sessionInfo, JsonObject json, TbMsgMetaData metaData, SessionMsgType sessionMsgType, TbQueueCallback callback) {
    DeviceProfileId deviceProfileId = new DeviceProfileId(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB()));
    DeviceProfile deviceProfile = deviceProfileCache.get(deviceProfileId);
    RuleChainId ruleChainId;
    String queueName;
    if (deviceProfile == null) {
        log.warn("[{}] Device profile is null!", deviceProfileId);
        ruleChainId = null;
        queueName = ServiceQueue.MAIN;
    } else {
        ruleChainId = deviceProfile.getDefaultRuleChainId();
        String defaultQueueName = deviceProfile.getDefaultQueueName();
        queueName = defaultQueueName != null ? defaultQueueName : ServiceQueue.MAIN;
    }
    TbMsg tbMsg = TbMsg.newMsg(queueName, sessionMsgType.name(), deviceId, customerId, metaData, gson.toJson(json), ruleChainId, null);
    sendToRuleEngine(tenantId, tbMsg, callback);
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) ByteString(com.google.protobuf.ByteString) UUID(java.util.UUID) TbMsg(org.thingsboard.server.common.msg.TbMsg)

Example 14 with DeviceProfileId

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

the class DefaultTransportService method onDeviceUpdate.

private void onDeviceUpdate(Device device) {
    long deviceIdMSB = device.getId().getId().getMostSignificantBits();
    long deviceIdLSB = device.getId().getId().getLeastSignificantBits();
    long deviceProfileIdMSB = device.getDeviceProfileId().getId().getMostSignificantBits();
    long deviceProfileIdLSB = device.getDeviceProfileId().getId().getLeastSignificantBits();
    sessions.forEach((id, md) -> {
        if ((md.getSessionInfo().getDeviceIdMSB() == deviceIdMSB && md.getSessionInfo().getDeviceIdLSB() == deviceIdLSB)) {
            DeviceProfile newDeviceProfile;
            if (md.getSessionInfo().getDeviceProfileIdMSB() != deviceProfileIdMSB || md.getSessionInfo().getDeviceProfileIdLSB() != deviceProfileIdLSB) {
                // TODO: if transport types are different - we should close the session.
                newDeviceProfile = deviceProfileCache.get(new DeviceProfileId(new UUID(deviceProfileIdMSB, deviceProfileIdLSB)));
            } else {
                newDeviceProfile = null;
            }
            TransportProtos.SessionInfoProto newSessionInfo = TransportProtos.SessionInfoProto.newBuilder().mergeFrom(md.getSessionInfo()).setDeviceProfileIdMSB(deviceProfileIdMSB).setDeviceProfileIdLSB(deviceProfileIdLSB).setDeviceName(device.getName()).setDeviceType(device.getType()).build();
            if (device.getAdditionalInfo().has("gateway") && device.getAdditionalInfo().get("gateway").asBoolean() && device.getAdditionalInfo().has(OVERWRITE_ACTIVITY_TIME) && device.getAdditionalInfo().get(OVERWRITE_ACTIVITY_TIME).isBoolean()) {
                md.setOverwriteActivityTime(device.getAdditionalInfo().get(OVERWRITE_ACTIVITY_TIME).asBoolean());
            }
            md.setSessionInfo(newSessionInfo);
            transportCallbackExecutor.submit(() -> md.getListener().onDeviceUpdate(newSessionInfo, device, Optional.ofNullable(newDeviceProfile)));
        }
    });
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) UUID(java.util.UUID)

Example 15 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)

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