Update firmware only when it is really changed
This commit is contained in:
parent
279e221523
commit
9b01f67d81
@ -126,16 +126,11 @@ public class DeviceController extends BaseController {
|
||||
checkEntity(device.getId(), device, Resource.DEVICE);
|
||||
|
||||
boolean created = device.getId() == null;
|
||||
|
||||
boolean isFirmwareChanged = false;
|
||||
|
||||
if (created) {
|
||||
isFirmwareChanged = true;
|
||||
Device oldDevice;
|
||||
if (!created) {
|
||||
oldDevice = deviceService.findDeviceById(getTenantId(), device.getId());
|
||||
} else {
|
||||
Device oldDevice = deviceService.findDeviceById(getTenantId(), device.getId());
|
||||
if (!Objects.equals(device.getFirmwareId(), oldDevice.getFirmwareId()) || !oldDevice.getDeviceProfileId().equals(device.getDeviceProfileId())) {
|
||||
isFirmwareChanged = true;
|
||||
}
|
||||
oldDevice = null;
|
||||
}
|
||||
|
||||
Device savedDevice = checkNotNull(deviceService.saveDeviceWithAccessToken(device, accessToken));
|
||||
@ -145,7 +140,7 @@ public class DeviceController extends BaseController {
|
||||
savedDevice.getId(), savedDevice.getName(), savedDevice.getType()), null);
|
||||
tbClusterService.onEntityStateChange(savedDevice.getTenantId(), savedDevice.getId(), created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED);
|
||||
|
||||
if (device.getId() != null) {
|
||||
if (!created) {
|
||||
sendEntityNotificationMsg(savedDevice.getTenantId(), savedDevice.getId(), EdgeEventActionType.UPDATED);
|
||||
}
|
||||
|
||||
@ -159,9 +154,7 @@ public class DeviceController extends BaseController {
|
||||
deviceStateService.onDeviceUpdated(savedDevice);
|
||||
}
|
||||
|
||||
if (isFirmwareChanged) {
|
||||
firmwareStateService.update(savedDevice, created);
|
||||
}
|
||||
firmwareStateService.update(savedDevice, oldDevice);
|
||||
|
||||
return savedDevice;
|
||||
} catch (
|
||||
|
||||
@ -43,6 +43,7 @@ import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.thingsboard.server.common.data.DataConstants.FIRMWARE_CHECKSUM;
|
||||
@ -69,19 +70,30 @@ public class DefaultFirmwareStateService implements FirmwareStateService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Device device, boolean created) {
|
||||
FirmwareId firmwareId = device.getFirmwareId();
|
||||
if (firmwareId == null) {
|
||||
DeviceProfile deviceProfile = deviceProfileService.findDeviceProfileById(device.getTenantId(), device.getDeviceProfileId());
|
||||
firmwareId = deviceProfile.getFirmwareId();
|
||||
public void update(Device device, Device oldDevice) {
|
||||
FirmwareId newFirmwareId = device.getFirmwareId();
|
||||
if (newFirmwareId == null) {
|
||||
DeviceProfile newDeviceProfile = deviceProfileService.findDeviceProfileById(device.getTenantId(), device.getDeviceProfileId());
|
||||
newFirmwareId = newDeviceProfile.getFirmwareId();
|
||||
}
|
||||
|
||||
if (firmwareId == null) {
|
||||
if (!created) {
|
||||
if (oldDevice != null) {
|
||||
if (newFirmwareId != null) {
|
||||
FirmwareId oldFirmwareId = oldDevice.getFirmwareId();
|
||||
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 {
|
||||
// Device was updated and new firmware is not set.
|
||||
remove(device);
|
||||
}
|
||||
} else {
|
||||
update(device, firmwareService.findFirmwareById(device.getTenantId(), firmwareId), System.currentTimeMillis());
|
||||
} else if (newFirmwareId != null) {
|
||||
// Device was created and firmware is defined.
|
||||
update(device, firmwareService.findFirmwareById(device.getTenantId(), newFirmwareId), System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -20,7 +20,7 @@ import org.thingsboard.server.common.data.DeviceProfile;
|
||||
|
||||
public interface FirmwareStateService {
|
||||
|
||||
void update(Device device, boolean created);
|
||||
void update(Device device, Device oldDevice);
|
||||
|
||||
void update(DeviceProfile deviceProfile);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user