Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.math.BigDecimal;
import java.time.Duration;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -60,6 +61,16 @@ public class Setters {

private static final Set<ResourceType> LIGHT_TYPES = Set.of(ResourceType.LIGHT, ResourceType.GROUPED_LIGHT);

/*
* Comparator to sort Resources so that scene activation resources come last.
*/
private static final Comparator<Resource> SCENE_ACTIVE_COMPARATOR = new Comparator<>() {
@Override
public int compare(Resource r1, Resource r2) {
return (r1.getSceneActive().orElse(false) ? 1 : 0) + (r2.getSceneActive().orElse(false) ? -1 : 0);
}
};

/**
* Setter for Alert field:
* Use the given command value to set the target resource DTO value based on the attributes of the source resource
Expand Down Expand Up @@ -398,7 +409,21 @@ public static Collection<Resource> mergeLightResources(Collection<Resource> reso
iterator.remove();
}
}
return resources;
}

/**
* Sort a list of resources so that scene activation event resources (if any) get
* processed after scene de-activation event resources (if any).
*
* @param resources list of resources to be sorted.
* @return the sorted list with scene activation event resources moved to the end.
*/
public static List<Resource> sortSceneResources(List<Resource> resources) {
if (resources.stream().filter(r -> r.getSceneActive().isPresent()).count() < 2) {
return resources;
}
resources.sort(SCENE_ACTIVE_COMPARATOR);
return resources;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ private void onResourcesEventTask(List<Resource> resources) {
if (numberOfResources != resources.size()) {
logger.debug("onResourcesEventTask() merged to {} resources", resources.size());
}
Setters.sortSceneResources(resources);
getThing().getThings().forEach(thing -> {
if (thing.getHandler() instanceof Clip2ThingHandler clip2ThingHandler) {
resources.forEach(resource -> clip2ThingHandler.onResource(resource));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.hue.internal.clip2;

import static org.junit.jupiter.api.Assertions.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.Instant;
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.hue.internal.api.dto.clip2.Resource;
import org.openhab.binding.hue.internal.api.dto.clip2.Resources;
import org.openhab.binding.hue.internal.api.dto.clip2.helper.Setters;
import org.openhab.binding.hue.internal.api.serialization.InstantDeserializer;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
* JUnit test for sorting the sequence of scene events in a list.
*
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
class SortSceneResourcesTest {

private static final Gson GSON = new GsonBuilder().registerTypeAdapter(Instant.class, new InstantDeserializer())
.create();

private static final String ACTIVE_SCENE_RESOURCE_ID = "ACTIVE00-0000-0000-0000-000000000001";

private String loadJson(String fileName) {
try (FileReader file = new FileReader(String.format("src/test/resources/%s.json", fileName));
BufferedReader reader = new BufferedReader(file)) {
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
return builder.toString();
} catch (IOException e) {
fail(e.getMessage());
}
return "";
}

@Test
void testSceneSorting() {
Resources resources = GSON.fromJson(loadJson("scene_event"), Resources.class);
assertNotNull(resources);
List<Resource> list = resources.getResources();
assertNotNull(list);
list.forEach(r -> r.markAsSparse());

assertEquals(5, list.size());
assertEquals(ACTIVE_SCENE_RESOURCE_ID, list.get(1).getId());

Setters.sortSceneResources(list);

assertEquals(5, list.size());
assertEquals(ACTIVE_SCENE_RESOURCE_ID, list.get(4).getId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"errors": [
],
"data": [
{
"id": "00000000-0000-0000-0000-000000000000",
"id_v1": "/lights/40",
"on": {
"on": false
},
"type": "light"
},
{
"id": "ACTIVE00-0000-0000-0000-000000000001",
"id_v1": "/scenes/BK-UE-77VNFHKRi",
"status": {
"active": "static"
},
"type": "scene"
},
{
"id": "00000000-0000-0000-0000-000000000002",
"id_v1": "/lights/40",
"on": {
"on": false
},
"type": "light"
},
{
"id": "00000000-0000-0000-0000-000000000003",
"id_v1": "/scenes/rULmu6mjOgC4U7I",
"status": {
"active": "inactive"
},
"type": "scene"
},
{
"id": "00000000-0000-0000-0000-000000000004",
"id_v1": "/lights/40",
"on": {
"on": false
},
"type": "light"
}
]
}