]> git.basschouten.com Git - openhab-addons.git/blob
cf37669eb9090c8961928fafa261bca0bc484cb4
[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.nuvo.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.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;
28
29 /**
30  * Some automation actions to be used with a {@link NuvoThingActions}
31  *
32  * @author Michael Lobstein - initial contribution
33  *
34  */
35 @ThingActionsScope(name = "nuvo")
36 @NonNullByDefault
37 public class NuvoThingActions implements ThingActions, INuvoThingActions {
38
39     private final Logger logger = LoggerFactory.getLogger(NuvoThingActions.class);
40
41     private @Nullable NuvoHandler handler;
42
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);
49         } else {
50             logger.warn("unable to send command, NuvoHandler was null");
51         }
52     }
53
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);
58     }
59
60     @Override
61     public void setThingHandler(@Nullable ThingHandler handler) {
62         this.handler = (NuvoHandler) handler;
63     }
64
65     @Override
66     public @Nullable ThingHandler getThingHandler() {
67         return this.handler;
68     }
69
70     private static INuvoThingActions invokeMethodOf(@Nullable ThingActions actions) {
71         if (actions == null) {
72             throw new IllegalArgumentException("actions cannot be null");
73         }
74         if (actions.getClass().getName().equals(NuvoThingActions.class.getName())) {
75             if (actions instanceof NuvoThingActions) {
76                 return (INuvoThingActions) actions;
77             } else {
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);
83                         });
84             }
85         }
86         throw new IllegalArgumentException("Actions is not an instance of NuvoThingActions");
87     }
88 }