]> git.basschouten.com Git - openhab-addons.git/blob
085b76adda9758b6f365ed6edf5e20475a7af5c8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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         // only process real actuator things
56         if (!VeluxBindingConstants.ACTUATOR_THINGS.contains(bridgeHandler.thingTypeUIDOf(channelUID))) {
57             logger.trace("mapThing2Velux(): channel {} is not an Actuator Thing, exiting.", channelUID);
58             return;
59         }
60
61         // the uniqueIndex is the serial number (if valid)
62         String uniqueIndex = null;
63         boolean invert = false;
64         if (ThingConfiguration.exists(bridgeHandler, channelUID, VeluxBindingProperties.CONFIG_ACTUATOR_SERIALNUMBER)) {
65             String serial = (String) ThingConfiguration.getValue(bridgeHandler, channelUID,
66                     VeluxBindingProperties.CONFIG_ACTUATOR_SERIALNUMBER);
67             invert = VeluxProductSerialNo.indicatesRevertedValues(serial);
68             serial = VeluxProductSerialNo.cleaned(serial);
69             uniqueIndex = ("".equals(serial) || serial.equals(VeluxProductSerialNo.UNKNOWN)) ? null : serial;
70         }
71         logger.trace("mapThing2Velux(): in {} serialNumber={}.", channelUID, uniqueIndex);
72
73         // if serial number not valid, the uniqueIndex is name (if valid)
74         if (uniqueIndex == null) {
75             if (ThingConfiguration.exists(bridgeHandler, channelUID, VeluxBindingProperties.PROPERTY_ACTUATOR_NAME)) {
76                 String name = (String) ThingConfiguration.getValue(bridgeHandler, channelUID,
77                         VeluxBindingProperties.PROPERTY_ACTUATOR_NAME);
78                 uniqueIndex = ("".equals(name) || name.equals(VeluxBindingConstants.UNKNOWN)) ? null : name;
79             }
80             logger.trace("mapThing2Velux(): in {} name={}.", channelUID, uniqueIndex);
81         }
82
83         if (uniqueIndex == null) {
84             logger.warn("mapThing2Velux(): in {} cannot find a uniqueIndex, aborting.", channelUID);
85             return;
86         } else {
87             logger.trace("mapThing2Velux(): in {} uniqueIndex={}, proceeding.", channelUID, uniqueIndex);
88         }
89
90         // handle value inversion
91         if (!invert) {
92             if (ThingConfiguration.exists(bridgeHandler, channelUID,
93                     VeluxBindingProperties.PROPERTY_ACTUATOR_INVERTED)) {
94                 invert = (boolean) ThingConfiguration.getValue(bridgeHandler, channelUID,
95                         VeluxBindingProperties.PROPERTY_ACTUATOR_INVERTED);
96             }
97         }
98         isInverted = invert;
99         logger.trace("mapThing2Velux(): in {} isInverted={}.", channelUID, isInverted);
100
101         VeluxExistingProducts existing = bridgeHandler.bridgeParameters.actuators.getChannel().existingProducts;
102
103         if (!existing.isRegistered(uniqueIndex)) {
104             logger.warn("mapThing2Velux(): actuator with uniqueIndex={} is not registered", uniqueIndex);
105             return;
106         }
107
108         logger.trace("mapThing2Velux(): fetching actuator for {}.", uniqueIndex);
109         thisProduct = existing.get(uniqueIndex);
110         logger.debug("mapThing2Velux(): found actuator {}.", thisProduct);
111         return;
112     }
113
114     // Constructor
115
116     /**
117      * Constructor.
118      * <P>
119      *
120      * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
121      *            information for this channel.
122      * @param thisChannelUID The item passed as type {@link ChannelUID} for which a refresh is intended.
123      */
124     public Thing2VeluxActuator(VeluxBridgeHandler thisBridgeHandler, ChannelUID thisChannelUID) {
125         bridgeHandler = thisBridgeHandler;
126         channelUID = thisChannelUID;
127     }
128
129     // Public methods
130
131     /**
132      * Returns the Velux gateway index for accessing a Velux device based on the Thing configuration which belongs to
133      * the channel passed during constructor.
134      * <p>
135      *
136      * @return <b>bridgeProductIndex</B> for accessing the Velux device (or ProductBridgeIndex.UNKNOWN if not found).
137      */
138     public ProductBridgeIndex getProductBridgeIndex() {
139         if (VeluxProduct.UNKNOWN.equals(thisProduct)) {
140             mapThing2Velux();
141         }
142         if (VeluxProduct.UNKNOWN.equals(thisProduct)) {
143             return ProductBridgeIndex.UNKNOWN;
144         }
145         return thisProduct.getBridgeProductIndex();
146     }
147
148     /**
149      * Returns true, if the actuator is known within the bridge.
150      * <p>
151      *
152      * @return <b>isKnown</B> as boolean.
153      */
154     public boolean isKnown() {
155         return (!(ProductBridgeIndex.UNKNOWN.equals(getProductBridgeIndex())));
156     }
157
158     /**
159      * Returns the flag whether a value inversion in intended for the Velux device based on the Thing configuration
160      * which belongs to the channel passed during constructor.
161      * <p>
162      *
163      * @return <b>isInverted</B> for handling of values of the Velux device (or false if not found)..
164      */
165     public boolean isInverted() {
166         if (VeluxProduct.UNKNOWN.equals(thisProduct)) {
167             mapThing2Velux();
168         }
169         if (VeluxProduct.UNKNOWN.equals(thisProduct)) {
170             logger.warn("isInverted(): Thing not found in Velux Bridge.");
171         }
172         return isInverted;
173     }
174 }