]> git.basschouten.com Git - openhab-addons.git/blob
2f900ce714011b91f6dff03f44496889d826ad80
[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.tr064.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, Tr064DynamicStateDescriptionProvider.class })
38 public class Tr064DynamicStateDescriptionProvider implements DynamicStateDescriptionProvider {
39     private final Logger logger = LoggerFactory.getLogger(Tr064DynamicStateDescriptionProvider.class);
40     private final Map<ChannelUID, StateDescription> descriptions = new ConcurrentHashMap<>();
41
42     /**
43      * Set a state description for a channel. This description will be used when preparing the channel state by
44      * the framework for presentation. A previous description, if existed, will be replaced.
45      *
46      * @param channelUID channel UID
47      * @param description state description for the channel
48      */
49     public void setDescription(ChannelUID channelUID, StateDescription description) {
50         logger.trace("adding state description for channel {}", channelUID);
51         descriptions.put(channelUID, description);
52     }
53
54     /**
55      * remove all descriptions for a given thing
56      *
57      * @param thingUID the thing's UID
58      */
59     public void removeDescriptionsForThing(ThingUID thingUID) {
60         logger.trace("removing state description for thing {}", thingUID);
61         descriptions.entrySet().removeIf(entry -> entry.getKey().getThingUID().equals(thingUID));
62     }
63
64     @Override
65     public @Nullable StateDescription getStateDescription(Channel channel,
66             @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
67         if (descriptions.containsKey(channel.getUID())) {
68             return descriptions.get(channel.getUID());
69         } else {
70             return null;
71         }
72     }
73 }