]> git.basschouten.com Git - openhab-addons.git/blob
e4e6e9c74c3ea6a65d9e2ff2dda512c2da082d52
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.heos.internal.action;
14
15 import java.io.IOException;
16 import java.lang.reflect.Method;
17 import java.lang.reflect.Proxy;
18
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;
32
33 /**
34  * The class is responsible to call corresponding action on HEOS Handler
35  * <p>
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.
40  *
41  * @author Martin van Wingerden - Initial contribution
42  */
43 @ThingActionsScope(name = "heos")
44 @NonNullByDefault
45 public class HeosActions implements ThingActions, IHeosActions {
46
47     private final static Logger logger = LoggerFactory.getLogger(HeosActions.class);
48
49     private @Nullable HeosBridgeHandler handler;
50
51     @Override
52     public void setThingHandler(@Nullable ThingHandler handler) {
53         if (handler instanceof HeosBridgeHandler) {
54             this.handler = (HeosBridgeHandler) handler;
55         }
56     }
57
58     @Override
59     public @Nullable ThingHandler getThingHandler() {
60         return this.handler;
61     }
62
63     private @Nullable HeosFacade getConnection() throws HeosNotConnectedException {
64         if (handler == null) {
65             return null;
66         }
67
68         return handler.getApiConnection();
69     }
70
71     @Override
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) {
78             logger.debug(
79                     "Skipping HEOS playInputFromPlayer due to null value: sourcePlayer: {}, input: {}, destination: {}",
80                     sourcePlayer, input, destinationPlayer);
81             return;
82         }
83
84         try {
85             HeosFacade connection = getConnection();
86
87             if (connection == null) {
88                 logger.debug("Skipping HEOS playInputFromPlayer because no connection was available");
89                 return;
90             }
91
92             connection.playInputSource(destinationPlayer.toString(), sourcePlayer.toString(), input);
93         } catch (IOException | Telnet.ReadException e) {
94             logger.warn("Failed to play input source!", e);
95         }
96     }
97
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);
101     }
102
103     private static IHeosActions invokeMethodOf(@Nullable ThingActions actions) {
104         if (actions == null) {
105             throw new IllegalArgumentException("actions cannot be null");
106         }
107         if (actions.getClass().getName().equals(HeosActions.class.getName())) {
108             if (actions instanceof IHeosActions) {
109                 return (IHeosActions) actions;
110             } else {
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);
116                         });
117             }
118         }
119         throw new IllegalArgumentException("Actions is not an instance of HeosActions");
120     }
121 }