Commit 37891ec6c6f4313fb74f46fc8e83371e56367f65

Authored by Andrii Shvaika
1 parent ab10dd44

Minor refactoring

... ... @@ -24,7 +24,7 @@ import org.thingsboard.server.common.data.DataConstants;
24 24 import org.thingsboard.server.common.data.Device;
25 25 import org.thingsboard.server.common.data.DeviceProfile;
26 26 import org.thingsboard.server.common.data.FirmwareInfo;
27   -import org.thingsboard.server.common.data.firmware.FirmwareKeyUtil;
  27 +import org.thingsboard.server.common.data.firmware.FirmwareUtil;
28 28 import org.thingsboard.server.common.data.firmware.FirmwareType;
29 29 import org.thingsboard.server.common.data.id.DeviceId;
30 30 import org.thingsboard.server.common.data.id.FirmwareId;
... ... @@ -66,9 +66,9 @@ import static org.thingsboard.server.common.data.firmware.FirmwareKey.STATE;
66 66 import static org.thingsboard.server.common.data.firmware.FirmwareKey.TITLE;
67 67 import static org.thingsboard.server.common.data.firmware.FirmwareKey.TS;
68 68 import static org.thingsboard.server.common.data.firmware.FirmwareKey.VERSION;
69   -import static org.thingsboard.server.common.data.firmware.FirmwareKeyUtil.getAttributeKey;
70   -import static org.thingsboard.server.common.data.firmware.FirmwareKeyUtil.getTargetTelemetryKey;
71   -import static org.thingsboard.server.common.data.firmware.FirmwareKeyUtil.getTelemetryKey;
  69 +import static org.thingsboard.server.common.data.firmware.FirmwareUtil.getAttributeKey;
  70 +import static org.thingsboard.server.common.data.firmware.FirmwareUtil.getTargetTelemetryKey;
  71 +import static org.thingsboard.server.common.data.firmware.FirmwareUtil.getTelemetryKey;
