[hdpowerview] Refactor null-handling, maintenance period, response logging (#12950)

* Treat HDPowerViewWebTargets as non-null since initialized by initialize()
* Simplify maintenance period logic slightly
* Improve response logging

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2022-06-19 14:12:51 +02:00 committed by GitHub
parent efb1bfc772
commit b5489057b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 60 deletions

View File

@ -12,6 +12,7 @@
*/ */
package org.openhab.binding.hdpowerview.internal; package org.openhab.binding.hdpowerview.internal;
import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -81,8 +82,8 @@ public class HDPowerViewWebTargets {
* exception handling during the five minute maintenance period after a 423 * exception handling during the five minute maintenance period after a 423
* error is received * error is received
*/ */
private final int maintenancePeriod = 300; private final Duration maintenancePeriod = Duration.ofMinutes(5);
private Instant maintenanceScheduledEnd = Instant.now().minusSeconds(2 * maintenancePeriod); private Instant maintenanceScheduledEnd = Instant.MIN;
private final String base; private final String base;
private final String firmwareVersion; private final String firmwareVersion;
@ -576,7 +577,7 @@ public class HDPowerViewWebTargets {
int statusCode = response.getStatus(); int statusCode = response.getStatus();
if (statusCode == HttpStatus.LOCKED_423) { if (statusCode == HttpStatus.LOCKED_423) {
// set end of maintenance window, and throw a "softer" exception // set end of maintenance window, and throw a "softer" exception
maintenanceScheduledEnd = Instant.now().plusSeconds(maintenancePeriod); maintenanceScheduledEnd = Instant.now().plus(maintenancePeriod);
logger.debug("Hub undergoing maintenance"); logger.debug("Hub undergoing maintenance");
throw new HubMaintenanceException("Hub undergoing maintenance"); throw new HubMaintenanceException("Hub undergoing maintenance");
} }
@ -585,13 +586,13 @@ public class HDPowerViewWebTargets {
throw new HubProcessingException(String.format("HTTP %d error", statusCode)); throw new HubProcessingException(String.format("HTTP %d error", statusCode));
} }
String jsonResponse = response.getContentAsString(); String jsonResponse = response.getContentAsString();
if ("".equals(jsonResponse)) {
logger.warn("Hub returned no content");
throw new HubProcessingException("Missing response entity");
}
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("JSON response = {}", jsonResponse); logger.trace("JSON response = {}", jsonResponse);
} }
if (jsonResponse == null || jsonResponse.isEmpty()) {
logger.warn("Hub returned no content");
throw new HubProcessingException("Missing response entity");
}
return jsonResponse; return jsonResponse;
} }

View File

