From 45b4451962a6bedbc1823434b85f37a0c4c3313c Mon Sep 17 00:00:00 2001 From: Mike Major Date: Mon, 12 Dec 2022 10:43:42 +0000 Subject: [PATCH 1/2] Fix for Alexa failing to discover all devices. Signed-off-by: Mike Major --- .../io/hueemulation/internal/ConfigStore.java | 17 ++++++---- .../internal/rest/CommonSetup.java | 2 +- .../rest/ItemUIDtoHueIDMappingTests.java | 32 +++++++++++++++++++ .../hueemulation/internal/upnp/UpnpTests.java | 2 +- 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/ConfigStore.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/ConfigStore.java index bfd6de49f2f6..b504e8a3faca 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/ConfigStore.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/ConfigStore.java @@ -271,15 +271,14 @@ private String getConfiguredHostAddress(InetAddress configuredAddress) { */ private String getHueIDPrefixFromUUID(final String uuid) { // Hue API example of a unique id is AA:BB:CC:DD:EE:FF:00:11-XX - // XX is generated from the item. + // 00:11-XX is generated from the item. String prefix = uuid; try { // Generate prefix if uuid is a randomly generated UUID if (UUID.fromString(uuid).version() == 4) { final StringBuilder sb = new StringBuilder(23); sb.append(uuid, 0, 2).append(":").append(uuid, 2, 4).append(":").append(uuid, 4, 6).append(":") - .append(uuid, 6, 8).append(":").append(uuid, 9, 11).append(":").append(uuid, 11, 13).append(":") - .append(uuid, 14, 16).append(":").append(uuid, 16, 18); + .append(uuid, 6, 8).append(":").append(uuid, 9, 11).append(":").append(uuid, 11, 13); prefix = sb.toString().toUpperCase(); } } catch (final IllegalArgumentException e) { @@ -352,14 +351,20 @@ public String mapItemUIDtoHueID(Item item) { * @return The unique id */ public String getHueUniqueId(final String hueId) { - String unique = hueId; + String unique; + try { - unique = String.format("%02X", Integer.valueOf(hueId)); + final String id = String.format("%06X", Integer.valueOf(hueId)); + final StringBuilder sb = new StringBuilder(26); + sb.append(hueIDPrefix).append(":").append(id, 0, 2).append(":").append(id, 2, 4).append("-").append(id, 4, + 6); + unique = sb.toString(); } catch (final NumberFormatException | IllegalFormatException e) { // Use the hueId as is + unique = hueIDPrefix + "-" + hueId; } - return hueIDPrefix + "-" + unique; + return unique; } public boolean isReady() { diff --git a/bundles/org.openhab.io.hueemulation/src/test/java/org/openhab/io/hueemulation/internal/rest/CommonSetup.java b/bundles/org.openhab.io.hueemulation/src/test/java/org/openhab/io/hueemulation/internal/rest/CommonSetup.java index 75ab4a9fb36c..0a039a5a3aaf 100644 --- a/bundles/org.openhab.io.hueemulation/src/test/java/org/openhab/io/hueemulation/internal/rest/CommonSetup.java +++ b/bundles/org.openhab.io.hueemulation/src/test/java/org/openhab/io/hueemulation/internal/rest/CommonSetup.java @@ -122,7 +122,7 @@ public CommonSetup(boolean withMetadata) throws IOException { } else { cs = new ConfigStoreWithoutMetadata(networkAddressService, configAdmin, scheduler); } - cs.activate(Collections.singletonMap("uuid", "demouuid")); + cs.activate(Collections.singletonMap("uuid", "a668dc9b-7172-49c3-832f-acb07dda2a20")); cs.switchFilter = Collections.singleton("Switchable"); cs.whiteFilter = Collections.singleton("Switchable"); cs.colorFilter = Collections.singleton("ColorLighting"); diff --git a/bundles/org.openhab.io.hueemulation/src/test/java/org/openhab/io/hueemulation/internal/rest/ItemUIDtoHueIDMappingTests.java b/bundles/org.openhab.io.hueemulation/src/test/java/org/openhab/io/hueemulation/internal/rest/ItemUIDtoHueIDMappingTests.java index baad4b1d0a4a..3e8e0210ef14 100644 --- a/bundles/org.openhab.io.hueemulation/src/test/java/org/openhab/io/hueemulation/internal/rest/ItemUIDtoHueIDMappingTests.java +++ b/bundles/org.openhab.io.hueemulation/src/test/java/org/openhab/io/hueemulation/internal/rest/ItemUIDtoHueIDMappingTests.java @@ -116,4 +116,36 @@ public void mapItemWithHueID() { assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(1)); } + + @Test + public void uniqueIdForLargeHueID() { + ConfigStore cs = commonSetup.cs; + assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(1)); + + SwitchItem item = new SwitchItem("switch1"); + item.setCategory("Light"); + commonSetup.metadataRegistry.add(new Metadata(new MetadataKey(ConfigStore.METAKEY, "switch1"), "255", null)); + itemRegistry.add(item); + + String hueID = cs.mapItemUIDtoHueID(item); + assertThat(hueID, CoreMatchers.is("255")); + + HueLightEntry device = cs.ds.lights.get(hueID); + assertThat(device.item, is(item)); + assertThat(device.state, is(instanceOf(HueStatePlug.class))); + assertThat(device.uniqueid, CoreMatchers.is("A6:68:DC:9B:71:72:00:00-FF")); + + item = new SwitchItem("switch2"); + item.setCategory("Light"); + commonSetup.metadataRegistry.add(new Metadata(new MetadataKey(ConfigStore.METAKEY, "switch2"), "256000", null)); + itemRegistry.add(item); + + hueID = cs.mapItemUIDtoHueID(item); + assertThat(hueID, CoreMatchers.is("256000")); + + device = cs.ds.lights.get(hueID); + assertThat(device.item, is(item)); + assertThat(device.state, is(instanceOf(HueStatePlug.class))); + assertThat(device.uniqueid, CoreMatchers.is("A6:68:DC:9B:71:72:03:E8-00")); + } } diff --git a/bundles/org.openhab.io.hueemulation/src/test/java/org/openhab/io/hueemulation/internal/upnp/UpnpTests.java b/bundles/org.openhab.io.hueemulation/src/test/java/org/openhab/io/hueemulation/internal/upnp/UpnpTests.java index e94aacbb6b08..1ee891a09256 100644 --- a/bundles/org.openhab.io.hueemulation/src/test/java/org/openhab/io/hueemulation/internal/upnp/UpnpTests.java +++ b/bundles/org.openhab.io.hueemulation/src/test/java/org/openhab/io/hueemulation/internal/upnp/UpnpTests.java @@ -136,7 +136,7 @@ public void descriptionWithAddress() sendSocket.receive(p); String received = new String(buffer); assertThat(received, CoreMatchers.startsWith("HTTP/1.1 200 OK")); - assertThat(received, CoreMatchers.containsString("hue-bridgeid: DEMOUUID")); + assertThat(received, CoreMatchers.containsString("hue-bridgeid: A668DC9B7172")); } r.dispose(); From 9fbb2f05c904f54d272c8b4e5f5790fb1ddb1bef Mon Sep 17 00:00:00 2001 From: Mike Major Date: Tue, 13 Dec 2022 10:19:02 +0000 Subject: [PATCH 2/2] Correct size of StringBuilder Signed-off-by: Mike Major --- .../java/org/openhab/io/hueemulation/internal/ConfigStore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/ConfigStore.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/ConfigStore.java index b504e8a3faca..72469d23f408 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/ConfigStore.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/ConfigStore.java @@ -276,7 +276,7 @@ private String getHueIDPrefixFromUUID(final String uuid) { try { // Generate prefix if uuid is a randomly generated UUID if (UUID.fromString(uuid).version() == 4) { - final StringBuilder sb = new StringBuilder(23); + final StringBuilder sb = new StringBuilder(17); sb.append(uuid, 0, 2).append(":").append(uuid, 2, 4).append(":").append(uuid, 4, 6).append(":") .append(uuid, 6, 8).append(":").append(uuid, 9, 11).append(":").append(uuid, 11, 13); prefix = sb.toString().toUpperCase();