2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.roku.internal.communication;
15 import java.io.StringReader;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.TimeoutException;
20 import javax.xml.bind.JAXBContext;
21 import javax.xml.bind.JAXBException;
22 import javax.xml.bind.Unmarshaller;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jetty.client.HttpClient;
26 import org.eclipse.jetty.http.HttpMethod;
27 import org.openhab.binding.roku.internal.RokuHttpException;
28 import org.openhab.binding.roku.internal.dto.ActiveApp;
29 import org.openhab.binding.roku.internal.dto.Apps;
30 import org.openhab.binding.roku.internal.dto.Apps.App;
31 import org.openhab.binding.roku.internal.dto.DeviceInfo;
32 import org.openhab.binding.roku.internal.dto.Player;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Methods for accessing the HTTP interface of the Roku
39 * @author Michael Lobstein - Initial contribution
42 public class RokuCommunicator {
43 private final Logger logger = LoggerFactory.getLogger(RokuCommunicator.class);
44 private final HttpClient httpClient;
46 private final String urlKeyPress;
47 private final String urlLaunchApp;
48 private final String urlQryDevice;
49 private final String urlQryActiveApp;
50 private final String urlQryApps;
51 private final String urlQryPlayer;
53 public RokuCommunicator(HttpClient httpClient, String host, int port) {
54 this.httpClient = httpClient;
56 final String baseUrl = "http://" + host + ":" + port;
57 urlKeyPress = baseUrl + "/keypress/";
58 urlLaunchApp = baseUrl + "/launch/";
59 urlQryDevice = baseUrl + "/query/device-info";
60 urlQryActiveApp = baseUrl + "/query/active-app";
61 urlQryApps = baseUrl + "/query/apps";
62 urlQryPlayer = baseUrl + "/query/media-player";
66 * Send a keypress command to the Roku
68 * @param key The key code to send
71 public void keyPress(String key) throws RokuHttpException {
72 postCommand(urlKeyPress + key);
76 * Send a launch app command to the Roku
78 * @param appId The appId of the app to launch
81 public void launchApp(String appId) throws RokuHttpException {
82 postCommand(urlLaunchApp + appId);
86 * Send a command to get device-info from the Roku and return a DeviceInfo object
88 * @return A DeviceInfo object populated with information about the connected Roku
89 * @throws RokuHttpException
91 public DeviceInfo getDeviceInfo() throws RokuHttpException {
93 JAXBContext ctx = JAXBUtils.JAXBCONTEXT_DEVICE_INFO;
95 Unmarshaller unmarshaller = ctx.createUnmarshaller();
96 if (unmarshaller != null) {
97 DeviceInfo device = (DeviceInfo) unmarshaller.unmarshal(new StringReader(getCommand(urlQryDevice)));
103 throw new RokuHttpException("No DeviceInfo model in response");
104 } catch (JAXBException e) {
105 throw new RokuHttpException("Exception creating DeviceInfo Unmarshaller: " + e.getLocalizedMessage());
110 * Send a command to get active-app from the Roku and return an ActiveApp object
112 * @return An ActiveApp object populated with information about the current running app on the Roku
113 * @throws RokuHttpException
115 public ActiveApp getActiveApp() throws RokuHttpException {
117 JAXBContext ctx = JAXBUtils.JAXBCONTEXT_ACTIVE_APP;
119 Unmarshaller unmarshaller = ctx.createUnmarshaller();
120 if (unmarshaller != null) {
121 ActiveApp activeApp = (ActiveApp) unmarshaller
122 .unmarshal(new StringReader(getCommand(urlQryActiveApp)));
123 if (activeApp != null) {
128 throw new RokuHttpException("No ActiveApp model in response");
129 } catch (JAXBException e) {
130 throw new RokuHttpException("Exception creating ActiveApp Unmarshaller: " + e.getLocalizedMessage());
135 * Send a command to get the installed app list from the Roku and return a List of App objects
137 * @return A List of App objects for all apps currently installed on the Roku
138 * @throws RokuHttpException
140 public List<App> getAppList() throws RokuHttpException {
142 JAXBContext ctx = JAXBUtils.JAXBCONTEXT_APPS;
144 Unmarshaller unmarshaller = ctx.createUnmarshaller();
145 if (unmarshaller != null) {
146 Apps appList = (Apps) unmarshaller.unmarshal(new StringReader(getCommand(urlQryApps)));
147 if (appList != null) {
148 return appList.getApp();
152 throw new RokuHttpException("No AppList model in response");
153 } catch (JAXBException e) {
154 throw new RokuHttpException("Exception creating AppList Unmarshaller: " + e.getLocalizedMessage());
159 * Send a command to get media-player from the Roku and return a Player object
161 * @return A Player object populated with information about the current stream playing on the Roku
162 * @throws RokuHttpException
164 public Player getPlayerInfo() throws RokuHttpException {
166 JAXBContext ctx = JAXBUtils.JAXBCONTEXT_PLAYER;
168 Unmarshaller unmarshaller = ctx.createUnmarshaller();
169 if (unmarshaller != null) {
170 Player playerInfo = (Player) unmarshaller.unmarshal(new StringReader(getCommand(urlQryPlayer)));
171 if (playerInfo != null) {
176 throw new RokuHttpException("No Player info model in response");
177 } catch (JAXBException e) {
178 throw new RokuHttpException("Exception creating Player info Unmarshaller: " + e.getLocalizedMessage());
183 * Sends a GET command to the Roku
185 * @param url The url to send with the command embedded in the URI
186 * @return The response content of the http request
188 private String getCommand(String url) {
190 return httpClient.GET(url).getContentAsString();
191 } catch (InterruptedException | TimeoutException | ExecutionException e) {
192 logger.debug("Error executing player GET command, URL: {}, {} ", url, e.getMessage());
198 * Sends a POST command to the Roku
200 * @param url The url to send with the command embedded in the URI
201 * @throws RokuHttpException
203 private void postCommand(String url) throws RokuHttpException {
205 httpClient.POST(url).method(HttpMethod.POST).send();
206 } catch (InterruptedException | TimeoutException | ExecutionException e) {
207 throw new RokuHttpException("Error executing player POST command, URL: " + url + e.getMessage());