|
34 | 34 | import org.springframework.stereotype.Service;
|
35 | 35 |
|
36 | 36 | import java.io.IOException;
|
| 37 | +import java.util.Collection; |
37 | 38 | import java.util.List;
|
38 | 39 | import java.util.Map;
|
39 | 40 | import java.util.Objects;
|
40 | 41 | import java.util.Optional;
|
| 42 | +import java.util.function.Function; |
| 43 | +import java.util.stream.Collectors; |
| 44 | +import java.util.stream.Stream; |
41 | 45 |
|
42 | 46 |
|
43 | 47 | @Slf4j
|
@@ -182,8 +186,22 @@ public Device updateLocalDevice(String identifier, String deviceId, ThingSpec th
|
182 | 186 | addAdditionalEntities(integrationId, deviceKey, entities);
|
183 | 187 |
|
184 | 188 | val device = deviceServiceProvider.findByIdentifier(identifier, integrationId);
|
| 189 | + |
| 190 | + // keep entity name |
| 191 | + val keyToUpdatedEntity = entities.stream().collect(Collectors.toMap(Entity::getKey, Function.identity(), (a, b) -> a)); |
| 192 | + device.getEntities().stream() |
| 193 | + .flatMap(existingEntity -> Optional.ofNullable(existingEntity.getChildren()) |
| 194 | + .map(Collection::stream) |
| 195 | + .map(childrenStream -> Stream.concat(Stream.of(existingEntity), childrenStream)) |
| 196 | + .orElseGet(() -> Stream.of(existingEntity))) |
| 197 | + .forEach(existingEntity -> { |
| 198 | + val updatedEntity = keyToUpdatedEntity.get(existingEntity.getKey()); |
| 199 | + if (updatedEntity != null) { |
| 200 | + updatedEntity.setName(existingEntity.getName()); |
| 201 | + } |
| 202 | + }); |
| 203 | + |
185 | 204 | // update device attributes except name
|
186 |
| -// device.setIdentifier(identifier); |
187 | 205 | device.setAdditional(Map.of(MscIntegrationConstants.DeviceAdditionalDataName.DEVICE_ID, deviceId));
|
188 | 206 | device.setEntities(entities);
|
189 | 207 | deviceServiceProvider.save(device);
|
|
0 commit comments