2 * Copyright (c) 2010-2024 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.lgwebos.internal;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.lgwebos.internal.handler.LGWebOSHandler;
24 import org.openhab.binding.lgwebos.internal.handler.command.ServiceSubscription;
25 import org.openhab.binding.lgwebos.internal.handler.core.AppInfo;
26 import org.openhab.binding.lgwebos.internal.handler.core.LaunchSession;
27 import org.openhab.binding.lgwebos.internal.handler.core.ResponseListener;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.RefreshType;
32 import org.openhab.core.types.StateOption;
33 import org.openhab.core.types.UnDefType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Provides ability to launch an application on the TV.
40 * @author Sebastian Prehn - Initial contribution
43 public class LauncherApplication extends BaseChannelHandler<AppInfo> {
44 private final Logger logger = LoggerFactory.getLogger(LauncherApplication.class);
45 private final Map<ThingUID, List<AppInfo>> applicationListCache = new HashMap<>();
46 private final ResponseListener<LaunchSession> launchSessionResponseListener = createResponseListener();
49 public void onDeviceReady(String channelId, LGWebOSHandler handler) {
50 super.onDeviceReady(channelId, handler);
52 handler.getSocket().getAppList(new ResponseListener<List<AppInfo>>() {
55 public void onError(String error) {
56 logger.warn("Error requesting application list: {}.", error);
61 public void onSuccess(List<AppInfo> appInfos) {
62 if (logger.isDebugEnabled()) {
63 for (AppInfo a : appInfos) {
64 logger.debug("AppInfo {} - {}", a.getId(), a.getName());
67 applicationListCache.put(handler.getThing().getUID(), appInfos);
68 List<StateOption> options = new ArrayList<>();
69 for (AppInfo appInfo : appInfos) {
70 options.add(new StateOption(appInfo.getId(), appInfo.getName()));
72 handler.setOptions(channelId, options);
78 public void onDeviceRemoved(String channelId, LGWebOSHandler handler) {
79 super.onDeviceRemoved(channelId, handler);
80 applicationListCache.remove(handler.getThing().getUID());
84 public void onReceiveCommand(String channelId, LGWebOSHandler handler, Command command) {
85 if (RefreshType.REFRESH == command) {
86 handler.getSocket().getRunningApp(createResponseListener(channelId, handler));
90 final String value = command.toString();
92 List<AppInfo> appInfos = applicationListCache.get(handler.getThing().getUID());
93 if (appInfos == null) {
94 logger.warn("No application list cached for this device {}, ignoring command.",
95 handler.getThing().getUID());
97 Optional<AppInfo> appInfo = appInfos.stream().filter(a -> a.getId().equals(value)).findFirst();
98 if (appInfo.isPresent()) {
99 handler.getSocket().launchAppWithInfo(appInfo.get(), launchSessionResponseListener);
101 logger.warn("TV does not support any app with id: {}.", value);
107 protected Optional<ServiceSubscription<AppInfo>> getSubscription(String channelId, LGWebOSHandler handler) {
108 return Optional.of(handler.getSocket().subscribeRunningApp(createResponseListener(channelId, handler)));
111 private ResponseListener<AppInfo> createResponseListener(String channelId, LGWebOSHandler handler) {
112 return new ResponseListener<AppInfo>() {
115 public void onError(@Nullable String error) {
116 logger.debug("Error in retrieving application: {}.", error);
120 public void onSuccess(@Nullable AppInfo appInfo) {
121 if (appInfo == null || appInfo.getId().isEmpty()) {
122 handler.postUpdate(channelId, UnDefType.UNDEF);
124 handler.postUpdate(channelId, new StringType(appInfo.getId()));
130 public @Nullable List<AppInfo> getAppInfos(ThingUID key) {
131 return applicationListCache.get(key);
134 public List<String> reportApplications(ThingUID thingUID) {
135 List<String> report = new ArrayList<>();
136 List<AppInfo> appInfos = applicationListCache.get(thingUID);
137 if (appInfos != null) {
138 for (AppInfo a : appInfos) {
139 report.add(a.getId() + " : " + a.getName());