Search in sources :

Example 76 with DeviceProfile

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

the class LwM2mClientContextImpl method doGetAndCache.

private Lwm2mDeviceProfileTransportConfiguration doGetAndCache(UUID profileId) {
    Lwm2mDeviceProfileTransportConfiguration result = profiles.get(profileId);
    if (result == null) {
        log.debug("Fetching profile [{}]", profileId);
        DeviceProfile deviceProfile = deviceProfileCache.get(new DeviceProfileId(profileId));
        if (deviceProfile != null) {
            result = profileUpdate(deviceProfile);
        } else {
            log.warn("Device profile was not found! Most probably device profile [{}] has been removed from the database.", profileId);
        }
    }
    return result;
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration)

Example 77 with DeviceProfile

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

the class DeviceProfileServiceImpl method findOrCreateDeviceProfile.

@Cacheable(cacheNames = DEVICE_PROFILE_CACHE, key = "{#tenantId.id, #name}")
@Override
public DeviceProfile findOrCreateDeviceProfile(TenantId tenantId, String name) {
    log.trace("Executing findOrCreateDefaultDeviceProfile");
    DeviceProfile deviceProfile = findDeviceProfileByName(tenantId, name);
    if (deviceProfile == null) {
        findOrCreateLock.lock();
        try {
            deviceProfile = findDeviceProfileByName(tenantId, name);
            if (deviceProfile == null) {
                deviceProfile = this.doCreateDefaultDeviceProfile(tenantId, name, name.equals("default"));
            }
        } finally {
            findOrCreateLock.unlock();
        }
    }
    return deviceProfile;
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 78 with DeviceProfile

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

the class DeviceProfileServiceImpl method deleteDeviceProfile.

@Override
public void deleteDeviceProfile(TenantId tenantId, DeviceProfileId deviceProfileId) {
    log.trace("Executing deleteDeviceProfile [{}]", deviceProfileId);
    Validator.validateId(deviceProfileId, INCORRECT_DEVICE_PROFILE_ID + deviceProfileId);
    DeviceProfile deviceProfile = deviceProfileDao.findById(tenantId, deviceProfileId.getId());
    if (deviceProfile != null && deviceProfile.isDefault()) {
        throw new DataValidationException("Deletion of Default Device Profile is prohibited!");
    }
    this.removeDeviceProfile(tenantId, deviceProfile);
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException)

Example 79 with DeviceProfile

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

the class DeviceProfileServiceImpl method saveDeviceProfile.

@Override
public DeviceProfile saveDeviceProfile(DeviceProfile deviceProfile) {
    log.trace("Executing saveDeviceProfile [{}]", deviceProfile);
    deviceProfileValidator.validate(deviceProfile, DeviceProfile::getTenantId);
    DeviceProfile oldDeviceProfile = null;
    if (deviceProfile.getId() != null) {
        oldDeviceProfile = deviceProfileDao.findById(deviceProfile.getTenantId(), deviceProfile.getId().getId());
    }
    DeviceProfile savedDeviceProfile;
    try {
        savedDeviceProfile = deviceProfileDao.saveAndFlush(deviceProfile.getTenantId(), deviceProfile);
    } catch (Exception t) {
        ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
        if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_profile_name_unq_key")) {
            throw new DataValidationException("Device profile with such name already exists!");
        } else if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_provision_key_unq_key")) {
            throw new DataValidationException("Device profile with such provision device key already exists!");
        } else {
            throw t;
        }
    }
    Cache cache = cacheManager.getCache(DEVICE_PROFILE_CACHE);
    cache.evict(Collections.singletonList(savedDeviceProfile.getId().getId()));
    cache.evict(Arrays.asList("info", savedDeviceProfile.getId().getId()));
    cache.evict(Arrays.asList(deviceProfile.getTenantId().getId(), deviceProfile.getName()));
    if (savedDeviceProfile.isDefault()) {
        cache.evict(Arrays.asList("default", savedDeviceProfile.getTenantId().getId()));
        cache.evict(Arrays.asList("default", "info", savedDeviceProfile.getTenantId().getId()));
    }
    if (oldDeviceProfile != null && !oldDeviceProfile.getName().equals(deviceProfile.getName())) {
        PageLink pageLink = new PageLink(100);
        PageData<Device> pageData;
        do {
            pageData = deviceDao.findDevicesByTenantIdAndProfileId(deviceProfile.getTenantId().getId(), deviceProfile.getUuidId(), pageLink);
            for (Device device : pageData.getData()) {
                device.setType(deviceProfile.getName());
                deviceService.saveDevice(device);
            }
            pageLink = pageLink.nextPageLink();
        } while (pageData.hasNext());
    }
    return savedDeviceProfile;
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Device(org.thingsboard.server.common.data.Device) PageLink(org.thingsboard.server.common.data.page.PageLink) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Cache(org.springframework.cache.Cache)

Example 80 with DeviceProfile

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

the class DeviceProfileServiceImpl method doCreateDefaultDeviceProfile.

private DeviceProfile doCreateDefaultDeviceProfile(TenantId tenantId, String profileName, boolean defaultProfile) {
    validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
    DeviceProfile deviceProfile = new DeviceProfile();
    deviceProfile.setTenantId(tenantId);
    deviceProfile.setDefault(defaultProfile);
    deviceProfile.setName(profileName);
    deviceProfile.setType(DeviceProfileType.DEFAULT);
    deviceProfile.setTransportType(DeviceTransportType.DEFAULT);
    deviceProfile.setProvisionType(DeviceProfileProvisionType.DISABLED);
    deviceProfile.setDescription("Default device profile");
    DeviceProfileData deviceProfileData = new DeviceProfileData();
    DefaultDeviceProfileConfiguration configuration = new DefaultDeviceProfileConfiguration();
    DefaultDeviceProfileTransportConfiguration transportConfiguration = new DefaultDeviceProfileTransportConfiguration();
    DisabledDeviceProfileProvisionConfiguration provisionConfiguration = new DisabledDeviceProfileProvisionConfiguration(null);
    deviceProfileData.setConfiguration(configuration);
    deviceProfileData.setTransportConfiguration(transportConfiguration);
    deviceProfileData.setProvisionConfiguration(provisionConfiguration);
    deviceProfile.setProfileData(deviceProfileData);
    return saveDeviceProfile(deviceProfile);
}
Also used : DefaultDeviceProfileConfiguration(org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DefaultDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration) 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