2 * Copyright (c) 2010-2022 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 // 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);
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;
71 logger.trace("mapThing2Velux(): in {} serialNumber={}.", channelUID, uniqueIndex);
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;
80 logger.trace("mapThing2Velux(): in {} name={}.", channelUID, uniqueIndex);
83 if (uniqueIndex == null) {
84 logger.warn("mapThing2Velux(): in {} cannot find a uniqueIndex, aborting.", channelUID);
87 logger.trace("mapThing2Velux(): in {} uniqueIndex={}, proceeding.", channelUID, uniqueIndex);
90 // handle value inversion
92 if (ThingConfiguration.exists(bridgeHandler, channelUID,
93 VeluxBindingProperties.PROPERTY_ACTUATOR_INVERTED)) {
94 invert = (boolean) ThingConfiguration.getValue(bridgeHandler, channelUID,
95 VeluxBindingProperties.PROPERTY_ACTUATOR_INVERTED);
99 logger.trace("mapThing2Velux(): in {} isInverted={}.", channelUID, isInverted);
101 VeluxExistingProducts existing = bridgeHandler.bridgeParameters.actuators.getChannel().existingProducts;
103 if (!existing.isRegistered(uniqueIndex)) {
104 logger.warn("mapThing2Velux(): actuator with uniqueIndex={} is not registered", uniqueIndex);
108 logger.trace("mapThing2Velux(): fetching actuator for {}.", uniqueIndex);
109 thisProduct = existing.get(uniqueIndex);
110 logger.debug("mapThing2Velux(): found actuator {}.", thisProduct);
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.
124 public Thing2VeluxActuator(VeluxBridgeHandler thisBridgeHandler, ChannelUID thisChannelUID) {
125 bridgeHandler = thisBridgeHandler;
126 channelUID = thisChannelUID;
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.
136 * @return <b>bridgeProductIndex</B> for accessing the Velux device (or ProductBridgeIndex.UNKNOWN if not found).
138 public ProductBridgeIndex getProductBridgeIndex() {
139 if (VeluxProduct.UNKNOWN.equals(thisProduct)) {
142 if (VeluxProduct.UNKNOWN.equals(thisProduct)) {
143 return ProductBridgeIndex.UNKNOWN;
145 return thisProduct.getBridgeProductIndex();
149 * Returns true, if the actuator is known within the bridge.
152 * @return <b>isKnown</B> as boolean.
154 public boolean isKnown() {
155 return (!(ProductBridgeIndex.UNKNOWN.equals(getProductBridgeIndex())));
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.
163 * @return <b>isInverted</B> for handling of values of the Velux device (or false if not found)..
165 public boolean isInverted() {
166 if (VeluxProduct.UNKNOWN.equals(thisProduct)) {
169 if (VeluxProduct.UNKNOWN.equals(thisProduct)) {
170 logger.warn("isInverted(): Thing not found in Velux Bridge.");