Skip to content

Commit

Permalink
Fix multiple state updates
Browse files Browse the repository at this point in the history
Fixes openhab#15700

Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed Nov 16, 2023
1 parent 12f0212 commit 539157d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,7 @@ private void onResourcesEventTask(List<Resource> resources) {
getThing().getThings().forEach(thing -> {
ThingHandler handler = thing.getHandler();
if (handler instanceof Clip2ThingHandler) {
resources.forEach(resource -> {
((Clip2ThingHandler) handler).onResource(resource);
});
((Clip2ThingHandler) handler).onResources(resources);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,48 +630,77 @@ public void initialize() {
}

/**
* Update the channel state depending on a new resource sent from the bridge.
* Update the channel states depending on a new resources sent from the bridge.
*
* @param resource a Resource object containing the new state.
* @param resources a Collection of Resource objects containing the new states.
*/
public void onResource(Resource resource) {
if (!disposing) {
boolean resourceConsumed = false;
public void onResources(Collection<Resource> resources) {
if (disposing) {
return;
}

Resource mergedResource = null;
for (Resource resource : resources) {
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);
}
if (resourceId.equals(incomingResourceId) || SUPPORTED_SCENE_TYPES.contains(resource.getType())) {
onResource(resource);
continue;
}
if (mergedResource == null) {
mergedResource = 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);
}
Setters.setResource(mergedResource, resource);
}
}
if (mergedResource != null) {
onResource(mergedResource);
}
}

/**
* Update the channel state depending on a new resource sent from the bridge.
*
* @param resource a Resource object containing the new state.
*/
private void onResource(Resource resource) {
if (disposing) {
return;
}
boolean resourceConsumed = false;
String incomingResourceId = resource.getId();
if (resourceId.equals(incomingResourceId)) {
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 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 (resourceConsumed) {
logger.debug("{} -> onResource() consumed resource {}", resourceId, resource);
}
}

Expand Down

0 comments on commit 539157d

Please sign in to comment.