Search in sources :

Example 6 with BasicMqttCredentials

use of org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials in project thingsboard by thingsboard.

the class DeviceServiceImpl method saveDevice.

@Override
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#profile.tenantId, #provisionRequest.deviceName}")
@Transactional
public Device saveDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
    Device device = new Device();
    device.setName(provisionRequest.getDeviceName());
    device.setType(profile.getName());
    device.setTenantId(profile.getTenantId());
    Device savedDevice = saveDevice(device);
    if (!StringUtils.isEmpty(provisionRequest.getCredentialsData().getToken()) || !StringUtils.isEmpty(provisionRequest.getCredentialsData().getX509CertHash()) || !StringUtils.isEmpty(provisionRequest.getCredentialsData().getUsername()) || !StringUtils.isEmpty(provisionRequest.getCredentialsData().getPassword()) || !StringUtils.isEmpty(provisionRequest.getCredentialsData().getClientId())) {
        DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedDevice.getTenantId(), savedDevice.getId());
        if (deviceCredentials == null) {
            deviceCredentials = new DeviceCredentials();
        }
        deviceCredentials.setDeviceId(savedDevice.getId());
        deviceCredentials.setCredentialsType(provisionRequest.getCredentialsType());
        switch(provisionRequest.getCredentialsType()) {
            case ACCESS_TOKEN:
                deviceCredentials.setCredentialsId(provisionRequest.getCredentialsData().getToken());
                break;
            case MQTT_BASIC:
                BasicMqttCredentials mqttCredentials = new BasicMqttCredentials();
                mqttCredentials.setClientId(provisionRequest.getCredentialsData().getClientId());
                mqttCredentials.setUserName(provisionRequest.getCredentialsData().getUsername());
                mqttCredentials.setPassword(provisionRequest.getCredentialsData().getPassword());
                deviceCredentials.setCredentialsValue(JacksonUtil.toString(mqttCredentials));
                break;
            case X509_CERTIFICATE:
                deviceCredentials.setCredentialsValue(provisionRequest.getCredentialsData().getX509CertHash());
                break;
            case LWM2M_CREDENTIALS:
                break;
        }
        try {
            deviceCredentialsService.updateDeviceCredentials(savedDevice.getTenantId(), deviceCredentials);
        } catch (Exception e) {
            throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
        }
    }
    // eviction by name is described as annotation @CacheEvict above
    cacheManager.removeDeviceFromCacheById(savedDevice.getTenantId(), savedDevice.getId());
    return savedDevice;
}
Also used : BasicMqttCredentials(org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials) ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) 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) CacheEvict(org.springframework.cache.annotation.CacheEvict) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

BasicMqttCredentials (org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials)6 Device (org.thingsboard.server.common.data.Device)3 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)3 JsonObject (com.google.gson.JsonObject)1 ExecutionException (java.util.concurrent.ExecutionException)1 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 Before (org.junit.Before)1 CacheEvict (org.springframework.cache.annotation.CacheEvict)1 Transactional (org.springframework.transaction.annotation.Transactional)1 Tenant (org.thingsboard.server.common.data.Tenant)1 User (org.thingsboard.server.common.data.User)1 ProvisionFailedException (org.thingsboard.server.dao.device.provision.ProvisionFailedException)1 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)1 DeviceCredentialsValidationException (org.thingsboard.server.dao.exception.DeviceCredentialsValidationException)1 CredentialsDataProto (org.thingsboard.server.gen.transport.TransportProtos.CredentialsDataProto)1 ProvisionDeviceResponseMsg (org.thingsboard.server.gen.transport.TransportProtos.ProvisionDeviceResponseMsg)1