Minor refactoring

This commit is contained in:
Andrii Shvaika 2021-04-30 11:14:29 +03:00
parent ab10dd4494
commit 37891ec6c6
6 changed files with 56 additions and 49 deletions

View File

@ -24,7 +24,7 @@ import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.FirmwareInfo;
import org.thingsboard.server.common.data.firmware.FirmwareKeyUtil;
import org.thingsboard.server.common.data.firmware.FirmwareUtil;
import org.thingsboard.server.common.data.firmware.FirmwareType;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.FirmwareId;
@ -66,9 +66,9 @@ import static org.thingsboard.server.common.data.firmware.FirmwareKey.STATE;
import static org.thingsboard.server.common.data.firmware.FirmwareKey.TITLE;
import static org.thingsboard.server.common.data.firmware.FirmwareKey.TS;
import static org.thingsboard.server.common.data.firmware.FirmwareKey.VERSION;
import static org.thingsboard.server.common.data.firmware.FirmwareKeyUtil.getAttributeKey;
import static org.thingsboard.server.common.data.firmware.FirmwareKeyUtil.getTargetTelemetryKey;
import static org.thingsboard.server.common.data.firmware.FirmwareKeyUtil.getTelemetryKey;
import static org.thingsboard.server.common.data.firmware.FirmwareUtil.getAttributeKey;
import static org.thingsboard.server.common.data.firmware.FirmwareUtil.getTargetTelemetryKey;
import static org.thingsboard.server.common.data.firmware.FirmwareUtil.getTelemetryKey;
import static org.thingsboard.server.common.data.firmware.FirmwareType.FIRMWARE;
import static org.thingsboard.server.common.data.firmware.FirmwareType.SOFTWARE;
@ -217,30 +217,10 @@ public class DefaultFirmwareStateService implements FirmwareStateService {
if (device == null) {
log.warn("[{}] [{}] Device was removed during firmware update msg was queued!", tenantId, deviceId);
} else {
FirmwareId currentFirmwareId;
switch (firmwareType) {
case FIRMWARE:
currentFirmwareId = device.getFirmwareId();
break;
case SOFTWARE:
currentFirmwareId = device.getSoftwareId();
break;
default:
log.warn("Unsupported firmware type: [{}]", firmwareType);
return false;
}
FirmwareId currentFirmwareId = FirmwareUtil.getFirmwareId(device, firmwareType);
if (currentFirmwareId == null) {
DeviceProfile deviceProfile = deviceProfileService.findDeviceProfileById(tenantId, device.getDeviceProfileId());
switch (firmwareType) {
case FIRMWARE:
currentFirmwareId = deviceProfile.getFirmwareId();
break;
case SOFTWARE:
currentFirmwareId = deviceProfile.getSoftwareId();
break;
}
currentFirmwareId = FirmwareUtil.getFirmwareId(deviceProfile, firmwareType);
}
if (targetFirmwareId.equals(currentFirmwareId)) {
@ -333,13 +313,13 @@ public class DefaultFirmwareStateService implements FirmwareStateService {
}
private void remove(Device device, FirmwareType firmwareType) {
telemetryService.deleteAndNotify(device.getTenantId(), device.getId(), DataConstants.SHARED_SCOPE, FirmwareKeyUtil.getAttributeKeys(firmwareType),
telemetryService.deleteAndNotify(device.getTenantId(), device.getId(), DataConstants.SHARED_SCOPE, FirmwareUtil.getAttributeKeys(firmwareType),
new FutureCallback<>() {
@Override
public void onSuccess(@Nullable Void tmp) {
log.trace("[{}] Success remove target firmware attributes!", device.getId());
Set<AttributeKey> keysToNotify = new HashSet<>();
FirmwareKeyUtil.ALL_FW_ATTRIBUTE_KEYS.forEach(key -> keysToNotify.add(new AttributeKey(DataConstants.SHARED_SCOPE, key)));
FirmwareUtil.ALL_FW_ATTRIBUTE_KEYS.forEach(key -> keysToNotify.add(new AttributeKey(DataConstants.SHARED_SCOPE, key)));
tbClusterService.pushMsgToCore(DeviceAttributesEventNotificationMsg.onDelete(device.getTenantId(), device.getId(), keysToNotify), null);
}

View File

@ -41,6 +41,7 @@ import org.thingsboard.server.common.data.device.credentials.BasicMqttCredential
import org.thingsboard.server.common.data.device.credentials.ProvisionDeviceCredentialsData;
import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileCredentials;
import org.thingsboard.server.common.data.firmware.FirmwareType;
import org.thingsboard.server.common.data.firmware.FirmwareUtil;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.DeviceProfileId;
@ -463,26 +464,10 @@ public class DefaultTransportApiService implements TransportApiService {
return getEmptyTransportApiResponseFuture();
}
FirmwareId firmwareId = null;
switch (firmwareType) {
case FIRMWARE:
firmwareId = device.getFirmwareId();
break;
case SOFTWARE:
firmwareId = device.getSoftwareId();
break;
}
FirmwareId firmwareId = FirmwareUtil.getFirmwareId(device, firmwareType);
if (firmwareId == null) {
DeviceProfile deviceProfile = deviceProfileCache.find(device.getDeviceProfileId());
switch (firmwareType) {
case FIRMWARE:
firmwareId = deviceProfile.getFirmwareId();
break;
case SOFTWARE:
firmwareId = deviceProfile.getSoftwareId();
break;
}
firmwareId = FirmwareUtil.getFirmwareId(deviceProfile, firmwareType);
}
TransportProtos.GetFirmwareResponseMsg.Builder builder = TransportProtos.GetFirmwareResponseMsg.newBuilder();

View File

@ -32,7 +32,7 @@ import java.io.IOException;
@EqualsAndHashCode(callSuper = true)
@Slf4j
public class Device extends SearchTextBasedWithAdditionalInfo<DeviceId> implements HasName, HasTenantId, HasCustomerId {
public class Device extends SearchTextBasedWithAdditionalInfo<DeviceId> implements HasName, HasTenantId, HasCustomerId, HasFirmware {
private static final long serialVersionUID = 2807343040519543363L;

View File

@ -36,7 +36,7 @@ import static org.thingsboard.server.common.data.SearchTextBasedWithAdditionalIn
@Data
@EqualsAndHashCode(callSuper = true)
@Slf4j
public class DeviceProfile extends SearchTextBased<DeviceProfileId> implements HasName, HasTenantId {
public class DeviceProfile extends SearchTextBased<DeviceProfileId> implements HasName, HasTenantId, HasFirmware {
private TenantId tenantId;
@NoXss

View File

@ -0,0 +1,25 @@
/**
* Copyright © 2016-2021 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.common.data;
import org.thingsboard.server.common.data.id.FirmwareId;
public interface HasFirmware {
FirmwareId getFirmwareId();
FirmwareId getSoftwareId();
}

View File

@ -15,6 +15,10 @@
*/
package org.thingsboard.server.common.data.firmware;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.server.common.data.HasFirmware;
import org.thingsboard.server.common.data.id.FirmwareId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -22,7 +26,8 @@ import java.util.List;
import static org.thingsboard.server.common.data.firmware.FirmwareType.FIRMWARE;
import static org.thingsboard.server.common.data.firmware.FirmwareType.SOFTWARE;
public class FirmwareKeyUtil {
@Slf4j
public class FirmwareUtil {
public static final List<String> ALL_FW_ATTRIBUTE_KEYS;
@ -71,4 +76,16 @@ public class FirmwareKeyUtil {
public static String getTelemetryKey(FirmwareType type, FirmwareKey key) {
return type.getKeyPrefix() + "_" + key.getValue();
}
public static FirmwareId getFirmwareId(HasFirmware entity, FirmwareType firmwareType) {
switch (firmwareType) {
case FIRMWARE:
return entity.getFirmwareId();
case SOFTWARE:
return entity.getSoftwareId();
default:
log.warn("Unsupported firmware type: [{}]", firmwareType);
return null;
}
}
}