Rework ThingActions (#8732)

* Remove proxy workarounds
* Move ThingActions and a few other classes into the internal package
* Use more consistent action labels/descriptions

Related to:

* openhab/openhab-core#1714
* openhab/openhab-core#1639

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born
2020-10-14 19:01:12 +02:00
committed by GitHub
parent 4adc214c69
commit bef1046258
86 changed files with 708 additions and 2096 deletions

View File

@@ -1,46 +0,0 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.lgwebos.action;
import java.io.IOException;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* The {@link ILGWebOSActions} defines the interface for all thing actions supported by the binding.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface ILGWebOSActions {
public void showToast(String text) throws IOException;
public void showToast(String icon, String text) throws IOException;
public void launchBrowser(String url);
public void launchApplication(String appId);
public void launchApplication(String appId, String params);
public void sendText(String text);
public void sendButton(String button);
public void increaseChannel();
public void decreaseChannel();
public void sendRCButton(String rcButton);
}

View File

@@ -10,14 +10,12 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.lgwebos.action;
package org.openhab.binding.lgwebos.internal.action;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
@@ -53,18 +51,13 @@ import com.google.gson.JsonParser;
/**
* The {@link LGWebOSActions} defines the thing actions for the LGwebOS binding.
* <p>
* <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
* the test <i>actions instanceof LGWebOSActions</i> fails. This test can fail
* due to an issue in openHAB core v2.5.0 where the {@link LGWebOSActions} class
* can be loaded by a different classloader than the <i>actions</i> instance.
*
* @author Sebastian Prehn - Initial contribution
* @author Laurent Garnier - new method invokeMethodOf + interface ILGWebOSActions
*/
@ThingActionsScope(name = "lgwebos")
@NonNullByDefault
public class LGWebOSActions implements ThingActions, ILGWebOSActions {
public class LGWebOSActions implements ThingActions {
private final Logger logger = LoggerFactory.getLogger(LGWebOSActions.class);
private final ResponseListener<TextInputStatusInfo> textInputListener = createTextInputStatusListener();
private @Nullable LGWebOSHandler handler;
@@ -76,7 +69,7 @@ public class LGWebOSActions implements ThingActions, ILGWebOSActions {
@Override
public @Nullable ThingHandler getThingHandler() {
return this.handler;
return handler;
}
// a NonNull getter for handler
@@ -101,7 +94,6 @@ public class LGWebOSActions implements ThingActions, ILGWebOSActions {
OK
}
@Override
@RuleAction(label = "@text/actionShowToastLabel", description = "@text/actionShowToastDesc")
public void showToast(
@ActionInput(name = "text", label = "@text/actionShowToastInputTextLabel", description = "@text/actionShowToastInputTextDesc") String text)
@@ -109,7 +101,6 @@ public class LGWebOSActions implements ThingActions, ILGWebOSActions {
getConnectedSocket().ifPresent(control -> control.showToast(text, createResponseListener()));
}
@Override
@RuleAction(label = "@text/actionShowToastWithIconLabel", description = "@text/actionShowToastWithIconLabel")
public void showToast(
@ActionInput(name = "icon", label = "@text/actionShowToastInputIconLabel", description = "@text/actionShowToastInputIconDesc") String icon,
@@ -123,7 +114,6 @@ public class LGWebOSActions implements ThingActions, ILGWebOSActions {
}
}
@Override
@RuleAction(label = "@text/actionLaunchBrowserLabel", description = "@text/actionLaunchBrowserDesc")
public void launchBrowser(
@ActionInput(name = "url", label = "@text/actionLaunchBrowserInputUrlLabel", description = "@text/actionLaunchBrowserInputUrlDesc") String url) {
@@ -146,7 +136,6 @@ public class LGWebOSActions implements ThingActions, ILGWebOSActions {
return appInfos;
}
@Override
@RuleAction(label = "@text/actionLaunchApplicationLabel", description = "@text/actionLaunchApplicationDesc")
public void launchApplication(
@ActionInput(name = "appId", label = "@text/actionLaunchApplicationInputAppIDLabel", description = "@text/actionLaunchApplicationInputAppIDDesc") String appId) {
@@ -160,7 +149,6 @@ public class LGWebOSActions implements ThingActions, ILGWebOSActions {
}
}
@Override
@RuleAction(label = "@text/actionLaunchApplicationWithParamsLabel", description = "@text/actionLaunchApplicationWithParamsDesc")
public void launchApplication(
@ActionInput(name = "appId", label = "@text/actionLaunchApplicationInputAppIDLabel", description = "@text/actionLaunchApplicationInputAppIDDesc") String appId,
@@ -184,7 +172,6 @@ public class LGWebOSActions implements ThingActions, ILGWebOSActions {
}
}
@Override
@RuleAction(label = "@text/actionSendTextLabel", description = "@text/actionSendTextDesc")
public void sendText(
@ActionInput(name = "text", label = "@text/actionSendTextInputTextLabel", description = "@text/actionSendTextInputTextDesc") String text) {
@@ -195,7 +182,6 @@ public class LGWebOSActions implements ThingActions, ILGWebOSActions {
});
}
@Override
@RuleAction(label = "@text/actionSendButtonLabel", description = "@text/actionSendButtonDesc")
public void sendButton(
@ActionInput(name = "text", label = "@text/actionSendButtonInputButtonLabel", description = "@text/actionSendButtonInputButtonDesc") String button) {
@@ -235,19 +221,16 @@ public class LGWebOSActions implements ThingActions, ILGWebOSActions {
}
}
@Override
@RuleAction(label = "@text/actionIncreaseChannelLabel", description = "@text/actionIncreaseChannelDesc")
public void increaseChannel() {
getConnectedSocket().ifPresent(control -> control.channelUp(createResponseListener()));
}
@Override
@RuleAction(label = "@text/actionDecreaseChannelLabel", description = "@text/actionDecreaseChannelDesc")
public void decreaseChannel() {
getConnectedSocket().ifPresent(control -> control.channelDown(createResponseListener()));
}
@Override
@RuleAction(label = "@text/actionSendRCButtonLabel", description = "@text/actionSendRCButtonDesc")
public void sendRCButton(
@ActionInput(name = "text", label = "@text/actionSendRCButtonInputTextLabel", description = "@text/actionSendRCButtonInputTextDesc") String rcButton) {
@@ -298,62 +281,83 @@ public class LGWebOSActions implements ThingActions, ILGWebOSActions {
// delegation methods for "legacy" rule support
private static ILGWebOSActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
}
if (actions.getClass().getName().equals(LGWebOSActions.class.getName())) {
if (actions instanceof ILGWebOSActions) {
return (ILGWebOSActions) actions;
} else {
return (ILGWebOSActions) Proxy.newProxyInstance(ILGWebOSActions.class.getClassLoader(),
new Class[] { ILGWebOSActions.class }, (Object proxy, Method method, Object[] args) -> {
Method m = actions.getClass().getDeclaredMethod(method.getName(),
method.getParameterTypes());
return m.invoke(actions, args);
});
}
}
throw new IllegalArgumentException("Actions is not an instance of LGWebOSActions");
}
public static void showToast(@Nullable ThingActions actions, String text) throws IOException {
invokeMethodOf(actions).showToast(text);
if (actions instanceof LGWebOSActions) {
((LGWebOSActions) actions).showToast(text);
} else {
throw new IllegalArgumentException("Actions is not an instance of LGWebOSActions");
}
}
public static void showToast(@Nullable ThingActions actions, String icon, String text) throws IOException {
invokeMethodOf(actions).showToast(icon, text);
if (actions instanceof LGWebOSActions) {
((LGWebOSActions) actions).showToast(icon, text);
} else {
throw new IllegalArgumentException("Actions is not an instance of LGWebOSActions");
}
}
public static void launchBrowser(@Nullable ThingActions actions, String url) {
invokeMethodOf(actions).launchBrowser(url);
if (actions instanceof LGWebOSActions) {
((LGWebOSActions) actions).launchBrowser(url);
} else {
throw new IllegalArgumentException("Actions is not an instance of LGWebOSActions");
}
}
public static void launchApplication(@Nullable ThingActions actions, String appId) {
invokeMethodOf(actions).launchApplication(appId);
if (actions instanceof LGWebOSActions) {
((LGWebOSActions) actions).launchApplication(appId);
} else {
throw new IllegalArgumentException("Actions is not an instance of LGWebOSActions");
}
}
public static void launchApplication(@Nullable ThingActions actions, String appId, String param) {
invokeMethodOf(actions).launchApplication(appId, param);
if (actions instanceof LGWebOSActions) {
((LGWebOSActions) actions).launchApplication(appId, param);
} else {
throw new IllegalArgumentException("Actions is not an instance of LGWebOSActions");
}
}
public static void sendText(@Nullable ThingActions actions, String text) {
invokeMethodOf(actions).sendText(text);
if (actions instanceof LGWebOSActions) {
((LGWebOSActions) actions).sendText(text);
} else {
throw new IllegalArgumentException("Actions is not an instance of LGWebOSActions");
}
}
public static void sendButton(@Nullable ThingActions actions, String button) {
invokeMethodOf(actions).sendButton(button);
if (actions instanceof LGWebOSActions) {
((LGWebOSActions) actions).sendButton(button);
} else {
throw new IllegalArgumentException("Actions is not an instance of LGWebOSActions");
}
}
public static void increaseChannel(@Nullable ThingActions actions) {
invokeMethodOf(actions).increaseChannel();
if (actions instanceof LGWebOSActions) {
((LGWebOSActions) actions).increaseChannel();
} else {
throw new IllegalArgumentException("Actions is not an instance of LGWebOSActions");
}
}
public static void decreaseChannel(@Nullable ThingActions actions) {
invokeMethodOf(actions).decreaseChannel();
if (actions instanceof LGWebOSActions) {
((LGWebOSActions) actions).decreaseChannel();
} else {
throw new IllegalArgumentException("Actions is not an instance of LGWebOSActions");
}
}
public static void sendRCButton(@Nullable ThingActions actions, String rcButton) {
invokeMethodOf(actions).sendRCButton(rcButton);
if (actions instanceof LGWebOSActions) {
((LGWebOSActions) actions).sendRCButton(rcButton);
} else {
throw new IllegalArgumentException("Actions is not an instance of LGWebOSActions");
}
}
}

View File

@@ -27,7 +27,6 @@ import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.openhab.binding.lgwebos.action.LGWebOSActions;
import org.openhab.binding.lgwebos.internal.ChannelHandler;
import org.openhab.binding.lgwebos.internal.LGWebOSBindingConstants;
import org.openhab.binding.lgwebos.internal.LGWebOSStateDescriptionOptionProvider;
@@ -41,6 +40,7 @@ import org.openhab.binding.lgwebos.internal.ToastControlToast;
import org.openhab.binding.lgwebos.internal.VolumeControlMute;
import org.openhab.binding.lgwebos.internal.VolumeControlVolume;
import org.openhab.binding.lgwebos.internal.WakeOnLanUtility;
import org.openhab.binding.lgwebos.internal.action.LGWebOSActions;
import org.openhab.binding.lgwebos.internal.handler.LGWebOSTVSocket.WebOSTVSocketListener;
import org.openhab.binding.lgwebos.internal.handler.core.AppInfo;
import org.openhab.binding.lgwebos.internal.handler.core.ResponseListener;