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,41 +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.enigma2.actions;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* The {@link IEnigma2Actions} defines the interface for all thing actions supported by the binding.
*
* @author Guido Dolfen - Initial contribution
*/
@NonNullByDefault
public interface IEnigma2Actions {
void sendRcCommand(String rcButton);
void sendInfo(String text);
void sendInfo(String text, int timeout);
void sendWarning(String text);
void sendWarning(String text, int timeout);
void sendError(String text);
void sendError(String text, int timeout);
void sendQuestion(String text);
void sendQuestion(String text, int timeout);
}

View File

@@ -16,7 +16,7 @@ import static org.openhab.binding.enigma2.internal.Enigma2BindingConstants.*;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.enigma2.handler.Enigma2Handler;
import org.openhab.binding.enigma2.internal.handler.Enigma2Handler;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory;

View File

@@ -10,15 +10,12 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enigma2.actions;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
package org.openhab.binding.enigma2.internal.actions;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.enigma2.handler.Enigma2Handler;
import org.openhab.binding.enigma2.internal.Enigma2BindingConstants;
import org.openhab.binding.enigma2.internal.handler.Enigma2Handler;
import org.openhab.core.automation.annotation.ActionInput;
import org.openhab.core.automation.annotation.RuleAction;
import org.openhab.core.thing.binding.ThingActions;
@@ -33,7 +30,7 @@ import org.openhab.core.thing.binding.ThingHandler;
*/
@ThingActionsScope(name = "enigma2")
@NonNullByDefault
public class Enigma2Actions implements ThingActions, IEnigma2Actions {
public class Enigma2Actions implements ThingActions {
private @Nullable Enigma2Handler handler;
@Override
@@ -43,10 +40,9 @@ public class Enigma2Actions implements ThingActions, IEnigma2Actions {
@Override
public @Nullable Enigma2Handler getThingHandler() {
return this.handler;
return handler;
}
@Override
@RuleAction(label = "@text/actions.enigma2.send-rc-button.label", description = "@text/actions.enigma2.send-rc-button.description")
@SuppressWarnings("null")
public void sendRcCommand(
@@ -54,7 +50,6 @@ public class Enigma2Actions implements ThingActions, IEnigma2Actions {
handler.sendRcCommand(rcButton);
}
@Override
@RuleAction(label = "@text/actions.enigma2.send-info.label", description = "@text/actions.enigma2.send-info.description")
@SuppressWarnings("null")
public void sendInfo(
@@ -62,7 +57,6 @@ public class Enigma2Actions implements ThingActions, IEnigma2Actions {
handler.sendInfo(Enigma2BindingConstants.MESSAGE_TIMEOUT, text);
}
@Override
@RuleAction(label = "@text/actions.enigma2.send-info.label", description = "@text/actions.enigma2.send-info.description")
@SuppressWarnings("null")
public void sendInfo(
@@ -71,7 +65,6 @@ public class Enigma2Actions implements ThingActions, IEnigma2Actions {
handler.sendInfo(timeout, text);
}
@Override
@RuleAction(label = "@text/actions.enigma2.send-warning.label", description = "@text/actions.enigma2.send-warning.description")
@SuppressWarnings("null")
public void sendWarning(
@@ -79,7 +72,6 @@ public class Enigma2Actions implements ThingActions, IEnigma2Actions {
handler.sendWarning(Enigma2BindingConstants.MESSAGE_TIMEOUT, text);
}
@Override
@RuleAction(label = "@text/actions.enigma2.send-warning.label", description = "@text/actions.enigma2.send-warning.description")
@SuppressWarnings("null")
public void sendWarning(
@@ -88,7 +80,6 @@ public class Enigma2Actions implements ThingActions, IEnigma2Actions {
handler.sendWarning(timeout, text);
}
@Override
@RuleAction(label = "@text/actions.enigma2.send-error.label", description = "@text/actions.enigma2.send-error.description")
@SuppressWarnings("null")
public void sendError(
@@ -96,7 +87,6 @@ public class Enigma2Actions implements ThingActions, IEnigma2Actions {
handler.sendError(Enigma2BindingConstants.MESSAGE_TIMEOUT, text);
}
@Override
@RuleAction(label = "@text/actions.enigma2.send-error.label", description = "@text/actions.enigma2.send-error.description")
@SuppressWarnings("null")
public void sendError(
@@ -105,7 +95,6 @@ public class Enigma2Actions implements ThingActions, IEnigma2Actions {
handler.sendError(timeout, text);
}
@Override
@RuleAction(label = "@text/actions.enigma2.send-error.label", description = "@text/actions.enigma2.send-question.description")
@SuppressWarnings("null")
public void sendQuestion(
@@ -113,7 +102,6 @@ public class Enigma2Actions implements ThingActions, IEnigma2Actions {
handler.sendQuestion(Enigma2BindingConstants.MESSAGE_TIMEOUT, text);
}
@Override
@RuleAction(label = "@text/actions.enigma2.send-error.label", description = "@text/actions.enigma2.send-question.description")
@SuppressWarnings("null")
public void sendQuestion(
@@ -124,57 +112,74 @@ public class Enigma2Actions implements ThingActions, IEnigma2Actions {
// delegation methods for "legacy" rule support
public static void sendRcCommand(@Nullable ThingActions actions, String rcButton) {
invokeMethodOf(actions).sendRcCommand(rcButton);
if (actions instanceof Enigma2Actions) {
((Enigma2Actions) actions).sendRcCommand(rcButton);
} else {
throw new IllegalArgumentException("Actions is not an instance of Enigma2Actions");
}
}
public static void sendInfo(@Nullable ThingActions actions, String info) {
invokeMethodOf(actions).sendInfo(info);
if (actions instanceof Enigma2Actions) {
((Enigma2Actions) actions).sendInfo(info);
} else {
throw new IllegalArgumentException("Actions is not an instance of Enigma2Actions");
}
}
public static void sendInfo(@Nullable ThingActions actions, String info, int timeout) {
invokeMethodOf(actions).sendInfo(info, timeout);
if (actions instanceof Enigma2Actions) {
((Enigma2Actions) actions).sendInfo(info, timeout);
} else {
throw new IllegalArgumentException("Actions is not an instance of Enigma2Actions");
}
}
public static void sendWarning(@Nullable ThingActions actions, String warning) {
invokeMethodOf(actions).sendWarning(warning);
if (actions instanceof Enigma2Actions) {
((Enigma2Actions) actions).sendWarning(warning);
} else {
throw new IllegalArgumentException("Actions is not an instance of Enigma2Actions");
}
}
public static void sendWarning(@Nullable ThingActions actions, String warning, int timeout) {
invokeMethodOf(actions).sendWarning(warning, timeout);
if (actions instanceof Enigma2Actions) {
((Enigma2Actions) actions).sendWarning(warning, timeout);
} else {
throw new IllegalArgumentException("Actions is not an instance of Enigma2Actions");
}
}
public static void sendError(@Nullable ThingActions actions, String error) {
invokeMethodOf(actions).sendError(error);
if (actions instanceof Enigma2Actions) {
((Enigma2Actions) actions).sendError(error);
} else {
throw new IllegalArgumentException("Actions is not an instance of Enigma2Actions");
}
}
public static void sendError(@Nullable ThingActions actions, String error, int timeout) {
invokeMethodOf(actions).sendError(error, timeout);
if (actions instanceof Enigma2Actions) {
((Enigma2Actions) actions).sendError(error, timeout);
} else {
throw new IllegalArgumentException("Actions is not an instance of Enigma2Actions");
}
}
public static void sendQuestion(@Nullable ThingActions actions, String text) {
invokeMethodOf(actions).sendQuestion(text);
if (actions instanceof Enigma2Actions) {
((Enigma2Actions) actions).sendQuestion(text);
} else {
throw new IllegalArgumentException("Actions is not an instance of Enigma2Actions");
}
}
public static void sendQuestion(@Nullable ThingActions actions, String text, int timeout) {
invokeMethodOf(actions).sendQuestion(text, timeout);
}
private static IEnigma2Actions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
if (actions instanceof Enigma2Actions) {
((Enigma2Actions) actions).sendQuestion(text, timeout);
} else {
throw new IllegalArgumentException("Actions is not an instance of Enigma2Actions");
}
if (actions.getClass().getName().equals(Enigma2Actions.class.getName())) {
if (actions instanceof IEnigma2Actions) {
return (IEnigma2Actions) actions;
} else {
return (IEnigma2Actions) Proxy.newProxyInstance(IEnigma2Actions.class.getClassLoader(),
new Class[] { IEnigma2Actions.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 Enigma2Actions");
}
}

View File

@@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enigma2.handler;
package org.openhab.binding.enigma2.internal.handler;
import static org.openhab.binding.enigma2.internal.Enigma2BindingConstants.*;
@@ -25,10 +25,10 @@ import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.enigma2.actions.Enigma2Actions;
import org.openhab.binding.enigma2.internal.Enigma2Client;
import org.openhab.binding.enigma2.internal.Enigma2Configuration;
import org.openhab.binding.enigma2.internal.Enigma2RemoteKey;
import org.openhab.binding.enigma2.internal.actions.Enigma2Actions;
import org.openhab.core.library.types.*;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;

View File

@@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enigma2.actions;
package org.openhab.binding.enigma2.internal.actions;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
@@ -21,8 +21,8 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.binding.enigma2.handler.Enigma2Handler;
import org.openhab.binding.enigma2.internal.Enigma2BindingConstants;
import org.openhab.binding.enigma2.internal.handler.Enigma2Handler;
/**
* The {@link Enigma2ActionsTest} class is responsible for testing {@link Enigma2Actions}.

View File

@@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enigma2.handler;
package org.openhab.binding.enigma2.internal.handler;
import static org.eclipse.jdt.annotation.Checks.requireNonNull;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -25,11 +25,11 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.binding.enigma2.actions.Enigma2Actions;
import org.openhab.binding.enigma2.internal.Enigma2BindingConstants;
import org.openhab.binding.enigma2.internal.Enigma2Client;
import org.openhab.binding.enigma2.internal.Enigma2Configuration;
import org.openhab.binding.enigma2.internal.Enigma2RemoteKey;
import org.openhab.binding.enigma2.internal.actions.Enigma2Actions;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.NextPreviousType;