Search in sources :

Example 51 with DeviceProfile

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

the class DeviceServiceImpl method saveDeviceWithoutCredentials.

private Device saveDeviceWithoutCredentials(Device device, boolean doValidate) {
    log.trace("Executing saveDevice [{}]", device);
    if (doValidate) {
        deviceValidator.validate(device, Device::getTenantId);
    }
    try {
        DeviceProfile deviceProfile;
        if (device.getDeviceProfileId() == null) {
            if (!StringUtils.isEmpty(device.getType())) {
                deviceProfile = this.deviceProfileService.findOrCreateDeviceProfile(device.getTenantId(), device.getType());
            } else {
                deviceProfile = this.deviceProfileService.findDefaultDeviceProfile(device.getTenantId());
            }
            device.setDeviceProfileId(new DeviceProfileId(deviceProfile.getId().getId()));
        } else {
            deviceProfile = this.deviceProfileService.findDeviceProfileById(device.getTenantId(), device.getDeviceProfileId());
            if (deviceProfile == null) {
                throw new DataValidationException("Device is referencing non existing device profile!");
            }
        }
        device.setType(deviceProfile.getName());
        device.setDeviceData(syncDeviceData(deviceProfile, device.getDeviceData()));
        return deviceDao.saveAndFlush(device.getTenantId(), device);
    } catch (Exception t) {
        ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
        if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_name_unq_key")) {
            // remove device from cache in case null value cached in the distributed redis.
            cacheManager.removeDeviceFromCacheByName(device.getTenantId(), device.getName());
            cacheManager.removeDeviceFromCacheById(device.getTenantId(), device.getId());
            throw new DataValidationException("Device with such name already exists!");
        } else {
            throw t;
        }
    }
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) Device(org.thingsboard.server.common.data.Device) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 52 with DeviceProfile

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

the class DeviceProfileServiceImpl method setDefaultDeviceProfile.

@Override
public boolean setDefaultDeviceProfile(TenantId tenantId, DeviceProfileId deviceProfileId) {
    log.trace("Executing setDefaultDeviceProfile [{}]", deviceProfileId);
    Validator.validateId(deviceProfileId, INCORRECT_DEVICE_PROFILE_ID + deviceProfileId);
    DeviceProfile deviceProfile = deviceProfileDao.findById(tenantId, deviceProfileId.getId());
    if (!deviceProfile.isDefault()) {
        Cache cache = cacheManager.getCache(DEVICE_PROFILE_CACHE);
        deviceProfile.setDefault(true);
        DeviceProfile previousDefaultDeviceProfile = findDefaultDeviceProfile(tenantId);
        boolean changed = false;
        if (previousDefaultDeviceProfile == null) {
            deviceProfileDao.save(tenantId, deviceProfile);
            changed = true;
        } else if (!previousDefaultDeviceProfile.getId().equals(deviceProfile.getId())) {
            previousDefaultDeviceProfile.setDefault(false);
            deviceProfileDao.save(tenantId, previousDefaultDeviceProfile);
            deviceProfileDao.save(tenantId, deviceProfile);
            cache.evict(Collections.singletonList(previousDefaultDeviceProfile.getId().getId()));
            cache.evict(Arrays.asList("info", previousDefaultDeviceProfile.getId().getId()));
            cache.evict(Arrays.asList(tenantId.getId(), previousDefaultDeviceProfile.getName()));
            changed = true;
        }
        if (changed) {
            cache.evict(Collections.singletonList(deviceProfile.getId().getId()));
            cache.evict(Arrays.asList("info", deviceProfile.getId().getId()));
            cache.evict(Arrays.asList("default", tenantId.getId()));
            cache.evict(Arrays.asList("default", "info", tenantId.getId()));
            cache.evict(Arrays.asList(tenantId.getId(), deviceProfile.getName()));
        }
        return changed;
    }
    return false;
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) Cache(org.springframework.cache.Cache)

Example 53 with DeviceProfile

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

the class BaseOtaPackageServiceTest method testDeleteFirmwareWithReferenceByDeviceProfile.

@Test
public void testDeleteFirmwareWithReferenceByDeviceProfile() {
    DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Test Device Profile");
    DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
    OtaPackage firmware = new OtaPackage();
    firmware.setTenantId(tenantId);
    firmware.setDeviceProfileId(savedDeviceProfile.getId());
    firmware.setType(FIRMWARE);
    firmware.setTitle(TITLE);
    firmware.setVersion(VERSION);
    firmware.setFileName(FILE_NAME);
    firmware.setContentType(CONTENT_TYPE);
    firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
    firmware.setChecksum(CHECKSUM);
    firmware.setData(DATA);
    firmware.setDataSize(DATA_SIZE);
    OtaPackage savedFirmware = otaPackageService.saveOtaPackage(firmware);
    savedDeviceProfile.setFirmwareId(savedFirmware.getId());
    deviceProfileService.saveDeviceProfile(savedDeviceProfile);
    try {
        thrown.expect(DataValidationException.class);
        thrown.expectMessage("The otaPackage referenced by the device profile cannot be deleted!");
        otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
    } finally {
        deviceProfileService.deleteDeviceProfile(tenantId, savedDeviceProfile.getId());
        otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
    }
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) OtaPackage(org.thingsboard.server.common.data.OtaPackage) Test(org.junit.Test)

