]> git.basschouten.com Git - openhab-addons.git/blob
d0041b327edd7eb4d945c9b2e5d19a55254ab0fa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.action;
14
15 import java.awt.image.BufferedImage;
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18 import java.io.OutputStream;
19 import java.net.URL;
20 import java.nio.charset.StandardCharsets;
21 import java.util.Base64;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.stream.Collectors;
26 import java.util.stream.Stream;
27
28 import javax.imageio.ImageIO;
29
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.openhab.binding.lgwebos.internal.handler.LGWebOSHandler;
33 import org.openhab.binding.lgwebos.internal.handler.LGWebOSTVMouseSocket.ButtonType;
34 import org.openhab.binding.lgwebos.internal.handler.LGWebOSTVSocket;
35 import org.openhab.binding.lgwebos.internal.handler.LGWebOSTVSocket.State;
36 import org.openhab.binding.lgwebos.internal.handler.command.ServiceSubscription;
37 import org.openhab.binding.lgwebos.internal.handler.core.AppInfo;
38 import org.openhab.binding.lgwebos.internal.handler.core.ResponseListener;
39 import org.openhab.binding.lgwebos.internal.handler.core.TextInputStatusInfo;
40 import org.openhab.core.automation.annotation.ActionInput;
41 import org.openhab.core.automation.annotation.RuleAction;
42 import org.openhab.core.thing.binding.ThingActions;
43 import org.openhab.core.thing.binding.ThingActionsScope;
44 import org.openhab.core.thing.binding.ThingHandler;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import com.google.gson.JsonObject;
49 import com.google.gson.JsonParseException;
50 import com.google.gson.JsonParser;
51
52 /**
53  * The {@link LGWebOSActions} defines the thing actions for the LGwebOS binding.
54  *
55  * @author Sebastian Prehn - Initial contribution
56  * @author Laurent Garnier - new method invokeMethodOf + interface ILGWebOSActions
57  */
58 @ThingActionsScope(name = "lgwebos")
59 @NonNullByDefault
60 public class LGWebOSActions implements ThingActions {
61     private final Logger logger = LoggerFactory.getLogger(LGWebOSActions.class);
62     private final ResponseListener<TextInputStatusInfo> textInputListener = createTextInputStatusListener();
63     private @Nullable LGWebOSHandler handler;
64
65     @Override
66     public void setThingHandler(@Nullable ThingHandler handler) {
67         this.handler = (LGWebOSHandler) handler;
68     }
69
70     @Override
71     public @Nullable ThingHandler getThingHandler() {
72         return handler;
73     }
74
75     // a NonNull getter for handler
76     private LGWebOSHandler getLGWebOSHandler() {
77         LGWebOSHandler lgWebOSHandler = this.handler;
78         if (lgWebOSHandler == null) {
79             throw new IllegalStateException(
80                     "ThingHandler must be set before any action may be invoked on LGWebOSActions.");
81         }
82         return lgWebOSHandler;
83     }
84
85     private enum Button {
86         UP,
87         DOWN,
88         LEFT,
89         RIGHT,
90         BACK,
91         DELETE,
92         ENTER,
93         HOME,
94         OK
95     }
96
97     @RuleAction(label = "@text/actionShowToastLabel", description = "@text/actionShowToastDesc")
98     public void showToast(
99             @ActionInput(name = "text", label = "@text/actionShowToastInputTextLabel", description = "@text/actionShowToastInputTextDesc") String text)
100             throws IOException {
101         getConnectedSocket().ifPresent(control -> control.showToast(text, createResponseListener()));
102     }
103
104     @RuleAction(label = "@text/actionShowToastWithIconLabel", description = "@text/actionShowToastWithIconDesc")
105     public void showToast(
106             @ActionInput(name = "icon", label = "@text/actionShowToastInputIconLabel", description = "@text/actionShowToastInputIconDesc") String icon,
107             @ActionInput(name = "text", label = "@text/actionShowToastInputTextLabel", description = "@text/actionShowToastInputTextDesc") String text)
108             throws IOException {
109         BufferedImage bi = ImageIO.read(new URL(icon));
110         try (ByteArrayOutputStream os = new ByteArrayOutputStream(); OutputStream b64 = Base64.getEncoder().wrap(os)) {
111             ImageIO.write(bi, "png", b64);
112             String string = os.toString(StandardCharsets.UTF_8.name());
113             getConnectedSocket().ifPresent(control -> control.showToast(text, string, "png", createResponseListener()));
114         }
115     }
116
117     @RuleAction(label = "@text/actionLaunchBrowserLabel", description = "@text/actionLaunchBrowserDesc")
118     public void launchBrowser(
119             @ActionInput(name = "url", label = "@text/actionLaunchBrowserInputUrlLabel", description = "@text/actionLaunchBrowserInputUrlDesc") String url) {
120         getConnectedSocket().ifPresent(control -> control.launchBrowser(url, createResponseListener()));
121     }
122
123     private List<AppInfo> getAppInfos() {
124         LGWebOSHandler lgWebOSHandler = getLGWebOSHandler();
125
126         if (!this.getConnectedSocket().isPresent()) {
127             return Collections.emptyList();
128         }
129
130         List<AppInfo> appInfos = lgWebOSHandler.getLauncherApplication()
131                 .getAppInfos(lgWebOSHandler.getThing().getUID());
132         if (appInfos == null) {
133             logger.warn("No AppInfos found for device with ThingID {}.", lgWebOSHandler.getThing().getUID());
134             return Collections.emptyList();
135         }
136         return appInfos;
137     }
138
139     @RuleAction(label = "@text/actionLaunchApplicationLabel", description = "@text/actionLaunchApplicationDesc")
140     public void launchApplication(
141             @ActionInput(name = "appId", label = "@text/actionLaunchApplicationInputAppIDLabel", description = "@text/actionLaunchApplicationInputAppIDDesc") String appId) {
142         Optional<AppInfo> appInfo = getAppInfos().stream().filter(a -> a.getId().equals(appId)).findFirst();
143         if (appInfo.isPresent()) {
144             getConnectedSocket()
145                     .ifPresent(control -> control.launchAppWithInfo(appInfo.get(), createResponseListener()));
146         } else {
147             logger.warn("Device with ThingID {} does not support any app with id: {}.",
148                     getLGWebOSHandler().getThing().getUID(), appId);
149         }
150     }
151
152     @RuleAction(label = "@text/actionLaunchApplicationWithParamsLabel", description = "@text/actionLaunchApplicationWithParamsDesc")
153     public void launchApplication(
154             @ActionInput(name = "appId", label = "@text/actionLaunchApplicationInputAppIDLabel", description = "@text/actionLaunchApplicationInputAppIDDesc") String appId,
155             @ActionInput(name = "params", label = "@text/actionLaunchApplicationInputParamsLabel", description = "@text/actionLaunchApplicationInputParamsDesc") String params) {
156         try {
157             JsonObject payload = (JsonObject) JsonParser.parseString(params);
158
159             Optional<AppInfo> appInfo = getAppInfos().stream().filter(a -> a.getId().equals(appId)).findFirst();
160             if (appInfo.isPresent()) {
161                 getConnectedSocket().ifPresent(
162                         control -> control.launchAppWithInfo(appInfo.get(), payload, createResponseListener()));
163             } else {
164                 logger.warn("Device with ThingID {} does not support any app with id: {}.",
165                         getLGWebOSHandler().getThing().getUID(), appId);
166             }
167         } catch (JsonParseException ex) {
168             logger.warn("Parameters value ({}) is not in a valid JSON format. {}", params, ex.getMessage());
169             return;
170         }
171     }
172
173     @RuleAction(label = "@text/actionSendTextLabel", description = "@text/actionSendTextDesc")
174     public void sendText(
175             @ActionInput(name = "text", label = "@text/actionSendTextInputTextLabel", description = "@text/actionSendTextInputTextDesc") String text) {
176         getConnectedSocket().ifPresent(control -> {
177             ServiceSubscription<TextInputStatusInfo> subscription = control.subscribeTextInputStatus(textInputListener);
178             control.sendText(text);
179             control.unsubscribe(subscription);
180         });
181     }
182
183     @RuleAction(label = "@text/actionSendButtonLabel", description = "@text/actionSendButtonDesc")
184     public void sendButton(
185             @ActionInput(name = "text", label = "@text/actionSendButtonInputButtonLabel", description = "@text/actionSendButtonInputButtonDesc") String button) {
186         try {
187             switch (Button.valueOf(button)) {
188                 case UP:
189                     getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.button(ButtonType.UP)));
190                     break;
191                 case DOWN:
192                     getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.button(ButtonType.DOWN)));
193                     break;
194                 case LEFT:
195                     getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.button(ButtonType.LEFT)));
196                     break;
197                 case RIGHT:
198                     getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.button(ButtonType.RIGHT)));
199                     break;
200                 case BACK:
201                     getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.button(ButtonType.BACK)));
202                     break;
203                 case DELETE:
204                     getConnectedSocket().ifPresent(control -> control.sendDelete());
205                     break;
206                 case ENTER:
207                     getConnectedSocket().ifPresent(control -> control.sendEnter());
208                     break;
209                 case HOME:
210                     getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.button("HOME")));
211                     break;
212                 case OK:
213                     getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.click()));
214                     break;
215             }
216         } catch (IllegalArgumentException ex) {
217             logger.warn("{} is not a valid value for button - available are: {}", button,
218                     Stream.of(Button.values()).map(b -> b.name()).collect(Collectors.joining(", ")));
219         }
220     }
221
222     @RuleAction(label = "@text/actionIncreaseChannelLabel", description = "@text/actionIncreaseChannelDesc")
223     public void increaseChannel() {
224         getConnectedSocket().ifPresent(control -> control.channelUp(createResponseListener()));
225     }
226
227     @RuleAction(label = "@text/actionDecreaseChannelLabel", description = "@text/actionDecreaseChannelDesc")
228     public void decreaseChannel() {
229         getConnectedSocket().ifPresent(control -> control.channelDown(createResponseListener()));
230     }
231
232     @RuleAction(label = "@text/actionSendRCButtonLabel", description = "@text/actionSendRCButtonDesc")
233     public void sendRCButton(
234             @ActionInput(name = "text", label = "@text/actionSendRCButtonInputTextLabel", description = "@text/actionSendRCButtonInputTextDesc") String rcButton) {
235         getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.button(rcButton)));
236     }
237
238     private Optional<LGWebOSTVSocket> getConnectedSocket() {
239         LGWebOSHandler lgWebOSHandler = getLGWebOSHandler();
240         final LGWebOSTVSocket socket = lgWebOSHandler.getSocket();
241
242         if (socket.getState() != State.REGISTERED) {
243             logger.warn("Device with ThingID {} is currently not connected.", lgWebOSHandler.getThing().getUID());
244             return Optional.empty();
245         }
246
247         return Optional.of(socket);
248     }
249
250     private ResponseListener<TextInputStatusInfo> createTextInputStatusListener() {
251         return new ResponseListener<TextInputStatusInfo>() {
252
253             @Override
254             public void onError(@Nullable String error) {
255                 logger.warn("Response: {}", error);
256             }
257
258             @Override
259             public void onSuccess(@Nullable TextInputStatusInfo info) {
260                 logger.debug("Response: {}", info == null ? "OK" : info.getRawData());
261             }
262         };
263     }
264
265     private <O> ResponseListener<O> createResponseListener() {
266         return new ResponseListener<O>() {
267
268             @Override
269             public void onError(@Nullable String error) {
270                 logger.warn("Response: {}", error);
271             }
272
273             @Override
274             public void onSuccess(@Nullable O object) {
275                 logger.debug("Response: {}", object == null ? "OK" : object.toString());
276             }
277         };
278     }
279
280     // delegation methods for "legacy" rule support
281
282     public static void showToast(ThingActions actions, String text) throws IOException {
283         ((LGWebOSActions) actions).showToast(text);
284     }
285
286     public static void showToast(ThingActions actions, String icon, String text) throws IOException {
287         ((LGWebOSActions) actions).showToast(icon, text);
288     }
289
290     public static void launchBrowser(ThingActions actions, String url) {
291         ((LGWebOSActions) actions).launchBrowser(url);
292     }
293
294     public static void launchApplication(ThingActions actions, String appId) {
295         ((LGWebOSActions) actions).launchApplication(appId);
296     }
297
298     public static void launchApplication(ThingActions actions, String appId, String param) {
299         ((LGWebOSActions) actions).launchApplication(appId, param);
300     }
301
302     public static void sendText(ThingActions actions, String text) {
303         ((LGWebOSActions) actions).sendText(text);
304     }
305
306     public static void sendButton(ThingActions actions, String button) {
307         ((LGWebOSActions) actions).sendButton(button);
308     }
309
310     public static void increaseChannel(ThingActions actions) {
311         ((LGWebOSActions) actions).increaseChannel();
312     }
313
314     public static void decreaseChannel(ThingActions actions) {
315         ((LGWebOSActions) actions).decreaseChannel();
316     }
317
318     public static void sendRCButton(ThingActions actions, String rcButton) {
319         ((LGWebOSActions) actions).sendRCButton(rcButton);
320     }
321 }