Skip to content

Commit

Permalink
[heos] Workaround for thing actions bug (openhab#8136)
Browse files Browse the repository at this point in the history
Related to openhab#8116

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored and andrewfg committed Aug 31, 2020
1 parent 686fb6d commit b324e10
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
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;
Expand All @@ -29,14 +31,18 @@
import org.slf4j.LoggerFactory;

/**
* The class is responsible to call corresponding
* action on HEOS Handler
* 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 {
public class HeosActions implements ThingActions, IHeosActions {

private final static Logger logger = LoggerFactory.getLogger(HeosActions.class);

Expand All @@ -62,6 +68,7 @@ public void setThingHandler(@Nullable ThingHandler handler) {
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,
Expand Down Expand Up @@ -90,10 +97,25 @@ public void playInputFromPlayer(

public static void playInputFromPlayer(@Nullable ThingActions actions, @Nullable Integer sourcePlayer,
@Nullable String input, @Nullable Integer destinationPlayer) {
if (actions instanceof HeosActions) {
((HeosActions) actions).playInputFromPlayer(sourcePlayer, input, destinationPlayer);
} else {
throw new IllegalArgumentException("Instance is not an HeosActionsService class.");
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.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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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);
}

0 comments on commit b324e10

Please sign in to comment.