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.avmfritz.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.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;
31 * The {@link AVMFritzHeatingActions} defines thing actions for heating devices / groups of the avmfritz binding.
33 * @author Christoph Weitkamp - Initial contribution
35 @ThingActionsScope(name = "avmfritz")
37 public class AVMFritzHeatingActions implements ThingActions, IAVMFritzHeatingActions {
39 private final Logger logger = LoggerFactory.getLogger(AVMFritzHeatingActions.class);
41 private @Nullable AVMFritzHeatingActionsHandler handler;
44 public void setThingHandler(@Nullable ThingHandler handler) {
45 this.handler = (AVMFritzHeatingActionsHandler) handler;
49 public @Nullable ThingHandler getThingHandler() {
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!");
61 if (duration == null) {
62 throw new IllegalArgumentException("Cannot set Boost mode as 'duration' is null!");
64 actionsHandler.setBoostMode(duration.longValue());
67 public static void setBoostMode(@Nullable ThingActions actions, @Nullable Long duration) {
68 invokeMethodOf(actions).setBoostMode(duration);
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!");
79 if (duration == null) {
80 throw new IllegalArgumentException("Cannot set Window Open mode as 'duration' is null!");
82 actionsHandler.setWindowOpenMode(duration.longValue());
85 public static void setWindowOpenMode(@Nullable ThingActions actions, @Nullable Long duration) {
86 invokeMethodOf(actions).setWindowOpenMode(duration);
89 private static IAVMFritzHeatingActions invokeMethodOf(@Nullable ThingActions actions) {
90 if (actions == null) {
91 throw new IllegalArgumentException("actions cannot be null");
93 if (actions.getClass().getName().equals(AVMFritzHeatingActions.class.getName())) {
94 if (actions instanceof IAVMFritzHeatingActions) {
95 return (IAVMFritzHeatingActions) actions;
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);
105 throw new IllegalArgumentException("Actions is not an instance of AVMFritzHeatingActions");