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

@@ -13,8 +13,6 @@
package org.openhab.binding.heos.internal.action;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@@ -32,17 +30,12 @@ import org.slf4j.LoggerFactory;
/**
* The class is responsible to call corresponding action on HEOS Handler
* <p>
* <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
* the test <i>actions instanceof HeosActions</i> fails. This test can fail
* due to an issue in openHAB core v2.5.0 where the {@link HeosActions} class
* can be loaded by a different classloader than the <i>actions</i> instance.
*
* @author Martin van Wingerden - Initial contribution
*/
@ThingActionsScope(name = "heos")
@NonNullByDefault
public class HeosActions implements ThingActions, IHeosActions {
public class HeosActions implements ThingActions {
private final static Logger logger = LoggerFactory.getLogger(HeosActions.class);
@@ -57,10 +50,11 @@ public class HeosActions implements ThingActions, IHeosActions {
@Override
public @Nullable ThingHandler getThingHandler() {
return this.handler;
return handler;
}
private @Nullable HeosFacade getConnection() throws HeosNotConnectedException {
HeosBridgeHandler handler = this.handler;
if (handler == null) {
return null;
}
@@ -68,7 +62,6 @@ public class HeosActions implements ThingActions, IHeosActions {
return handler.getApiConnection();
}
@Override
@RuleAction(label = "Play Input", description = "Play an input from another device")
public void playInputFromPlayer(
@ActionInput(name = "source", label = "Source Player", description = "Player used for input") @Nullable Integer sourcePlayer,
@@ -97,25 +90,10 @@ public class HeosActions implements ThingActions, IHeosActions {
public static void playInputFromPlayer(@Nullable ThingActions actions, @Nullable Integer sourcePlayer,
@Nullable String input, @Nullable Integer destinationPlayer) {
invokeMethodOf(actions).playInputFromPlayer(sourcePlayer, input, destinationPlayer);
}
private static IHeosActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
if (actions instanceof HeosActions) {
((HeosActions) actions).playInputFromPlayer(sourcePlayer, input, destinationPlayer);
} else {
throw new IllegalArgumentException("Actions is not an instance of HeosActions");
}
if (actions.getClass().getName().equals(HeosActions.class.getName())) {
if (actions instanceof IHeosActions) {
return (IHeosActions) actions;
} else {
return (IHeosActions) Proxy.newProxyInstance(IHeosActions.class.getClassLoader(),
new Class[] { IHeosActions.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 HeosActions");
}
}

View File

@@ -1,28 +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.heos.internal.action;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/**
* The {@link IHeosActions} defines the interface for all thing actions supported by the binding.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface IHeosActions {
public void playInputFromPlayer(@Nullable Integer sourcePlayer, @Nullable String input,
@Nullable Integer destinationPlayer);
}