]> git.basschouten.com Git - openhab-addons.git/blob
3b2a0d86c809f6bb55b313a369bc7c5548155234
[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.onkyo.internal.automation.modules;
14
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.onkyo.internal.handler.OnkyoHandler;
21 import org.openhab.core.automation.annotation.ActionInput;
22 import org.openhab.core.automation.annotation.RuleAction;
23 import org.openhab.core.thing.binding.ThingActions;
24 import org.openhab.core.thing.binding.ThingActionsScope;
25 import org.openhab.core.thing.binding.ThingHandler;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Some automation actions to be used with a {@link OnkyoThingActionsService}
31  * <p>
32  * <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
33  * the test <i>actions instanceof OnkyoThingActionsService</i> fails. This test can fail
34  * due to an issue in openHAB core v2.5.0 where the {@link OnkyoThingActionsService} class
35  * can be loaded by a different classloader than the <i>actions</i> instance.
36  *
37  * @author David Masshardt - initial contribution
38  *
39  */
40 @ThingActionsScope(name = "onkyo")
41 @NonNullByDefault
42 public class OnkyoThingActionsService implements ThingActions, OnkyoThingActions {
43
44     private final Logger logger = LoggerFactory.getLogger(OnkyoThingActionsService.class);
45
46     private @Nullable OnkyoHandler handler;
47
48     @Override
49     @SuppressWarnings("null")
50     @RuleAction(label = "Onkyo sendRawCommand", description = "Action that sends raw command to the receiver")
51     public void sendRawCommand(@ActionInput(name = "command") @Nullable String command,
52             @ActionInput(name = "command") @Nullable String value) {
53         logger.debug("sendRawCommand called with raw command: {} value: {}", command, value);
54         if (handler == null) {
55             logger.warn("Onkyo Action service ThingHandler is null!");
56             return;
57         }
58         handler.sendRawCommand(command, value);
59     }
60
61     public static void sendRawCommand(@Nullable ThingActions actions, @Nullable String command,
62             @Nullable String value) {
63         invokeMethodOf(actions).sendRawCommand(command, value);
64     }
65
66     private static OnkyoThingActions invokeMethodOf(@Nullable ThingActions actions) {
67         if (actions == null) {
68             throw new IllegalArgumentException("actions cannot be null");
69         }
70         if (actions.getClass().getName().equals(OnkyoThingActionsService.class.getName())) {
71             if (actions instanceof OnkyoThingActions) {
72                 return (OnkyoThingActions) actions;
73             } else {
74                 return (OnkyoThingActions) Proxy.newProxyInstance(OnkyoThingActions.class.getClassLoader(),
75                         new Class[] { OnkyoThingActions.class }, (Object proxy, Method method, Object[] args) -> {
76                             Method m = actions.getClass().getDeclaredMethod(method.getName(),
77                                     method.getParameterTypes());
78                             return m.invoke(actions, args);
79                         });
80             }
81         }
82         throw new IllegalArgumentException("Actions is not an instance of OnkyoThingActionsService");
83     }
84
85     @Override
86     public void setThingHandler(@Nullable ThingHandler handler) {
87         if (handler instanceof OnkyoHandler) {
88             this.handler = (OnkyoHandler) handler;
89         }
90     }
91
92     @Override
93     public @Nullable ThingHandler getThingHandler() {
94         return this.handler;
95     }
96 }