@ -40,9 +40,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* Discovers an HD PowerView Shade from an existing hub * Discovers HD PowerView Shades and Repeaters from an existing hub
* *
* @author Andy Lintner - Initial contribution * @author Andy Lintner - Initial contribution
* @author Jacob Laursen - Add Repeater discovery
*/ */
@NonNullByDefault @NonNullByDefault
public class HDPowerViewDeviceDiscoveryService extends AbstractDiscoveryService { public class HDPowerViewDeviceDiscoveryService extends AbstractDiscoveryService {
@ -87,9 +88,6 @@ public class HDPowerViewDeviceDiscoveryService extends AbstractDiscoveryService
return () -> { return () -> {
try { try {
HDPowerViewWebTargets webTargets = hub.getWebTargets(); HDPowerViewWebTargets webTargets = hub.getWebTargets();
if (webTargets == null) {
throw new HubProcessingException("Web targets not initialized");
}
discoverShades(webTargets); discoverShades(webTargets);
discoverRepeaters(webTargets); discoverRepeaters(webTargets);
} catch (HubMaintenanceException e) { } catch (HubMaintenanceException e) {

View File

@ -22,8 +22,6 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.ws.rs.ProcessingException;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpClient;
@ -89,7 +87,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
private long hardRefreshPositionInterval; private long hardRefreshPositionInterval;
private long hardRefreshBatteryLevelInterval; private long hardRefreshBatteryLevelInterval;
private @Nullable HDPowerViewWebTargets webTargets; private @NonNullByDefault({}) HDPowerViewWebTargets webTargets;
private @Nullable ScheduledFuture<?> pollFuture; private @Nullable ScheduledFuture<?> pollFuture;
private @Nullable ScheduledFuture<?> hardRefreshPositionFuture; private @Nullable ScheduledFuture<?> hardRefreshPositionFuture;
private @Nullable ScheduledFuture<?> hardRefreshBatteryLevelFuture; private @Nullable ScheduledFuture<?> hardRefreshBatteryLevelFuture;
@ -129,10 +127,6 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
} }
try { try {
HDPowerViewWebTargets webTargets = this.webTargets;
if (webTargets == null) {
throw new ProcessingException("Web targets not initialized");
}
int id = Integer.parseInt(channelUID.getIdWithoutGroup()); int id = Integer.parseInt(channelUID.getIdWithoutGroup());
if (sceneChannelTypeUID.equals(channel.getChannelTypeUID()) && OnOffType.ON == command) { if (sceneChannelTypeUID.equals(channel.getChannelTypeUID()) && OnOffType.ON == command) {
webTargets.activateScene(id); webTargets.activateScene(id);
@ -164,7 +158,6 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
return; return;
} }
updateStatus(ThingStatus.UNKNOWN);
pendingShadeInitializations.clear(); pendingShadeInitializations.clear();
webTargets = new HDPowerViewWebTargets(httpClient, host); webTargets = new HDPowerViewWebTargets(httpClient, host);
refreshInterval = config.refresh; refreshInterval = config.refresh;
@ -172,6 +165,8 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
hardRefreshBatteryLevelInterval = config.hardRefreshBatteryLevel; hardRefreshBatteryLevelInterval = config.hardRefreshBatteryLevel;
initializeChannels(); initializeChannels();
firmwareVersions = null; firmwareVersions = null;
updateStatus(ThingStatus.UNKNOWN);
schedulePoll(); schedulePoll();
} }
@ -184,7 +179,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
deprecatedChannelsCreated = false; deprecatedChannelsCreated = false;
} }
public @Nullable HDPowerViewWebTargets getWebTargets() { public HDPowerViewWebTargets getWebTargets() {
return webTargets; return webTargets;
} }
@ -320,10 +315,6 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
if (firmwareVersions != null) { if (firmwareVersions != null) {
return; return;
} }
HDPowerViewWebTargets webTargets = this.webTargets;
if (webTargets == null) {
throw new ProcessingException("Web targets not initialized");
}
FirmwareVersions firmwareVersions = webTargets.getFirmwareVersions(); FirmwareVersions firmwareVersions = webTargets.getFirmwareVersions();
Firmware mainProcessor = firmwareVersions.mainProcessor; Firmware mainProcessor = firmwareVersions.mainProcessor;
if (mainProcessor == null) { if (mainProcessor == null) {
@ -346,11 +337,6 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
} }
private void pollShades() throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException { private void pollShades() throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
HDPowerViewWebTargets webTargets = this.webTargets;
if (webTargets == null) {
throw new ProcessingException("Web targets not initialized");
}
Shades shades = webTargets.getShades(); Shades shades = webTargets.getShades();
List<ShadeData> shadesData = shades.shadeData; List<ShadeData> shadesData = shades.shadeData;
if (shadesData == null) { if (shadesData == null) {
@ -434,11 +420,6 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
private List<Scene> fetchScenes() private List<Scene> fetchScenes()
throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException { throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
HDPowerViewWebTargets webTargets = this.webTargets;
if (webTargets == null) {
throw new ProcessingException("Web targets not initialized");
}
Scenes scenes = webTargets.getScenes(); Scenes scenes = webTargets.getScenes();
List<Scene> sceneData = scenes.sceneData; List<Scene> sceneData = scenes.sceneData;
if (sceneData == null) { if (sceneData == null) {
@ -520,11 +501,6 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
private List<SceneCollection> fetchSceneCollections() private List<SceneCollection> fetchSceneCollections()
throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException { throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
HDPowerViewWebTargets webTargets = this.webTargets;
if (webTargets == null) {
throw new ProcessingException("Web targets not initialized");
}
SceneCollections sceneCollections = webTargets.getSceneCollections(); SceneCollections sceneCollections = webTargets.getSceneCollections();
List<SceneCollection> sceneCollectionData = sceneCollections.sceneCollectionData; List<SceneCollection> sceneCollectionData = sceneCollections.sceneCollectionData;
if (sceneCollectionData == null) { if (sceneCollectionData == null) {
@ -565,11 +541,6 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
private List<ScheduledEvent> fetchScheduledEvents() private List<ScheduledEvent> fetchScheduledEvents()
throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException { throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
HDPowerViewWebTargets webTargets = this.webTargets;
if (webTargets == null) {
throw new ProcessingException("Web targets not initialized");
}
ScheduledEvents scheduledEvents = webTargets.getScheduledEvents(); ScheduledEvents scheduledEvents = webTargets.getScheduledEvents();
List<ScheduledEvent> scheduledEventData = scheduledEvents.scheduledEventData; List<ScheduledEvent> scheduledEventData = scheduledEvents.scheduledEventData;
if (scheduledEventData == null) { if (scheduledEventData == null) {

View File

@ -94,11 +94,6 @@ public class HDPowerViewRepeaterHandler extends AbstractHubbedThingHandler {
return; return;
} }
HDPowerViewWebTargets webTargets = bridge.getWebTargets(); HDPowerViewWebTargets webTargets = bridge.getWebTargets();
if (webTargets == null) {
logger.warn("Web targets not initialized");
return;
}
try { try {
RepeaterData repeaterData; RepeaterData repeaterData;
@ -200,10 +195,6 @@ public class HDPowerViewRepeaterHandler extends AbstractHubbedThingHandler {
return; return;
} }
HDPowerViewWebTargets webTargets = bridge.getWebTargets(); HDPowerViewWebTargets webTargets = bridge.getWebTargets();
if (webTargets == null) {
logger.warn("Web targets not initialized");
return;
}
try { try {
logger.debug("Polling for status information"); logger.debug("Polling for status information");

View File

@ -162,10 +162,6 @@ public class HDPowerViewShadeHandler extends AbstractHubbedThingHandler {
return; return;
} }
HDPowerViewWebTargets webTargets = bridge.getWebTargets(); HDPowerViewWebTargets webTargets = bridge.getWebTargets();
if (webTargets == null) {
logger.warn("Web targets not initialized");
return;
}
try { try {
handleShadeCommand(channelId, command, webTargets, shadeId); handleShadeCommand(channelId, command, webTargets, shadeId);
} catch (HubInvalidResponseException e) { } catch (HubInvalidResponseException e) {
@ -523,9 +519,6 @@ public class HDPowerViewShadeHandler extends AbstractHubbedThingHandler {
throw new HubProcessingException("Missing bridge handler"); throw new HubProcessingException("Missing bridge handler");
} }
HDPowerViewWebTargets webTargets = bridge.getWebTargets(); HDPowerViewWebTargets webTargets = bridge.getWebTargets();
if (webTargets == null) {
throw new HubProcessingException("Web targets not initialized");
}
ShadeData shadeData; ShadeData shadeData;
switch (kind) { switch (kind) {
case POSITION: case POSITION: