Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Lobstein <[email protected]>
  • Loading branch information
mlobstein committed Dec 18, 2024
1 parent 1bb95af commit 017f12b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,4 @@ public class RokuBindingConstants {
public static final String TV_APP = "tvinput.dtv";
public static final String TV_INPUT = "tvinput";
public static final String POWER_ON = "POWERON";

public static final String LIMITED_MODE_RESPONSE = "ECP command not allowed";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.roku.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link RokuLimitedModeException} extends RokuHttpException
*
* @author Michael Lobstein - Initial contribution
*/
@NonNullByDefault
public class RokuLimitedModeException extends RokuHttpException {
private static final long serialVersionUID = 1L;

public RokuLimitedModeException(String errorMessage, Throwable t) {
super(errorMessage, t);
}

public RokuLimitedModeException(String errorMessage) {
super(errorMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package org.openhab.binding.roku.internal.communication;

import static org.openhab.binding.roku.internal.RokuBindingConstants.*;

import java.io.StringReader;
import java.util.List;
import java.util.concurrent.ExecutionException;
Expand All @@ -30,6 +28,7 @@
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.http.HttpMethod;
import org.openhab.binding.roku.internal.RokuHttpException;
import org.openhab.binding.roku.internal.RokuLimitedModeException;
import org.openhab.binding.roku.internal.dto.ActiveApp;
import org.openhab.binding.roku.internal.dto.Apps;
import org.openhab.binding.roku.internal.dto.Apps.App;
Expand All @@ -49,6 +48,7 @@
@NonNullByDefault
public class RokuCommunicator {
private static final int REQUEST_TIMEOUT = 5000;
private static final String LIMITED_MODE_RESPONSE = "ECP command not allowed";

private final Logger logger = LoggerFactory.getLogger(RokuCommunicator.class);
private final HttpClient httpClient;
Expand Down Expand Up @@ -288,9 +288,9 @@ private String getCommand(String url) throws RokuHttpException {
final String response = httpClient.newRequest(url).method(HttpMethod.GET)
.timeout(REQUEST_TIMEOUT, TimeUnit.MILLISECONDS).send().getContentAsString();
if (response != null && response.contains(LIMITED_MODE_RESPONSE)) {
throw new RokuHttpException(LIMITED_MODE_RESPONSE);
throw new RokuLimitedModeException(url + ": " + response);
}
return response != null ? response : EMPTY;
return response != null ? response : "";
} catch (TimeoutException | ExecutionException e) {
throw new RokuHttpException("Error executing GET command for URL: " + url, e);
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.roku.internal.RokuConfiguration;
import org.openhab.binding.roku.internal.RokuHttpException;
import org.openhab.binding.roku.internal.RokuLimitedModeException;
import org.openhab.binding.roku.internal.RokuStateDescriptionOptionProvider;
import org.openhab.binding.roku.internal.communication.RokuCommunicator;
import org.openhab.binding.roku.internal.dto.Apps.App;
Expand Down Expand Up @@ -211,14 +212,13 @@ private void refreshPlayerState() {
}
} catch (NumberFormatException e) {
logger.debug("Unable to parse playerInfo integer value. Exception: {}", e.getMessage());
} catch (RokuLimitedModeException e) {
logger.debug("RokuLimitedModeException: {}", e.getMessage());
limitedMode = 1;
} catch (RokuHttpException e) {
if (isLimitedModeResponse(e)) {
limitedMode = 1;
} else {
logger.debug("Unable to retrieve Roku media-player info. Exception: {}", e.getMessage(), e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
return;
}
logger.debug("Unable to retrieve Roku media-player info. Exception: {}", e.getMessage(), e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
return;
}
} else {
updateState(PLAY_MODE, UnDefType.UNDEF);
Expand All @@ -238,14 +238,13 @@ private void refreshPlayerState() {
updateState(PROGRAM_TITLE, new StringType(tvChannel.getChannel().getProgramTitle()));
updateState(PROGRAM_DESCRIPTION, new StringType(tvChannel.getChannel().getProgramDescription()));
updateState(PROGRAM_RATING, new StringType(tvChannel.getChannel().getProgramRatings()));
} catch (RokuLimitedModeException e) {
logger.debug("RokuLimitedModeException: {}", e.getMessage());
limitedMode = 1;
} catch (RokuHttpException e) {
if (isLimitedModeResponse(e)) {
limitedMode = 1;
} else {
logger.debug("Unable to retrieve Roku tv-active-channel. Exception: {}", e.getMessage(), e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
return;
}
logger.debug("Unable to retrieve Roku tv-active-channel info. Exception: {}", e.getMessage(), e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
return;
}
}

Expand All @@ -257,17 +256,6 @@ private void refreshPlayerState() {
}
}

/**
* Determines if the Roku is configured for Limited mode by examining the exception message
*
* @param ex the RokuHttpException
* @return boolean indicating if the Roku is configured for Limited Mode
*/
private boolean isLimitedModeResponse(RokuHttpException ex) {
final String message = ex.getMessage();
return message != null && message.contains(LIMITED_MODE_RESPONSE);
}

/**
* Start the job to periodically update list of apps installed on the the Roku
*/
Expand Down

0 comments on commit 017f12b

Please sign in to comment.