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