2 * Copyright (c) 2010-2023 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.common.GetProduct;
20 import org.openhab.binding.velux.internal.bridge.common.RunProductCommand;
21 import org.openhab.binding.velux.internal.bridge.slip.FunctionalParameters;
22 import org.openhab.binding.velux.internal.bridge.slip.SCrunProductCommand;
23 import org.openhab.binding.velux.internal.handler.utils.Thing2VeluxActuator;
24 import org.openhab.binding.velux.internal.things.StatusReply;
25 import org.openhab.binding.velux.internal.things.VeluxExistingProducts;
26 import org.openhab.binding.velux.internal.things.VeluxProduct;
27 import org.openhab.binding.velux.internal.things.VeluxProduct.ProductBridgeIndex;
28 import org.openhab.binding.velux.internal.things.VeluxProduct.ProductState;
29 import org.openhab.binding.velux.internal.things.VeluxProductPosition;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.library.types.PercentType;
32 import org.openhab.core.library.types.StopMoveType;
33 import org.openhab.core.library.types.UpDownType;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.State;
37 import org.openhab.core.types.UnDefType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * <B>Channel-specific retrieval and modification.</B>
44 * This class implements the Channel <B>position</B> of the Thing <B>actuator</B>:
46 * <LI><I>Velux</I> <B>bridge</B> → <B>OpenHAB</B>:
48 * Information retrieval by method {@link #handleRefresh}.</LI>
51 * <LI><B>OpenHAB</B> Event Bus → <I>Velux</I> <B>bridge</B>
53 * Sending commands and value updates by method {@link #handleCommand}.</LI>
56 * @author Guenther Schreiner - Initial contribution.
57 * @author Andrew Fiddian-Green - Refactoring and use alternate API set for Vane Position.
60 final class ChannelActuatorPosition extends ChannelHandlerTemplate {
61 private static final Logger LOGGER = LoggerFactory.getLogger(ChannelActuatorPosition.class);
66 * Suppress default constructor for non-instantiability.
68 private ChannelActuatorPosition() {
69 throw new AssertionError();
75 * Communication method to retrieve information to update the channel value.
77 * @param channelUID The item passed as type {@link ChannelUID} for which a refresh is intended.
78 * @param channelId The same item passed as type {@link String} for which a refresh is intended.
79 * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
80 * information for this channel.
81 * @return newState The value retrieved for the passed channel, or <I>null</I> in case if there is no (new) value.
83 static @Nullable State handleRefresh(ChannelUID channelUID, String channelId,
84 VeluxBridgeHandler thisBridgeHandler) {
85 LOGGER.debug("handleRefresh({},{},{}) called.", channelUID, channelId, thisBridgeHandler);
86 State newState = null;
87 do { // just for common exit
88 if (thisBridgeHandler.bridgeParameters.actuators.autoRefresh(thisBridgeHandler.thisBridge)) {
89 LOGGER.trace("handleRefresh(): there are some existing products.");
92 Thing2VeluxActuator veluxActuator = thisBridgeHandler.channel2VeluxActuator.get(channelUID);
93 if (veluxActuator == null || !veluxActuator.isKnown()) {
94 LOGGER.warn("handleRefresh(): unknown actuator.");
98 GetProduct bcp = null;
100 case CHANNEL_ACTUATOR_POSITION:
101 case CHANNEL_ACTUATOR_STATE:
102 case CHANNEL_VANE_POSITION:
103 bcp = thisBridgeHandler.thisBridge.bridgeAPI().getProductStatus();
105 // unknown channel, will exit
109 LOGGER.trace("handleRefresh(): aborting processing as handler is null.");
113 bcp.setProductId(veluxActuator.getProductBridgeIndex().toInt());
114 if ((!thisBridgeHandler.thisBridge.bridgeCommunicate(bcp)) || (!bcp.isCommunicationSuccessful())) {
115 LOGGER.trace("handleRefresh(): bridge communication request failed.");
119 VeluxProduct newProduct = bcp.getProduct();
120 ProductBridgeIndex productBridgeIndex = newProduct.getBridgeProductIndex();
121 VeluxExistingProducts existingProducts = thisBridgeHandler.existingProducts();
122 VeluxProduct existingProduct = existingProducts.get(productBridgeIndex);
123 ProductState productState = newProduct.getProductState();
124 switch (productState) {
129 if (!VeluxProduct.UNKNOWN.equals(existingProduct)) {
131 case CHANNEL_VANE_POSITION:
132 case CHANNEL_ACTUATOR_POSITION:
133 case CHANNEL_ACTUATOR_STATE: {
134 if (existingProducts.update(newProduct)) {
135 existingProduct = existingProducts.get(productBridgeIndex);
136 int posValue = VeluxProductPosition.VPP_VELUX_UNKNOWN;
138 case CHANNEL_VANE_POSITION:
139 posValue = existingProduct.getVaneDisplayPosition();
141 case CHANNEL_ACTUATOR_POSITION:
142 case CHANNEL_ACTUATOR_STATE:
143 posValue = existingProduct.getDisplayPosition();
145 VeluxProductPosition position = new VeluxProductPosition(posValue);
146 if (position.isValid()) {
148 case CHANNEL_VANE_POSITION:
149 newState = position.getPositionAsPercentType(false);
151 case CHANNEL_ACTUATOR_POSITION:
153 .getPositionAsPercentType(veluxActuator.isInverted());
155 case CHANNEL_ACTUATOR_STATE:
156 newState = OnOffType.from(
157 position.getPositionAsPercentType(veluxActuator.isInverted())
166 case WAITING_FOR_POWER:
168 StatusReply statusReply = productState == ProductState.WAITING_FOR_POWER
169 ? StatusReply.NODE_WAITING_FOR_POWER
170 : bcp.getStatusReply();
171 if (statusReply.isError()) {
172 String id = VeluxProduct.UNKNOWN.equals(existingProduct)
173 ? newProduct.getBridgeProductIndex().toString()
174 : existingProduct.getProductUniqueIndex();
175 if (statusReply.isCriticalError()) {
176 LOGGER.warn("Product Id:{} encountered an error with StatusReply:{}", id, statusReply);
178 LOGGER.info("Product Id:{} encountered an error with StatusReply:{}", id, statusReply);
184 if (newState == null) {
185 newState = UnDefType.UNDEF;
187 } while (false); // common exit
188 LOGGER.trace("handleRefresh(): new state for channel id '{}' is '{}'.", channelId, newState);
193 * Communication method to update the real world according to the passed channel value (or command).
195 * @param channelUID The item passed as type {@link ChannelUID} for which to following command is addressed to.
196 * @param channelId The same item passed as type {@link String} for which a refresh is intended.
197 * @param command The command passed as type {@link Command} for the mentioned item.
198 * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
199 * information for this channel.
200 * @return newValue ...
202 static @Nullable Command handleCommand(ChannelUID channelUID, String channelId, Command command,
203 VeluxBridgeHandler thisBridgeHandler) {
204 LOGGER.debug("handleCommand({},{},{},{}) called.", channelUID, channelId, command, thisBridgeHandler);
205 Command newValue = null;
206 do { // just for common exit
207 if (thisBridgeHandler.bridgeParameters.actuators.autoRefresh(thisBridgeHandler.thisBridge)) {
208 LOGGER.trace("handleCommand(): there are some existing products.");
211 Thing2VeluxActuator veluxActuator = thisBridgeHandler.channel2VeluxActuator.get(channelUID);
212 if (veluxActuator == null || !veluxActuator.isKnown()) {
213 LOGGER.warn("handleRefresh(): unknown actuator.");
217 VeluxProductPosition mainParameter = null;
218 FunctionalParameters functionalParameters = null;
219 VeluxExistingProducts existingProducts = thisBridgeHandler.existingProducts();
220 ProductBridgeIndex productBridgeIndex = veluxActuator.getProductBridgeIndex();
223 case CHANNEL_VANE_POSITION:
224 if (command instanceof PercentType) {
225 VeluxProduct existingProductClone = existingProducts.get(productBridgeIndex).clone();
226 existingProductClone.setVanePosition(
227 new VeluxProductPosition((PercentType) command).getPositionAsVeluxType());
228 functionalParameters = existingProductClone.getFunctionalParameters();
232 case CHANNEL_ACTUATOR_POSITION:
233 if (command instanceof UpDownType) {
234 mainParameter = UpDownType.UP.equals(command) ^ veluxActuator.isInverted()
235 ? new VeluxProductPosition(PercentType.ZERO)
236 : new VeluxProductPosition(PercentType.HUNDRED);
237 } else if (command instanceof StopMoveType) {
238 mainParameter = StopMoveType.STOP.equals(command) ? new VeluxProductPosition() : mainParameter;
239 } else if (command instanceof PercentType) {
240 PercentType ptCommand = (PercentType) command;
241 if (veluxActuator.isInverted()) {
242 ptCommand = new PercentType(PercentType.HUNDRED.intValue() - ptCommand.intValue());
244 mainParameter = new VeluxProductPosition(ptCommand);
248 case CHANNEL_ACTUATOR_STATE:
249 if (command instanceof OnOffType) {
250 mainParameter = OnOffType.OFF.equals(command) ^ veluxActuator.isInverted()
251 ? new VeluxProductPosition(PercentType.ZERO)
252 : new VeluxProductPosition(PercentType.HUNDRED);
257 // unknown channel => do nothing..
260 if ((mainParameter != null) || (functionalParameters != null)) {
261 LOGGER.debug("handleCommand(): sending command '{}' for channel id '{}'.", command, channelId);
262 RunProductCommand bcp = thisBridgeHandler.thisBridge.bridgeAPI().runProductCommand();
263 boolean success = false;
264 if (bcp instanceof SCrunProductCommand) {
266 if (bcp.setNodeIdAndParameters(productBridgeIndex.toInt(), mainParameter, functionalParameters)
267 && thisBridgeHandler.thisBridge.bridgeCommunicate(bcp)
268 && bcp.isCommunicationSuccessful()) {
270 if (thisBridgeHandler.bridgeParameters.actuators
271 .autoRefresh(thisBridgeHandler.thisBridge)) {
272 LOGGER.trace("handleCommand(): actuator position will be updated via polling.");
274 if (existingProducts.update(((SCrunProductCommand) bcp).getProduct())) {
275 LOGGER.trace("handleCommand(): actuator position immediate update requested.");
280 LOGGER.debug("handleCommand(): sendCommand() finished {}.",
281 (success ? "successfully" : "with failure"));
283 LOGGER.info("handleCommand(): ignoring command '{}' for channel id '{}'.", command, channelId);
285 } while (false); // common exit