]> git.basschouten.com Git - openhab-addons.git/blob
f35b24100358c70804c07f0ec4b83528540a1337
[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.deconz.internal;
14
15 import java.util.Locale;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.concurrent.ConcurrentHashMap;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.thing.Channel;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.ThingUID;
25 import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
26 import org.openhab.core.thing.events.ThingEventFactory;
27 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
28 import org.openhab.core.types.StateDescription;
29 import org.openhab.core.types.StateDescriptionFragment;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Dynamic channel state description provider.
36  * Overrides the state description for the controls, which receive its configuration in the runtime.
37  *
38  * @author Jan N. Klug - Initial contribution
39  */
40 @NonNullByDefault
41 @Component(service = { DynamicStateDescriptionProvider.class, DeconzDynamicStateDescriptionProvider.class })
42 public class DeconzDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
43     private final Logger logger = LoggerFactory.getLogger(DeconzDynamicStateDescriptionProvider.class);
44
45     private final Map<ChannelUID, StateDescriptionFragment> stateDescriptionFragments = new ConcurrentHashMap<>();
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
52      *            channel UID
53      * @param stateDescriptionFragment
54      *            state description for the channel
55      */
56     public void setDescriptionFragment(ChannelUID channelUID, StateDescriptionFragment stateDescriptionFragment) {
57         StateDescriptionFragment oldStateDescriptionFragment = stateDescriptionFragments.get(channelUID);
58         if (!stateDescriptionFragment.equals(oldStateDescriptionFragment)) {
59             logger.trace("adding state description for channel {}", channelUID);
60             stateDescriptionFragments.put(channelUID, stateDescriptionFragment);
61             postEvent(ThingEventFactory.createChannelDescriptionChangedEvent(channelUID,
62                     itemChannelLinkRegistry != null ? itemChannelLinkRegistry.getLinkedItemNames(channelUID) : Set.of(),
63                     stateDescriptionFragment, oldStateDescriptionFragment));
64         }
65     }
66
67     /**
68      * remove all descriptions for a given thing
69      *
70      * @param thingUID the thing's UID
71      */
72     public void removeDescriptionsForThing(ThingUID thingUID) {
73         logger.trace("removing state description for thing {}", thingUID);
74         stateDescriptionFragments.entrySet().removeIf(entry -> entry.getKey().getThingUID().equals(thingUID));
75     }
76
77     @Override
78     public @Nullable StateDescription getStateDescription(Channel channel,
79             @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
80         StateDescriptionFragment stateDescriptionFragment = stateDescriptionFragments.get(channel.getUID());
81         if (stateDescriptionFragment != null) {
82             logger.trace("returning new stateDescription for {}", channel.getUID());
83             return stateDescriptionFragment.toStateDescription();
84         } else {
85             return super.getStateDescription(channel, originalStateDescription, locale);
86         }
87     }
88 }