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.max.actions;
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.max.internal.actions.IMaxDevicesActions;
21 import org.openhab.binding.max.internal.handler.MaxDevicesHandler;
22 import org.openhab.core.automation.annotation.ActionOutput;
23 import org.openhab.core.automation.annotation.RuleAction;
24 import org.openhab.core.thing.binding.ThingActions;
25 import org.openhab.core.thing.binding.ThingActionsScope;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link MaxDevicesActions} class defines rule actions for MAX! devices
33 * @author Christoph Weitkamp - Initial contribution
35 @ThingActionsScope(name = "max-devices")
37 public class MaxDevicesActions implements ThingActions, IMaxDevicesActions {
39 private final Logger logger = LoggerFactory.getLogger(MaxDevicesActions.class);
41 private @Nullable MaxDevicesHandler handler;
44 public void setThingHandler(@Nullable ThingHandler handler) {
45 if (handler instanceof MaxDevicesHandler) {
46 this.handler = (MaxDevicesHandler) handler;
51 public @Nullable ThingHandler getThingHandler() {
56 @RuleAction(label = "Delete Device from Cube", description = "Deletes the device from the MAX! Cube. Device will need to be included again!")
57 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean deleteFromCube() {
58 MaxDevicesHandler actionsHandler = handler;
59 if (actionsHandler == null) {
60 logger.info("MaxDevicesActions: Action service ThingHandler is null!");
63 actionsHandler.deviceDelete();
67 public static boolean deleteFromCube(@Nullable ThingActions actions) {
68 return invokeMethodOf(actions).deleteFromCube();
71 private static IMaxDevicesActions invokeMethodOf(@Nullable ThingActions actions) {
72 if (actions == null) {
73 throw new IllegalArgumentException("actions cannot be null");
75 if (actions.getClass().getName().equals(MaxDevicesActions.class.getName())) {
76 if (actions instanceof IMaxDevicesActions) {
77 return (IMaxDevicesActions) actions;
79 return (IMaxDevicesActions) Proxy.newProxyInstance(IMaxDevicesActions.class.getClassLoader(),
80 new Class[] { IMaxDevicesActions.class }, (Object proxy, Method method, Object[] args) -> {
81 Method m = actions.getClass().getDeclaredMethod(method.getName(),
82 method.getParameterTypes());
83 return m.invoke(actions, args);
87 throw new IllegalArgumentException("Actions is not an instance of MaxDevicesActions");