From f325448846a92d7a3ff5a1fdcdc8e3e3f09aff65 Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Tue, 8 Sep 2020 11:47:05 +0300 Subject: [PATCH] Improve initial device data generation --- .../server/dao/device/DeviceServiceImpl.java | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java index ead57c0b08..ad66ed47fd 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java @@ -176,27 +176,8 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe deviceProfile = this.deviceProfileService.findOrCreateDeviceProfile(device.getTenantId(), device.getType()); } else { deviceProfile = this.deviceProfileService.findDefaultDeviceProfile(device.getTenantId()); - device.setType(deviceProfile.getName()); } device.setDeviceProfileId(new DeviceProfileId(deviceProfile.getId().getId())); - DeviceData deviceData = new DeviceData(); - switch (deviceProfile.getType()) { - case DEFAULT: - deviceData.setConfiguration(new DefaultDeviceConfiguration()); - break; - } - switch (deviceProfile.getTransportType()) { - case DEFAULT: - deviceData.setTransportConfiguration(new DefaultDeviceTransportConfiguration()); - break; - case MQTT: - deviceData.setTransportConfiguration(new MqttDeviceTransportConfiguration()); - break; - case LWM2M: - deviceData.setTransportConfiguration(new Lwm2mDeviceTransportConfiguration()); - break; - } - device.setDeviceData(deviceData); } else { deviceProfile = this.deviceProfileService.findDeviceProfileById(device.getTenantId(), device.getDeviceProfileId()); if (deviceProfile == null) { @@ -204,6 +185,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe } } device.setType(deviceProfile.getName()); + device.setDeviceData(syncDeviceData(deviceProfile, device.getDeviceData())); savedDevice = deviceDao.save(device.getTenantId(), device); } catch (Exception t) { @@ -224,6 +206,33 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe return savedDevice; } + private DeviceData syncDeviceData(DeviceProfile deviceProfile, DeviceData deviceData) { + if (deviceData == null) { + deviceData = new DeviceData(); + } + if (deviceData.getConfiguration() == null || !deviceProfile.getType().equals(deviceData.getConfiguration().getType())) { + switch (deviceProfile.getType()) { + case DEFAULT: + deviceData.setConfiguration(new DefaultDeviceConfiguration()); + break; + } + } + if (deviceData.getTransportConfiguration() == null || !deviceProfile.getTransportType().equals(deviceData.getTransportConfiguration().getType())) { + switch (deviceProfile.getTransportType()) { + case DEFAULT: + deviceData.setTransportConfiguration(new DefaultDeviceTransportConfiguration()); + break; + case MQTT: + deviceData.setTransportConfiguration(new MqttDeviceTransportConfiguration()); + break; + case LWM2M: + deviceData.setTransportConfiguration(new Lwm2mDeviceTransportConfiguration()); + break; + } + } + return deviceData; + } + @Override public Device assignDeviceToCustomer(TenantId tenantId, DeviceId deviceId, CustomerId customerId) { Device device = findDeviceById(tenantId, deviceId);