]> git.basschouten.com Git - openhab-addons.git/blob
e33bbb283bda23dacb9e40c86ae169a1d6fe766f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.velux.internal.handler;
14
15 import static org.openhab.binding.velux.internal.VeluxBindingConstants.*;
16
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;
34
35 /**
36  * <B>Channel-specific retrieval and modification.</B>
37  * <P>
38  * This class implements the Channel <B>position</B> of the Thing <B>actuator</B>:
39  * <UL>
40  * <LI><I>Velux</I> <B>bridge</B> &rarr; <B>OpenHAB</B>:
41  * <P>
42  * Information retrieval by method {@link #handleRefresh}.</LI>
43  * </UL>
44  * <UL>
45  * <LI><B>OpenHAB</B> Event Bus &rarr; <I>Velux</I> <B>bridge</B>
46  * <P>
47  * Sending commands and value updates by method {@link #handleCommand}.</LI>
48  * </UL>
49  *
50  * @author Guenther Schreiner - Initial contribution.
51  */
52 @NonNullByDefault
53 final class ChannelActuatorPosition extends ChannelHandlerTemplate {
54     private static final Logger LOGGER = LoggerFactory.getLogger(ChannelActuatorPosition.class);
55
56     // Constructors
57
58     /**
59      * Suppress default constructor for non-instantiability.
60      */
61     private ChannelActuatorPosition() {
62         throw new AssertionError();
63     }
64
65     // Public methods
66
67     /**
68      * Communication method to retrieve information to update the channel value.
69      *
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.
75      */
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.");
83             }
84             Thing2VeluxActuator veluxActuator = thisBridgeHandler.channel2VeluxActuator.get(channelUID);
85             if (veluxActuator == null || !veluxActuator.isKnown()) {
86                 LOGGER.warn("handleRefresh(): unknown actuator.");
87                 break;
88             }
89             GetProduct bcp = thisBridgeHandler.thisBridge.bridgeAPI().getProduct();
90             if (bcp == null) {
91                 LOGGER.trace("handleRefresh(): aborting processing as handler is null.");
92                 break;
93             }
94             bcp.setProductId(veluxActuator.getProductBridgeIndex().toInt());
95             if (thisBridgeHandler.thisBridge.bridgeCommunicate(bcp) && bcp.isCommunicationSuccessful()) {
96                 try {
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);
103                             break;
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);
108                             break;
109                         }
110                     }
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());
115                 }
116             }
117         } while (false); // common exit
118         LOGGER.trace("handleRefresh() returns {}.", newState);
119         return newState;
120     }
121
122     /**
123      * Communication method to update the real world according to the passed channel value (or command).
124      *
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 ...
131      */
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.");
140             }
141             Thing2VeluxActuator veluxActuator = thisBridgeHandler.channel2VeluxActuator.get(channelUID);
142             if (veluxActuator == null || !veluxActuator.isKnown()) {
143                 LOGGER.warn("handleRefresh(): unknown actuator.");
144                 break;
145             }
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());
161                     }
162                     LOGGER.trace("handleCommand(): found command to set level to {}.", ptCommand);
163                     targetLevel = new VeluxProductPosition(ptCommand);
164                 }
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);
171                 }
172             }
173             if (targetLevel == VeluxProductPosition.UNKNOWN) {
174                 LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
175                 break;
176             }
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.");
183             }
184         } while (false); // common exit
185         return newValue;
186     }
187 }