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;
}
Aggregations