Search in sources :

Example 16 with DeviceProfile

use of org.thingsboard.server.common.data.DeviceProfile in project thingsboard by thingsboard.

the class DeviceBulkImportService method saveEntity.

@Override
protected Device saveEntity(Device entity, Map<BulkImportColumnType, String> fields) {
    DeviceCredentials deviceCredentials;
    try {
        deviceCredentials = createDeviceCredentials(fields);
        deviceCredentialsService.formatCredentials(deviceCredentials);
    } catch (Exception e) {
        throw new DeviceCredentialsValidationException("Invalid device credentials: " + e.getMessage());
    }
    DeviceProfile deviceProfile;
    if (deviceCredentials.getCredentialsType() == DeviceCredentialsType.LWM2M_CREDENTIALS) {
        deviceProfile = setUpLwM2mDeviceProfile(entity.getTenantId(), entity);
    } else if (StringUtils.isNotEmpty(entity.getType())) {
        deviceProfile = deviceProfileService.findOrCreateDeviceProfile(entity.getTenantId(), entity.getType());
    } else {
        deviceProfile = deviceProfileService.findDefaultDeviceProfile(entity.getTenantId());
    }
    entity.setDeviceProfileId(deviceProfile.getId());
    return deviceService.saveDeviceWithCredentials(entity, deviceCredentials);
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) DeviceCredentialsValidationException(org.thingsboard.server.dao.exception.DeviceCredentialsValidationException) DeviceCredentialsValidationException(org.thingsboard.server.dao.exception.DeviceCredentialsValidationException)

Example 17 with DeviceProfile

use of org.thingsboard.server.common.data.DeviceProfile in project thingsboard by thingsboard.

the class AbstractCoapIntegrationTest method processBeforeTest.

protected void processBeforeTest(String deviceName, CoapDeviceType coapDeviceType, TransportPayloadType payloadType, String telemetryProtoSchema, String attributesProtoSchema, String rpcResponseProtoSchema, String rpcRequestProtoSchema, String provisionKey, String provisionSecret, DeviceProfileProvisionType provisionType) throws Exception {
    loginSysAdmin();
    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);
    tenantAdmin = new User();
    tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
    tenantAdmin.setTenantId(savedTenant.getId());
    tenantAdmin.setEmail("tenant" + atomicInteger.getAndIncrement() + "@thingsboard.org");
    tenantAdmin.setFirstName("Joe");
    tenantAdmin.setLastName("Downs");
    tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
    Device device = new Device();
    device.setName(deviceName);
    device.setType("default");
    if (coapDeviceType != null) {
        DeviceProfile coapDeviceProfile = createCoapDeviceProfile(payloadType, coapDeviceType, provisionSecret, provisionType, provisionKey, attributesProtoSchema, telemetryProtoSchema, rpcResponseProtoSchema, rpcRequestProtoSchema);
        deviceProfile = doPost("/api/deviceProfile", coapDeviceProfile, DeviceProfile.class);
        device.setType(deviceProfile.getName());
        device.setDeviceProfileId(deviceProfile.getId());
    }
    savedDevice = doPost("/api/device", device, Device.class);
    DeviceCredentials deviceCredentials = doGet("/api/device/" + savedDevice.getId().getId().toString() + "/credentials", DeviceCredentials.class);
    assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId());
    accessToken = deviceCredentials.getCredentialsId();
    assertNotNull(accessToken);
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) Tenant(org.thingsboard.server.common.data.Tenant) User(org.thingsboard.server.common.data.User) Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Example 18 with DeviceProfile

use of org.thingsboard.server.common.data.DeviceProfile in project thingsboard by thingsboard.

the class DefaultOtaPackageStateService method updateFirmware.

private void updateFirmware(Device device, Device oldDevice) {
    OtaPackageId newFirmwareId = device.getFirmwareId();
    if (newFirmwareId == null) {
        DeviceProfile newDeviceProfile = deviceProfileService.findDeviceProfileById(device.getTenantId(), device.getDeviceProfileId());
        newFirmwareId = newDeviceProfile.getFirmwareId();
    }
    if (oldDevice != null) {
        OtaPackageId oldFirmwareId = oldDevice.getFirmwareId();
        if (oldFirmwareId == null) {
            DeviceProfile oldDeviceProfile = deviceProfileService.findDeviceProfileById(oldDevice.getTenantId(), oldDevice.getDeviceProfileId());
            oldFirmwareId = oldDeviceProfile.getFirmwareId();
        }
        if (newFirmwareId != null) {
            if (!newFirmwareId.equals(oldFirmwareId)) {
                // Device was updated and new firmware is different from previous firmware.
                send(device.getTenantId(), device.getId(), newFirmwareId, System.currentTimeMillis(), FIRMWARE);
            }
        } else if (oldFirmwareId != null) {
            // Device was updated and new firmware is not set.
            remove(device, FIRMWARE);
        }
    } else if (newFirmwareId != null) {
        // Device was created and firmware is defined.
        send(device.getTenantId(), device.getId(), newFirmwareId, System.currentTimeMillis(), FIRMWARE);
    }
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId)

