2 * Copyright (c) 2010-2020 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.heos.internal.action;
15 import java.io.IOException;
16 import java.lang.reflect.Method;
17 import java.lang.reflect.Proxy;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.heos.internal.api.HeosFacade;
22 import org.openhab.binding.heos.internal.exception.HeosNotConnectedException;
23 import org.openhab.binding.heos.internal.handler.HeosBridgeHandler;
24 import org.openhab.binding.heos.internal.resources.Telnet;
25 import org.openhab.core.automation.annotation.ActionInput;
26 import org.openhab.core.automation.annotation.RuleAction;
27 import org.openhab.core.thing.binding.ThingActions;
28 import org.openhab.core.thing.binding.ThingActionsScope;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * The class is responsible to call corresponding action on HEOS Handler
36 * <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
37 * the test <i>actions instanceof HeosActions</i> fails. This test can fail
38 * due to an issue in openHAB core v2.5.0 where the {@link HeosActions} class
39 * can be loaded by a different classloader than the <i>actions</i> instance.
41 * @author Martin van Wingerden - Initial contribution
43 @ThingActionsScope(name = "heos")
45 public class HeosActions implements ThingActions, IHeosActions {
47 private final static Logger logger = LoggerFactory.getLogger(HeosActions.class);
49 private @Nullable HeosBridgeHandler handler;
52 public void setThingHandler(@Nullable ThingHandler handler) {
53 if (handler instanceof HeosBridgeHandler) {
54 this.handler = (HeosBridgeHandler) handler;
59 public @Nullable ThingHandler getThingHandler() {
63 private @Nullable HeosFacade getConnection() throws HeosNotConnectedException {
64 if (handler == null) {
68 return handler.getApiConnection();
72 @RuleAction(label = "Play Input", description = "Play an input from another device")
73 public void playInputFromPlayer(
74 @ActionInput(name = "source", label = "Source Player", description = "Player used for input") @Nullable Integer sourcePlayer,
75 @ActionInput(name = "input", label = "Source Input", description = "Input source used") @Nullable String input,
76 @ActionInput(name = "destination", label = "Destination Player", description = "Device for audio output") @Nullable Integer destinationPlayer) {
77 if (sourcePlayer == null || input == null || destinationPlayer == null) {
79 "Skipping HEOS playInputFromPlayer due to null value: sourcePlayer: {}, input: {}, destination: {}",
80 sourcePlayer, input, destinationPlayer);
85 HeosFacade connection = getConnection();
87 if (connection == null) {
88 logger.debug("Skipping HEOS playInputFromPlayer because no connection was available");
92 connection.playInputSource(destinationPlayer.toString(), sourcePlayer.toString(), input);
93 } catch (IOException | Telnet.ReadException e) {
94 logger.warn("Failed to play input source!", e);
98 public static void playInputFromPlayer(@Nullable ThingActions actions, @Nullable Integer sourcePlayer,
99 @Nullable String input, @Nullable Integer destinationPlayer) {
100 invokeMethodOf(actions).playInputFromPlayer(sourcePlayer, input, destinationPlayer);
103 private static IHeosActions invokeMethodOf(@Nullable ThingActions actions) {
104 if (actions == null) {
105 throw new IllegalArgumentException("actions cannot be null");
107 if (actions.getClass().getName().equals(HeosActions.class.getName())) {
108 if (actions instanceof IHeosActions) {
109 return (IHeosActions) actions;
111 return (IHeosActions) Proxy.newProxyInstance(IHeosActions.class.getClassLoader(),
112 new Class[] { IHeosActions.class }, (Object proxy, Method method, Object[] args) -> {
113 Method m = actions.getClass().getDeclaredMethod(method.getName(),
114 method.getParameterTypes());
115 return m.invoke(actions, args);
119 throw new IllegalArgumentException("Actions is not an instance of HeosActions");