]> git.basschouten.com Git - openhab-addons.git/blob
577422439c42a81ae2380ad9a317a1e05eb2ed70
[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.kaleidescape.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.kaleidescape.internal.handler.KaleidescapeHandler;
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 KaleidescapeThingActions}
31  *
32  * @author Michael Lobstein - initial contribution
33  *
34  */
35 @ThingActionsScope(name = "kaleidescape")
36 @NonNullByDefault
37 public class KaleidescapeThingActions implements ThingActions, IKaleidescapeThingActions {
38     private final Logger logger = LoggerFactory.getLogger(KaleidescapeThingActions.class);
39
40     private @Nullable KaleidescapeHandler handler;
41
42     @RuleAction(label = "sendKCommand", description = "Action that sends raw command to the kaleidescape zone")
43     public void sendKCommand(@ActionInput(name = "sendKCommand") String kCommand) {
44         KaleidescapeHandler localHandler = handler;
45         if (localHandler != null) {
46             localHandler.handleRawCommand(kCommand);
47             logger.debug("sendKCommand called with command: {}", kCommand);
48         } else {
49             logger.warn("unable to send command, KaleidescapeHandler was null");
50         }
51     }
52
53     /** Static alias to support the old DSL rules engine and make the action available there. */
54     public static void sendKCommand(@Nullable ThingActions actions, String kCommand) throws IllegalArgumentException {
55         invokeMethodOf(actions).sendKCommand(kCommand);
56     }
57
58     @Override
59     public void setThingHandler(@Nullable ThingHandler handler) {
60         this.handler = (KaleidescapeHandler) handler;
61     }
62
63     @Override
64     public @Nullable ThingHandler getThingHandler() {
65         return this.handler;
66     }
67
68     private static IKaleidescapeThingActions invokeMethodOf(@Nullable ThingActions actions) {
69         if (actions == null) {
70             throw new IllegalArgumentException("actions cannot be null");
71         }
72         if (actions.getClass().getName().equals(KaleidescapeThingActions.class.getName())) {
73             if (actions instanceof KaleidescapeThingActions) {
74                 return (IKaleidescapeThingActions) actions;
75             } else {
76                 return (IKaleidescapeThingActions) Proxy.newProxyInstance(
77                         IKaleidescapeThingActions.class.getClassLoader(),
78                         new Class[] { IKaleidescapeThingActions.class },
79                         (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 KaleidescapeThingActions");
87     }
88 }