String friendlyName = properties.get("friendlyName");
String hostName = properties.get("host_name");
String ip = properties.get("ip");
+ String uuid = properties.get("uuid");
if (friendlyName != null && !friendlyName.isBlank() && hostName != null && !hostName.isBlank()
- && ip != null && !ip.isBlank() && !responses.contains(hostName)) {
+ && ip != null && !ip.isBlank() && uuid != null && !uuid.isBlank()
+ && !responses.contains(hostName)) {
responses.add(hostName);
- hubDiscovered(ip, friendlyName, hostName);
+ hubDiscovered(ip, friendlyName, hostName, uuid);
}
}
} catch (IOException | IndexOutOfBoundsException e) {
}
}
- private void hubDiscovered(String ip, String friendlyName, String hostName) {
+ private void hubDiscovered(String ip, String friendlyName, String hostName, String uuid) {
String thingId = hostName.replaceAll("[^A-Za-z0-9\\-_]", "");
logger.trace("Adding HarmonyHub {} ({}) at host {}", friendlyName, thingId, ip);
ThingUID uid = new ThingUID(HARMONY_HUB_THING_TYPE, thingId);
.withLabel("HarmonyHub " + friendlyName)
.withProperty(HUB_PROPERTY_HOST, ip)
.withProperty(HUB_PROPERTY_NAME, friendlyName)
+ .withProperty(HUB_PROPERTY_ID, uuid)
+ .withRepresentationProperty(HUB_PROPERTY_ID)
.build());
// @formatter:on
}
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import com.digitaldan.harmony.config.Activity;
import com.digitaldan.harmony.config.Activity.Status;
import com.digitaldan.harmony.config.HarmonyConfig;
+import com.digitaldan.harmony.config.Ping;
/**
* The {@link HarmonyHubHandler} is responsible for handling commands for Harmony Hubs, which are
private final HarmonyClient client;
private @Nullable ScheduledFuture<?> retryJob;
private @Nullable ScheduledFuture<?> heartBeatJob;
+ private boolean propertiesUpdated;
private int heartBeatInterval;
config = getConfigAs(HarmonyHubConfig.class);
cancelRetry();
updateStatus(ThingStatus.UNKNOWN);
- retryJob = scheduler.schedule(this::connect, 0, TimeUnit.SECONDS);
+ scheduleRetry(0);
}
@Override
public void hubConnected() {
heartBeatJob = scheduler.scheduleWithFixedDelay(() -> {
try {
- client.sendPing();
+ Ping ping = client.sendPing().get();
+ if (!propertiesUpdated) {
+ Map<String, String> properties = editProperties();
+ properties.put(HUB_PROPERTY_ID, ping.getUuid());
+ updateProperties(properties);
+ propertiesUpdated = true;
+ }
} catch (Exception e) {
logger.debug("heartbeat failed", e);
setOfflineAndReconnect("Hearbeat failed");
}
- }, heartBeatInterval, heartBeatInterval, TimeUnit.SECONDS);
+ }, 5, heartBeatInterval, TimeUnit.SECONDS);
updateStatus(ThingStatus.ONLINE);
getConfigFuture().thenAcceptAsync(harmonyConfig -> updateCurrentActivityChannel(harmonyConfig), scheduler)
.exceptionally(e -> {
private void setOfflineAndReconnect(String error) {
disconnectFromHub();
- retryJob = scheduler.schedule(this::connect, RETRY_TIME, TimeUnit.SECONDS);
+ scheduleRetry(RETRY_TIME);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, error);
}
}
}
+ private synchronized void scheduleRetry(int retrySeconds) {
+ cancelRetry();
+ retryJob = scheduler.schedule(this::connect, retrySeconds, TimeUnit.SECONDS);
+ }
+
private void updateState(@Nullable Activity activity) {
if (activity != null) {
logger.debug("Updating current activity to {}", activity.getLabel());