Skip to content

Commit

Permalink
thinguid uses serial number
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Fiddian-Green <[email protected]>
  • Loading branch information
andrewfg committed Feb 8, 2025
1 parent b3f6411 commit 613b6ce
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ public class HDPowerViewBindingConstants {
public static final String PROPERTY_NAME = "name";
public static final String PROPERTY_POWER_TYPE = "powerType";
public static final String PROPERTY_BLE_NAME = "bleName";

// keys for hub/gateway label translation
public static final String LABEL_KEY_HUB = "discovery.hub.label";
public static final String LABEL_KEY_GATEWAY = "discovery.gateway.label";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import static org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants.*;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.jmdns.ServiceInfo;

Expand Down Expand Up @@ -43,34 +45,32 @@
@Component
public class GatewayDiscoveryParticipant implements MDNSDiscoveryParticipant {

private static final String LABEL_KEY = "discovery.gateway.label";

private final Logger logger = LoggerFactory.getLogger(GatewayDiscoveryParticipant.class);

private final HDPowerviewPropertyGetter propertyGetter;

// map of serial numbers to host addresses
private final Map<String, String> serialHostMap = new ConcurrentHashMap<>();

@Activate
public GatewayDiscoveryParticipant(@Reference HDPowerviewPropertyGetter propertyGetter) {
this.propertyGetter = propertyGetter;
}

@Override
public @Nullable DiscoveryResult createResult(ServiceInfo service) {
for (String host : service.getHostAddresses()) {
if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
try {
String serial = propertyGetter.getSerialNumberApiV3(host);
ThingUID thingUID = new ThingUID(THING_TYPE_GATEWAY, host.replace('.', '_'));
String label = String.format("@text/%s [\"%s\"]", LABEL_KEY, host);
DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
.withProperty(HDPowerViewHubConfiguration.HOST, host)
.withProperty(Thing.PROPERTY_SERIAL_NUMBER, serial)
.withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).withLabel(label).build();
logger.debug("mDNS discovered Gen 3 gateway on host '{}'", host);
return hub;
} catch (HubException e) {
logger.debug("Error discovering hub", e);
}
ThingUID thingUID = getThingUID(service);
if (thingUID != null) {
String serial = thingUID.getId();
String host = serialHostMap.get(serial);
if (host != null) {
String label = String.format("@text/%s [\"%s\"]", LABEL_KEY_GATEWAY, host);
DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
.withProperty(HDPowerViewHubConfiguration.HOST, host)
.withProperty(Thing.PROPERTY_SERIAL_NUMBER, serial)
.withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).withLabel(label).build();
logger.debug("mDNS discovered Gen 3 gateway '{}' on host '{}'", thingUID, host);
return hub;
}
}
return null;
Expand All @@ -90,7 +90,13 @@ public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
public @Nullable ThingUID getThingUID(ServiceInfo service) {
for (String host : service.getHostAddresses()) {
if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
return new ThingUID(THING_TYPE_GATEWAY, host.replace('.', '_'));
try {
String serial = propertyGetter.getSerialNumberApiV3(host);
serialHostMap.put(serial, host);
return new ThingUID(THING_TYPE_GATEWAY, serial);
} catch (HubException e) {
logger.debug("Error discovering gateway", e);
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,13 @@ private Runnable createScanner() {
if (address != null) {
String host = address.getInetAddress().getHostAddress();
String serial = propertyGetter.getSerialNumberApiV1(host);
ThingUID thingUID = new ThingUID(THING_TYPE_HUB, host.replace('.', '_'));
String label = String.format("@text/%s [\"%s\", \"%s\"]",
HDPowerViewHubMDNSDiscoveryParticipant.LABEL_KEY_HUB, "1", host);
ThingUID thingUID = new ThingUID(THING_TYPE_HUB, serial);
String label = String.format("@text/%s [\"%s\", \"%s\"]", LABEL_KEY_HUB, "1", host);
DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
.withProperty(HDPowerViewHubConfiguration.HOST, host)
.withProperty(Thing.PROPERTY_SERIAL_NUMBER, serial)
.withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).withLabel(label).build();
logger.debug("NetBios discovered hub on host '{}'", host);
logger.debug("NetBios discovered Gen 1 hub '{}' on host '{}'", thingUID, host);
thingDiscovered(hub);
}
} catch (HubException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import static org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants.*;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.jmdns.ServiceInfo;

Expand Down Expand Up @@ -43,11 +45,13 @@
@Component
public class HDPowerViewHubMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {

public static final String LABEL_KEY_HUB = "discovery.hub.label";

private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubMDNSDiscoveryParticipant.class);

private final HDPowerviewPropertyGetter propertyGetter;

// map of serial numbers to host addresses
private final Map<String, String> serialHostMap = new ConcurrentHashMap<>();

@Activate
public HDPowerViewHubMDNSDiscoveryParticipant(@Reference HDPowerviewPropertyGetter propertyGetter) {
this.propertyGetter = propertyGetter;
Expand All @@ -65,27 +69,24 @@ public String getServiceType() {

@Override
public @Nullable DiscoveryResult createResult(ServiceInfo service) {
for (String host : service.getHostAddresses()) {
if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
ThingUID thingUID = getThingUID(service);
if (thingUID != null) {
String serial = thingUID.getId();
String host = serialHostMap.get(serial);
if (host != null) {
String generation;
try {
generation = propertyGetter.getGenerationApiV1(host);
} catch (HubException e) {
generation = "1/2";
}
try {
String serial = propertyGetter.getSerialNumberApiV1(host);
ThingUID thingUID = new ThingUID(THING_TYPE_HUB, host.replace('.', '_'));
String label = String.format("@text/%s [\"%s\", \"%s\"]", LABEL_KEY_HUB, generation, host);
DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
.withProperty(HDPowerViewHubConfiguration.HOST, host)
.withProperty(Thing.PROPERTY_SERIAL_NUMBER, serial)
.withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).withLabel(label).build();
logger.debug("mDNS discovered Gen {} hub on host '{}'", generation, host);
return hub;
} catch (HubException e) {
logger.debug("Error discovering hub", e);
}
String label = String.format("@text/%s [\"%s\", \"%s\"]", LABEL_KEY_HUB, generation, host);
DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
.withProperty(HDPowerViewHubConfiguration.HOST, host)
.withProperty(Thing.PROPERTY_SERIAL_NUMBER, serial)
.withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).withLabel(label).build();
logger.debug("mDNS discovered Gen {} hub '{}' on host '{}'", generation, thingUID, host);
return hub;
}
}
return null;
Expand All @@ -95,7 +96,13 @@ public String getServiceType() {
public @Nullable ThingUID getThingUID(ServiceInfo service) {
for (String host : service.getHostAddresses()) {
if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
return new ThingUID(THING_TYPE_HUB, host.replace('.', '_'));
try {
String serial = propertyGetter.getSerialNumberApiV1(host);
serialHostMap.put(serial, host);
return new ThingUID(THING_TYPE_HUB, serial);
} catch (HubException e) {
logger.debug("Error discovering hub", e);
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@
@Component
public class HDPowerViewSddpDiscoveryParticipant implements SddpDiscoveryParticipant {

private static final String LABEL_KEY_GATEWAY = "discovery.gateway.label";

private static final String HUNTER_DOUGLAS = "hunterdouglas:";
private static final String POWERVIEW_HUB_ID = "hub:powerview";
private static final String POWERVIEW_GEN3_ID = "powerview:gen3:gateway";

private final Logger logger = LoggerFactory.getLogger(HDPowerViewSddpDiscoveryParticipant.class);

private final HDPowerviewPropertyGetter propertyGetter;

@Activate
Expand All @@ -63,44 +62,38 @@ public Set<ThingTypeUID> getSupportedThingTypeUIDs() {

@Override
public @Nullable DiscoveryResult createResult(SddpDevice device) {
final ThingUID thingUID = getThingUID(device);
ThingUID thingUID = getThingUID(device);
if (thingUID != null) {
try {
String label;
String serial;
int generation = getGeneration(device);
if (generation < 3) {
label = String.format("@text/%s [\"%s\", \"%s\"]",
HDPowerViewHubMDNSDiscoveryParticipant.LABEL_KEY_HUB, generation, device.ipAddress);
serial = propertyGetter.getSerialNumberApiV1(device.ipAddress);
} else {
label = String.format("@text/%s [\"%s\"]", LABEL_KEY_GATEWAY, device.ipAddress);
serial = propertyGetter.getSerialNumberApiV3(device.ipAddress);
}
DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
.withProperty(HDPowerViewHubConfiguration.HOST, device.ipAddress)
.withProperty(Thing.PROPERTY_SERIAL_NUMBER, serial)
.withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).withLabel(label).build();
logger.debug("SDDP discovered Gen {} hub/gateway '{}' on host '{}'", generation, thingUID,
device.ipAddress);
return hub;
} catch (HubException | IllegalArgumentException e) {
logger.debug("Error discovering hub", e);
}
String serial = thingUID.getId();
String host = device.ipAddress;
int generation = getGeneration(device);
String label = generation < 3 //
? String.format("@text/%s [\"%s\", \"%s\"]", LABEL_KEY_HUB, generation, host)
: String.format("@text/%s [\"%s\"]", LABEL_KEY_GATEWAY, host);
DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
.withProperty(HDPowerViewHubConfiguration.HOST, host)
.withProperty(Thing.PROPERTY_SERIAL_NUMBER, serial)
.withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).withLabel(label).build();
logger.debug("SDDP discovered Gen {} hub/gateway '{}' on host '{}'", generation, thingUID, host);
return hub;
}
return null;
}

@Override
public @Nullable ThingUID getThingUID(SddpDevice device) {
if (device.type.startsWith(HUNTER_DOUGLAS)) {
try {
if (VALID_IP_V4_ADDRESS.matcher(device.ipAddress).matches()) {
return new ThingUID(getGeneration(device) == 3 ? THING_TYPE_GATEWAY : THING_TYPE_HUB,
device.ipAddress.replace('.', '_'));
String host = device.ipAddress;
if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
try {
int generation = getGeneration(device);
String serial = generation < 3 //
? propertyGetter.getSerialNumberApiV1(host)
: propertyGetter.getSerialNumberApiV3(host);
return new ThingUID(generation < 3 ? THING_TYPE_HUB : THING_TYPE_GATEWAY, serial);
} catch (HubException | IllegalArgumentException e) {
logger.debug("Error discovering hub/gateway", e);
}
} catch (IllegalArgumentException e) {
// error already logged, so fall through
}
}
return null;
Expand Down

0 comments on commit 613b6ce

Please sign in to comment.