Adds a RepresentationProperty to discovery to avoid duplicate inobx entries (#11941)

Signed-off-by: Dan Cunningham <dan@digitaldan.com>
This commit is contained in:
Dan Cunningham 2022-01-03 00:22:45 -08:00 committed by GitHub
parent 2d32067533
commit 3884b378bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 9 deletions

View File

@ -22,7 +22,7 @@
<dependency>
<groupId>com.github.digitaldan</groupId>
<artifactId>harmony-client</artifactId>
<version>1.1.5</version>
<version>1.1.6</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -42,7 +42,7 @@ public class HarmonyHubBindingConstants {
public static final String DEVICE_PROPERTY_ID = "id";
public static final String DEVICE_PROPERTY_NAME = "name";
public static final String HUB_PROPERTY_ID = "id";
public static final String HUB_PROPERTY_ID = "uuid";
public static final String HUB_PROPERTY_HOST = "host";
public static final String HUB_PROPERTY_NAME = "name";
}

View File

@ -255,10 +255,12 @@ public class HarmonyHubDiscoveryService extends AbstractDiscoveryService {
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) {
@ -270,7 +272,7 @@ public class HarmonyHubDiscoveryService extends AbstractDiscoveryService {
}
}
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);
@ -279,6 +281,8 @@ public class HarmonyHubDiscoveryService extends AbstractDiscoveryService {
.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
}

View File

@ -19,6 +19,7 @@ import java.util.Collections;
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;
@ -59,6 +60,7 @@ import com.digitaldan.harmony.HarmonyClientListener;
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
@ -88,6 +90,7 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien
private final HarmonyClient client;
private @Nullable ScheduledFuture<?> retryJob;
private @Nullable ScheduledFuture<?> heartBeatJob;
private boolean propertiesUpdated;
private int heartBeatInterval;
@ -183,7 +186,7 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien
config = getConfigAs(HarmonyHubConfig.class);
cancelRetry();
updateStatus(ThingStatus.UNKNOWN);
retryJob = scheduler.schedule(this::connect, 0, TimeUnit.SECONDS);
scheduleRetry(0);
}
@Override
@ -221,12 +224,18 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien
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 -> {
@ -294,7 +303,7 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien
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);
}
@ -305,6 +314,11 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien
}
}
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());