]> git.basschouten.com Git - openhab-addons.git/blob
916c21c628c435f46fe06506bab9fceaad5e703c
[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.utils;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.velux.internal.VeluxBindingConstants;
17 import org.openhab.binding.velux.internal.VeluxBindingProperties;
18 import org.openhab.binding.velux.internal.handler.VeluxBridgeHandler;
19 import org.openhab.binding.velux.internal.things.VeluxExistingProducts;
20 import org.openhab.binding.velux.internal.things.VeluxProduct;
21 import org.openhab.binding.velux.internal.things.VeluxProduct.ProductBridgeIndex;
22 import org.openhab.binding.velux.internal.things.VeluxProductSerialNo;
23 import org.openhab.core.thing.ChannelUID;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /***
28  * The class {@link Thing2VeluxActuator} provides simplified access to Velux device behind the Velux bridge by
29  * evaluating the
30  * Thing property belonging to a channel and comparing them with the bridge registered objects. To put it in a nutshell,
31  * the methods provide a cache for faster access,
32  * <ul>
33  * <li>{@link #Thing2VeluxActuator} Constructor,</LI>
34  * <li>{@link #isKnown} returns whether actuator is well-known,</LI>
35  * <li>{@link #getProductBridgeIndex} returns the Velux bridge index for access,</LI>
36  * <li>{@link #isInverted} returns a flag about value inversion.</LI>
37  * </UL>
38  *
39  * @author Guenther Schreiner - Initial contribution
40  */
41 @NonNullByDefault
42 public class Thing2VeluxActuator {
43     private final Logger logger = LoggerFactory.getLogger(Thing2VeluxActuator.class);
44
45     // Class internal
46
47     private VeluxBridgeHandler bridgeHandler;
48     private ChannelUID channelUID;
49     private boolean isInverted = false;
50     private VeluxProduct thisProduct = VeluxProduct.UNKNOWN;
51
52     // Private
53
54     private void mapThing2Velux() {
55         if (channelUID.toString().startsWith(VeluxBindingConstants.BRIDGE_THING_TYPE_UID)) {
56             logger.trace("mapThing2Velux(): channel {} is on a Bridge Thing, exiting.", channelUID);
57             return;
58         }
59
60         // the uniqueIndex is the serial number (if valid)
61         String uniqueIndex = null;
62         boolean invert = false;
63         if (ThingConfiguration.exists(bridgeHandler, channelUID, VeluxBindingProperties.CONFIG_ACTUATOR_SERIALNUMBER)) {
64             String serial = (String) ThingConfiguration.getValue(bridgeHandler, channelUID,
65                     VeluxBindingProperties.CONFIG_ACTUATOR_SERIALNUMBER);
66             invert = VeluxProductSerialNo.indicatesRevertedValues(serial);
67             serial = VeluxProductSerialNo.cleaned(serial);
68             uniqueIndex = ("".equals(serial) || serial.equals(VeluxProductSerialNo.UNKNOWN)) ? null : serial;
69         }
70         logger.trace("mapThing2Velux(): in {} serialNumber={}.", channelUID, uniqueIndex);
71
72         // if serial number not valid, the uniqueIndex is name (if valid)
73         if (uniqueIndex == null) {
74             if (ThingConfiguration.exists(bridgeHandler, channelUID, VeluxBindingProperties.PROPERTY_ACTUATOR_NAME)) {
75                 String name = (String) ThingConfiguration.getValue(bridgeHandler, channelUID,
76                         VeluxBindingProperties.PROPERTY_ACTUATOR_NAME);
77                 uniqueIndex = ("".equals(name) || name.equals(VeluxBindingConstants.UNKNOWN)) ? null : name;
78             }
79             logger.trace("mapThing2Velux(): in {} name={}.", channelUID, uniqueIndex);
80         }
81
82         if (uniqueIndex == null) {
83             logger.warn("mapThing2Velux(): in {} cannot find a uniqueIndex, aborting.", channelUID);
84             return;
85         } else {
86             logger.trace("mapThing2Velux(): in {} uniqueIndex={}, proceeding.", channelUID, uniqueIndex);
87         }
88
89         // handle value inversion
90         if (!invert) {
91             if (ThingConfiguration.exists(bridgeHandler, channelUID,
92                     VeluxBindingProperties.PROPERTY_ACTUATOR_INVERTED)) {
93                 invert = (boolean) ThingConfiguration.getValue(bridgeHandler, channelUID,
94                         VeluxBindingProperties.PROPERTY_ACTUATOR_INVERTED);
95             }
96         }
97         isInverted = invert;
98         logger.trace("mapThing2Velux(): in {} isInverted={}.", channelUID, isInverted);
99
100         VeluxExistingProducts existing = bridgeHandler.bridgeParameters.actuators.getChannel().existingProducts;
101
102         if (!existing.isRegistered(uniqueIndex)) {
103             logger.warn("mapThing2Velux(): actuator with uniqueIndex={} is not registered", uniqueIndex);
104             return;
105         }
106
107         logger.trace("mapThing2Velux(): fetching actuator for {}.", uniqueIndex);
108         thisProduct = existing.get(uniqueIndex);
109         logger.debug("mapThing2Velux(): found actuator {}.", thisProduct);
110         return;
111     }
112
113     // Constructor
114
115     /**
116      * Constructor.
117      * <P>
118      *
119      * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
120      *            information for this channel.
121      * @param thisChannelUID The item passed as type {@link ChannelUID} for which a refresh is intended.
122      */
123     public Thing2VeluxActuator(VeluxBridgeHandler thisBridgeHandler, ChannelUID thisChannelUID) {
124         bridgeHandler = thisBridgeHandler;
125         channelUID = thisChannelUID;
126     }
127
128     // Public methods
129
130     /**
131      * Returns the Velux gateway index for accessing a Velux device based on the Thing configuration which belongs to
132      * the channel passed during constructor.
133      * <p>
134      *
135      * @return <b>bridgeProductIndex</B> for accessing the Velux device (or ProductBridgeIndex.UNKNOWN if not found).
136      */
137     public ProductBridgeIndex getProductBridgeIndex() {
138         if (thisProduct == VeluxProduct.UNKNOWN) {
139             mapThing2Velux();
140         }
141         if (thisProduct == VeluxProduct.UNKNOWN) {
142             return ProductBridgeIndex.UNKNOWN;
143         }
144         return thisProduct.getBridgeProductIndex();
145     }
146
147     /**
148      * Returns true, if the actuator is known within the bridge.
149      * <p>
150      *
151      * @return <b>isKnown</B> as boolean.
152      */
153     public boolean isKnown() {
154         return (!(this.getProductBridgeIndex() == ProductBridgeIndex.UNKNOWN));
155     }
156
157     /**
158      * Returns the flag whether a value inversion in intended for the Velux device based on the Thing configuration
159      * which belongs to the channel passed during constructor.
160      * <p>
161      *
162      * @return <b>isInverted</B> for handling of values of the Velux device (or false if not found)..
163      */
164     public boolean isInverted() {
165         if (thisProduct == VeluxProduct.UNKNOWN) {
166             mapThing2Velux();
167         }
168         if (thisProduct == VeluxProduct.UNKNOWN) {
169             logger.warn("isInverted(): Thing not found in Velux Bridge.");
170         }
171         return isInverted;
172     }
173 }