]> git.basschouten.com Git - openhab-addons.git/blob
ce3891c6855f9fbf5f23e6f0beb206b6196a3133
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.roku.internal.communication;
14
15 import java.io.StringReader;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.TimeoutException;
19
20 import javax.xml.bind.JAXBContext;
21 import javax.xml.bind.JAXBException;
22 import javax.xml.bind.Unmarshaller;
23 import javax.xml.stream.XMLStreamException;
24 import javax.xml.stream.XMLStreamReader;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jetty.client.HttpClient;
28 import org.eclipse.jetty.http.HttpMethod;
29 import org.openhab.binding.roku.internal.RokuHttpException;
30 import org.openhab.binding.roku.internal.dto.ActiveApp;
31 import org.openhab.binding.roku.internal.dto.Apps;
32 import org.openhab.binding.roku.internal.dto.Apps.App;
33 import org.openhab.binding.roku.internal.dto.DeviceInfo;
34 import org.openhab.binding.roku.internal.dto.Player;
35 import org.openhab.binding.roku.internal.dto.TvChannel;
36 import org.openhab.binding.roku.internal.dto.TvChannels;
37 import org.openhab.binding.roku.internal.dto.TvChannels.Channel;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Methods for accessing the HTTP interface of the Roku
43  *
44  * @author Michael Lobstein - Initial contribution
45  */
46 @NonNullByDefault
47 public class RokuCommunicator {
48     private final Logger logger = LoggerFactory.getLogger(RokuCommunicator.class);
49     private final HttpClient httpClient;
50
51     private final String urlKeyPress;
52     private final String urlLaunchApp;
53     private final String urlLaunchTvChannel;
54     private final String urlQryDevice;
55     private final String urlQryActiveApp;
56     private final String urlQryApps;
57     private final String urlQryPlayer;
58     private final String urlQryActiveTvChannel;
59     private final String urlQryTvChannels;
60
61     public RokuCommunicator(HttpClient httpClient, String host, int port) {
62         this.httpClient = httpClient;
63
64         final String baseUrl = "http://" + host + ":" + port;
65         urlKeyPress = baseUrl + "/keypress/";
66         urlLaunchApp = baseUrl + "/launch/";
67         urlLaunchTvChannel = baseUrl + "/launch/tvinput.dtv?ch=";
68         urlQryDevice = baseUrl + "/query/device-info";
69         urlQryActiveApp = baseUrl + "/query/active-app";
70         urlQryApps = baseUrl + "/query/apps";
71         urlQryPlayer = baseUrl + "/query/media-player";
72         urlQryActiveTvChannel = baseUrl + "/query/tv-active-channel";
73         urlQryTvChannels = baseUrl + "/query/tv-channels";
74     }
75
76     /**
77      * Send a keypress command to the Roku
78      *
79      * @param key The key code to send
80      *
81      */
82     public void keyPress(String key) throws RokuHttpException {
83         postCommand(urlKeyPress + key);
84     }
85
86     /**
87      * Send a launch app command to the Roku
88      *
89      * @param appId The appId of the app to launch
90      *
91      */
92     public void launchApp(String appId) throws RokuHttpException {
93         postCommand(urlLaunchApp + appId);
94     }
95
96     /**
97      * Send a TV channel change command to the Roku TV
98      *
99      * @param channelNumber The channel number of the channel to tune into, ie: 2.1
100      *
101      */
102     public void launchTvChannel(String channelNumber) throws RokuHttpException {
103         postCommand(urlLaunchTvChannel + channelNumber);
104     }
105
106     /**
107      * Send a command to get device-info from the Roku and return a DeviceInfo object
108      *
109      * @return A DeviceInfo object populated with information about the connected Roku
110      * @throws RokuHttpException
111      */
112     public DeviceInfo getDeviceInfo() throws RokuHttpException {
113         try {
114             JAXBContext ctx = JAXBUtils.JAXBCONTEXT_DEVICE_INFO;
115             if (ctx != null) {
116                 Unmarshaller unmarshaller = ctx.createUnmarshaller();
117                 if (unmarshaller != null) {
118                     XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
119                             .createXMLStreamReader(new StringReader(getCommand(urlQryDevice)));
120                     DeviceInfo device = (DeviceInfo) unmarshaller.unmarshal(xsr);
121                     if (device != null) {
122                         return device;
123                     }
124                 }
125             }
126             throw new RokuHttpException("No DeviceInfo model in response");
127         } catch (JAXBException | XMLStreamException e) {
128             throw new RokuHttpException("Exception creating DeviceInfo Unmarshaller: " + e.getLocalizedMessage());
129         }
130     }
131
132     /**
133      * Send a command to get active-app from the Roku and return an ActiveApp object
134      *
135      * @return An ActiveApp object populated with information about the current running app on the Roku
136      * @throws RokuHttpException
137      */
138     public ActiveApp getActiveApp() throws RokuHttpException {
139         try {
140             JAXBContext ctx = JAXBUtils.JAXBCONTEXT_ACTIVE_APP;
141             if (ctx != null) {
142                 Unmarshaller unmarshaller = ctx.createUnmarshaller();
143                 if (unmarshaller != null) {
144                     XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
145                             .createXMLStreamReader(new StringReader(getCommand(urlQryActiveApp)));
146                     ActiveApp activeApp = (ActiveApp) unmarshaller.unmarshal(xsr);
147                     if (activeApp != null) {
148                         return activeApp;
149                     }
150                 }
151             }
152             throw new RokuHttpException("No ActiveApp model in response");
153         } catch (JAXBException | XMLStreamException e) {
154             throw new RokuHttpException("Exception creating ActiveApp Unmarshaller: " + e.getLocalizedMessage());
155         }
156     }
157
158     /**
159      * Send a command to get the installed app list from the Roku and return a List of App objects
160      *
161      * @return A List of App objects for all apps currently installed on the Roku
162      * @throws RokuHttpException
163      */
164     public List<App> getAppList() throws RokuHttpException {
165         try {
166             JAXBContext ctx = JAXBUtils.JAXBCONTEXT_APPS;
167             if (ctx != null) {
168                 Unmarshaller unmarshaller = ctx.createUnmarshaller();
169                 if (unmarshaller != null) {
170                     XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
171                             .createXMLStreamReader(new StringReader(getCommand(urlQryApps)));
172                     Apps appList = (Apps) unmarshaller.unmarshal(xsr);
173                     if (appList != null) {
174                         return appList.getApp();
175                     }
176                 }
177             }
178             throw new RokuHttpException("No AppList model in response");
179         } catch (JAXBException | XMLStreamException e) {
180             throw new RokuHttpException("Exception creating AppList Unmarshaller: " + e.getLocalizedMessage());
181         }
182     }
183
184     /**
185      * Send a command to get media-player from the Roku and return a Player object
186      *
187      * @return A Player object populated with information about the current stream playing on the Roku
188      * @throws RokuHttpException
189      */
190     public Player getPlayerInfo() throws RokuHttpException {
191         try {
192             JAXBContext ctx = JAXBUtils.JAXBCONTEXT_PLAYER;
193             if (ctx != null) {
194                 Unmarshaller unmarshaller = ctx.createUnmarshaller();
195                 if (unmarshaller != null) {
196                     XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
197                             .createXMLStreamReader(new StringReader(getCommand(urlQryPlayer)));
198                     Player playerInfo = (Player) unmarshaller.unmarshal(xsr);
199                     if (playerInfo != null) {
200                         return playerInfo;
201                     }
202                 }
203             }
204             throw new RokuHttpException("No Player info model in response");
205         } catch (JAXBException | XMLStreamException e) {
206             throw new RokuHttpException("Exception creating Player info Unmarshaller: " + e.getLocalizedMessage());
207         }
208     }
209
210     /**
211      * Send a command to get tv-active-channel from the Roku TV and return a TvChannel object
212      *
213      * @return A TvChannel object populated with information about the current active TV Channel
214      * @throws RokuHttpException
215      */
216     public TvChannel getActiveTvChannel() throws RokuHttpException {
217         try {
218             JAXBContext ctx = JAXBUtils.JAXBCONTEXT_TVCHANNEL;
219             if (ctx != null) {
220                 Unmarshaller unmarshaller = ctx.createUnmarshaller();
221                 if (unmarshaller != null) {
222                     XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
223                             .createXMLStreamReader(new StringReader(getCommand(urlQryActiveTvChannel)));
224                     TvChannel tvChannelInfo = (TvChannel) unmarshaller.unmarshal(xsr);
225                     if (tvChannelInfo != null) {
226                         return tvChannelInfo;
227                     }
228                 }
229             }
230             throw new RokuHttpException("No TvChannel info model in response");
231         } catch (JAXBException | XMLStreamException e) {
232             throw new RokuHttpException("Exception creating TvChannel info Unmarshaller: " + e.getLocalizedMessage());
233         }
234     }
235
236     /**
237      * Send a command to get tv-channels from the Roku TV and return a list of Channel objects
238      *
239      * @return A List of Channel objects for all TV channels currently available on the Roku TV
240      * @throws RokuHttpException
241      */
242     public List<Channel> getTvChannelList() throws RokuHttpException {
243         try {
244             JAXBContext ctx = JAXBUtils.JAXBCONTEXT_TVCHANNELS;
245             if (ctx != null) {
246                 Unmarshaller unmarshaller = ctx.createUnmarshaller();
247                 if (unmarshaller != null) {
248                     XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
249                             .createXMLStreamReader(new StringReader(getCommand(urlQryTvChannels)));
250                     TvChannels tvChannels = (TvChannels) unmarshaller.unmarshal(xsr);
251                     if (tvChannels != null) {
252                         return tvChannels.getChannel();
253                     }
254                 }
255             }
256             throw new RokuHttpException("No TvChannels info model in response");
257         } catch (JAXBException | XMLStreamException e) {
258             throw new RokuHttpException("Exception creating TvChannel info Unmarshaller: " + e.getLocalizedMessage());
259         }
260     }
261
262     /**
263      * Sends a GET command to the Roku
264      *
265      * @param url The url to send with the command embedded in the URI
266      * @return The response content of the http request
267      */
268     private String getCommand(String url) {
269         try {
270             return httpClient.GET(url).getContentAsString();
271         } catch (InterruptedException | TimeoutException | ExecutionException e) {
272             logger.debug("Error executing player GET command, URL: {}, {} ", url, e.getMessage());
273             return "";
274         }
275     }
276
277     /**
278      * Sends a POST command to the Roku
279      *
280      * @param url The url to send with the command embedded in the URI
281      * @throws RokuHttpException
282      */
283     private void postCommand(String url) throws RokuHttpException {
284         try {
285             httpClient.POST(url).method(HttpMethod.POST).send();
286         } catch (InterruptedException | TimeoutException | ExecutionException e) {
287             throw new RokuHttpException("Error executing player POST command, URL: " + url + e.getMessage());
288         }
289     }
290 }