2 * Copyright (c) 2010-2020 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.VeluxBindingProperties;
17 import org.openhab.binding.velux.internal.handler.VeluxBridgeHandler;
18 import org.openhab.binding.velux.internal.things.VeluxProduct;
19 import org.openhab.binding.velux.internal.things.VeluxProduct.ProductBridgeIndex;
20 import org.openhab.binding.velux.internal.things.VeluxProductSerialNo;
21 import org.openhab.core.thing.ChannelUID;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * The class {@link Thing2VeluxActuator} provides simplified access to Velux device behind the Velux bridge by
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,
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>
37 * @author Guenther Schreiner - Initial contribution
40 public class Thing2VeluxActuator {
41 private final Logger logger = LoggerFactory.getLogger(Thing2VeluxActuator.class);
45 private VeluxBridgeHandler bridgeHandler;
46 private ChannelUID channelUID;
47 private boolean isInverted = false;
48 private VeluxProduct thisProduct = VeluxProduct.UNKNOWN;
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);
59 String actuatorSerial = (String) ThingConfiguration.getValue(bridgeHandler, channelUID,
60 VeluxBindingProperties.CONFIG_ACTUATOR_SERIALNUMBER);
61 logger.trace("mapThing2Velux(): found actuatorSerial={}.", actuatorSerial);
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);
69 isInverted = propertyInverted || VeluxProductSerialNo.indicatesRevertedValues(actuatorSerial);
70 logger.trace("mapThing2Velux(): found isInverted={}.", isInverted);
71 actuatorSerial = VeluxProductSerialNo.cleaned(actuatorSerial);
73 if (!bridgeHandler.bridgeParameters.actuators.getChannel().existingProducts.isRegistered(actuatorSerial)) {
74 logger.warn("mapThing2Velux(): cannot work on unknown actuator with serial {}.", actuatorSerial);
77 logger.trace("mapThing2Velux(): fetching actuator for {}.", actuatorSerial);
78 thisProduct = bridgeHandler.bridgeParameters.actuators.getChannel().existingProducts.get(actuatorSerial);
79 logger.debug("mapThing2Velux(): found actuator {}.", thisProduct);
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.
93 public Thing2VeluxActuator(VeluxBridgeHandler thisBridgeHandler, ChannelUID thisChannelUID) {
94 bridgeHandler = thisBridgeHandler;
95 channelUID = thisChannelUID;
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.
105 * @return <b>bridgeProductIndex</B> for accessing the Velux device (or ProductBridgeIndex.UNKNOWN if not found).
107 public ProductBridgeIndex getProductBridgeIndex() {
108 if (thisProduct == VeluxProduct.UNKNOWN) {
111 if (thisProduct == VeluxProduct.UNKNOWN) {
112 return ProductBridgeIndex.UNKNOWN;
114 return thisProduct.getBridgeProductIndex();
118 * Returns true, if the actuator is known within the bridge.
121 * @return <b>isKnown</B> as boolean.
123 public boolean isKnown() {
124 return (!(this.getProductBridgeIndex() == ProductBridgeIndex.UNKNOWN));
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.
132 * @return <b>isInverted</B> for handling of values of the Velux device (or false if not found)..
134 public boolean isInverted() {
135 if (thisProduct == VeluxProduct.UNKNOWN) {
138 if (thisProduct == VeluxProduct.UNKNOWN) {
139 logger.warn("isInverted(): Thing not found in Velux Bridge.");