]> git.basschouten.com Git - openhab-addons.git/blob
730dc4971a232be5295c43ec5db5c13408a8289e
[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.radiothermostat.internal;
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.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;
28
29 /**
30  * Some automation actions to be used with a {@link RadioThermostatThingActions}
31  *
32  * @author Michael Lobstein - initial contribution
33  *
34  */
35 @ThingActionsScope(name = "radiothermostat")
36 @NonNullByDefault
37 public class RadioThermostatThingActions implements ThingActions, IRadioThermostatThingActions {
38     private final Logger logger = LoggerFactory.getLogger(RadioThermostatThingActions.class);
39
40     private @Nullable RadioThermostatHandler handler;
41
42     @Override
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");
48             return;
49         }
50
51         if (localHandler != null) {
52             localHandler.handleRawCommand(rawCommand);
53             logger.debug("sendRawCommand called with raw command: {}", rawCommand);
54         }
55     }
56
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);
61     }
62
63     @Override
64     public void setThingHandler(@Nullable ThingHandler handler) {
65         this.handler = (RadioThermostatHandler) handler;
66     }
67
68     @Override
69     public @Nullable ThingHandler getThingHandler() {
70         return this.handler;
71     }
72
73     private static IRadioThermostatThingActions invokeMethodOf(@Nullable ThingActions actions) {
74         if (actions == null) {
75             throw new IllegalArgumentException("actions cannot be null");
76         }
77         if (actions.getClass().getName().equals(RadioThermostatThingActions.class.getName())) {
78             if (actions instanceof RadioThermostatThingActions) {
79                 return (IRadioThermostatThingActions) actions;
80             } else {
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);
88                         });
89             }
90         }
91         throw new IllegalArgumentException("Actions is not an instance of RadioThermostatThingActions");
92     }
93 }