Search in sources :

Example 21 with DeviceProfile

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

the class CoapTransportResource method processAccessTokenRequest.

private void processAccessTokenRequest(CoapExchange exchange, SessionMsgType type, Request request) {
    Optional<DeviceTokenCredentials> credentials = decodeCredentials(request);
    if (credentials.isEmpty()) {
        exchange.respond(CoAP.ResponseCode.UNAUTHORIZED);
        return;
    }
    transportService.process(DeviceTransportType.COAP, TransportProtos.ValidateDeviceTokenRequestMsg.newBuilder().setToken(credentials.get().getCredentialsId()).build(), new CoapDeviceAuthCallback(exchange, (deviceCredentials, deviceProfile) -> processRequest(exchange, type, request, deviceCredentials, deviceProfile)));
}
Also used : JsonParseException(com.google.gson.JsonParseException) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) Response(org.eclipse.californium.core.coap.Response) FeatureType(org.thingsboard.server.common.msg.session.FeatureType) TbCoapDtlsSessionInfo(org.thingsboard.server.coapserver.TbCoapDtlsSessionInfo) Exchange(org.eclipse.californium.core.network.Exchange) TransportServiceCallback(org.thingsboard.server.common.transport.TransportServiceCallback) CoapDeviceAuthCallback(org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback) TransportPayloadType(org.thingsboard.server.common.data.TransportPayloadType) CoapNoOpCallback(org.thingsboard.server.transport.coap.callback.CoapNoOpCallback) Random(java.util.Random) ConcurrentMap(java.util.concurrent.ConcurrentMap) SessionMsgType(org.thingsboard.server.common.msg.session.SessionMsgType) ObserveRelation(org.eclipse.californium.core.observe.ObserveRelation) Resource(org.eclipse.californium.core.server.resources.Resource) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeviceTransportType(org.thingsboard.server.common.data.DeviceTransportType) JsonConverter(org.thingsboard.server.common.transport.adaptor.JsonConverter) GetAttributesSyncSessionCallback(org.thingsboard.server.transport.coap.callback.GetAttributesSyncSessionCallback) CoapServerService(org.thingsboard.server.coapserver.CoapServerService) ToServerRpcSyncSessionCallback(org.thingsboard.server.transport.coap.callback.ToServerRpcSyncSessionCallback) CoapClientContext(org.thingsboard.server.transport.coap.client.CoapClientContext) TbCoapClientState(org.thingsboard.server.transport.coap.client.TbCoapClientState) CoapOkCallback(org.thingsboard.server.transport.coap.callback.CoapOkCallback) DataConstants(org.thingsboard.server.common.data.DataConstants) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) ResourceObserver(org.eclipse.californium.core.server.resources.ResourceObserver) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) List(java.util.List) Request(org.eclipse.californium.core.coap.Request) CoAP(org.eclipse.californium.core.coap.CoAP) KEY_SESSION_ID(org.eclipse.californium.elements.DtlsEndpointContext.KEY_SESSION_ID) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) Optional(java.util.Optional) ValidateDeviceCredentialsResponse(org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) CoapDeviceAuthCallback(org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback)

Example 22 with DeviceProfile

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

the class DeviceProfileEntity method toData.

@Override
public DeviceProfile toData() {
    DeviceProfile deviceProfile = new DeviceProfile(new DeviceProfileId(this.getUuid()));
    deviceProfile.setCreatedTime(createdTime);
    if (tenantId != null) {
        deviceProfile.setTenantId(TenantId.fromUUID(tenantId));
    }
    deviceProfile.setName(name);
    deviceProfile.setType(type);
    deviceProfile.setImage(image);
    deviceProfile.setTransportType(transportType);
    deviceProfile.setProvisionType(provisionType);
    deviceProfile.setDescription(description);
    deviceProfile.setDefault(isDefault);
    deviceProfile.setProfileData(JacksonUtil.convertValue(profileData, DeviceProfileData.class));
    if (defaultRuleChainId != null) {
        deviceProfile.setDefaultRuleChainId(new RuleChainId(defaultRuleChainId));
    }
    if (defaultDashboardId != null) {
        deviceProfile.setDefaultDashboardId(new DashboardId(defaultDashboardId));
    }
    deviceProfile.setDefaultQueueName(defaultQueueName);
    deviceProfile.setProvisionDeviceKey(provisionDeviceKey);
    if (firmwareId != null) {
        deviceProfile.setFirmwareId(new OtaPackageId(firmwareId));
    }
    if (softwareId != null) {
        deviceProfile.setSoftwareId(new OtaPackageId(softwareId));
    }
    return deviceProfile;
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) DashboardId(org.thingsboard.server.common.data.id.DashboardId) DeviceProfileData(org.thingsboard.server.common.data.device.profile.DeviceProfileData)

Example 23 with DeviceProfile

use of org.thingsboard.server.common.data.DeviceProfile 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 24 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 25 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)

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