]> git.basschouten.com Git - openhab-addons.git/blob
d9291fa754f3910dbd46d28fcbb0b9b983f6f89a
[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.max.actions;
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.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;
29
30 /**
31  * The {@link MaxDevicesActions} class defines rule actions for MAX! devices
32  *
33  * @author Christoph Weitkamp - Initial contribution
34  */
35 @ThingActionsScope(name = "max-devices")
36 @NonNullByDefault
37 public class MaxDevicesActions implements ThingActions, IMaxDevicesActions {
38
39     private final Logger logger = LoggerFactory.getLogger(MaxDevicesActions.class);
40
41     private @Nullable MaxDevicesHandler handler;
42
43     @Override
44     public void setThingHandler(@Nullable ThingHandler handler) {
45         if (handler instanceof MaxDevicesHandler) {
46             this.handler = (MaxDevicesHandler) handler;
47         }
48     }
49
50     @Override
51     public @Nullable ThingHandler getThingHandler() {
52         return this.handler;
53     }
54
55     @Override
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!");
61             return false;
62         }
63         actionsHandler.deviceDelete();
64         return true;
65     }
66
67     public static boolean deleteFromCube(@Nullable ThingActions actions) {
68         return invokeMethodOf(actions).deleteFromCube();
69     }
70
71     private static IMaxDevicesActions invokeMethodOf(@Nullable ThingActions actions) {
72         if (actions == null) {
73             throw new IllegalArgumentException("actions cannot be null");
74         }
75         if (actions.getClass().getName().equals(MaxDevicesActions.class.getName())) {
76             if (actions instanceof IMaxDevicesActions) {
77                 return (IMaxDevicesActions) actions;
78             } else {
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);
84                         });
85             }
86         }
87         throw new IllegalArgumentException("Actions is not an instance of MaxDevicesActions");
88     }
89 }