]> git.basschouten.com Git - openhab-addons.git/blob
45a20311e8ba69591a449cab2b72f830ce0499f1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.velux.internal.action;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.velux.internal.handler.VeluxBridgeHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.ActionOutput;
20 import org.openhab.core.automation.annotation.RuleAction;
21 import org.openhab.core.thing.binding.ThingActions;
22 import org.openhab.core.thing.binding.ThingActionsScope;
23 import org.openhab.core.thing.binding.ThingHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link VeluxActions} implementation of the rule action for rebooting the bridge
29  *
30  * @author Andrew Fiddian-Green - Initial contribution
31  */
32 @ThingActionsScope(name = "velux")
33 @NonNullByDefault
34 public class VeluxActions implements ThingActions, IVeluxActions {
35
36     private final Logger logger = LoggerFactory.getLogger(VeluxActions.class);
37
38     private @Nullable VeluxBridgeHandler bridgeHandler;
39
40     @Override
41     public void setThingHandler(@Nullable ThingHandler handler) {
42         if (handler instanceof VeluxBridgeHandler) {
43             this.bridgeHandler = (VeluxBridgeHandler) handler;
44         }
45     }
46
47     @Override
48     public @Nullable ThingHandler getThingHandler() {
49         return this.bridgeHandler;
50     }
51
52     @Override
53     @RuleAction(label = "reboot Bridge", description = "issues a reboot command to the KLF200 bridge")
54     public @ActionOutput(name = "executing", type = "java.lang.Boolean", label = "executing", description = "indicates the command was issued") Boolean rebootBridge()
55             throws IllegalStateException {
56         logger.trace("rebootBridge(): action called");
57         VeluxBridgeHandler bridge = bridgeHandler;
58         if (bridge == null) {
59             throw new IllegalStateException("Bridge instance is null");
60         }
61         return bridge.runReboot();
62     }
63
64     @Override
65     @RuleAction(label = "move relative", description = "issues a relative move command to an actuator")
66     public @ActionOutput(name = "executing", type = "java.lang.Boolean", label = "executing", description = "indicates the command was issued") Boolean moveRelative(
67             @ActionInput(name = "nodeId", required = true, label = "nodeId", description = "actuator id in the bridge", type = "java.lang.String") String nodeId,
68             @ActionInput(name = "relativePercent", required = true, label = "relativePercent", description = "position delta from current", type = "java.lang.String") String relativePercent)
69             throws NumberFormatException, IllegalStateException {
70         logger.trace("moveRelative(): action called");
71         VeluxBridgeHandler bridge = bridgeHandler;
72         if (bridge == null) {
73             throw new IllegalStateException("Bridge instance is null");
74         }
75         int node = Integer.parseInt(nodeId);
76         if (node < 0 || node > 200) {
77             throw new NumberFormatException("Node Id out of range");
78         }
79         int relPct = Integer.parseInt(relativePercent);
80         if (Math.abs(relPct) > 100) {
81             throw new NumberFormatException("Relative Percent out of range");
82         }
83         return bridge.moveRelative(node, relPct);
84     }
85
86     /**
87      * Static method to send a reboot command to a Velux Bridge
88      *
89      * @param actions ThingActions from the caller
90      * @return true if the command was sent
91      * @throws IllegalArgumentException if actions is invalid
92      * @throws IllegalStateException if anything else is wrong
93      */
94     public static Boolean rebootBridge(@Nullable ThingActions actions)
95             throws IllegalArgumentException, IllegalStateException {
96         if (!(actions instanceof IVeluxActions)) {
97             throw new IllegalArgumentException("Unsupported action");
98         }
99         return ((IVeluxActions) actions).rebootBridge();
100     }
101
102     /**
103      * Static method to send a relative move command to a Velux actuator
104      *
105      * @param actions ThingActions from the caller
106      * @param nodeId the node Id in the bridge
107      * @param relativePercent the target position relative to its current position (-100% <= relativePercent <= +100%)
108      * @return true if the command was sent
109      * @throws IllegalArgumentException if actions is invalid
110      * @throws NumberFormatException if either of nodeId or relativePercent is not an integer, or out of range
111      * @throws IllegalStateException if anything else is wrong
112      */
113     public static Boolean moveRelative(@Nullable ThingActions actions, String nodeId, String relativePercent)
114             throws IllegalArgumentException, NumberFormatException, IllegalStateException {
115         if (!(actions instanceof IVeluxActions)) {
116             throw new IllegalArgumentException("Unsupported action");
117         }
118         return ((IVeluxActions) actions).moveRelative(nodeId, relativePercent);
119     }
120 }