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.radiothermostat.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.radiothermostat.internal.handler.RadioThermostatHandler;
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 RadioThermostatThingActions}
32 * @author Michael Lobstein - initial contribution
35 @ThingActionsScope(name = "radiothermostat")
37 public class RadioThermostatThingActions implements ThingActions, IRadioThermostatThingActions {
38 private final Logger logger = LoggerFactory.getLogger(RadioThermostatThingActions.class);
40 private @Nullable RadioThermostatHandler handler;
43 @RuleAction(label = "sendRawCommand", description = "Action that sends raw command to the thermostat")
44 public void sendRawCommand(@ActionInput(name = "sendRawCommand") @Nullable String rawCommand) {
45 RadioThermostatHandler localHandler = handler;
46 if (rawCommand == null) {
47 logger.warn("sendRawCommand called with null command, ignoring");
51 if (localHandler != null) {
52 localHandler.handleRawCommand(rawCommand);
53 logger.debug("sendRawCommand called with raw command: {}", rawCommand);
57 /** Static alias to support the old DSL rules engine and make the action available there. */
58 public static void sendRawCommand(@Nullable ThingActions actions, @Nullable String rawCommand)
59 throws IllegalArgumentException {
60 invokeMethodOf(actions).sendRawCommand(rawCommand);
64 public void setThingHandler(@Nullable ThingHandler handler) {
65 this.handler = (RadioThermostatHandler) handler;
69 public @Nullable ThingHandler getThingHandler() {
73 private static IRadioThermostatThingActions invokeMethodOf(@Nullable ThingActions actions) {
74 if (actions == null) {
75 throw new IllegalArgumentException("actions cannot be null");
77 if (actions.getClass().getName().equals(RadioThermostatThingActions.class.getName())) {
78 if (actions instanceof RadioThermostatThingActions) {
79 return (IRadioThermostatThingActions) actions;
81 return (IRadioThermostatThingActions) Proxy.newProxyInstance(
82 IRadioThermostatThingActions.class.getClassLoader(),
83 new Class[] { IRadioThermostatThingActions.class },
84 (Object proxy, Method method, Object[] args) -> {
85 Method m = actions.getClass().getDeclaredMethod(method.getName(),
86 method.getParameterTypes());
87 return m.invoke(actions, args);
91 throw new IllegalArgumentException("Actions is not an instance of RadioThermostatThingActions");