2 * Copyright (c) 2010-2022 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.action;
15 import java.awt.image.BufferedImage;
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18 import java.io.OutputStream;
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;
28 import javax.imageio.ImageIO;
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;
48 import com.google.gson.JsonObject;
49 import com.google.gson.JsonParseException;
50 import com.google.gson.JsonParser;
53 * The {@link LGWebOSActions} defines the thing actions for the LGwebOS binding.
55 * @author Sebastian Prehn - Initial contribution
56 * @author Laurent Garnier - new method invokeMethodOf + interface ILGWebOSActions
58 @ThingActionsScope(name = "lgwebos")
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;
66 public void setThingHandler(@Nullable ThingHandler handler) {
67 this.handler = (LGWebOSHandler) handler;
71 public @Nullable ThingHandler getThingHandler() {
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.");
82 return lgWebOSHandler;
97 @RuleAction(label = "@text/actionShowToastLabel", description = "@text/actionShowToastDesc")
98 public void showToast(
99 @ActionInput(name = "text", label = "@text/actionShowToastInputTextLabel", description = "@text/actionShowToastInputTextDesc") String text)
101 getConnectedSocket().ifPresent(control -> control.showToast(text, createResponseListener()));
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)
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()));
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()));
123 private List<AppInfo> getAppInfos() {
124 LGWebOSHandler lgWebOSHandler = getLGWebOSHandler();
126 if (!this.getConnectedSocket().isPresent()) {
127 return Collections.emptyList();
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();
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()) {
145 .ifPresent(control -> control.launchAppWithInfo(appInfo.get(), createResponseListener()));
147 logger.warn("Device with ThingID {} does not support any app with id: {}.",
148 getLGWebOSHandler().getThing().getUID(), appId);
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) {
157 JsonObject payload = (JsonObject) JsonParser.parseString(params);
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()));
164 logger.warn("Device with ThingID {} does not support any app with id: {}.",
165 getLGWebOSHandler().getThing().getUID(), appId);
167 } catch (JsonParseException ex) {
168 logger.warn("Parameters value ({}) is not in a valid JSON format. {}", params, ex.getMessage());
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);
183 @RuleAction(label = "@text/actionSendButtonLabel", description = "@text/actionSendButtonDesc")
184 public void sendButton(
185 @ActionInput(name = "text", label = "@text/actionSendButtonInputButtonLabel", description = "@text/actionSendButtonInputButtonDesc") String button) {
187 switch (Button.valueOf(button)) {
189 getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.button(ButtonType.UP)));
192 getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.button(ButtonType.DOWN)));
195 getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.button(ButtonType.LEFT)));
198 getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.button(ButtonType.RIGHT)));
201 getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.button(ButtonType.BACK)));
204 getConnectedSocket().ifPresent(control -> control.sendDelete());
207 getConnectedSocket().ifPresent(control -> control.sendEnter());
210 getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.button("HOME")));
213 getConnectedSocket().ifPresent(control -> control.executeMouse(s -> s.click()));
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(", ")));
222 @RuleAction(label = "@text/actionIncreaseChannelLabel", description = "@text/actionIncreaseChannelDesc")
223 public void increaseChannel() {
224 getConnectedSocket().ifPresent(control -> control.channelUp(createResponseListener()));
227 @RuleAction(label = "@text/actionDecreaseChannelLabel", description = "@text/actionDecreaseChannelDesc")
228 public void decreaseChannel() {
229 getConnectedSocket().ifPresent(control -> control.channelDown(createResponseListener()));
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)));
238 private Optional<LGWebOSTVSocket> getConnectedSocket() {
239 LGWebOSHandler lgWebOSHandler = getLGWebOSHandler();
240 final LGWebOSTVSocket socket = lgWebOSHandler.getSocket();
242 if (socket.getState() != State.REGISTERED) {
243 logger.warn("Device with ThingID {} is currently not connected.", lgWebOSHandler.getThing().getUID());
244 return Optional.empty();
247 return Optional.of(socket);
250 private ResponseListener<TextInputStatusInfo> createTextInputStatusListener() {
251 return new ResponseListener<TextInputStatusInfo>() {
254 public void onError(@Nullable String error) {
255 logger.warn("Response: {}", error);
259 public void onSuccess(@Nullable TextInputStatusInfo info) {
260 logger.debug("Response: {}", info == null ? "OK" : info.getRawData());
265 private <O> ResponseListener<O> createResponseListener() {
266 return new ResponseListener<O>() {
269 public void onError(@Nullable String error) {
270 logger.warn("Response: {}", error);
274 public void onSuccess(@Nullable O object) {
275 logger.debug("Response: {}", object == null ? "OK" : object.toString());
280 // delegation methods for "legacy" rule support
282 public static void showToast(ThingActions actions, String text) throws IOException {
283 ((LGWebOSActions) actions).showToast(text);
286 public static void showToast(ThingActions actions, String icon, String text) throws IOException {
287 ((LGWebOSActions) actions).showToast(icon, text);
290 public static void launchBrowser(ThingActions actions, String url) {
291 ((LGWebOSActions) actions).launchBrowser(url);
294 public static void launchApplication(ThingActions actions, String appId) {
295 ((LGWebOSActions) actions).launchApplication(appId);
298 public static void launchApplication(ThingActions actions, String appId, String param) {
299 ((LGWebOSActions) actions).launchApplication(appId, param);
302 public static void sendText(ThingActions actions, String text) {
303 ((LGWebOSActions) actions).sendText(text);
306 public static void sendButton(ThingActions actions, String button) {
307 ((LGWebOSActions) actions).sendButton(button);
310 public static void increaseChannel(ThingActions actions) {
311 ((LGWebOSActions) actions).increaseChannel();
314 public static void decreaseChannel(ThingActions actions) {
315 ((LGWebOSActions) actions).decreaseChannel();
318 public static void sendRCButton(ThingActions actions, String rcButton) {
319 ((LGWebOSActions) actions).sendRCButton(rcButton);