]> git.basschouten.com Git - openhab-addons.git/blob
662beefcdc2d4a08b3e94caf68000f9f48f4f95d
[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.avmfritz.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.avmfritz.internal.actions.IAVMFritzHeatingActions;
21 import org.openhab.binding.avmfritz.internal.handler.AVMFritzHeatingActionsHandler;
22 import org.openhab.core.automation.annotation.ActionInput;
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 AVMFritzHeatingActions} defines thing actions for heating devices / groups of the avmfritz binding.
32  *
33  * @author Christoph Weitkamp - Initial contribution
34  */
35 @ThingActionsScope(name = "avmfritz")
36 @NonNullByDefault
37 public class AVMFritzHeatingActions implements ThingActions, IAVMFritzHeatingActions {
38
39     private final Logger logger = LoggerFactory.getLogger(AVMFritzHeatingActions.class);
40
41     private @Nullable AVMFritzHeatingActionsHandler handler;
42
43     @Override
44     public void setThingHandler(@Nullable ThingHandler handler) {
45         this.handler = (AVMFritzHeatingActionsHandler) handler;
46     }
47
48     @Override
49     public @Nullable ThingHandler getThingHandler() {
50         return handler;
51     }
52
53     @Override
54     @RuleAction(label = "@text/setBoostModeModeActionLabel", description = "@text/setBoostModeActionDescription")
55     public void setBoostMode(
56             @ActionInput(name = "Duration", label = "@text/setBoostModeDurationInputLabel", description = "@text/setBoostModeDurationInputDescription", type = "java.lang.Long", required = true) @Nullable Long duration) {
57         AVMFritzHeatingActionsHandler actionsHandler = handler;
58         if (actionsHandler == null) {
59             throw new IllegalArgumentException("AVMFritzHeatingActions ThingHandler is null!");
60         }
61         if (duration == null) {
62             throw new IllegalArgumentException("Cannot set Boost mode as 'duration' is null!");
63         }
64         actionsHandler.setBoostMode(duration.longValue());
65     }
66
67     public static void setBoostMode(@Nullable ThingActions actions, @Nullable Long duration) {
68         invokeMethodOf(actions).setBoostMode(duration);
69     }
70
71     @Override
72     @RuleAction(label = "@text/setWindowOpenModeActionLabel", description = "@text/setWindowOpenModeActionDescription")
73     public void setWindowOpenMode(
74             @ActionInput(name = "Duration", label = "@text/setWindowOpenModeDurationInputLabel", description = "@text/setWindowOpenModeDurationInputDescription", type = "java.lang.Long", required = true) @Nullable Long duration) {
75         AVMFritzHeatingActionsHandler actionsHandler = handler;
76         if (actionsHandler == null) {
77             throw new IllegalArgumentException("AVMFritzHeatingActions ThingHandler is null!");
78         }
79         if (duration == null) {
80             throw new IllegalArgumentException("Cannot set Window Open mode as 'duration' is null!");
81         }
82         actionsHandler.setWindowOpenMode(duration.longValue());
83     }
84
85     public static void setWindowOpenMode(@Nullable ThingActions actions, @Nullable Long duration) {
86         invokeMethodOf(actions).setWindowOpenMode(duration);
87     }
88
89     private static IAVMFritzHeatingActions invokeMethodOf(@Nullable ThingActions actions) {
90         if (actions == null) {
91             throw new IllegalArgumentException("actions cannot be null");
92         }
93         if (actions.getClass().getName().equals(AVMFritzHeatingActions.class.getName())) {
94             if (actions instanceof IAVMFritzHeatingActions) {
95                 return (IAVMFritzHeatingActions) actions;
96             } else {
97                 return (IAVMFritzHeatingActions) Proxy.newProxyInstance(IAVMFritzHeatingActions.class.getClassLoader(),
98                         new Class[] { IAVMFritzHeatingActions.class }, (Object proxy, Method method, Object[] args) -> {
99                             Method m = actions.getClass().getDeclaredMethod(method.getName(),
100                                     method.getParameterTypes());
101                             return m.invoke(actions, args);
102                         });
103             }
104         }
105         throw new IllegalArgumentException("Actions is not an instance of AVMFritzHeatingActions");
106     }
107 }