Commit 9b01f67d8148e9602d6f3c62297484475764809b
1 parent
279e2215
Update firmware only when it is really changed
Showing
3 changed files
with
29 additions
and
24 deletions
@@ -126,16 +126,11 @@ public class DeviceController extends BaseController { | @@ -126,16 +126,11 @@ public class DeviceController extends BaseController { | ||
126 | checkEntity(device.getId(), device, Resource.DEVICE); | 126 | checkEntity(device.getId(), device, Resource.DEVICE); |
127 | 127 | ||
128 | boolean created = device.getId() == null; | 128 | boolean created = device.getId() == null; |
129 | - | ||
130 | - boolean isFirmwareChanged = false; | ||
131 | - | ||
132 | - if (created) { | ||
133 | - isFirmwareChanged = true; | 129 | + Device oldDevice; |
130 | + if (!created) { | ||
131 | + oldDevice = deviceService.findDeviceById(getTenantId(), device.getId()); | ||
134 | } else { | 132 | } else { |
135 | - Device oldDevice = deviceService.findDeviceById(getTenantId(), device.getId()); | ||
136 | - if (!Objects.equals(device.getFirmwareId(), oldDevice.getFirmwareId()) || !oldDevice.getDeviceProfileId().equals(device.getDeviceProfileId())) { | ||
137 | - isFirmwareChanged = true; | ||
138 | - } | 133 | + oldDevice = null; |
139 | } | 134 | } |
140 | 135 | ||
141 | Device savedDevice = checkNotNull(deviceService.saveDeviceWithAccessToken(device, accessToken)); | 136 | Device savedDevice = checkNotNull(deviceService.saveDeviceWithAccessToken(device, accessToken)); |
@@ -145,7 +140,7 @@ public class DeviceController extends BaseController { | @@ -145,7 +140,7 @@ public class DeviceController extends BaseController { | ||
145 | savedDevice.getId(), savedDevice.getName(), savedDevice.getType()), null); | 140 | savedDevice.getId(), savedDevice.getName(), savedDevice.getType()), null); |
146 | tbClusterService.onEntityStateChange(savedDevice.getTenantId(), savedDevice.getId(), created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); | 141 | tbClusterService.onEntityStateChange(savedDevice.getTenantId(), savedDevice.getId(), created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); |
147 | 142 | ||
148 | - if (device.getId() != null) { | 143 | + if (!created) { |
149 | sendEntityNotificationMsg(savedDevice.getTenantId(), savedDevice.getId(), EdgeEventActionType.UPDATED); | 144 | sendEntityNotificationMsg(savedDevice.getTenantId(), savedDevice.getId(), EdgeEventActionType.UPDATED); |
150 | } | 145 | } |
151 | 146 | ||
@@ -159,9 +154,7 @@ public class DeviceController extends BaseController { | @@ -159,9 +154,7 @@ public class DeviceController extends BaseController { | ||
159 | deviceStateService.onDeviceUpdated(savedDevice); | 154 | deviceStateService.onDeviceUpdated(savedDevice); |
160 | } | 155 | } |
161 | 156 | ||
162 | - if (isFirmwareChanged) { | ||
163 | - firmwareStateService.update(savedDevice, created); | ||
164 | - } | 157 | + firmwareStateService.update(savedDevice, oldDevice); |
165 | 158 | ||
166 | return savedDevice; | 159 | return savedDevice; |
167 | } catch ( | 160 | } catch ( |
@@ -43,6 +43,7 @@ import javax.annotation.Nullable; | @@ -43,6 +43,7 @@ import javax.annotation.Nullable; | ||
43 | import java.util.ArrayList; | 43 | import java.util.ArrayList; |
44 | import java.util.Arrays; | 44 | import java.util.Arrays; |
45 | import java.util.List; | 45 | import java.util.List; |
46 | +import java.util.Objects; | ||
46 | import java.util.function.Consumer; | 47 | import java.util.function.Consumer; |
47 | 48 | ||
48 | import static org.thingsboard.server.common.data.DataConstants.FIRMWARE_CHECKSUM; | 49 | import static org.thingsboard.server.common.data.DataConstants.FIRMWARE_CHECKSUM; |
@@ -69,19 +70,30 @@ public class DefaultFirmwareStateService implements FirmwareStateService { | @@ -69,19 +70,30 @@ public class DefaultFirmwareStateService implements FirmwareStateService { | ||
69 | } | 70 | } |
70 | 71 | ||
71 | @Override | 72 | @Override |
72 | - public void update(Device device, boolean created) { | ||
73 | - FirmwareId firmwareId = device.getFirmwareId(); | ||
74 | - if (firmwareId == null) { | ||
75 | - DeviceProfile deviceProfile = deviceProfileService.findDeviceProfileById(device.getTenantId(), device.getDeviceProfileId()); | ||
76 | - firmwareId = deviceProfile.getFirmwareId(); | 73 | + public void update(Device device, Device oldDevice) { |
74 | + FirmwareId newFirmwareId = device.getFirmwareId(); | ||
75 | + if (newFirmwareId == null) { | ||
76 | + DeviceProfile newDeviceProfile = deviceProfileService.findDeviceProfileById(device.getTenantId(), device.getDeviceProfileId()); | ||
77 | + newFirmwareId = newDeviceProfile.getFirmwareId(); | ||
77 | } | 78 | } |
78 | - | ||
79 | - if (firmwareId == null) { | ||
80 | - if (!created) { | 79 | + if (oldDevice != null) { |
80 | + if (newFirmwareId != null) { | ||
81 | + FirmwareId oldFirmwareId = oldDevice.getFirmwareId(); | ||
82 | + if (oldFirmwareId == null) { | ||
83 | + DeviceProfile oldDeviceProfile = deviceProfileService.findDeviceProfileById(oldDevice.getTenantId(), oldDevice.getDeviceProfileId()); | ||
84 | + oldFirmwareId = oldDeviceProfile.getFirmwareId(); | ||
85 | + } | ||
86 | + if (!newFirmwareId.equals(oldFirmwareId)) { | ||
87 | + // Device was updated and new firmware is different from previous firmware. | ||
88 | + update(device, firmwareService.findFirmwareById(device.getTenantId(), newFirmwareId), System.currentTimeMillis()); | ||
89 | + } | ||
90 | + } else { | ||
91 | + // Device was updated and new firmware is not set. | ||
81 | remove(device); | 92 | remove(device); |
82 | } | 93 | } |
83 | - } else { | ||
84 | - update(device, firmwareService.findFirmwareById(device.getTenantId(), firmwareId), System.currentTimeMillis()); | 94 | + } else if (newFirmwareId != null) { |
95 | + // Device was created and firmware is defined. | ||
96 | + update(device, firmwareService.findFirmwareById(device.getTenantId(), newFirmwareId), System.currentTimeMillis()); | ||
85 | } | 97 | } |
86 | } | 98 | } |
87 | 99 |
@@ -20,7 +20,7 @@ import org.thingsboard.server.common.data.DeviceProfile; | @@ -20,7 +20,7 @@ import org.thingsboard.server.common.data.DeviceProfile; | ||
20 | 20 | ||
21 | public interface FirmwareStateService { | 21 | public interface FirmwareStateService { |
22 | 22 | ||
23 | - void update(Device device, boolean created); | 23 | + void update(Device device, Device oldDevice); |
24 | 24 | ||
25 | void update(DeviceProfile deviceProfile); | 25 | void update(DeviceProfile deviceProfile); |
26 | 26 |