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.nuvo.internal;
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.nuvo.internal.handler.NuvoHandler;
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 NuvoThingActions}
32 * @author Michael Lobstein - initial contribution
35 @ThingActionsScope(name = "nuvo")
37 public class NuvoThingActions implements ThingActions, INuvoThingActions {
39 private final Logger logger = LoggerFactory.getLogger(NuvoThingActions.class);
41 private @Nullable NuvoHandler handler;
43 @RuleAction(label = "sendNuvoCommand", description = "Action that sends raw command to the amplifer")
44 public void sendNuvoCommand(@ActionInput(name = "sendNuvoCommand") String rawCommand) {
45 NuvoHandler localHandler = handler;
46 if (localHandler != null) {
47 localHandler.handleRawCommand(rawCommand);
48 logger.debug("sendNuvoCommand called with raw command: {}", rawCommand);
50 logger.warn("unable to send command, NuvoHandler was null");
54 /** Static alias to support the old DSL rules engine and make the action available there. */
55 public static void sendNuvoCommand(@Nullable ThingActions actions, String rawCommand)
56 throws IllegalArgumentException {
57 invokeMethodOf(actions).sendNuvoCommand(rawCommand);
61 public void setThingHandler(@Nullable ThingHandler handler) {
62 this.handler = (NuvoHandler) handler;
66 public @Nullable ThingHandler getThingHandler() {
70 private static INuvoThingActions invokeMethodOf(@Nullable ThingActions actions) {
71 if (actions == null) {
72 throw new IllegalArgumentException("actions cannot be null");
74 if (actions.getClass().getName().equals(NuvoThingActions.class.getName())) {
75 if (actions instanceof NuvoThingActions) {
76 return (INuvoThingActions) actions;
78 return (INuvoThingActions) Proxy.newProxyInstance(INuvoThingActions.class.getClassLoader(),
79 new Class[] { INuvoThingActions.class }, (Object proxy, Method method, Object[] args) -> {
80 Method m = actions.getClass().getDeclaredMethod(method.getName(),
81 method.getParameterTypes());
82 return m.invoke(actions, args);
86 throw new IllegalArgumentException("Actions is not an instance of NuvoThingActions");