72 72 import static org.thingsboard.server.common.data.firmware.FirmwareType.FIRMWARE;
73 73 import static org.thingsboard.server.common.data.firmware.FirmwareType.SOFTWARE;
74 74
... ... @@ -217,30 +217,10 @@ public class DefaultFirmwareStateService implements FirmwareStateService {
217 217 if (device == null) {
218 218 log.warn("[{}] [{}] Device was removed during firmware update msg was queued!", tenantId, deviceId);
219 219 } else {
220   - FirmwareId currentFirmwareId;
221   -
222   - switch (firmwareType) {
223   - case FIRMWARE:
224   - currentFirmwareId = device.getFirmwareId();
225   - break;
226   - case SOFTWARE:
227   - currentFirmwareId = device.getSoftwareId();
228   - break;
229   - default:
230   - log.warn("Unsupported firmware type: [{}]", firmwareType);
231   - return false;
232   - }
233   -
  220 + FirmwareId currentFirmwareId = FirmwareUtil.getFirmwareId(device, firmwareType);
234 221 if (currentFirmwareId == null) {
235 222 DeviceProfile deviceProfile = deviceProfileService.findDeviceProfileById(tenantId, device.getDeviceProfileId());
236   - switch (firmwareType) {
237   - case FIRMWARE:
238   - currentFirmwareId = deviceProfile.getFirmwareId();
239   - break;
240   - case SOFTWARE:
241   - currentFirmwareId = deviceProfile.getSoftwareId();
242   - break;
243   - }
  223 + currentFirmwareId = FirmwareUtil.getFirmwareId(deviceProfile, firmwareType);
244 224 }
245 225
246 226 if (targetFirmwareId.equals(currentFirmwareId)) {
... ... @@ -333,13 +313,13 @@ public class DefaultFirmwareStateService implements FirmwareStateService {
333 313 }
334 314
335 315 private void remove(Device device, FirmwareType firmwareType) {
336   - telemetryService.deleteAndNotify(device.getTenantId(), device.getId(), DataConstants.SHARED_SCOPE, FirmwareKeyUtil.getAttributeKeys(firmwareType),
  316 + telemetryService.deleteAndNotify(device.getTenantId(), device.getId(), DataConstants.SHARED_SCOPE, FirmwareUtil.getAttributeKeys(firmwareType),
337 317 new FutureCallback<>() {
338 318 @Override
339 319 public void onSuccess(@Nullable Void tmp) {
340 320 log.trace("[{}] Success remove target firmware attributes!", device.getId());
341 321 Set<AttributeKey> keysToNotify = new HashSet<>();
342   - FirmwareKeyUtil.ALL_FW_ATTRIBUTE_KEYS.forEach(key -> keysToNotify.add(new AttributeKey(DataConstants.SHARED_SCOPE, key)));
  322 + FirmwareUtil.ALL_FW_ATTRIBUTE_KEYS.forEach(key -> keysToNotify.add(new AttributeKey(DataConstants.SHARED_SCOPE, key)));
343 323 tbClusterService.pushMsgToCore(DeviceAttributesEventNotificationMsg.onDelete(device.getTenantId(), device.getId(), keysToNotify), null);
344 324 }
345 325
... ...
... ... @@ -41,6 +41,7 @@ import org.thingsboard.server.common.data.device.credentials.BasicMqttCredential
41 41 import org.thingsboard.server.common.data.device.credentials.ProvisionDeviceCredentialsData;
42 42 import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileCredentials;
43 43 import org.thingsboard.server.common.data.firmware.FirmwareType;
  44 +import org.thingsboard.server.common.data.firmware.FirmwareUtil;
44 45 import org.thingsboard.server.common.data.id.CustomerId;
45 46 import org.thingsboard.server.common.data.id.DeviceId;
46 47 import org.thingsboard.server.common.data.id.DeviceProfileId;
... ... @@ -463,26 +464,10 @@ public class DefaultTransportApiService implements TransportApiService {
463 464 return getEmptyTransportApiResponseFuture();
464 465 }
465 466
466   - FirmwareId firmwareId = null;
467   - switch (firmwareType) {
468   - case FIRMWARE:
469   - firmwareId = device.getFirmwareId();
470   - break;
471   - case SOFTWARE:
472   - firmwareId = device.getSoftwareId();
473   - break;
474   - }
475   -
  467 + FirmwareId firmwareId = FirmwareUtil.getFirmwareId(device, firmwareType);
476 468 if (firmwareId == null) {
477 469 DeviceProfile deviceProfile = deviceProfileCache.find(device.getDeviceProfileId());
478   - switch (firmwareType) {
479   - case FIRMWARE:
480   - firmwareId = deviceProfile.getFirmwareId();
481   - break;
482   - case SOFTWARE:
483   - firmwareId = deviceProfile.getSoftwareId();
484   - break;
485   - }
  470 + firmwareId = FirmwareUtil.getFirmwareId(deviceProfile, firmwareType);
486 471 }
487 472
488 473 TransportProtos.GetFirmwareResponseMsg.Builder builder = TransportProtos.GetFirmwareResponseMsg.newBuilder();
... ...
... ... @@ -32,7 +32,7 @@ import java.io.IOException;
32 32
33 33 @EqualsAndHashCode(callSuper = true)
34 34 @Slf4j
35   -public class Device extends SearchTextBasedWithAdditionalInfo<DeviceId> implements HasName, HasTenantId, HasCustomerId {
  35 +public class Device extends SearchTextBasedWithAdditionalInfo<DeviceId> implements HasName, HasTenantId, HasCustomerId, HasFirmware {
36 36
37 37 private static final long serialVersionUID = 2807343040519543363L;
38 38
... ...
... ... @@ -36,7 +36,7 @@ import static org.thingsboard.server.common.data.SearchTextBasedWithAdditionalIn
36 36 @Data
37 37 @EqualsAndHashCode(callSuper = true)
38 38 @Slf4j
39   -public class DeviceProfile extends SearchTextBased<DeviceProfileId> implements HasName, HasTenantId {
  39 +public class DeviceProfile extends SearchTextBased<DeviceProfileId> implements HasName, HasTenantId, HasFirmware {
40 40
41 41 private TenantId tenantId;
42 42 @NoXss
... ...
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.common.data;
  17 +
  18 +import org.thingsboard.server.common.data.id.FirmwareId;
  19 +
  20 +public interface HasFirmware {
  21 +
  22 + FirmwareId getFirmwareId();
  23 +
  24 + FirmwareId getSoftwareId();
  25 +}
... ...
common/data/src/main/java/org/thingsboard/server/common/data/firmware/FirmwareUtil.java renamed from common/data/src/main/java/org/thingsboard/server/common/data/firmware/FirmwareKeyUtil.java
... ... @@ -15,6 +15,10 @@
15 15 */
16 16 package org.thingsboard.server.common.data.firmware;
17 17
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.thingsboard.server.common.data.HasFirmware;
  20 +import org.thingsboard.server.common.data.id.FirmwareId;
  21 +
18 22 import java.util.ArrayList;
19 23 import java.util.Collections;
20 24 import java.util.List;
... ... @@ -22,7 +26,8 @@ import java.util.List;
22 26 import static org.thingsboard.server.common.data.firmware.FirmwareType.FIRMWARE;
23 27 import static org.thingsboard.server.common.data.firmware.FirmwareType.SOFTWARE;
24 28
25   -public class FirmwareKeyUtil {
  29 +@Slf4j
  30 +public class FirmwareUtil {
26 31
27 32 public static final List<String> ALL_FW_ATTRIBUTE_KEYS;
28 33
... ... @@ -71,4 +76,16 @@ public class FirmwareKeyUtil {
71 76 public static String getTelemetryKey(FirmwareType type, FirmwareKey key) {
72 77 return type.getKeyPrefix() + "_" + key.getValue();
73 78 }
  79 +
  80 + public static FirmwareId getFirmwareId(HasFirmware entity, FirmwareType firmwareType) {
  81 + switch (firmwareType) {
  82 + case FIRMWARE:
  83 + return entity.getFirmwareId();
  84 + case SOFTWARE:
  85 + return entity.getSoftwareId();
  86 + default:
  87 + log.warn("Unsupported firmware type: [{}]", firmwareType);
  88 + return null;
  89 + }
  90 + }
74 91 }
... ...