]> git.basschouten.com Git - openhab-addons.git/blob
bb886851704ccef7f1c4e72c12a9717d76f2e10b
[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.handler;
14
15 import static org.openhab.binding.roku.internal.RokuBindingConstants.*;
16
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.eclipse.jetty.client.HttpClient;
27 import org.openhab.binding.roku.internal.RokuConfiguration;
28 import org.openhab.binding.roku.internal.RokuHttpException;
29 import org.openhab.binding.roku.internal.RokuStateDescriptionOptionProvider;
30 import org.openhab.binding.roku.internal.communication.RokuCommunicator;
31 import org.openhab.binding.roku.internal.dto.Apps.App;
32 import org.openhab.binding.roku.internal.dto.DeviceInfo;
33 import org.openhab.binding.roku.internal.dto.Player;
34 import org.openhab.binding.roku.internal.dto.TvChannel;
35 import org.openhab.binding.roku.internal.dto.TvChannels.Channel;
36 import org.openhab.core.library.types.NextPreviousType;
37 import org.openhab.core.library.types.OnOffType;
38 import org.openhab.core.library.types.PlayPauseType;
39 import org.openhab.core.library.types.QuantityType;
40 import org.openhab.core.library.types.StringType;
41 import org.openhab.core.thing.ChannelUID;
42 import org.openhab.core.thing.Thing;
43 import org.openhab.core.thing.ThingStatus;
44 import org.openhab.core.thing.ThingStatusDetail;
45 import org.openhab.core.thing.ThingTypeUID;
46 import org.openhab.core.thing.binding.BaseThingHandler;
47 import org.openhab.core.types.Command;
48 import org.openhab.core.types.RefreshType;
49 import org.openhab.core.types.StateOption;
50 import org.openhab.core.types.UnDefType;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 /**
55  * The {@link RokuHandler} is responsible for handling commands, which are
56  * sent to one of the channels.
57  *
58  * @author Michael Lobstein - Initial contribution
59  */
60 @NonNullByDefault
61 public class RokuHandler extends BaseThingHandler {
62     private static final int DEFAULT_REFRESH_PERIOD_SEC = 10;
63
64     private final Logger logger = LoggerFactory.getLogger(RokuHandler.class);
65     private final HttpClient httpClient;
66     private final RokuStateDescriptionOptionProvider stateDescriptionProvider;
67
68     private @Nullable ScheduledFuture<?> refreshJob;
69     private @Nullable ScheduledFuture<?> appListJob;
70
71     private ThingTypeUID thingTypeUID = THING_TYPE_ROKU_PLAYER;
72     private RokuCommunicator communicator;
73     private DeviceInfo deviceInfo = new DeviceInfo();
74     private int refreshInterval = DEFAULT_REFRESH_PERIOD_SEC;
75     private boolean tvActive = false;
76     private Map<String, String> appMap = new HashMap<>();
77
78     private Object sequenceLock = new Object();
79
80     public RokuHandler(Thing thing, HttpClient httpClient,
81             RokuStateDescriptionOptionProvider stateDescriptionProvider) {
82         super(thing);
83         this.httpClient = httpClient;
84         this.stateDescriptionProvider = stateDescriptionProvider;
85         this.communicator = new RokuCommunicator(httpClient, EMPTY, -1);
86     }
87
88     @Override
89     public void initialize() {
90         logger.debug("Initializing Roku handler");
91         RokuConfiguration config = getConfigAs(RokuConfiguration.class);
92         this.thingTypeUID = this.getThing().getThingTypeUID();
93
94         final @Nullable String host = config.hostName;
95
96         if (host != null && !EMPTY.equals(host)) {
97             this.communicator = new RokuCommunicator(httpClient, host, config.port);
98         } else {
99             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Host Name must be specified");
100             return;
101         }
102
103         if (config.refresh >= 1) {
104             refreshInterval = config.refresh;
105         }
106
107         updateStatus(ThingStatus.UNKNOWN);
108
109         try {
110             deviceInfo = communicator.getDeviceInfo();
111             thing.setProperty(PROPERTY_MODEL_NAME, deviceInfo.getModelName());
112             thing.setProperty(PROPERTY_MODEL_NUMBER, deviceInfo.getModelNumber());
113             thing.setProperty(PROPERTY_DEVICE_LOCAITON, deviceInfo.getUserDeviceLocation());
114             thing.setProperty(PROPERTY_SERIAL_NUMBER, deviceInfo.getSerialNumber());
115             thing.setProperty(PROPERTY_DEVICE_ID, deviceInfo.getDeviceId());
116             thing.setProperty(PROPERTY_SOFTWARE_VERSION, deviceInfo.getSoftwareVersion());
117             updateStatus(ThingStatus.ONLINE);
118         } catch (RokuHttpException e) {
119             logger.debug("Unable to retrieve Roku device-info. Exception: {}", e.getMessage(), e);
120         }
121         startAutomaticRefresh();
122         startAppListRefresh();
123     }
124
125     /**
126      * Start the job to periodically get status updates from the Roku
127      */
128     private void startAutomaticRefresh() {
129         ScheduledFuture<?> refreshJob = this.refreshJob;
130         if (refreshJob == null || refreshJob.isCancelled()) {
131             this.refreshJob = scheduler.scheduleWithFixedDelay(this::refreshPlayerState, 0, refreshInterval,
132                     TimeUnit.SECONDS);
133         }
134     }
135
136     /**
137      * Get a status update from the Roku and update the channels
138      */
139     private void refreshPlayerState() {
140         synchronized (sequenceLock) {
141             String activeAppId = ROKU_HOME_ID;
142             try {
143                 if (thingTypeUID.equals(THING_TYPE_ROKU_TV)) {
144                     try {
145                         deviceInfo = communicator.getDeviceInfo();
146                         String powerMode = deviceInfo.getPowerMode();
147                         updateState(POWER_STATE, new StringType(powerMode));
148                         updateState(POWER, OnOffType.from(POWER_ON.equalsIgnoreCase(powerMode)));
149                     } catch (RokuHttpException e) {
150                         logger.debug("Unable to retrieve Roku device-info.", e);
151                     }
152                 }
153
154                 activeAppId = communicator.getActiveApp().getApp().getId();
155
156                 // 562859 is now reported when on the home screen, reset to -1
157                 if (ROKU_HOME_ID_562859.equals(activeAppId)) {
158                     activeAppId = ROKU_HOME_ID;
159                 }
160
161                 updateState(ACTIVE_APP, new StringType(activeAppId));
162                 updateState(ACTIVE_APPNAME, new StringType(appMap.get(activeAppId)));
163
164                 if (TV_APP.equals(activeAppId)) {
165                     tvActive = true;
166                 } else {
167                     if (tvActive) {
168                         updateState(SIGNAL_MODE, UnDefType.UNDEF);
169                         updateState(SIGNAL_QUALITY, UnDefType.UNDEF);
170                         updateState(CHANNEL_NAME, UnDefType.UNDEF);
171                         updateState(PROGRAM_TITLE, UnDefType.UNDEF);
172                         updateState(PROGRAM_DESCRIPTION, UnDefType.UNDEF);
173                         updateState(PROGRAM_RATING, UnDefType.UNDEF);
174                     }
175                     tvActive = false;
176                 }
177                 updateStatus(ThingStatus.ONLINE);
178             } catch (RokuHttpException e) {
179                 logger.debug("Unable to retrieve Roku active-app info. Exception: {}", e.getMessage(), e);
180                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
181             }
182
183             // On the home app and when using the TV or TV inputs, do not update the play mode or time channels
184             if (!ROKU_HOME_ID.equals(activeAppId) && !activeAppId.contains(TV_INPUT)) {
185                 try {
186                     Player playerInfo = communicator.getPlayerInfo();
187                     // When nothing playing, 'close' is reported, replace with 'stop'
188                     updateState(PLAY_MODE, new StringType(playerInfo.getState().replaceAll(CLOSE, STOP)));
189                     updateState(CONTROL,
190                             PLAY.equalsIgnoreCase(playerInfo.getState()) ? PlayPauseType.PLAY : PlayPauseType.PAUSE);
191
192                     // Remove non-numeric from string, ie: ' ms'
193                     String position = playerInfo.getPosition().replaceAll(NON_DIGIT_PATTERN, EMPTY);
194                     if (!EMPTY.equals(position)) {
195                         updateState(TIME_ELAPSED,
196                                 new QuantityType<>(Integer.parseInt(position) / 1000, API_SECONDS_UNIT));
197                     } else {
198                         updateState(TIME_ELAPSED, UnDefType.UNDEF);
199                     }
200
201                     String duration = playerInfo.getDuration().replaceAll(NON_DIGIT_PATTERN, EMPTY);
202                     if (!EMPTY.equals(duration)) {
203                         updateState(TIME_TOTAL,
204                                 new QuantityType<>(Integer.parseInt(duration) / 1000, API_SECONDS_UNIT));
205                     } else {
206                         updateState(TIME_TOTAL, UnDefType.UNDEF);
207                     }
208                 } catch (RokuHttpException e) {
209                     logger.debug("Unable to retrieve Roku media-player info. Exception: {}", e.getMessage(), e);
210                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
211                 }
212             } else {
213                 updateState(PLAY_MODE, UnDefType.UNDEF);
214                 updateState(TIME_ELAPSED, UnDefType.UNDEF);
215                 updateState(TIME_TOTAL, UnDefType.UNDEF);
216             }
217
218             if (thingTypeUID.equals(THING_TYPE_ROKU_TV) && tvActive) {
219                 try {
220                     TvChannel tvChannel = communicator.getActiveTvChannel();
221                     updateState(ACTIVE_CHANNEL, new StringType(tvChannel.getChannel().getNumber()));
222                     updateState(SIGNAL_MODE, new StringType(tvChannel.getChannel().getSignalMode()));
223                     updateState(SIGNAL_QUALITY,
224                             new QuantityType<>(tvChannel.getChannel().getSignalQuality(), API_PERCENT_UNIT));
225                     updateState(CHANNEL_NAME, new StringType(tvChannel.getChannel().getName()));
226                     updateState(PROGRAM_TITLE, new StringType(tvChannel.getChannel().getProgramTitle()));
227                     updateState(PROGRAM_DESCRIPTION, new StringType(tvChannel.getChannel().getProgramDescription()));
228                     updateState(PROGRAM_RATING, new StringType(tvChannel.getChannel().getProgramRatings()));
229                 } catch (RokuHttpException e) {
230                     logger.debug("Unable to retrieve Roku tv-active-channel info. Exception: {}", e.getMessage(), e);
231                 }
232             }
233         }
234     }
235
236     /**
237      * Start the job to periodically update list of apps installed on the the Roku
238      */
239     private void startAppListRefresh() {
240         ScheduledFuture<?> appListJob = this.appListJob;
241         if (appListJob == null || appListJob.isCancelled()) {
242             this.appListJob = scheduler.scheduleWithFixedDelay(this::refreshAppList, 10, 600, TimeUnit.SECONDS);
243         }
244     }
245
246     /**
247      * Update the dropdown that lists all apps installed on the Roku
248      */
249     private void refreshAppList() {
250         synchronized (sequenceLock) {
251             try {
252                 List<App> appList = communicator.getAppList();
253                 Map<String, String> appMap = new HashMap<>();
254
255                 List<StateOption> appListOptions = new ArrayList<>();
256                 // Roku Home will be selected in the drop-down any time an app is not running.
257                 appListOptions.add(new StateOption(ROKU_HOME_ID, ROKU_HOME));
258                 appMap.put(ROKU_HOME_ID, ROKU_HOME);
259
260                 appList.forEach(app -> {
261                     appListOptions.add(new StateOption(app.getId(), app.getValue()));
262                     appMap.put(app.getId(), app.getValue());
263                 });
264
265                 stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), ACTIVE_APP),
266                         appListOptions);
267
268                 this.appMap = appMap;
269             } catch (RokuHttpException e) {
270                 logger.debug("Unable to retrieve Roku installed app-list. Exception: {}", e.getMessage(), e);
271             }
272
273             if (thingTypeUID.equals(THING_TYPE_ROKU_TV)) {
274                 try {
275                     List<Channel> channelsList = communicator.getTvChannelList();
276
277                     List<StateOption> channelListOptions = new ArrayList<>();
278                     channelsList.forEach(channel -> {
279                         if (!channel.isUserHidden()) {
280                             channelListOptions.add(new StateOption(channel.getNumber(),
281                                     channel.getNumber() + " - " + channel.getName()));
282                         }
283                     });
284
285                     stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), ACTIVE_CHANNEL),
286                             channelListOptions);
287
288                 } catch (RokuHttpException e) {
289                     logger.debug("Unable to retrieve Roku tv-channels. Exception: {}", e.getMessage(), e);
290                 }
291             }
292         }
293     }
294
295     @Override
296     public void dispose() {
297         ScheduledFuture<?> refreshJob = this.refreshJob;
298         if (refreshJob != null) {
299             refreshJob.cancel(true);
300             this.refreshJob = null;
301         }
302
303         ScheduledFuture<?> appListJob = this.appListJob;
304         if (appListJob != null) {
305             appListJob.cancel(true);
306             this.appListJob = null;
307         }
308     }
309
310     @Override
311     public void handleCommand(ChannelUID channelUID, Command command) {
312         if (command instanceof RefreshType) {
313             logger.debug("Unsupported refresh command: {}", command);
314         } else if (channelUID.getId().equals(BUTTON)) {
315             synchronized (sequenceLock) {
316                 try {
317                     communicator.keyPress(command.toString());
318                 } catch (RokuHttpException e) {
319                     logger.debug("Unable to send keypress to Roku, key: {}, Exception: {}", command, e.getMessage());
320                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
321                 }
322             }
323         } else if (channelUID.getId().equals(ACTIVE_APP)) {
324             synchronized (sequenceLock) {
325                 try {
326                     String appId = command.toString();
327                     // Roku Home(-1) is not a real appId, just press the home button instead
328                     if (!ROKU_HOME_ID.equals(appId)) {
329                         communicator.launchApp(appId);
330                     } else {
331                         communicator.keyPress(ROKU_HOME_BUTTON);
332                     }
333                 } catch (RokuHttpException e) {
334                     logger.debug("Unable to launch app on Roku, appId: {}, Exception: {}", command, e.getMessage());
335                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
336                 }
337             }
338         } else if (channelUID.getId().equals(ACTIVE_CHANNEL)) {
339             synchronized (sequenceLock) {
340                 try {
341                     communicator.launchTvChannel(command.toString());
342                 } catch (RokuHttpException e) {
343                     logger.debug("Unable to change channel on Roku TV, channelNumber: {}, Exception: {}", command,
344                             e.getMessage());
345                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
346                 }
347             }
348         } else if (POWER.equals(channelUID.getId())) {
349             synchronized (sequenceLock) {
350                 if (command instanceof OnOffType) {
351                     try {
352                         if (command.equals(OnOffType.ON)) {
353                             communicator.keyPress(POWER_ON);
354                         } else {
355                             communicator.keyPress("PowerOff");
356                         }
357                     } catch (RokuHttpException e) {
358                         logger.debug("Unable to send keypress to Roku, key: {}, Exception: {}", command,
359                                 e.getMessage());
360                         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
361                     }
362                 }
363             }
364         } else if (channelUID.getId().equals(CONTROL)) {
365             synchronized (sequenceLock) {
366                 try {
367                     if (command instanceof PlayPauseType) {
368                         communicator.keyPress(ROKU_PLAY_BUTTON);
369                     } else if (command instanceof NextPreviousType) {
370                         if (command == NextPreviousType.NEXT) {
371                             communicator.keyPress(ROKU_NEXT_BUTTON);
372                         } else if (command == NextPreviousType.PREVIOUS) {
373                             communicator.keyPress(ROKU_PREV_BUTTON);
374                         }
375                     } else {
376                         logger.warn("Unknown control command: {}", command);
377                     }
378                 } catch (RokuHttpException e) {
379                     logger.debug("Unable to send control cmd to Roku, cmd: {}, Exception: {}", command, e.getMessage());
380                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
381                 }
382             }
383         } else {
384             logger.debug("Unsupported command: {}", command);
385         }
386     }
387 }