Skip to content

Commit

Permalink
introduce getIpV4Address() method
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 9, 2025
1 parent 613b6ce commit 19e5f4a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

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

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

import javax.jmdns.ServiceInfo;

Expand Down Expand Up @@ -49,9 +48,6 @@ public class GatewayDiscoveryParticipant implements MDNSDiscoveryParticipant {

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;
Expand All @@ -62,7 +58,7 @@ public GatewayDiscoveryParticipant(@Reference HDPowerviewPropertyGetter property
ThingUID thingUID = getThingUID(service);
if (thingUID != null) {
String serial = thingUID.getId();
String host = serialHostMap.get(serial);
String host = getIpV4Address(service);
if (host != null) {
String label = String.format("@text/%s [\"%s\"]", LABEL_KEY_GATEWAY, host);
DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
Expand All @@ -88,17 +84,20 @@ public Set<ThingTypeUID> getSupportedThingTypeUIDs() {

@Override
public @Nullable ThingUID getThingUID(ServiceInfo service) {
for (String host : service.getHostAddresses()) {
if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
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);
}
String host = getIpV4Address(service);
if (host != null) {
try {
String serial = propertyGetter.getSerialNumberApiV3(host);
return new ThingUID(THING_TYPE_GATEWAY, serial);
} catch (HubException e) {
logger.debug("Error discovering gateway", e);
}
}
return null;
}

private static @Nullable String getIpV4Address(ServiceInfo service) {
return Arrays.stream(service.getHostAddresses())
.filter(host -> VALID_IP_V4_ADDRESS.matcher(host).matches()).findFirst().orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

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

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

import javax.jmdns.ServiceInfo;

Expand Down Expand Up @@ -49,9 +48,6 @@ public class HDPowerViewHubMDNSDiscoveryParticipant implements MDNSDiscoveryPart

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 @@ -72,7 +68,7 @@ public String getServiceType() {
ThingUID thingUID = getThingUID(service);
if (thingUID != null) {
String serial = thingUID.getId();
String host = serialHostMap.get(serial);
String host = getIpV4Address(service);
if (host != null) {
String generation;
try {
Expand All @@ -94,17 +90,20 @@ public String getServiceType() {

@Override
public @Nullable ThingUID getThingUID(ServiceInfo service) {
for (String host : service.getHostAddresses()) {
if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
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);
}
String host = getIpV4Address(service);
if (host != null) {
try {
String serial = propertyGetter.getSerialNumberApiV1(host);
return new ThingUID(THING_TYPE_HUB, serial);
} catch (HubException e) {
logger.debug("Error discovering hub", e);
}
}
return null;
}

private static @Nullable String getIpV4Address(ServiceInfo service) {
return Arrays.stream(service.getHostAddresses())
.filter(host -> VALID_IP_V4_ADDRESS.matcher(host).matches()).findFirst().orElse(null);
}
}

0 comments on commit 19e5f4a

Please sign in to comment.