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