diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/Clip2ThingHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/Clip2ThingHandler.java index aef0ff5bf27ae..292416ec94f90 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/Clip2ThingHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/Clip2ThingHandler.java @@ -635,46 +635,53 @@ public void initialize() { * @param resource a Resource object containing the new state. */ public void onResource(Resource resource) { - if (!disposing) { - boolean resourceConsumed = false; - String incomingResourceId = resource.getId(); - if (resourceId.equals(incomingResourceId)) { - if (resource.hasFullState()) { - thisResource = resource; - if (!updatePropertiesDone) { - updateProperties(resource); - resourceConsumed = updatePropertiesDone; - } - } - if (!updateDependenciesDone) { - resourceConsumed = true; - cancelTask(updateDependenciesTask, false); - updateDependenciesTask = scheduler.submit(() -> updateDependencies()); - } - } else if (SUPPORTED_SCENE_TYPES.contains(resource.getType())) { - Resource cachedScene = sceneContributorsCache.get(incomingResourceId); - if (Objects.nonNull(cachedScene)) { - Setters.setResource(resource, cachedScene); - resourceConsumed = updateChannels(resource); - sceneContributorsCache.put(incomingResourceId, resource); - } - } else { - Resource cachedService = serviceContributorsCache.get(incomingResourceId); - if (Objects.nonNull(cachedService)) { - Setters.setResource(resource, cachedService); - resourceConsumed = updateChannels(resource); - serviceContributorsCache.put(incomingResourceId, resource); - if (ResourceType.LIGHT == resource.getType() && !updateLightPropertiesDone) { - updateLightProperties(resource); - } + if (disposing) { + return; + } + boolean resourceConsumed = false; + if (resourceId.equals(resource.getId())) { + if (resource.hasFullState()) { + thisResource = resource; + if (!updatePropertiesDone) { + updateProperties(resource); + resourceConsumed = updatePropertiesDone; } } - if (resourceConsumed) { - logger.debug("{} -> onResource() consumed resource {}", resourceId, resource); + if (!updateDependenciesDone) { + resourceConsumed = true; + cancelTask(updateDependenciesTask, false); + updateDependenciesTask = scheduler.submit(() -> updateDependencies()); } + } else { + Resource cachedResource = getResourceFromCache(resource); + if (cachedResource != null) { + Setters.setResource(resource, cachedResource); + resourceConsumed = updateChannels(resource); + putResourceToCache(resource); + if (ResourceType.LIGHT == resource.getType() && !updateLightPropertiesDone) { + updateLightProperties(resource); + } + } + } + if (resourceConsumed) { + logger.debug("{} -> onResource() consumed resource {}", resourceId, resource); + } + } + + private void putResourceToCache(Resource resource) { + if (SUPPORTED_SCENE_TYPES.contains(resource.getType())) { + sceneContributorsCache.put(resource.getId(), resource); + } else { + serviceContributorsCache.put(resource.getId(), resource); } } + private @Nullable Resource getResourceFromCache(Resource resource) { + return SUPPORTED_SCENE_TYPES.contains(resource.getType()) // + ? sceneContributorsCache.get(resource.getId()) + : serviceContributorsCache.get(resource.getId()); + } + /** * Update the thing internal state depending on a full list of resources sent from the bridge. If the resourceType * is SCENE then call updateScenes(), otherwise if the resource refers to this thing, consume it via onResource() as