]> git.basschouten.com Git - openhab-addons.git/blob
1bdee744a4fdf66031cf9c36169d197841b4dc5d
[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.core.thing.ChannelUID;
17 import org.openhab.core.thing.Thing;
18 import org.openhab.core.thing.ThingUID;
19 import org.openhab.core.thing.binding.BaseBridgeHandler;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /***
24  * The class {@link ThingConfiguration} provides methods for dealing with
25  * properties.
26  * <ul>
27  * <li>{@link #exists} Check existence of a property,</LI>
28  * <li>{@link #getValue} Returns a property value.</LI>
29  * </UL>
30  * <P>
31  * Noninstantiable utility class
32  * </P>
33  *
34  * @author Guenther Schreiner - Initial contribution
35  */
36 @NonNullByDefault
37 public class ThingConfiguration {
38     private static final Logger LOGGER = LoggerFactory.getLogger(ThingConfiguration.class);
39
40     /*
41      * ************************
42      * ***** Constructors *****
43      */
44
45     // Suppress default constructor for non-Instantiability
46
47     private ThingConfiguration() {
48         throw new AssertionError();
49     }
50
51     /*
52      * **************************
53      * ***** Public Methods *****
54      */
55
56     /**
57      * Check existence of the configuration value for the given channel and
58      * desired configName which are defined within VeluxBindingProperties.
59      * <p>
60      *
61      * @param bridge which handles the mentioned Things,
62      * @param channelUID describes the channel to by scrutinized,
63      * @param configName defines the configuration entry which is to be evaluated.
64      * @return <b>exists</B> of type boolean.
65      */
66     public static boolean exists(BaseBridgeHandler bridge, ChannelUID channelUID, String configName) {
67         ThingUID channelTUID = channelUID.getThingUID();
68         Thing thingOfChannel = bridge.getThing().getThing(channelTUID);
69         boolean exists = false;
70         if (thingOfChannel == null) {
71             LOGGER.debug("exists(): Channel {} does not belong to a thing.", channelUID);
72         } else {
73             if (thingOfChannel.getConfiguration().get(configName) != null) {
74                 exists = true;
75             }
76         }
77         LOGGER.trace("exists({},{}) returns {}.", channelUID, configName, exists);
78         return exists;
79     }
80
81     /**
82      * Return the property value of type Object for the given channel and
83      * desired propertyName which are defined within VeluxBindingProperties.
84      * <p>
85      *
86      * @param bridge which handles the mentioned Things,
87      * @param channelUID describes the channel to by scrutinized,
88      * @param configName defines the configuration entry which is to be evaluated.
89      * @return <b>configurationValue</B> of type {@link Object}. Will return {@code null}, if not found, or if value
90      *         itself
91      *         is {@code null}.
92      */
93     public static Object getValue(BaseBridgeHandler bridge, ChannelUID channelUID, String configName) {
94         ThingUID channelTUID = channelUID.getThingUID();
95         Thing thingOfChannel = bridge.getThing().getThing(channelTUID);
96         if (thingOfChannel == null) {
97             LOGGER.warn("getValue(): Channel {} does not belong to a thing.", channelUID);
98             return true;
99         }
100         Object configurationValue = thingOfChannel.getConfiguration().get(configName);
101         LOGGER.trace("getValue({},{}) returns {}.", channelUID, configName, configurationValue);
102         return configurationValue;
103     }
104 }