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.core.library.types.OnOffType;
20 import org.openhab.core.library.types.PercentType;
21 import org.openhab.core.library.types.StopMoveType;
22 import org.openhab.core.library.types.UpDownType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.State;
26 import org.openhab.binding.velux.internal.bridge.VeluxBridgeRunProductCommand;
27 import org.openhab.binding.velux.internal.bridge.common.GetProduct;
28 import org.openhab.binding.velux.internal.handler.utils.Thing2VeluxActuator;
29 import org.openhab.binding.velux.internal.things.VeluxProductPosition;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * <B>Channel-specific retrieval and modification.</B>
36 * This class implements the Channel <B>position</B> of the Thing <B>actuator</B>:
38 * <LI><I>Velux</I> <B>bridge</B> → <B>OpenHAB</B>:
40 * Information retrieval by method {@link #handleRefresh}.</LI>
43 * <LI><B>OpenHAB</B> Event Bus → <I>Velux</I> <B>bridge</B>
45 * Sending commands and value updates by method {@link #handleCommand}.</LI>
48 * @author Guenther Schreiner - Initial contribution.
51 final class ChannelActuatorPosition extends ChannelHandlerTemplate {
52 private static final Logger LOGGER = LoggerFactory.getLogger(ChannelActuatorPosition.class);
57 * Suppress default constructor for non-instantiability.
59 private ChannelActuatorPosition() {
60 throw new AssertionError();
66 * Communication method to retrieve information to update the channel value.
68 * @param channelUID The item passed as type {@link ChannelUID} for which a refresh is intended.
69 * @param channelId The same item passed as type {@link String} for which a refresh is intended.
70 * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
71 * information for this channel.
72 * @return newState The value retrieved for the passed channel, or <I>null</I> in case if there is no (new) value.
74 static @Nullable State handleRefresh(ChannelUID channelUID, String channelId,
75 VeluxBridgeHandler thisBridgeHandler) {
76 LOGGER.debug("handleRefresh({},{},{}) called.", channelUID, channelId, thisBridgeHandler);
77 State newState = null;
78 do { // just for common exit
79 if (thisBridgeHandler.bridgeParameters.actuators.autoRefresh(thisBridgeHandler.thisBridge)) {
80 LOGGER.trace("handleRefresh(): there are some existing products.");
82 Thing2VeluxActuator veluxActuator = thisBridgeHandler.channel2VeluxActuator.get(channelUID);
83 GetProduct bcp = thisBridgeHandler.thisBridge.bridgeAPI().getProduct();
85 LOGGER.trace("handleRefresh(): aborting processing as handler is null.");
88 bcp.setProductId(veluxActuator.getProductBridgeIndex().toInt());
89 if (thisBridgeHandler.thisBridge.bridgeCommunicate(bcp) && bcp.isCommunicationSuccessful()) {
91 VeluxProductPosition position = new VeluxProductPosition(bcp.getProduct().getCurrentPosition());
92 if (position.isValid()) {
93 PercentType positionAsPercent = position.getPositionAsPercentType(veluxActuator.isInverted());
94 LOGGER.trace("handleRefresh(): found actuator at level {}.", positionAsPercent);
95 newState = positionAsPercent;
97 LOGGER.trace("handleRefresh(): level of actuator is unknown.");
99 } catch (Exception e) {
100 LOGGER.warn("handleRefresh(): getProducts() exception: {}.", e.getMessage());
103 } while (false); // common exit
104 LOGGER.trace("handleRefresh() returns {}.", newState);
109 * Communication method to update the real world according to the passed channel value (or command).
111 * @param channelUID The item passed as type {@link ChannelUID} for which to following command is addressed to.
112 * @param channelId The same item passed as type {@link String} for which a refresh is intended.
113 * @param command The command passed as type {@link Command} for the mentioned item.
114 * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
115 * information for this channel.
116 * @return newValue ...
118 static @Nullable Command handleCommand(ChannelUID channelUID, String channelId, Command command,
119 VeluxBridgeHandler thisBridgeHandler) {
120 LOGGER.debug("handleCommand({},{},{},{}) called.", channelUID, channelId, command, thisBridgeHandler);
121 Command newValue = null;
122 do { // just for common exit
123 assert thisBridgeHandler.bridgeParameters.actuators != null : "VeluxBridgeHandler.bridgeParameters.actuators not initialized.";
124 if (thisBridgeHandler.bridgeParameters.actuators.autoRefresh(thisBridgeHandler.thisBridge)) {
125 LOGGER.trace("handleCommand(): there are some existing products.");
127 Thing2VeluxActuator veluxActuator = thisBridgeHandler.channel2VeluxActuator.get(channelUID);
128 VeluxProductPosition targetLevel = VeluxProductPosition.UNKNOWN;
129 if (channelId.equals(CHANNEL_ACTUATOR_POSITION)) {
130 if ((command instanceof UpDownType) && (command == UpDownType.UP)) {
131 LOGGER.trace("handleCommand(): found UP command.");
132 targetLevel = veluxActuator.isInverted() ? new VeluxProductPosition(PercentType.HUNDRED)
133 : new VeluxProductPosition(PercentType.ZERO);
134 } else if ((command instanceof UpDownType) && (command == UpDownType.DOWN)) {
135 LOGGER.trace("handleCommand(): found DOWN command.");
136 targetLevel = veluxActuator.isInverted() ? new VeluxProductPosition(PercentType.ZERO)
137 : new VeluxProductPosition(PercentType.HUNDRED);
138 } else if ((command instanceof StopMoveType) && (command == StopMoveType.STOP)) {
139 LOGGER.trace("handleCommand(): found STOP command.");
140 targetLevel = new VeluxProductPosition();
141 } else if (command instanceof PercentType) {
142 LOGGER.trace("handleCommand(): found command of type PercentType.");
143 PercentType ptCommand = (PercentType) command;
144 if (veluxActuator.isInverted()) {
145 ptCommand = new PercentType(PercentType.HUNDRED.intValue() - ptCommand.intValue());
147 LOGGER.trace("handleCommand(): found command to set level to {}.", ptCommand);
148 targetLevel = new VeluxProductPosition(ptCommand);
150 LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
154 if ((command instanceof OnOffType) && (command == OnOffType.ON)) {
155 LOGGER.trace("handleCommand(): found ON command.");
156 targetLevel = veluxActuator.isInverted() ? new VeluxProductPosition(PercentType.HUNDRED)
157 : new VeluxProductPosition(PercentType.ZERO);
158 } else if ((command instanceof OnOffType) && (command == OnOffType.OFF)) {
159 LOGGER.trace("handleCommand(): found OFF command.");
160 targetLevel = veluxActuator.isInverted() ? new VeluxProductPosition(PercentType.ZERO)
161 : new VeluxProductPosition(PercentType.HUNDRED);
163 LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
167 LOGGER.debug("handleCommand(): sending command with target level {}.", targetLevel);
168 new VeluxBridgeRunProductCommand().sendCommand(thisBridgeHandler.thisBridge,
169 veluxActuator.getProductBridgeIndex().toInt(), targetLevel);
170 LOGGER.trace("handleCommand(): The new shutter level will be send through the home monitoring events.");
172 if (thisBridgeHandler.bridgeParameters.actuators.autoRefresh(thisBridgeHandler.thisBridge)) {
173 LOGGER.trace("handleCommand(): position of actuators are updated.");
175 } while (false); // common exit