import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
+import java.util.IllegalFormatException;
import java.util.LinkedHashSet;
import java.util.Map;
-import java.util.Random;
import java.util.Set;
+import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
private int highestAssignedHueID = 1;
+ private String hueIDPrefix = "";
+
public ConfigStore() {
scheduler = ThreadPoolManager.getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON);
}
determineHighestAssignedHueID();
if (config.uuid.isEmpty()) {
- config.uuid = getHueUUID();
+ config.uuid = UUID.randomUUID().toString();
writeUUIDFuture = scheduler.schedule(() -> {
logger.info("No unique ID assigned yet. Assigning {} and restarting...", config.uuid);
WriteConfig.setUUID(configAdmin, config.uuid);
}
}
- private static String getHueUUID() {
- // Hue API example is AA:BB:CC:DD:EE:FF:00:11-XX
- // XX is generated from the item.
- final Random r = new Random();
- int n = r.nextInt(255);
- final StringBuilder uuid = new StringBuilder(String.format("%02X", n));
- for (int i = 0; i < 7; i++) {
- n = r.nextInt(255);
- uuid.append(String.format(":%02X", n));
- }
- return uuid.toString();
- }
-
private @Nullable InetAddress byName(@Nullable String address) {
if (address == null) {
return null;
ds.config.bridgeid = ds.config.bridgeid.substring(0, 12);
}
+ hueIDPrefix = getHueIDPrefixFromUUID(config.uuid);
+
if (config.permanentV1bridge) {
ds.config.makeV1bridge();
}
}
}
+ /**
+ * Get the prefix used to create a unique id
+ *
+ * @param uuid The uuid
+ * @return The prefix in the format of AA:BB:CC:DD:EE:FF:00:11 if uuid is a valid UUID, otherwise uuid is returned.
+ */
+ 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.
+ 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);
+ prefix = sb.toString().toUpperCase();
+ }
+ } catch (final IllegalArgumentException e) {
+ // uuid is not a valid UUID
+ }
+
+ return prefix;
+ }
+
@Deactivate
public void deactive(int reason) {
ScheduledFuture<?> future = pairingOffFuture;
metadataRegistry.add(new Metadata(key, String.valueOf(hueId), null));
}
- return String.format("%02X", hueId);
+ return String.valueOf(hueId);
+ }
+
+ /**
+ * Get the unique id
+ *
+ * @param hueId The item hueID
+ * @return The unique id
+ */
+ public String getHueUniqueId(final String hueId) {
+ String unique = hueId;
+ try {
+ unique = String.format("%02X", Integer.valueOf(hueId));
+ } catch (final NumberFormatException | IllegalFormatException e) {
+ // Use the hueId as is
+ }
+
+ return hueIDPrefix + "-" + unique;
}
public boolean isReady() {