]> git.basschouten.com Git - openhab-addons.git/blob
d5d36831b2b72b8cb7a6fcd2d21e4f9e8e79a696
[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.link.ItemChannelLinkRegistry;
28 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
29 import org.openhab.core.types.StateDescription;
30 import org.openhab.core.types.StateDescriptionFragment;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Dynamic channel state description provider.
37  * Overrides the state description for the controls, which receive its configuration in the runtime.
38  *
39  * @author Jan N. Klug - Initial contribution
40  */
41 @NonNullByDefault
42 @Component(service = { DynamicStateDescriptionProvider.class, DeconzDynamicStateDescriptionProvider.class })
43 public class DeconzDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
44     private final Logger logger = LoggerFactory.getLogger(DeconzDynamicStateDescriptionProvider.class);
45
46     private final Map<ChannelUID, StateDescriptionFragment> stateDescriptionFragments = new ConcurrentHashMap<>();
47
48     /**
49      * Set a state description for a channel. This description will be used when preparing the channel state by
50      * the framework for presentation. A previous description, if existed, will be replaced.
51      *
52      * @param channelUID
53      *            channel UID
54      * @param stateDescriptionFragment
55      *            state description for the channel
56      */
57     public void setDescriptionFragment(ChannelUID channelUID, StateDescriptionFragment stateDescriptionFragment) {
58         StateDescriptionFragment oldStateDescriptionFragment = stateDescriptionFragments.get(channelUID);
59         if (!stateDescriptionFragment.equals(oldStateDescriptionFragment)) {
60             logger.trace("adding state description for channel {}", channelUID);
61             stateDescriptionFragments.put(channelUID, stateDescriptionFragment);
62             ItemChannelLinkRegistry itemChannelLinkRegistry = this.itemChannelLinkRegistry;
63             postEvent(ThingEventFactory.createChannelDescriptionChangedEvent(channelUID,
64                     itemChannelLinkRegistry != null ? itemChannelLinkRegistry.getLinkedItemNames(channelUID) : Set.of(),
65                     stateDescriptionFragment, oldStateDescriptionFragment));
66         }
67     }
68
69     /**
70      * remove all descriptions for a given thing
71      *
72      * @param thingUID the thing's UID
73      */
74     public void removeDescriptionsForThing(ThingUID thingUID) {
75         logger.trace("removing state description for thing {}", thingUID);
76         stateDescriptionFragments.entrySet().removeIf(entry -> entry.getKey().getThingUID().equals(thingUID));
77     }
78
79     @Override
80     public @Nullable StateDescription getStateDescription(Channel channel,
81             @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
82         StateDescriptionFragment stateDescriptionFragment = stateDescriptionFragments.get(channel.getUID());
83         if (stateDescriptionFragment != null) {
84             logger.trace("returning new stateDescription for {}", channel.getUID());
85             return stateDescriptionFragment.toStateDescription();
86         } else {
87             return super.getStateDescription(channel, originalStateDescription, locale);
88         }
89     }
90 }