]> git.basschouten.com Git - openhab-addons.git/blob
1f58669be7004983b26f8dbe65d86740dc754dd9
[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;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Optional;
20
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;
36
37 /**
38  * Provides ability to launch an application on the TV.
39  *
40  * @author Sebastian Prehn - Initial contribution
41  */
42 @NonNullByDefault
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();
47
48     @Override
49     public void onDeviceReady(String channelId, LGWebOSHandler handler) {
50         super.onDeviceReady(channelId, handler);
51
52         handler.getSocket().getAppList(new ResponseListener<List<AppInfo>>() {
53
54             @Override
55             public void onError(String error) {
56                 logger.warn("Error requesting application list: {}.", error);
57             }
58
59             @Override
60             @NonNullByDefault({})
61             public void onSuccess(List<AppInfo> appInfos) {
62                 if (logger.isDebugEnabled()) {
63                     for (AppInfo a : appInfos) {
64                         logger.debug("AppInfo {} - {}", a.getId(), a.getName());
65                     }
66                 }
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()));
71                 }
72                 handler.setOptions(channelId, options);
73             }
74         });
75     }
76
77     @Override
78     public void onDeviceRemoved(String channelId, LGWebOSHandler handler) {
79         super.onDeviceRemoved(channelId, handler);
80         applicationListCache.remove(handler.getThing().getUID());
81     }
82
83     @Override
84     public void onReceiveCommand(String channelId, LGWebOSHandler handler, Command command) {
85         if (RefreshType.REFRESH == command) {
86             handler.getSocket().getRunningApp(createResponseListener(channelId, handler));
87             return;
88         }
89
90         final String value = command.toString();
91
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());
96         } else {
97             Optional<AppInfo> appInfo = appInfos.stream().filter(a -> a.getId().equals(value)).findFirst();
98             if (appInfo.isPresent()) {
99                 handler.getSocket().launchAppWithInfo(appInfo.get(), launchSessionResponseListener);
100             } else {
101                 logger.warn("TV does not support any app with id: {}.", value);
102             }
103         }
104     }
105
106     @Override
107     protected Optional<ServiceSubscription<AppInfo>> getSubscription(String channelId, LGWebOSHandler handler) {
108         return Optional.of(handler.getSocket().subscribeRunningApp(createResponseListener(channelId, handler)));
109     }
110
111     private ResponseListener<AppInfo> createResponseListener(String channelId, LGWebOSHandler handler) {
112         return new ResponseListener<AppInfo>() {
113
114             @Override
115             public void onError(@Nullable String error) {
116                 logger.debug("Error in retrieving application: {}.", error);
117             }
118
119             @Override
120             public void onSuccess(@Nullable AppInfo appInfo) {
121                 if (appInfo == null || appInfo.getId().isEmpty()) {
122                     handler.postUpdate(channelId, UnDefType.UNDEF);
123                 } else {
124                     handler.postUpdate(channelId, new StringType(appInfo.getId()));
125                 }
126             }
127         };
128     }
129
130     public @Nullable List<AppInfo> getAppInfos(ThingUID key) {
131         return applicationListCache.get(key);
132     }
133
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());
140             }
141         }
142         return report;
143     }
144 }