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.kaleidescape.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.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;
30 * Some automation actions to be used with a {@link KaleidescapeThingActions}
32 * @author Michael Lobstein - initial contribution
35 @ThingActionsScope(name = "kaleidescape")
37 public class KaleidescapeThingActions implements ThingActions, IKaleidescapeThingActions {
38 private final Logger logger = LoggerFactory.getLogger(KaleidescapeThingActions.class);
40 private @Nullable KaleidescapeHandler handler;
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);
49 logger.warn("unable to send command, KaleidescapeHandler was null");
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);
59 public void setThingHandler(@Nullable ThingHandler handler) {
60 this.handler = (KaleidescapeHandler) handler;
64 public @Nullable ThingHandler getThingHandler() {
68 private static IKaleidescapeThingActions invokeMethodOf(@Nullable ThingActions actions) {
69 if (actions == null) {
70 throw new IllegalArgumentException("actions cannot be null");
72 if (actions.getClass().getName().equals(KaleidescapeThingActions.class.getName())) {
73 if (actions instanceof KaleidescapeThingActions) {
74 return (IKaleidescapeThingActions) actions;
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);
86 throw new IllegalArgumentException("Actions is not an instance of KaleidescapeThingActions");