Commit f325448846a92d7a3ff5a1fdcdc8e3e3f09aff65

Authored by Igor Kulikov
1 parent 1980b078

Improve initial device data generation

... ... @@ -176,27 +176,8 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
176 176 deviceProfile = this.deviceProfileService.findOrCreateDeviceProfile(device.getTenantId(), device.getType());
177 177 } else {
178 178 deviceProfile = this.deviceProfileService.findDefaultDeviceProfile(device.getTenantId());
179   - device.setType(deviceProfile.getName());
180 179 }
181 180 device.setDeviceProfileId(new DeviceProfileId(deviceProfile.getId().getId()));
182   - DeviceData deviceData = new DeviceData();
183   - switch (deviceProfile.getType()) {
184   - case DEFAULT:
185   - deviceData.setConfiguration(new DefaultDeviceConfiguration());
186   - break;
187   - }
188   - switch (deviceProfile.getTransportType()) {
189   - case DEFAULT:
190   - deviceData.setTransportConfiguration(new DefaultDeviceTransportConfiguration());
191   - break;
192   - case MQTT:
193   - deviceData.setTransportConfiguration(new MqttDeviceTransportConfiguration());
194   - break;
195   - case LWM2M:
196   - deviceData.setTransportConfiguration(new Lwm2mDeviceTransportConfiguration());
197   - break;
198   - }
199   - device.setDeviceData(deviceData);
200 181 } else {
201 182 deviceProfile = this.deviceProfileService.findDeviceProfileById(device.getTenantId(), device.getDeviceProfileId());
202 183 if (deviceProfile == null) {
... ... @@ -204,6 +185,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
204 185 }
205 186 }
206 187 device.setType(deviceProfile.getName());
  188 + device.setDeviceData(syncDeviceData(deviceProfile, device.getDeviceData()));
207 189
208 190 savedDevice = deviceDao.save(device.getTenantId(), device);
209 191 } catch (Exception t) {
... ... @@ -224,6 +206,33 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
224 206 return savedDevice;
225 207 }
226 208
  209 + private DeviceData syncDeviceData(DeviceProfile deviceProfile, DeviceData deviceData) {
  210 + if (deviceData == null) {
  211 + deviceData = new DeviceData();
  212 + }
  213 + if (deviceData.getConfiguration() == null || !deviceProfile.getType().equals(deviceData.getConfiguration().getType())) {
  214 + switch (deviceProfile.getType()) {
  215 + case DEFAULT:
  216 + deviceData.setConfiguration(new DefaultDeviceConfiguration());
  217 + break;
  218 + }
  219 + }
  220 + if (deviceData.getTransportConfiguration() == null || !deviceProfile.getTransportType().equals(deviceData.getTransportConfiguration().getType())) {
  221 + switch (deviceProfile.getTransportType()) {
  222 + case DEFAULT:
  223 + deviceData.setTransportConfiguration(new DefaultDeviceTransportConfiguration());
  224 + break;
  225 + case MQTT:
  226 + deviceData.setTransportConfiguration(new MqttDeviceTransportConfiguration());
  227 + break;
  228 + case LWM2M:
  229 + deviceData.setTransportConfiguration(new Lwm2mDeviceTransportConfiguration());
  230 + break;
  231 + }
  232 + }
  233 + return deviceData;
  234 + }
  235 +
227 236 @Override
228 237 public Device assignDeviceToCustomer(TenantId tenantId, DeviceId deviceId, CustomerId customerId) {
229 238 Device device = findDeviceById(tenantId, deviceId);
... ...