Example 19 with DeviceProfile

use of org.thingsboard.server.common.data.DeviceProfile in project thingsboard by thingsboard.

the class DefaultOtaPackageStateService method process.

@Override
public boolean process(ToOtaPackageStateServiceMsg msg) {
    boolean isSuccess = false;
    OtaPackageId targetOtaPackageId = new OtaPackageId(new UUID(msg.getOtaPackageIdMSB(), msg.getOtaPackageIdLSB()));
    DeviceId deviceId = new DeviceId(new UUID(msg.getDeviceIdMSB(), msg.getDeviceIdLSB()));
    TenantId tenantId = TenantId.fromUUID(new UUID(msg.getTenantIdMSB(), msg.getTenantIdLSB()));
    OtaPackageType firmwareType = OtaPackageType.valueOf(msg.getType());
    long ts = msg.getTs();
    Device device = deviceService.findDeviceById(tenantId, deviceId);
    if (device == null) {
        log.warn("[{}] [{}] Device was removed during firmware update msg was queued!", tenantId, deviceId);
    } else {
        OtaPackageId currentOtaPackageId = OtaPackageUtil.getOtaPackageId(device, firmwareType);
        if (currentOtaPackageId == null) {
            DeviceProfile deviceProfile = deviceProfileService.findDeviceProfileById(tenantId, device.getDeviceProfileId());
            currentOtaPackageId = OtaPackageUtil.getOtaPackageId(deviceProfile, firmwareType);
        }
        if (targetOtaPackageId.equals(currentOtaPackageId)) {
            update(device, otaPackageService.findOtaPackageInfoById(device.getTenantId(), targetOtaPackageId), ts);
            isSuccess = true;
        } else {
            log.warn("[{}] [{}] Can`t update firmware for the device, target firmwareId: [{}], current firmwareId: [{}]!", tenantId, deviceId, targetOtaPackageId, currentOtaPackageId);
        }
    }
    return isSuccess;
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) TenantId(org.thingsboard.server.common.data.id.TenantId) OtaPackageType(org.thingsboard.server.common.data.ota.OtaPackageType) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) UUID(java.util.UUID)

Example 20 with DeviceProfile

use of org.thingsboard.server.common.data.DeviceProfile in project thingsboard by thingsboard.

the class DefaultTbDeviceProfileCache method get.

@Override
public DeviceProfile get(TenantId tenantId, DeviceProfileId deviceProfileId) {
    DeviceProfile profile = deviceProfilesMap.get(deviceProfileId);
    if (profile == null) {
        deviceProfileFetchLock.lock();
        try {
            profile = deviceProfilesMap.get(deviceProfileId);
            if (profile == null) {
                profile = deviceProfileService.findDeviceProfileById(tenantId, deviceProfileId);
                if (profile != null) {
                    deviceProfilesMap.put(deviceProfileId, profile);
                    log.debug("[{}] Fetch device profile into cache: {}", profile.getId(), profile);
                }
            }
        } finally {
            deviceProfileFetchLock.unlock();
        }
    }
    log.trace("[{}] Found device profile in cache: {}", deviceProfileId, profile);
    return profile;
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile)

Aggregations

DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)110 Test (org.junit.Test)48 Device (org.thingsboard.server.common.data.Device)30 DeviceProfileData (org.thingsboard.server.common.data.device.profile.DeviceProfileData)26 List (java.util.List)20 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)19 TbMsg (org.thingsboard.server.common.msg.TbMsg)19 DeviceProfileAlarm (org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm)18 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)18 AlarmConditionFilterKey (org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey)17 AlarmRule (org.thingsboard.server.common.data.device.profile.AlarmRule)17 AlarmCondition (org.thingsboard.server.common.data.device.profile.AlarmCondition)16 AlarmConditionFilter (org.thingsboard.server.common.data.device.profile.AlarmConditionFilter)16 NumericFilterPredicate (org.thingsboard.server.common.data.query.NumericFilterPredicate)16 DynamicValue (org.thingsboard.server.common.data.query.DynamicValue)14 DeviceProfileId (org.thingsboard.server.common.data.id.DeviceProfileId)13 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)13 AttributeKvCompositeKey (org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey)13 AttributeKvEntity (org.thingsboard.server.dao.model.sql.AttributeKvEntity)13 UUID (java.util.UUID)12