]> git.basschouten.com Git - openhab-addons.git/blob
634326139de9b664820a25430860476b393a353a
[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.VeluxProductPosition;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.PercentType;
25 import org.openhab.core.library.types.StopMoveType;
26 import org.openhab.core.library.types.UpDownType;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.State;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * <B>Channel-specific retrieval and modification.</B>
35  * <P>
36  * This class implements the Channel <B>position</B> of the Thing <B>actuator</B>:
37  * <UL>
38  * <LI><I>Velux</I> <B>bridge</B> &rarr; <B>OpenHAB</B>:
39  * <P>
40  * Information retrieval by method {@link #handleRefresh}.</LI>
41  * </UL>
42  * <UL>
43  * <LI><B>OpenHAB</B> Event Bus &rarr; <I>Velux</I> <B>bridge</B>
44  * <P>
45  * Sending commands and value updates by method {@link #handleCommand}.</LI>
46  * </UL>
47  *
48  * @author Guenther Schreiner - Initial contribution.
49  */
50 @NonNullByDefault
51 final class ChannelActuatorPosition extends ChannelHandlerTemplate {
52     private static final Logger LOGGER = LoggerFactory.getLogger(ChannelActuatorPosition.class);
53
54     // Constructors
55
56     /**
57      * Suppress default constructor for non-instantiability.
58      */
59     private ChannelActuatorPosition() {
60         throw new AssertionError();
61     }
62
63     // Public methods
64
65     /**
66      * Communication method to retrieve information to update the channel value.
67      *
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.
73      */
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.");
81             }
82             Thing2VeluxActuator veluxActuator = thisBridgeHandler.channel2VeluxActuator.get(channelUID);
83             GetProduct bcp = thisBridgeHandler.thisBridge.bridgeAPI().getProduct();
84             if (bcp == null) {
85                 LOGGER.trace("handleRefresh(): aborting processing as handler is null.");
86                 break;
87             }
88             bcp.setProductId(veluxActuator.getProductBridgeIndex().toInt());
89             if (thisBridgeHandler.thisBridge.bridgeCommunicate(bcp) && bcp.isCommunicationSuccessful()) {
90                 try {
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;
96                     } else {
97                         LOGGER.trace("handleRefresh(): level of actuator is unknown.");
98                     }
99                 } catch (Exception e) {
100                     LOGGER.warn("handleRefresh(): getProducts() exception: {}.", e.getMessage());
101                 }
102             }
103         } while (false); // common exit
104         LOGGER.trace("handleRefresh() returns {}.", newState);
105         return newState;
106     }
107
108     /**
109      * Communication method to update the real world according to the passed channel value (or command).
110      *
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 ...
117      */
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.");
126             }
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());
146                     }
147                     LOGGER.trace("handleCommand(): found command to set level to {}.", ptCommand);
148                     targetLevel = new VeluxProductPosition(ptCommand);
149                 } else {
150                     LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
151                     break;
152                 }
153             } else {
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);
162                 } else {
163                     LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
164                     break;
165                 }
166             }
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.");
171
172             if (thisBridgeHandler.bridgeParameters.actuators.autoRefresh(thisBridgeHandler.thisBridge)) {
173                 LOGGER.trace("handleCommand(): position of actuators are updated.");
174             }
175         } while (false); // common exit
176         return newValue;
177     }
178 }