]> git.basschouten.com Git - openhab-addons.git/blob
76e0356ec80e948eb1142b3bda6f195eac460f0b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.mqtt.generic;
14
15 import java.util.Locale;
16 import java.util.Map;
17 import java.util.concurrent.ConcurrentHashMap;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.mqtt.generic.internal.MqttThingHandlerFactory;
22 import org.openhab.binding.mqtt.generic.internal.handler.GenericMQTTThingHandler;
23 import org.openhab.core.thing.Channel;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
26 import org.openhab.core.types.StateDescription;
27 import org.osgi.service.component.annotations.Component;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * If the user configures a generic channel and defines for example minimum/maximum/readonly,
33  * we need to dynamically override the xml default state.
34  * This service is started on-demand only, as soon as {@link MqttThingHandlerFactory} requires it.
35  *
36  * It is filled with new state descriptions within the {@link GenericMQTTThingHandler}.
37  *
38  * @author David Graeff - Initial contribution
39  */
40 @Component(service = { DynamicStateDescriptionProvider.class, MqttChannelStateDescriptionProvider.class })
41 @NonNullByDefault
42 public class MqttChannelStateDescriptionProvider implements DynamicStateDescriptionProvider {
43
44     private final Map<ChannelUID, StateDescription> descriptions = new ConcurrentHashMap<>();
45     private final Logger logger = LoggerFactory.getLogger(MqttChannelStateDescriptionProvider.class);
46
47     /**
48      * Set a state description for a channel. This description will be used when preparing the channel state by
49      * the framework for presentation. A previous description, if existed, will be replaced.
50      *
51      * @param channelUID channel UID
52      * @param description state description for the channel
53      */
54     public void setDescription(ChannelUID channelUID, StateDescription description) {
55         logger.debug("Adding state description for channel {}", channelUID);
56         descriptions.put(channelUID, description);
57     }
58
59     /**
60      * Clear all registered state descriptions
61      */
62     public void removeAllDescriptions() {
63         logger.debug("Removing all state descriptions");
64         descriptions.clear();
65     }
66
67     @Override
68     public @Nullable StateDescription getStateDescription(Channel channel,
69             @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
70         StateDescription description = descriptions.get(channel.getUID());
71         logger.trace("Providing state description for channel {}", channel.getUID());
72         return description;
73     }
74
75     /**
76      * Removes the given channel state description.
77      *
78      * @param channel The channel
79      */
80     public void remove(ChannelUID channel) {
81         descriptions.remove(channel);
82     }
83 }