2 * Copyright (c) 2010-2022 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.handler;
15 import static org.openhab.binding.velux.internal.VeluxBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.velux.internal.bridge.VeluxBridgeRunProductCommand;
20 import org.openhab.binding.velux.internal.bridge.common.GetProduct;
21 import org.openhab.binding.velux.internal.handler.utils.Thing2VeluxActuator;
22 import org.openhab.binding.velux.internal.things.VeluxProduct;
23 import org.openhab.binding.velux.internal.things.VeluxProductPosition;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.library.types.StopMoveType;
27 import org.openhab.core.library.types.UpDownType;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.UnDefType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * <B>Channel-specific retrieval and modification.</B>
38 * This class implements the Channel <B>position</B> of the Thing <B>actuator</B>:
40 * <LI><I>Velux</I> <B>bridge</B> → <B>OpenHAB</B>:
42 * Information retrieval by method {@link #handleRefresh}.</LI>
45 * <LI><B>OpenHAB</B> Event Bus → <I>Velux</I> <B>bridge</B>
47 * Sending commands and value updates by method {@link #handleCommand}.</LI>
50 * @author Guenther Schreiner - Initial contribution.
53 final class ChannelActuatorPosition extends ChannelHandlerTemplate {
54 private static final Logger LOGGER = LoggerFactory.getLogger(ChannelActuatorPosition.class);
59 * Suppress default constructor for non-instantiability.
61 private ChannelActuatorPosition() {
62 throw new AssertionError();
68 * Communication method to retrieve information to update the channel value.
70 * @param channelUID The item passed as type {@link ChannelUID} for which a refresh is intended.
71 * @param channelId The same item passed as type {@link String} for which a refresh is intended.
72 * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
73 * information for this channel.
74 * @return newState The value retrieved for the passed channel, or <I>null</I> in case if there is no (new) value.
76 static @Nullable State handleRefresh(ChannelUID channelUID, String channelId,
77 VeluxBridgeHandler thisBridgeHandler) {
78 LOGGER.debug("handleRefresh({},{},{}) called.", channelUID, channelId, thisBridgeHandler);
79 State newState = null;
80 do { // just for common exit
81 if (thisBridgeHandler.bridgeParameters.actuators.autoRefresh(thisBridgeHandler.thisBridge)) {
82 LOGGER.trace("handleRefresh(): there are some existing products.");
84 Thing2VeluxActuator veluxActuator = thisBridgeHandler.channel2VeluxActuator.get(channelUID);
85 if (veluxActuator == null || !veluxActuator.isKnown()) {
86 LOGGER.warn("handleRefresh(): unknown actuator.");
89 GetProduct bcp = thisBridgeHandler.thisBridge.bridgeAPI().getProduct();
91 LOGGER.trace("handleRefresh(): aborting processing as handler is null.");
94 bcp.setProductId(veluxActuator.getProductBridgeIndex().toInt());
95 if (thisBridgeHandler.thisBridge.bridgeCommunicate(bcp) && bcp.isCommunicationSuccessful()) {
97 VeluxProduct product = bcp.getProduct();
98 VeluxProductPosition position = new VeluxProductPosition(product.getDisplayPosition());
99 if (position.isValid()) {
100 if (CHANNEL_ACTUATOR_POSITION.equals(channelId)) {
101 newState = position.getPositionAsPercentType(veluxActuator.isInverted());
102 LOGGER.trace("handleRefresh(): position of actuator is {}%.", newState);
104 } else if (CHANNEL_ACTUATOR_STATE.equals(channelId)) {
105 newState = OnOffType.from(
106 position.getPositionAsPercentType(veluxActuator.isInverted()).intValue() > 50);
107 LOGGER.trace("handleRefresh(): state of actuator is {}.", newState);
111 LOGGER.trace("handleRefresh(): position of actuator is 'UNDEFINED'.");
112 newState = UnDefType.UNDEF;
113 } catch (Exception e) {
114 LOGGER.warn("handleRefresh(): getProducts() exception: {}.", e.getMessage());
117 } while (false); // common exit
118 LOGGER.trace("handleRefresh() returns {}.", newState);
123 * Communication method to update the real world according to the passed channel value (or command).
125 * @param channelUID The item passed as type {@link ChannelUID} for which to following command is addressed to.
126 * @param channelId The same item passed as type {@link String} for which a refresh is intended.
127 * @param command The command passed as type {@link Command} for the mentioned item.
128 * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
129 * information for this channel.
130 * @return newValue ...
132 @SuppressWarnings("PMD.CompareObjectsWithEquals")
133 static @Nullable Command handleCommand(ChannelUID channelUID, String channelId, Command command,
134 VeluxBridgeHandler thisBridgeHandler) {
135 LOGGER.debug("handleCommand({},{},{},{}) called.", channelUID, channelId, command, thisBridgeHandler);
136 Command newValue = null;
137 do { // just for common exit
138 if (thisBridgeHandler.bridgeParameters.actuators.autoRefresh(thisBridgeHandler.thisBridge)) {
139 LOGGER.trace("handleCommand(): there are some existing products.");
141 Thing2VeluxActuator veluxActuator = thisBridgeHandler.channel2VeluxActuator.get(channelUID);
142 if (veluxActuator == null || !veluxActuator.isKnown()) {
143 LOGGER.warn("handleRefresh(): unknown actuator.");
146 VeluxProductPosition targetLevel = VeluxProductPosition.UNKNOWN;
147 if (CHANNEL_ACTUATOR_POSITION.equals(channelId)) {
148 if (command instanceof UpDownType) {
149 LOGGER.trace("handleCommand(): found UpDownType.{} command.", command);
150 targetLevel = UpDownType.UP.equals(command) ^ veluxActuator.isInverted()
151 ? new VeluxProductPosition(PercentType.ZERO)
152 : new VeluxProductPosition(PercentType.HUNDRED);
153 } else if (command instanceof StopMoveType) {
154 LOGGER.trace("handleCommand(): found StopMoveType.{} command.", command);
155 targetLevel = StopMoveType.STOP.equals(command) ? new VeluxProductPosition() : targetLevel;
156 } else if (command instanceof PercentType) {
157 LOGGER.trace("handleCommand(): found PercentType.{} command", command);
158 PercentType ptCommand = (PercentType) command;
159 if (veluxActuator.isInverted()) {
160 ptCommand = new PercentType(PercentType.HUNDRED.intValue() - ptCommand.intValue());
162 LOGGER.trace("handleCommand(): found command to set level to {}.", ptCommand);
163 targetLevel = new VeluxProductPosition(ptCommand);
165 } else if (CHANNEL_ACTUATOR_STATE.equals(channelId)) {
166 if (command instanceof OnOffType) {
167 LOGGER.trace("handleCommand(): found OnOffType.{} command.", command);
168 targetLevel = OnOffType.OFF.equals(command) ^ veluxActuator.isInverted()
169 ? new VeluxProductPosition(PercentType.ZERO)
170 : new VeluxProductPosition(PercentType.HUNDRED);
173 if (targetLevel == VeluxProductPosition.UNKNOWN) {
174 LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
177 LOGGER.debug("handleCommand(): sending command with target level {}.", targetLevel);
178 new VeluxBridgeRunProductCommand().sendCommand(thisBridgeHandler.thisBridge,
179 veluxActuator.getProductBridgeIndex().toInt(), targetLevel);
180 LOGGER.trace("handleCommand(): The new shutter level will be send through the home monitoring events.");
181 if (thisBridgeHandler.bridgeParameters.actuators.autoRefresh(thisBridgeHandler.thisBridge)) {
182 LOGGER.trace("handleCommand(): position of actuators are updated.");
184 } while (false); // common exit