Update firmware only when it is really changed

This commit is contained in:
Andrii Shvaika 2021-04-22 12:21:18 +03:00
parent 279e221523
commit 9b01f67d81
3 changed files with 29 additions and 24 deletions

View File

@ -126,16 +126,11 @@ public class DeviceController extends BaseController {
checkEntity(device.getId(), device, Resource.DEVICE); checkEntity(device.getId(), device, Resource.DEVICE);
boolean created = device.getId() == null; boolean created = device.getId() == null;
Device oldDevice;
boolean isFirmwareChanged = false; if (!created) {
oldDevice = deviceService.findDeviceById(getTenantId(), device.getId());
if (created) {
isFirmwareChanged = true;
} else { } else {
Device oldDevice = deviceService.findDeviceById(getTenantId(), device.getId()); oldDevice = null;
if (!Objects.equals(device.getFirmwareId(), oldDevice.getFirmwareId()) || !oldDevice.getDeviceProfileId().equals(device.getDeviceProfileId())) {
isFirmwareChanged = true;
}
} }
Device savedDevice = checkNotNull(deviceService.saveDeviceWithAccessToken(device, accessToken)); Device savedDevice = checkNotNull(deviceService.saveDeviceWithAccessToken(device, accessToken));
@ -145,7 +140,7 @@ public class DeviceController extends BaseController {
savedDevice.getId(), savedDevice.getName(), savedDevice.getType()), null); savedDevice.getId(), savedDevice.getName(), savedDevice.getType()), null);
tbClusterService.onEntityStateChange(savedDevice.getTenantId(), savedDevice.getId(), created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); tbClusterService.onEntityStateChange(savedDevice.getTenantId(), savedDevice.getId(), created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED);
if (device.getId() != null) { if (!created) {
sendEntityNotificationMsg(savedDevice.getTenantId(), savedDevice.getId(), EdgeEventActionType.UPDATED); sendEntityNotificationMsg(savedDevice.getTenantId(), savedDevice.getId(), EdgeEventActionType.UPDATED);
} }
@ -159,9 +154,7 @@ public class DeviceController extends BaseController {
deviceStateService.onDeviceUpdated(savedDevice); deviceStateService.onDeviceUpdated(savedDevice);
} }
if (isFirmwareChanged) { firmwareStateService.update(savedDevice, oldDevice);
firmwareStateService.update(savedDevice, created);
}
return savedDevice; return savedDevice;
} catch ( } catch (

View File

@ -43,6 +43,7 @@ import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.function.Consumer; import java.util.function.Consumer;
import static org.thingsboard.server.common.data.DataConstants.FIRMWARE_CHECKSUM; import static org.thingsboard.server.common.data.DataConstants.FIRMWARE_CHECKSUM;
@ -69,19 +70,30 @@ public class DefaultFirmwareStateService implements FirmwareStateService {
} }
@Override @Override
public void update(Device device, boolean created) { public void update(Device device, Device oldDevice) {
FirmwareId firmwareId = device.getFirmwareId(); FirmwareId newFirmwareId = device.getFirmwareId();
if (firmwareId == null) { if (newFirmwareId == null) {
DeviceProfile deviceProfile = deviceProfileService.findDeviceProfileById(device.getTenantId(), device.getDeviceProfileId()); DeviceProfile newDeviceProfile = deviceProfileService.findDeviceProfileById(device.getTenantId(), device.getDeviceProfileId());
firmwareId = deviceProfile.getFirmwareId(); newFirmwareId = newDeviceProfile.getFirmwareId();
} }
if (oldDevice != null) {
if (firmwareId == null) { if (newFirmwareId != null) {
if (!created) { FirmwareId oldFirmwareId = oldDevice.getFirmwareId();
remove(device); if (oldFirmwareId == null) {
DeviceProfile oldDeviceProfile = deviceProfileService.findDeviceProfileById(oldDevice.getTenantId(), oldDevice.getDeviceProfileId());
oldFirmwareId = oldDeviceProfile.getFirmwareId();
}
if (!newFirmwareId.equals(oldFirmwareId)) {
// Device was updated and new firmware is different from previous firmware.
update(device, firmwareService.findFirmwareById(device.getTenantId(), newFirmwareId), System.currentTimeMillis());
} }
} else { } else {
update(device, firmwareService.findFirmwareById(device.getTenantId(), firmwareId), System.currentTimeMillis()); // Device was updated and new firmware is not set.
remove(device);
}
} else if (newFirmwareId != null) {
// Device was created and firmware is defined.
update(device, firmwareService.findFirmwareById(device.getTenantId(), newFirmwareId), System.currentTimeMillis());
} }
} }

View File

@ -20,7 +20,7 @@ import org.thingsboard.server.common.data.DeviceProfile;
public interface FirmwareStateService { public interface FirmwareStateService {
void update(Device device, boolean created); void update(Device device, Device oldDevice);
void update(DeviceProfile deviceProfile); void update(DeviceProfile deviceProfile);