Example 54 with DeviceProfile

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

the class DeviceProvisionServiceImpl method provisionDevice.

@Override
public ProvisionResponse provisionDevice(ProvisionRequest provisionRequest) {
    String provisionRequestKey = provisionRequest.getCredentials().getProvisionDeviceKey();
    String provisionRequestSecret = provisionRequest.getCredentials().getProvisionDeviceSecret();
    if (!StringUtils.isEmpty(provisionRequest.getDeviceName())) {
        provisionRequest.setDeviceName(provisionRequest.getDeviceName().trim());
        if (StringUtils.isEmpty(provisionRequest.getDeviceName())) {
            log.warn("Provision request contains empty device name!");
            throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
        }
    }
    if (StringUtils.isEmpty(provisionRequestKey) || StringUtils.isEmpty(provisionRequestSecret)) {
        throw new ProvisionFailedException(ProvisionResponseStatus.NOT_FOUND.name());
    }
    DeviceProfile targetProfile = deviceProfileDao.findByProvisionDeviceKey(provisionRequestKey);
    if (targetProfile == null || targetProfile.getProfileData().getProvisionConfiguration() == null || targetProfile.getProfileData().getProvisionConfiguration().getProvisionDeviceSecret() == null) {
        throw new ProvisionFailedException(ProvisionResponseStatus.NOT_FOUND.name());
    }
    Device targetDevice = deviceDao.findDeviceByTenantIdAndName(targetProfile.getTenantId().getId(), provisionRequest.getDeviceName()).orElse(null);
    switch(targetProfile.getProvisionType()) {
        case ALLOW_CREATE_NEW_DEVICES:
            if (targetProfile.getProfileData().getProvisionConfiguration().getProvisionDeviceSecret().equals(provisionRequestSecret)) {
                if (targetDevice != null) {
                    log.warn("[{}] The device is present and could not be provisioned once more!", targetDevice.getName());
                    notify(targetDevice, provisionRequest, DataConstants.PROVISION_FAILURE, false);
                    throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
                } else {
                    return createDevice(provisionRequest, targetProfile);
                }
            }
            break;
        case CHECK_PRE_PROVISIONED_DEVICES:
            if (targetProfile.getProfileData().getProvisionConfiguration().getProvisionDeviceSecret().equals(provisionRequestSecret)) {
                if (targetDevice != null && targetDevice.getDeviceProfileId().equals(targetProfile.getId())) {
                    return processProvision(targetDevice, provisionRequest);
                } else {
                    log.warn("[{}] Failed to find pre provisioned device!", provisionRequest.getDeviceName());
                    throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
                }
            }
            break;
    }
    throw new ProvisionFailedException(ProvisionResponseStatus.NOT_FOUND.name());
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) Device(org.thingsboard.server.common.data.Device)

Example 55 with DeviceProfile

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

the class AbstractLwM2MIntegrationTest method createDeviceProfile.

protected void createDeviceProfile(Lwm2mDeviceProfileTransportConfiguration transportConfiguration) throws Exception {
    deviceProfile = new DeviceProfile();
    deviceProfile.setName("LwM2M");
    deviceProfile.setType(DeviceProfileType.DEFAULT);
    deviceProfile.setTenantId(tenantId);
    deviceProfile.setTransportType(DeviceTransportType.LWM2M);
    deviceProfile.setProvisionType(DeviceProfileProvisionType.DISABLED);
    deviceProfile.setDescription(deviceProfile.getName());
    DeviceProfileData deviceProfileData = new DeviceProfileData();
    deviceProfileData.setConfiguration(new DefaultDeviceProfileConfiguration());
    deviceProfileData.setProvisionConfiguration(new DisabledDeviceProfileProvisionConfiguration(null));
    deviceProfileData.setTransportConfiguration(transportConfiguration);
    deviceProfile.setProfileData(deviceProfileData);
    deviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class);
    Assert.assertNotNull(deviceProfile);
}
Also used : DefaultDeviceProfileConfiguration(org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DeviceProfileData(org.thingsboard.server.common.data.device.profile.DeviceProfileData) DisabledDeviceProfileProvisionConfiguration(org.thingsboard.server.common.data.device.profile.DisabledDeviceProfileProvisionConfiguration)

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