]> git.basschouten.com Git - openhab-addons.git/blob
3c7abd06ef4c440ffc243a8b8459bcf6c628cbd5
[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.http.internal;
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.core.thing.Channel;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.ThingUID;
24 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
25 import org.openhab.core.types.StateDescription;
26 import org.osgi.service.component.annotations.Component;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Dynamic channel state description provider.
32  * Overrides the state description for the controls, which receive its configuration in the runtime.
33  *
34  * @author Jan N. Klug - Initial contribution
35  */
36 @NonNullByDefault
37 @Component(service = { DynamicStateDescriptionProvider.class,
38         HttpDynamicStateDescriptionProvider.class }, immediate = true)
39 public class HttpDynamicStateDescriptionProvider implements DynamicStateDescriptionProvider {
40
41     private final Map<ChannelUID, StateDescription> descriptions = new ConcurrentHashMap<>();
42     private final Logger logger = LoggerFactory.getLogger(HttpDynamicStateDescriptionProvider.class);
43
44     /**
45      * Set a state description for a channel. This description will be used when preparing the channel state by
46      * the framework for presentation. A previous description, if existed, will be replaced.
47      *
48      * @param channelUID
49      *            channel UID
50      * @param description
51      *            state description for the channel
52      */
53     public void setDescription(ChannelUID channelUID, StateDescription description) {
54         logger.trace("adding state description for channel {}", channelUID);
55         descriptions.put(channelUID, description);
56     }
57
58     /**
59      * remove all descriptions for a given thing
60      *
61      * @param thingUID the thing's UID
62      */
63     public void removeDescriptionsForThing(ThingUID thingUID) {
64         logger.trace("removing state description for thing {}", thingUID);
65         descriptions.entrySet().removeIf(entry -> entry.getKey().getThingUID().equals(thingUID));
66     }
67
68     @Override
69     public @Nullable StateDescription getStateDescription(Channel channel,
70             @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
71         if (descriptions.containsKey(channel.getUID())) {
72             logger.trace("returning new stateDescription for {}", channel.getUID());
73             return descriptions.get(channel.getUID());
74         } else {
75             return null;
76         }
77     }
78 }