2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.velux.internal.handler.utils;
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;
28 * The class {@link Thing2VeluxActuator} provides simplified access to Velux device behind the Velux bridge by
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,
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>
39 * @author Guenther Schreiner - Initial contribution
42 public class Thing2VeluxActuator {
43 private final Logger logger = LoggerFactory.getLogger(Thing2VeluxActuator.class);
47 private VeluxBridgeHandler bridgeHandler;
48 private ChannelUID channelUID;
49 private boolean isInverted = false;
50 private VeluxProduct thisProduct = VeluxProduct.UNKNOWN;
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);
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;
70 logger.trace("mapThing2Velux(): in {} serialNumber={}.", channelUID, uniqueIndex);
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;
79 logger.trace("mapThing2Velux(): in {} name={}.", channelUID, uniqueIndex);
82 if (uniqueIndex == null) {
83 logger.warn("mapThing2Velux(): in {} cannot find a uniqueIndex, aborting.", channelUID);
86 logger.trace("mapThing2Velux(): in {} uniqueIndex={}, proceeding.", channelUID, uniqueIndex);
89 // handle value inversion
91 if (ThingConfiguration.exists(bridgeHandler, channelUID,
92 VeluxBindingProperties.PROPERTY_ACTUATOR_INVERTED)) {
93 invert = (boolean) ThingConfiguration.getValue(bridgeHandler, channelUID,
94 VeluxBindingProperties.PROPERTY_ACTUATOR_INVERTED);
98 logger.trace("mapThing2Velux(): in {} isInverted={}.", channelUID, isInverted);
100 VeluxExistingProducts existing = bridgeHandler.bridgeParameters.actuators.getChannel().existingProducts;
102 if (!existing.isRegistered(uniqueIndex)) {
103 logger.warn("mapThing2Velux(): actuator with uniqueIndex={} is not registered", uniqueIndex);
107 logger.trace("mapThing2Velux(): fetching actuator for {}.", uniqueIndex);
108 thisProduct = existing.get(uniqueIndex);
109 logger.debug("mapThing2Velux(): found actuator {}.", thisProduct);
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.
123 public Thing2VeluxActuator(VeluxBridgeHandler thisBridgeHandler, ChannelUID thisChannelUID) {
124 bridgeHandler = thisBridgeHandler;
125 channelUID = thisChannelUID;
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.
135 * @return <b>bridgeProductIndex</B> for accessing the Velux device (or ProductBridgeIndex.UNKNOWN if not found).
137 public ProductBridgeIndex getProductBridgeIndex() {
138 if (thisProduct == VeluxProduct.UNKNOWN) {
141 if (thisProduct == VeluxProduct.UNKNOWN) {
142 return ProductBridgeIndex.UNKNOWN;
144 return thisProduct.getBridgeProductIndex();
148 * Returns true, if the actuator is known within the bridge.
151 * @return <b>isKnown</B> as boolean.
153 public boolean isKnown() {
154 return (!(this.getProductBridgeIndex() == ProductBridgeIndex.UNKNOWN));
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.
162 * @return <b>isInverted</B> for handling of values of the Velux device (or false if not found)..
164 public boolean isInverted() {
165 if (thisProduct == VeluxProduct.UNKNOWN) {
168 if (thisProduct == VeluxProduct.UNKNOWN) {
169 logger.warn("isInverted(): Thing not found in Velux Bridge.");