2 * Copyright (c) 2010-2021 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.velux.internal.action;
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;
28 * The {@link VeluxActions} implementation of the rule action for rebooting the bridge
30 * @author Andrew Fiddian-Green - Initial contribution
32 @ThingActionsScope(name = "velux")
34 public class VeluxActions implements ThingActions, IVeluxActions {
36 private final Logger logger = LoggerFactory.getLogger(VeluxActions.class);
38 private @Nullable VeluxBridgeHandler bridgeHandler;
41 public void setThingHandler(@Nullable ThingHandler handler) {
42 if (handler instanceof VeluxBridgeHandler) {
43 this.bridgeHandler = (VeluxBridgeHandler) handler;
48 public @Nullable ThingHandler getThingHandler() {
49 return this.bridgeHandler;
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;
59 throw new IllegalStateException("Bridge instance is null");
61 return bridge.runReboot();
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;
73 throw new IllegalStateException("Bridge instance is null");
75 int node = Integer.parseInt(nodeId);
76 if (node < 0 || node > 200) {
77 throw new NumberFormatException("Node Id out of range");
79 int relPct = Integer.parseInt(relativePercent);
80 if (Math.abs(relPct) > 100) {
81 throw new NumberFormatException("Relative Percent out of range");
83 return bridge.moveRelative(node, relPct);
87 * Static method to send a reboot command to a Velux Bridge
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
94 public static Boolean rebootBridge(@Nullable ThingActions actions)
95 throws IllegalArgumentException, IllegalStateException {
96 if (!(actions instanceof IVeluxActions)) {
97 throw new IllegalArgumentException("Unsupported action");
99 return ((IVeluxActions) actions).rebootBridge();
103 * Static method to send a relative move command to a Velux actuator
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
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");
118 return ((IVeluxActions) actions).moveRelative(nodeId, relativePercent);