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.velux.internal.handler;
15 import static org.openhab.binding.velux.internal.VeluxBindingConstants.CHANNEL_ACTUATOR_POSITION;
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 GetProduct bcp = thisBridgeHandler.thisBridge.bridgeAPI().getProduct();
87 LOGGER.trace("handleRefresh(): aborting processing as handler is null.");
90 bcp.setProductId(veluxActuator.getProductBridgeIndex().toInt());
91 if (thisBridgeHandler.thisBridge.bridgeCommunicate(bcp) && bcp.isCommunicationSuccessful()) {
93 VeluxProduct product = bcp.getProduct();
94 VeluxProductPosition position = new VeluxProductPosition(product.getDisplayPosition());
95 if (position.isValid()) {
96 PercentType posPercent = position.getPositionAsPercentType(veluxActuator.isInverted());
97 LOGGER.trace("handleRefresh(): position of actuator is {}%.", posPercent);
98 newState = posPercent;
101 LOGGER.trace("handleRefresh(): position of actuator is 'UNDEFINED'.");
102 newState = UnDefType.UNDEF;
103 } catch (Exception e) {
104 LOGGER.warn("handleRefresh(): getProducts() exception: {}.", e.getMessage());
107 } while (false); // common exit
108 LOGGER.trace("handleRefresh() returns {}.", newState);
113 * Communication method to update the real world according to the passed channel value (or command).
115 * @param channelUID The item passed as type {@link ChannelUID} for which to following command is addressed to.
116 * @param channelId The same item passed as type {@link String} for which a refresh is intended.
117 * @param command The command passed as type {@link Command} for the mentioned item.
118 * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
119 * information for this channel.
120 * @return newValue ...
122 static @Nullable Command handleCommand(ChannelUID channelUID, String channelId, Command command,
123 VeluxBridgeHandler thisBridgeHandler) {
124 LOGGER.debug("handleCommand({},{},{},{}) called.", channelUID, channelId, command, thisBridgeHandler);
125 Command newValue = null;
126 do { // just for common exit
127 assert thisBridgeHandler.bridgeParameters.actuators != null : "VeluxBridgeHandler.bridgeParameters.actuators not initialized.";
128 if (thisBridgeHandler.bridgeParameters.actuators.autoRefresh(thisBridgeHandler.thisBridge)) {
129 LOGGER.trace("handleCommand(): there are some existing products.");
131 Thing2VeluxActuator veluxActuator = thisBridgeHandler.channel2VeluxActuator.get(channelUID);
132 VeluxProductPosition targetLevel = VeluxProductPosition.UNKNOWN;
133 if (channelId.equals(CHANNEL_ACTUATOR_POSITION)) {
134 if ((command instanceof UpDownType) && (command == UpDownType.UP)) {
135 LOGGER.trace("handleCommand(): found UP command.");
136 targetLevel = veluxActuator.isInverted() ? new VeluxProductPosition(PercentType.HUNDRED)
137 : new VeluxProductPosition(PercentType.ZERO);
138 } else if ((command instanceof UpDownType) && (command == UpDownType.DOWN)) {
139 LOGGER.trace("handleCommand(): found DOWN command.");
140 targetLevel = veluxActuator.isInverted() ? new VeluxProductPosition(PercentType.ZERO)
141 : new VeluxProductPosition(PercentType.HUNDRED);
142 } else if ((command instanceof StopMoveType) && (command == StopMoveType.STOP)) {
143 LOGGER.trace("handleCommand(): found STOP command.");
144 targetLevel = new VeluxProductPosition();
145 } else if (command instanceof PercentType) {
146 LOGGER.trace("handleCommand(): found command of type PercentType.");
147 PercentType ptCommand = (PercentType) command;
148 if (veluxActuator.isInverted()) {
149 ptCommand = new PercentType(PercentType.HUNDRED.intValue() - ptCommand.intValue());
151 LOGGER.trace("handleCommand(): found command to set level to {}.", ptCommand);
152 targetLevel = new VeluxProductPosition(ptCommand);
154 LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
158 if ((command instanceof OnOffType) && (command == OnOffType.ON)) {
159 LOGGER.trace("handleCommand(): found ON command.");
160 targetLevel = veluxActuator.isInverted() ? new VeluxProductPosition(PercentType.HUNDRED)
161 : new VeluxProductPosition(PercentType.ZERO);
162 } else if ((command instanceof OnOffType) && (command == OnOffType.OFF)) {
163 LOGGER.trace("handleCommand(): found OFF command.");
164 targetLevel = veluxActuator.isInverted() ? new VeluxProductPosition(PercentType.ZERO)
165 : new VeluxProductPosition(PercentType.HUNDRED);
167 LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
171 LOGGER.debug("handleCommand(): sending command with target level {}.", targetLevel);
172 new VeluxBridgeRunProductCommand().sendCommand(thisBridgeHandler.thisBridge,
173 veluxActuator.getProductBridgeIndex().toInt(), targetLevel);
174 LOGGER.trace("handleCommand(): The new shutter level will be send through the home monitoring events.");
176 if (thisBridgeHandler.bridgeParameters.actuators.autoRefresh(thisBridgeHandler.thisBridge)) {
177 LOGGER.trace("handleCommand(): position of actuators are updated.");
179 } while (false); // common exit