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.onkyo.internal.automation.modules;
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
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;
30 * Some automation actions to be used with a {@link OnkyoThingActionsService}
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.
37 * @author David Masshardt - initial contribution
40 @ThingActionsScope(name = "onkyo")
42 public class OnkyoThingActionsService implements ThingActions, OnkyoThingActions {
44 private final Logger logger = LoggerFactory.getLogger(OnkyoThingActionsService.class);
46 private @Nullable OnkyoHandler handler;
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!");
58 handler.sendRawCommand(command, value);
61 public static void sendRawCommand(@Nullable ThingActions actions, @Nullable String command,
62 @Nullable String value) {
63 invokeMethodOf(actions).sendRawCommand(command, value);
66 private static OnkyoThingActions invokeMethodOf(@Nullable ThingActions actions) {
67 if (actions == null) {
68 throw new IllegalArgumentException("actions cannot be null");
70 if (actions.getClass().getName().equals(OnkyoThingActionsService.class.getName())) {
71 if (actions instanceof OnkyoThingActions) {
72 return (OnkyoThingActions) actions;
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);
82 throw new IllegalArgumentException("Actions is not an instance of OnkyoThingActionsService");
86 public void setThingHandler(@Nullable ThingHandler handler) {
87 if (handler instanceof OnkyoHandler) {
88 this.handler = (OnkyoHandler) handler;
93 public @Nullable ThingHandler getThingHandler() {