]> git.basschouten.com Git - openhab-addons.git/blob
c9b83f0633aa1cb0e3fee7fc737bf5e96e73d48d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.CHANNEL_ACTUATOR_POSITION;
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             GetProduct bcp = thisBridgeHandler.thisBridge.bridgeAPI().getProduct();
86             if (bcp == null) {
87                 LOGGER.trace("handleRefresh(): aborting processing as handler is null.");
88                 break;
89             }
90             bcp.setProductId(veluxActuator.getProductBridgeIndex().toInt());
91             if (thisBridgeHandler.thisBridge.bridgeCommunicate(bcp) && bcp.isCommunicationSuccessful()) {
92                 try {
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;
99                         break;
100                     }
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());
105                 }
106             }
107         } while (false); // common exit
108         LOGGER.trace("handleRefresh() returns {}.", newState);
109         return newState;
110     }
111
112     /**
113      * Communication method to update the real world according to the passed channel value (or command).
114      *
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 ...
121      */
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.");
130             }
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());
150                     }
151                     LOGGER.trace("handleCommand(): found command to set level to {}.", ptCommand);
152                     targetLevel = new VeluxProductPosition(ptCommand);
153                 } else {
154                     LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
155                     break;
156                 }
157             } else {
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);
166                 } else {
167                     LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
168                     break;
169                 }
170             }
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.");
175
176             if (thisBridgeHandler.bridgeParameters.actuators.autoRefresh(thisBridgeHandler.thisBridge)) {
177                 LOGGER.trace("handleCommand(): position of actuators are updated.");
178             }
179         } while (false); // common exit
180         return newValue;
181     }
182 }