Search in sources :

Example 6 with DeviceProfileId

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

the class DeviceController method getCustomerDeviceInfos.

@ApiOperation(value = "Get Customer Device Infos (getCustomerDeviceInfos)", notes = "Returns a page of devices info objects assigned to customer. " + PAGE_DATA_PARAMETERS + DEVICE_INFO_DESCRIPTION + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/customer/{customerId}/deviceInfos", params = { "pageSize", "page" }, method = RequestMethod.GET)
@ResponseBody
public PageData<DeviceInfo> getCustomerDeviceInfos(@ApiParam(value = CUSTOMER_ID_PARAM_DESCRIPTION, required = true) @PathVariable("customerId") String strCustomerId, @ApiParam(value = PAGE_SIZE_DESCRIPTION, required = true) @RequestParam int pageSize, @ApiParam(value = PAGE_NUMBER_DESCRIPTION, required = true) @RequestParam int page, @ApiParam(value = DEVICE_TYPE_DESCRIPTION) @RequestParam(required = false) String type, @ApiParam(value = DEVICE_PROFILE_ID_PARAM_DESCRIPTION) @RequestParam(required = false) String deviceProfileId, @ApiParam(value = DEVICE_TEXT_SEARCH_DESCRIPTION) @RequestParam(required = false) String textSearch, @ApiParam(value = SORT_PROPERTY_DESCRIPTION, allowableValues = DEVICE_SORT_PROPERTY_ALLOWABLE_VALUES) @RequestParam(required = false) String sortProperty, @ApiParam(value = SORT_ORDER_DESCRIPTION, allowableValues = SORT_ORDER_ALLOWABLE_VALUES) @RequestParam(required = false) String sortOrder) throws ThingsboardException {
    checkParameter("customerId", strCustomerId);
    try {
        TenantId tenantId = getCurrentUser().getTenantId();
        CustomerId customerId = new CustomerId(toUUID(strCustomerId));
        checkCustomerId(customerId, Operation.READ);
        PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder);
        if (type != null && type.trim().length() > 0) {
            return checkNotNull(deviceService.findDeviceInfosByTenantIdAndCustomerIdAndType(tenantId, customerId, type, pageLink));
        } else if (deviceProfileId != null && deviceProfileId.length() > 0) {
            DeviceProfileId profileId = new DeviceProfileId(toUUID(deviceProfileId));
            return checkNotNull(deviceService.findDeviceInfosByTenantIdAndCustomerIdAndDeviceProfileId(tenantId, customerId, profileId, pageLink));
        } else {
            return checkNotNull(deviceService.findDeviceInfosByTenantIdAndCustomerId(tenantId, customerId, pageLink));
        }
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) PageLink(org.thingsboard.server.common.data.page.PageLink) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) IOException(java.io.IOException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with DeviceProfileId

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

the class DeviceProfileController method getTimeseriesKeys.

@ApiOperation(value = "Get time-series keys (getTimeseriesKeys)", notes = "Get a set of unique time-series keys used by devices that belong to specified profile. " + "If profile is not set returns a list of unique keys among all profiles. " + "The call is used for auto-complete in the UI forms. " + "The implementation limits the number of devices that participate in search to 100 as a trade of between accurate results and time-consuming queries. " + TENANT_AUTHORITY_PARAGRAPH, produces = "application/json")
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/deviceProfile/devices/keys/timeseries", method = RequestMethod.GET)
@ResponseBody
public List<String> getTimeseriesKeys(@ApiParam(value = DEVICE_PROFILE_ID_PARAM_DESCRIPTION) @RequestParam(name = DEVICE_PROFILE_ID, required = false) String deviceProfileIdStr) throws ThingsboardException {
    DeviceProfileId deviceProfileId;
    if (StringUtils.isNotEmpty(deviceProfileIdStr)) {
        deviceProfileId = new DeviceProfileId(UUID.fromString(deviceProfileIdStr));
        checkDeviceProfileId(deviceProfileId, Operation.READ);
    } else {
        deviceProfileId = null;
    }
    try {
        return timeseriesService.findAllKeysByDeviceProfileId(getTenantId(), deviceProfileId);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with DeviceProfileId

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

the class DeviceProfileController method setDefaultDeviceProfile.

@ApiOperation(value = "Make Device Profile Default (setDefaultDeviceProfile)", notes = "Marks device profile as default within a tenant scope." + TENANT_AUTHORITY_PARAGRAPH, produces = "application/json")
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/deviceProfile/{deviceProfileId}/default", method = RequestMethod.POST)
@ResponseBody
public DeviceProfile setDefaultDeviceProfile(@ApiParam(value = DEVICE_PROFILE_ID_PARAM_DESCRIPTION) @PathVariable(DEVICE_PROFILE_ID) String strDeviceProfileId) throws ThingsboardException {
    checkParameter(DEVICE_PROFILE_ID, strDeviceProfileId);
    try {
        DeviceProfileId deviceProfileId = new DeviceProfileId(toUUID(strDeviceProfileId));
        DeviceProfile deviceProfile = checkDeviceProfileId(deviceProfileId, Operation.WRITE);
        DeviceProfile previousDefaultDeviceProfile = deviceProfileService.findDefaultDeviceProfile(getTenantId());
        if (deviceProfileService.setDefaultDeviceProfile(getTenantId(), deviceProfileId)) {
            if (previousDefaultDeviceProfile != null) {
                previousDefaultDeviceProfile = deviceProfileService.findDeviceProfileById(getTenantId(), previousDefaultDeviceProfile.getId());
                logEntityAction(previousDefaultDeviceProfile.getId(), previousDefaultDeviceProfile, null, ActionType.UPDATED, null);
            }
            deviceProfile = deviceProfileService.findDeviceProfileById(getTenantId(), deviceProfileId);
            logEntityAction(deviceProfile.getId(), deviceProfile, null, ActionType.UPDATED, null);
        }
        return deviceProfile;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.DEVICE_PROFILE), null, null, ActionType.UPDATED, e, strDeviceProfileId);
        throw handleException(e);
    }
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 9 with DeviceProfileId

use of org.thingsboard.server.common.data.id.DeviceProfileId 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 10 with DeviceProfileId

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

the class DeviceProfileServiceImpl method removeDeviceProfile.

private void removeDeviceProfile(TenantId tenantId, DeviceProfile deviceProfile) {
    DeviceProfileId deviceProfileId = deviceProfile.getId();
    try {
        deviceProfileDao.removeById(tenantId, deviceProfileId.getId());
    } catch (Exception t) {
        ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
        if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("fk_device_profile")) {
            throw new DataValidationException("The device profile referenced by the devices cannot be deleted!");
        } else {
            throw t;
        }
    }
    deleteEntityRelations(tenantId, deviceProfileId);
    Cache cache = cacheManager.getCache(DEVICE_PROFILE_CACHE);
    cache.evict(Collections.singletonList(deviceProfileId.getId()));
    cache.evict(Arrays.asList("info", deviceProfileId.getId()));
    cache.evict(Arrays.asList(tenantId.getId(), deviceProfile.getName()));
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Cache(org.springframework.cache.Cache)

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