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

View File

@ -40,9 +40,10 @@ import org.slf4j.Logger;
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 Jacob Laursen - Add Repeater discovery
*/
@NonNullByDefault
public class HDPowerViewDeviceDiscoveryService extends AbstractDiscoveryService {
@ -87,9 +88,6 @@ public class HDPowerViewDeviceDiscoveryService extends AbstractDiscoveryService
return () -> {
try {
HDPowerViewWebTargets webTargets = hub.getWebTargets();
if (webTargets == null) {
throw new HubProcessingException("Web targets not initialized");
}
discoverShades(webTargets);
discoverRepeaters(webTargets);
} catch (HubMaintenanceException e) {

View File

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

View File

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

View File

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