Skip to content

Commit

Permalink
adopt reviewer suggestions
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 6, 2025
1 parent 60f0241 commit 2e9c3a5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,22 @@ private Runnable createScanner() {
try {
NbtAddress address = NbtAddress.getByName(netBiosName);
if (address != null) {
try {
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);
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);
thingDiscovered(hub);
} catch (HubException e) {
logger.debug("Error discovering hub", e);
}
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);
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);
thingDiscovered(hub);
}
} catch (HubException e) {
logger.debug("Error discovering hub", e);
} catch (UnknownHostException e) {
// Nothing to do here - the host couldn't be found, likely because it doesn't
// exist
// Nothing to do here - the host couldn't be found, likely because it doesn't exist
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,26 @@ public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
final ThingUID thingUID = getThingUID(device);
if (thingUID != null) {
try {
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 e) {
logger.debug("Error discovering hub", e);
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 e) {
logger.debug("Error discovering hub", e);
} catch (IllegalArgumentException e) {
// error already logged, so fall through
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package org.openhab.binding.hdpowerview.internal.discovery;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

Expand Down Expand Up @@ -80,7 +79,7 @@ public String getGenerationApiV1(String host) throws HubException {
}
throw new HubProcessingException("getGenerationApiV1(): HTTP error " + content.getReason());
} catch (JsonSyntaxException | InterruptedException | ExecutionException | TimeoutException e) {
throw new HubProcessingException("getGenerationApiV1(): " + e.getMessage());
throw new HubProcessingException("getSerialNumberApiV3(): " + e.getMessage(), e);
}
}

Expand All @@ -89,8 +88,7 @@ public String getGenerationApiV1(String host) throws HubException {
*
* @param host a dotted ipv4 address
* @return the serial number
* @throws IOException if anything failed
* @throws HubException
* @throws HubException if anything failed
*/
public String getSerialNumberApiV1(String host) throws HubException {
try {
Expand All @@ -107,7 +105,7 @@ public String getSerialNumberApiV1(String host) throws HubException {
}
throw new HubProcessingException("getSerialNumberApiV1(): " + content.getReason());
} catch (JsonSyntaxException | InterruptedException | ExecutionException | TimeoutException e) {
throw new HubProcessingException("getSerialNumberApiV1(): " + e.getMessage());
throw new HubProcessingException("getSerialNumberApiV3(): " + e.getMessage(), e);
}
}

Expand All @@ -133,7 +131,7 @@ public String getSerialNumberApiV3(String host) throws HubException {
}
throw new HubProcessingException("getSerialNumberApiV3(): " + content.getReason());
} catch (JsonSyntaxException | InterruptedException | ExecutionException | TimeoutException e) {
throw new HubProcessingException("getSerialNumberApiV3(): " + e.getMessage());
throw new HubProcessingException("getSerialNumberApiV3(): " + e.getMessage(), e);
}
}
}

0 comments on commit 2e9c3a5

Please sign in to comment.