Skip to content

Commit

Permalink
[hue] Refactor method to reduce nesting and code duplication(#15971)
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
  • Loading branch information
jlaur authored Nov 29, 2023
1 parent e33cccc commit b80273a
Showing 1 changed file with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b80273a

Please sign in to comment.