mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[hdpowerview] Improve Gen 1/2 discovery label (#16865)
* Improve Gen 1/2 discovery label Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
parent
20868ec5d8
commit
5e157262c5
@ -173,7 +173,7 @@ public class GatewayWebTargets implements Closeable, HostnameVerifier {
|
||||
Thing.PROPERTY_FIRMWARE_VERSION, result.getFwVersion(), //
|
||||
Thing.PROPERTY_SERIAL_NUMBER, result.getSerialNumber());
|
||||
} catch (JsonParseException e) {
|
||||
throw new HubProcessingException("getFirmwareVersions(): JsonParseException");
|
||||
throw new HubProcessingException("getInformation(): JsonParseException");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,13 +20,21 @@ import javax.jmdns.ServiceInfo;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.eclipse.jetty.client.HttpClient;
|
||||
import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets;
|
||||
import org.openhab.binding.hdpowerview.internal.config.HDPowerViewHubConfiguration;
|
||||
import org.openhab.binding.hdpowerview.internal.dto.Firmware;
|
||||
import org.openhab.binding.hdpowerview.internal.dto.HubFirmware;
|
||||
import org.openhab.binding.hdpowerview.internal.exceptions.HubException;
|
||||
import org.openhab.core.config.discovery.DiscoveryResult;
|
||||
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
|
||||
import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
|
||||
import org.openhab.core.io.net.http.HttpClientFactory;
|
||||
import org.openhab.core.thing.ThingTypeUID;
|
||||
import org.openhab.core.thing.ThingUID;
|
||||
import org.osgi.service.component.annotations.Activate;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.osgi.service.component.annotations.Reference;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -37,11 +45,17 @@ import org.slf4j.LoggerFactory;
|
||||
*/
|
||||
@NonNullByDefault
|
||||
@Component
|
||||
public class HDPowerViewHubDiscoveryParticipant implements MDNSDiscoveryParticipant {
|
||||
public class HDPowerViewHubMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
|
||||
|
||||
private static final String LABEL_KEY = "discovery.hub.label";
|
||||
public static final String LABEL_KEY_HUB = "discovery.hub.label";
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubDiscoveryParticipant.class);
|
||||
private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubMDNSDiscoveryParticipant.class);
|
||||
private final HttpClient httpClient;
|
||||
|
||||
@Activate
|
||||
public HDPowerViewHubMDNSDiscoveryParticipant(@Reference HttpClientFactory httpClientFactory) {
|
||||
httpClient = httpClientFactory.getCommonHttpClient();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
|
||||
@ -58,11 +72,12 @@ public class HDPowerViewHubDiscoveryParticipant implements MDNSDiscoveryParticip
|
||||
for (String host : service.getHostAddresses()) {
|
||||
if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
|
||||
ThingUID thingUID = new ThingUID(THING_TYPE_HUB, host.replace('.', '_'));
|
||||
String generation = this.getGeneration(host);
|
||||
DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
|
||||
.withProperty(HDPowerViewHubConfiguration.HOST, host)
|
||||
.withRepresentationProperty(HDPowerViewHubConfiguration.HOST)
|
||||
.withLabel(String.format("@text/%s [\"%s\"]", LABEL_KEY, host)).build();
|
||||
logger.debug("mDNS discovered Gen 1/2 hub on host '{}'", host);
|
||||
.withLabel(String.format("@text/%s [\"%s\", \"%s\"]", LABEL_KEY_HUB, generation, host)).build();
|
||||
logger.debug("mDNS discovered Gen {} hub on host '{}'", generation, host);
|
||||
return hub;
|
||||
}
|
||||
}
|
||||
@ -78,4 +93,18 @@ public class HDPowerViewHubDiscoveryParticipant implements MDNSDiscoveryParticip
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getGeneration(String host) {
|
||||
var webTargets = new HDPowerViewWebTargets(httpClient, host);
|
||||
try {
|
||||
HubFirmware firmware = webTargets.getFirmwareVersions();
|
||||
Firmware mainProcessor = firmware.mainProcessor;
|
||||
if (mainProcessor != null) {
|
||||
return String.valueOf(mainProcessor.revision);
|
||||
}
|
||||
} catch (HubException e) {
|
||||
logger.debug("Failed to discover hub firmware versions", e);
|
||||
}
|
||||
return "1/2";
|
||||
}
|
||||
}
|
@ -30,22 +30,21 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Discovers HD PowerView hubs by means of SDDP
|
||||
* Discovers HD PowerView hubs/gateways by means of SDDP
|
||||
*
|
||||
* @author Andrew Fiddian-Green - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
@Component
|
||||
public class HDPowerViewHubDiscoveryParticipantSddp implements SddpDiscoveryParticipant {
|
||||
public class HDPowerViewSddpDiscoveryParticipant implements SddpDiscoveryParticipant {
|
||||
|
||||
private static final String LABEL_KEY_HUB = "discovery.hub.label";
|
||||
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(HDPowerViewHubDiscoveryParticipantSddp.class);
|
||||
private final Logger logger = LoggerFactory.getLogger(HDPowerViewSddpDiscoveryParticipant.class);
|
||||
|
||||
@Override
|
||||
public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
|
||||
@ -57,13 +56,17 @@ public class HDPowerViewHubDiscoveryParticipantSddp implements SddpDiscoveryPart
|
||||
final ThingUID thingUID = getThingUID(device);
|
||||
if (thingUID != null) {
|
||||
try {
|
||||
int generation = getGeneration(device);
|
||||
String label = generation == 3 //
|
||||
? String.format("@text/%s [\"%s\"]", LABEL_KEY_GATEWAY, device.ipAddress)
|
||||
: String.format("@text/%s [\"%s\", \"%s\"]",
|
||||
HDPowerViewHubMDNSDiscoveryParticipant.LABEL_KEY_HUB, device.ipAddress, generation);
|
||||
|
||||
DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
|
||||
.withProperty(HDPowerViewHubConfiguration.HOST, device.ipAddress)
|
||||
.withRepresentationProperty(HDPowerViewHubConfiguration.HOST)
|
||||
.withLabel(String.format("@text/%s [\"%s\"]",
|
||||
isGateway(device) ? LABEL_KEY_GATEWAY : LABEL_KEY_HUB, device.ipAddress))
|
||||
.build();
|
||||
logger.debug("SDDP discovered hub/gateway '{}' on host '{}'", thingUID, device.ipAddress);
|
||||
.withRepresentationProperty(HDPowerViewHubConfiguration.HOST).withLabel(label).build();
|
||||
logger.debug("SDDP discovered Gen {} hub/gateway '{}' on host '{}'", generation, thingUID,
|
||||
device.ipAddress);
|
||||
return hub;
|
||||
} catch (IllegalArgumentException e) {
|
||||
// error already logged, so fall through
|
||||
@ -77,7 +80,7 @@ public class HDPowerViewHubDiscoveryParticipantSddp implements SddpDiscoveryPart
|
||||
if (device.type.startsWith(HUNTER_DOUGLAS)) {
|
||||
try {
|
||||
if (VALID_IP_V4_ADDRESS.matcher(device.ipAddress).matches()) {
|
||||
return new ThingUID(isGateway(device) ? THING_TYPE_GATEWAY : THING_TYPE_HUB,
|
||||
return new ThingUID(getGeneration(device) == 3 ? THING_TYPE_GATEWAY : THING_TYPE_HUB,
|
||||
device.ipAddress.replace('.', '_'));
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
@ -90,15 +93,15 @@ public class HDPowerViewHubDiscoveryParticipantSddp implements SddpDiscoveryPart
|
||||
/**
|
||||
* Check if the device 'type' property represents a Gen 3 gateway or a Gen 1/2 hub.
|
||||
*
|
||||
* @return true if a Gen 3 gateway or false if a Gen 1/2 hub.
|
||||
* @return 3 if a Gen 3 gateway, 2 if Gen 2 hub or 1 if Gen 1 hub.
|
||||
* @throws IllegalArgumentException if neither Gen 3, 2 or 1.
|
||||
*/
|
||||
private boolean isGateway(SddpDevice device) throws IllegalArgumentException {
|
||||
private int getGeneration(SddpDevice device) throws IllegalArgumentException {
|
||||
if (device.type.contains(POWERVIEW_GEN3_ID)) {
|
||||
return true;
|
||||
return 3;
|
||||
}
|
||||
if (device.type.contains(POWERVIEW_HUB_ID)) {
|
||||
return false;
|
||||
return device.type.endsWith("v2") ? 2 : 1;
|
||||
}
|
||||
final IllegalArgumentException e = new IllegalArgumentException("Device has unexpected 'type' property");
|
||||
logger.debug("{}", e.getMessage());
|
@ -86,7 +86,7 @@ offline.gone.shade-unknown-to-hub = Shade is unknown to Hub/Gateway
|
||||
|
||||
# discovery
|
||||
|
||||
discovery.hub.label = PowerView Gen 1/2 Hub ({0})
|
||||
discovery.hub.label = PowerView Gen {0} Hub ({1})
|
||||
discovery.gateway.label = PowerView Gen 3 Gateway ({0})
|
||||
|
||||
# dynamic channels
|
||||
|
Loading…
Reference in New Issue
Block a user