]> git.basschouten.com Git - openhab-addons.git/blob
65ae982f1efa5f27756aed54166e0dde3ef6011a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.lgwebos.internal.handler;
14
15 import static org.openhab.binding.lgwebos.internal.LGWebOSBindingConstants.*;
16
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.concurrent.ScheduledFuture;
26 import java.util.concurrent.TimeUnit;
27
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.eclipse.jetty.websocket.client.WebSocketClient;
31 import org.openhab.binding.lgwebos.internal.ChannelHandler;
32 import org.openhab.binding.lgwebos.internal.LGWebOSBindingConstants;
33 import org.openhab.binding.lgwebos.internal.LGWebOSStateDescriptionOptionProvider;
34 import org.openhab.binding.lgwebos.internal.LauncherApplication;
35 import org.openhab.binding.lgwebos.internal.MediaControlPlayer;
36 import org.openhab.binding.lgwebos.internal.MediaControlStop;
37 import org.openhab.binding.lgwebos.internal.PowerControlPower;
38 import org.openhab.binding.lgwebos.internal.RCButtonControl;
39 import org.openhab.binding.lgwebos.internal.TVControlChannel;
40 import org.openhab.binding.lgwebos.internal.ToastControlToast;
41 import org.openhab.binding.lgwebos.internal.VolumeControlMute;
42 import org.openhab.binding.lgwebos.internal.VolumeControlVolume;
43 import org.openhab.binding.lgwebos.internal.WakeOnLanUtility;
44 import org.openhab.binding.lgwebos.internal.action.LGWebOSActions;
45 import org.openhab.binding.lgwebos.internal.handler.LGWebOSTVSocket.WebOSTVSocketListener;
46 import org.openhab.binding.lgwebos.internal.handler.core.AppInfo;
47 import org.openhab.binding.lgwebos.internal.handler.core.ResponseListener;
48 import org.openhab.core.config.core.Configuration;
49 import org.openhab.core.library.types.OnOffType;
50 import org.openhab.core.thing.ChannelUID;
51 import org.openhab.core.thing.Thing;
52 import org.openhab.core.thing.ThingStatus;
53 import org.openhab.core.thing.ThingStatusDetail;
54 import org.openhab.core.thing.binding.BaseThingHandler;
55 import org.openhab.core.thing.binding.ThingHandlerService;
56 import org.openhab.core.types.Command;
57 import org.openhab.core.types.State;
58 import org.openhab.core.types.StateOption;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 /**
63  * The {@link LGWebOSHandler} is responsible for handling commands, which are
64  * sent to one of the channels.
65  *
66  * @author Sebastian Prehn - initial contribution
67  */
68 @NonNullByDefault
69 public class LGWebOSHandler extends BaseThingHandler
70         implements LGWebOSTVSocket.ConfigProvider, WebOSTVSocketListener, PowerControlPower.ConfigProvider {
71
72     /*
73      * constants for device polling
74      */
75     private static final int RECONNECT_INTERVAL_SECONDS = 10;
76     private static final int RECONNECT_START_UP_DELAY_SECONDS = 0;
77     private static final int CHANNEL_SUBSCRIPTION_DELAY_SECONDS = 1;
78     private static final String APP_ID_LIVETV = "com.webos.app.livetv";
79
80     private final Logger logger = LoggerFactory.getLogger(LGWebOSHandler.class);
81
82     // ChannelID to CommandHandler Map
83     private final Map<String, ChannelHandler> channelHandlers;
84
85     private final LauncherApplication appLauncher = new LauncherApplication();
86
87     private final WebSocketClient webSocketClient;
88
89     private final LGWebOSStateDescriptionOptionProvider stateDescriptionProvider;
90
91     private @Nullable LGWebOSTVSocket socket;
92
93     private @Nullable ScheduledFuture<?> reconnectJob;
94     private @Nullable ScheduledFuture<?> keepAliveJob;
95     private @Nullable ScheduledFuture<?> channelSubscriptionJob;
96
97     private @Nullable LGWebOSConfiguration config;
98
99     public LGWebOSHandler(Thing thing, WebSocketClient webSocketClient,
100             LGWebOSStateDescriptionOptionProvider stateDescriptionProvider) {
101         super(thing);
102         this.webSocketClient = webSocketClient;
103         this.stateDescriptionProvider = stateDescriptionProvider;
104
105         Map<String, ChannelHandler> handlers = new HashMap<>();
106         handlers.put(CHANNEL_VOLUME, new VolumeControlVolume());
107         handlers.put(CHANNEL_POWER, new PowerControlPower(this, scheduler));
108         handlers.put(CHANNEL_MUTE, new VolumeControlMute());
109         handlers.put(CHANNEL_CHANNEL, new TVControlChannel());
110         handlers.put(CHANNEL_APP_LAUNCHER, appLauncher);
111         handlers.put(CHANNEL_MEDIA_STOP, new MediaControlStop());
112         handlers.put(CHANNEL_TOAST, new ToastControlToast());
113         handlers.put(CHANNEL_MEDIA_PLAYER, new MediaControlPlayer());
114         handlers.put(CHANNEL_RCBUTTON, new RCButtonControl());
115         channelHandlers = Collections.unmodifiableMap(handlers);
116     }
117
118     private LGWebOSConfiguration getLGWebOSConfig() {
119         LGWebOSConfiguration c = config;
120         if (c == null) {
121             c = getConfigAs(LGWebOSConfiguration.class);
122             config = c;
123         }
124         return c;
125     }
126
127     @Override
128     public void initialize() {
129         logger.debug("Initializing handler for thing {}", getThing().getUID());
130         LGWebOSConfiguration c = getLGWebOSConfig();
131         logger.trace("Handler initialized with config {}", c);
132         String host = c.getHost();
133         if (host.isEmpty()) {
134             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
135                     "@text/offline.config-error-unknown-host");
136             return;
137         }
138
139         LGWebOSTVSocket s = new LGWebOSTVSocket(webSocketClient, this, host, c.getUseTLS(), scheduler);
140         s.setListener(this);
141         socket = s;
142
143         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/offline.tv-off");
144
145         startReconnectJob();
146     }
147
148     @Override
149     public void dispose() {
150         logger.debug("Disposing handler for thing {}", getThing().getUID());
151         stopKeepAliveJob();
152         stopReconnectJob();
153         stopChannelSubscriptionJob();
154
155         LGWebOSTVSocket s = socket;
156         if (s != null) {
157             s.setListener(null);
158             s.disconnect();
159         }
160         socket = null;
161         config = null; // ensure config gets actually refreshed during re-initialization
162         super.dispose();
163     }
164
165     private void startReconnectJob() {
166         ScheduledFuture<?> job = reconnectJob;
167         if (job == null || job.isCancelled()) {
168             reconnectJob = scheduler.scheduleWithFixedDelay(() -> {
169                 getSocket().disconnect();
170                 getSocket().connect();
171             }, RECONNECT_START_UP_DELAY_SECONDS, RECONNECT_INTERVAL_SECONDS, TimeUnit.SECONDS);
172         }
173     }
174
175     private void stopReconnectJob() {
176         ScheduledFuture<?> job = reconnectJob;
177         if (job != null && !job.isCancelled()) {
178             job.cancel(true);
179         }
180         reconnectJob = null;
181     }
182
183     /**
184      * Keep alive ensures that the web socket connection is used and does not time out.
185      */
186     private void startKeepAliveJob() {
187         ScheduledFuture<?> job = keepAliveJob;
188         if (job == null || job.isCancelled()) {
189             // half of idle time out setting
190             long keepAliveInterval = this.webSocketClient.getMaxIdleTimeout() / 2;
191
192             // it is irrelevant which service is queried. Only need to send some packets over the wire
193
194             keepAliveJob = scheduler.scheduleWithFixedDelay(() -> getSocket().getRunningApp(new ResponseListener<>() {
195
196                 @Override
197                 public void onSuccess(AppInfo responseObject) {
198                     // ignore - actual response is not relevant here
199                 }
200
201                 @Override
202                 public void onError(String message) {
203                     // ignore
204                 }
205             }), keepAliveInterval, keepAliveInterval, TimeUnit.MILLISECONDS);
206
207         }
208     }
209
210     private void stopKeepAliveJob() {
211         ScheduledFuture<?> job = keepAliveJob;
212         if (job != null && !job.isCancelled()) {
213             job.cancel(true);
214         }
215         keepAliveJob = null;
216     }
217
218     public LGWebOSTVSocket getSocket() {
219         LGWebOSTVSocket s = this.socket;
220         if (s == null) {
221             throw new IllegalStateException("Component called before it was initialized or already disposed.");
222         }
223         return s;
224     }
225
226     public LauncherApplication getLauncherApplication() {
227         return appLauncher;
228     }
229
230     @Override
231     public void handleCommand(ChannelUID channelUID, Command command) {
232         logger.debug("handleCommand({},{})", channelUID, command);
233         ChannelHandler handler = channelHandlers.get(channelUID.getId());
234         if (handler == null) {
235             logger.warn(
236                     "Unable to handle command {}. No handler found for channel {}. This must not happen. Please report as a bug.",
237                     command, channelUID);
238             return;
239         }
240
241         handler.onReceiveCommand(channelUID.getId(), this, command);
242     }
243
244     @Override
245     public String getMacAddress() {
246         return getLGWebOSConfig().getMacAddress();
247     }
248
249     @Override
250     public String getKey() {
251         return getLGWebOSConfig().getKey();
252     }
253
254     @Override
255     public void storeKey(@Nullable String key) {
256         if (!getKey().equals(key)) {
257             logger.debug("Store new access Key in the thing configuration");
258             // store it current configuration and avoiding complete re-initialization via handleConfigurationUpdate
259             getLGWebOSConfig().key = key;
260
261             // persist the configuration change
262             Configuration configuration = editConfiguration();
263             configuration.put(LGWebOSBindingConstants.CONFIG_KEY, key);
264             updateConfiguration(configuration);
265         }
266     }
267
268     @Override
269     public void storeProperties(Map<String, String> properties) {
270         logger.debug("storeProperties {}", properties);
271         Map<String, String> map = editProperties();
272         map.putAll(properties);
273         updateProperties(map);
274     }
275
276     @Override
277     public void onStateChanged(LGWebOSTVSocket.State state) {
278         switch (state) {
279             case DISCONNECTING:
280                 postUpdate(CHANNEL_POWER, OnOffType.OFF);
281                 break;
282             case DISCONNECTED:
283                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/offline.tv-off");
284                 channelHandlers.forEach((k, v) -> {
285                     v.onDeviceRemoved(k, this);
286                     v.removeAnySubscription(this);
287                 });
288
289                 stopKeepAliveJob();
290                 startReconnectJob();
291                 break;
292             case CONNECTING:
293                 stopReconnectJob();
294                 break;
295             case REGISTERING:
296                 updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "@text/online.registering");
297                 findMacAddress();
298                 break;
299             case REGISTERED:
300                 startKeepAliveJob();
301                 updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "@text/online.connected");
302
303                 channelHandlers.forEach((k, v) -> {
304                     // refresh subscriptions except on channel, which can only be subscribe in livetv app. see
305                     // postUpdate method
306                     if (!CHANNEL_CHANNEL.equals(k)) {
307                         v.refreshSubscription(k, this);
308                     }
309                     v.onDeviceReady(k, this);
310                 });
311
312                 break;
313
314         }
315     }
316
317     @Override
318     public void onError(String error) {
319         logger.debug("Connection failed - error: {}", error);
320
321         switch (getSocket().getState()) {
322             case DISCONNECTING:
323             case DISCONNECTED:
324                 break;
325             case CONNECTING:
326             case REGISTERING:
327             case REGISTERED:
328                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
329                         String.format("@text/offline.comm-error-connexion-failed [ \"%s\" ]", error));
330                 break;
331         }
332     }
333
334     public void setOptions(String channelId, List<StateOption> options) {
335         logger.debug("setOptions channelId={} options.size()={}", channelId, options.size());
336         stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), channelId), options);
337     }
338
339     public void postUpdate(String channelId, State state) {
340         if (isLinked(channelId)) {
341             updateState(channelId, state);
342         }
343
344         // channel subscription only works when livetv app is started,
345         // therefore we need to slightly delay the subscription
346         if (CHANNEL_APP_LAUNCHER.equals(channelId)) {
347             if (APP_ID_LIVETV.equals(state.toString())) {
348                 scheduleChannelSubscriptionJob();
349             } else {
350                 stopChannelSubscriptionJob();
351             }
352         }
353     }
354
355     private void scheduleChannelSubscriptionJob() {
356         ScheduledFuture<?> job = channelSubscriptionJob;
357         if (job == null || job.isCancelled()) {
358             logger.debug("Schedule channel subscription job");
359             channelSubscriptionJob = scheduler.schedule(
360                     () -> channelHandlers.get(CHANNEL_CHANNEL).refreshSubscription(CHANNEL_CHANNEL, this),
361                     CHANNEL_SUBSCRIPTION_DELAY_SECONDS, TimeUnit.SECONDS);
362         }
363     }
364
365     private void stopChannelSubscriptionJob() {
366         ScheduledFuture<?> job = channelSubscriptionJob;
367         if (job != null && !job.isCancelled()) {
368             logger.debug("Stop channel subscription job");
369             job.cancel(true);
370         }
371         channelSubscriptionJob = null;
372     }
373
374     @Override
375     public Collection<Class<? extends ThingHandlerService>> getServices() {
376         return Set.of(LGWebOSActions.class);
377     }
378
379     /**
380      * Make a best effort to automatically detect the MAC address of the TV.
381      * If this does not work automatically, users can still set it manually in the Thing config.
382      */
383     private void findMacAddress() {
384         LGWebOSConfiguration c = getLGWebOSConfig();
385         String host = c.getHost();
386         if (!host.isEmpty()) {
387             try {
388                 // validate host, so that no command can be injected
389                 String macAddress = WakeOnLanUtility.getMACAddress(InetAddress.getByName(host).getHostAddress());
390                 if (macAddress != null && !macAddress.equals(c.macAddress)) {
391                     c.macAddress = macAddress;
392                     // persist the configuration change
393                     Configuration configuration = editConfiguration();
394                     configuration.put(LGWebOSBindingConstants.CONFIG_MAC_ADDRESS, macAddress);
395                     updateConfiguration(configuration);
396                 }
397             } catch (UnknownHostException e) {
398                 logger.debug("Unable to determine MAC address: {}", e.getMessage());
399             }
400         }
401     }
402
403     public List<String> reportApplications() {
404         return appLauncher.reportApplications(getThing().getUID());
405     }
406
407     public List<String> reportChannels() {
408         return ((TVControlChannel) channelHandlers.get(CHANNEL_CHANNEL)).reportChannels(getThing().getUID());
409     }
410 }