Skip to content

Commit

Permalink
[roku] Improve TV discovery model name and add timeout (#16210)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Lobstein <michael.lobstein@gmail.com>
  • Loading branch information
mlobstein committed Jan 8, 2024
1 parent 71dbd78 commit c858e05
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
15 changes: 7 additions & 8 deletions bundles/org.openhab.binding.roku/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ The following channels are available:

Some Notes:

- The values for `activeApp`, `playMode`, `timeElapsed`, `timeTotal`, `activeChannel`, `signalMode`, `signalQuality`, `channelName`, `programTitle`, `programDescription` & `programRating` refresh automatically per the configured `refresh` interval (10 seconds minimum).
- The values for `activeApp`, `activeAppName`, `playMode`, `timeElapsed`, `timeTotal`, `activeChannel`, `signalMode`, `signalQuality`, `channelName`, `programTitle`, `programDescription`, `programRating`, `power` & `powerState` refresh automatically per the configured `refresh` interval.

**List of available button commands for Roku streaming devices:**
Home
Expand Down Expand Up @@ -109,17 +109,19 @@ roku:roku_tv:mytv1 "My Roku TV" [ hostName="192.168.10.1", refresh=10 ]

String Player_ActiveApp "Current App: [%s]" { channel="roku:roku_player:myplayer1:activeApp" }
String Player_ActiveAppName "Current App Name: [%s]" { channel="roku:roku_player:myplayer1:activeAppName" }
String Player_Button "Send Command to Roku" { channel="roku:roku_player:myplayer1:button" }
String Player_Button "Send Command to Roku" { channel="roku:roku_player:myplayer1:button", autoupdate="false" }
Player Player_Control "Control" { channel="roku:roku_player:myplayer1:control" }
String Player_PlayMode "Status: [%s]" { channel="roku:roku_player:myplayer1:playMode" }
Number:Time Player_TimeElapsed "Elapsed Time: [%d %unit%]" { channel="roku:roku_player:myplayer1:timeElapsed" }
Number:Time Player_TimeTotal "Total Time: [%d %unit%]" { channel="roku:roku_player:myplayer1:timeTotal" }

// Roku TV items:

Switch Player_Power "Power: [%s]" { channel="roku:roku_tv:mytv1:power" }
String Player_PowerState "Power State: [%s] { channel="roku:roku_tv:mytv1:powerState" }
String Player_ActiveApp "Current App: [%s]" { channel="roku:roku_tv:mytv1:activeApp" }
String Player_ActiveAppName "Current App Name: [%s]" { channel="roku:roku_tv:mytv1:activeAppName" }
String Player_Button "Send Command to Roku" { channel="roku:roku_tv:mytv1:button" }
String Player_Button "Send Command to Roku" { channel="roku:roku_tv:mytv1:button", autoupdate="false" }
Player Player_Control "Control" { channel="roku:roku_tv:mytv1:control" }
String Player_PlayMode "Status: [%s]" { channel="roku:roku_tv:mytv1:playMode" }
Number:Time Player_TimeElapsed "Elapsed Time: [%d %unit%]" { channel="roku:roku_tv:mytv1:timeElapsed" }
Expand All @@ -131,9 +133,6 @@ String Player_ChannelName "Channel Name: [%s]" { channel="roku:rok
String Player_ProgramTitle "Program Title: [%s]" { channel="roku:roku_tv:mytv1:programTitle" }
String Player_ProgramDescription "Program Description: [%s]" { channel="roku:roku_tv:mytv1:programDescription" }
String Player_ProgramRating "Program Rating: [%s]" { channel="roku:roku_tv:mytv1:programRating" }
Switch Player_Power "Power: [%s]" { channel="roku:roku_tv:mytv1:power" }
String Player_PowerState "Power State: [%s] { channel="roku:roku_tv:mytv1:powerState" }
```
### roku.sitemap:
Expand All @@ -149,15 +148,15 @@ sitemap roku label="Roku" {
Text item=Player_TimeElapsed icon="time"
Text item=Player_TimeTotal icon="time"
// The following items apply to Roku TVs only
Switch item=Player_Power
Text item=Player_PowerState
Selection item=Player_ActiveChannel icon="screen"
Text item=Player_SignalMode
Text item=Player_SignalQuality
Text item=Player_ChannelName
Text item=Player_ProgramTitle
Text item=Player_ProgramDescription
Text item=Player_ProgramRating
Switch item=Player_Power
Text item=Player_PowerState
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.StringReader;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.xml.bind.JAXBContext;
Expand Down Expand Up @@ -43,6 +44,8 @@
*/
@NonNullByDefault
public class RokuCommunicator {
private static final int REQUEST_TIMEOUT = 5000;

private final HttpClient httpClient;

private final String urlKeyPress;
Expand Down Expand Up @@ -265,7 +268,8 @@ public List<Channel> getTvChannelList() throws RokuHttpException {
*/
private String getCommand(String url) throws RokuHttpException {
try {
return httpClient.GET(url).getContentAsString();
return httpClient.newRequest(url).method(HttpMethod.GET).timeout(REQUEST_TIMEOUT, TimeUnit.MILLISECONDS)
.send().getContentAsString();
} catch (TimeoutException | ExecutionException e) {
throw new RokuHttpException("Error executing GET command for URL: " + url, e);
} catch (InterruptedException e) {
Expand All @@ -282,7 +286,7 @@ private String getCommand(String url) throws RokuHttpException {
*/
private void postCommand(String url) throws RokuHttpException {
try {
httpClient.POST(url).method(HttpMethod.POST).send();
httpClient.POST(url).method(HttpMethod.POST).timeout(REQUEST_TIMEOUT, TimeUnit.MILLISECONDS).send();
} catch (TimeoutException | ExecutionException e) {
throw new RokuHttpException("Error executing POST command, URL: " + url, e);
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ private void parseResponseCreateThing(String response) {
try {
RokuCommunicator communicator = new RokuCommunicator(httpClient, host, port);
DeviceInfo device = communicator.getDeviceInfo();
label = device.getModelName() + " " + device.getModelNumber();

// replace extraneous characters with spaces and remove any consecutive spaces
label = (device.getFriendlyModelName() + " " + device.getUserDeviceLocation())
.replaceAll("[^a-zA-Z0-9\\-_]", " ").trim().replaceAll(" +", " ");
if (device.isTv()) {
thingUid = new ThingUID(THING_TYPE_ROKU_TV, uuid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public void initialize() {
thing.setProperty(PROPERTY_SERIAL_NUMBER, deviceInfo.getSerialNumber());
thing.setProperty(PROPERTY_DEVICE_ID, deviceInfo.getDeviceId());
thing.setProperty(PROPERTY_SOFTWARE_VERSION, deviceInfo.getSoftwareVersion());
thing.setProperty(PROPERTY_UUID, deviceInfo.getSerialNumber().toLowerCase());
updateStatus(ThingStatus.ONLINE);
} catch (RokuHttpException e) {
logger.debug("Unable to retrieve Roku device-info. Exception: {}", e.getMessage(), e);
Expand Down

0 comments on commit c858e05

Please sign in to comment.