]> git.basschouten.com Git - openhab-addons.git/blob
7267c97186dc9d36bc4a0927ac59cddb7cafc5d2
[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.mqtt.generic;
14
15 import java.net.URI;
16 import java.util.Collection;
17 import java.util.Locale;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.mqtt.generic.internal.MqttThingHandlerFactory;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.binding.ThingTypeProvider;
27 import org.openhab.core.thing.type.ChannelGroupType;
28 import org.openhab.core.thing.type.ChannelGroupTypeProvider;
29 import org.openhab.core.thing.type.ChannelGroupTypeUID;
30 import org.openhab.core.thing.type.ChannelType;
31 import org.openhab.core.thing.type.ChannelTypeProvider;
32 import org.openhab.core.thing.type.ChannelTypeUID;
33 import org.openhab.core.thing.type.ThingType;
34 import org.openhab.core.thing.type.ThingTypeBuilder;
35 import org.openhab.core.thing.type.ThingTypeRegistry;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
39
40 /**
41  * An MQTT Extension might want to provide additional, dynamic channel types, based on auto-discovery.
42  * <p>
43  * Just retrieve the `MqttChannelTypeProvider` OSGi service and add (and remove) your channel types.
44  * <p>
45  * This provider is started on-demand only, as soon as {@link MqttThingHandlerFactory} or an extension requires it.
46  *
47  * @author David Graeff - Initial contribution
48  *
49  */
50 @NonNullByDefault
51 @Component(immediate = false, service = { ThingTypeProvider.class, ChannelTypeProvider.class,
52         ChannelGroupTypeProvider.class, MqttChannelTypeProvider.class })
53 public class MqttChannelTypeProvider implements ThingTypeProvider, ChannelGroupTypeProvider, ChannelTypeProvider {
54     private final ThingTypeRegistry typeRegistry;
55
56     private final Map<ChannelTypeUID, ChannelType> types = new ConcurrentHashMap<>();
57     private final Map<ChannelGroupTypeUID, ChannelGroupType> groups = new ConcurrentHashMap<>();
58     private final Map<ThingTypeUID, ThingType> things = new ConcurrentHashMap<>();
59
60     @Activate
61     public MqttChannelTypeProvider(@Reference ThingTypeRegistry typeRegistry) {
62         super();
63         this.typeRegistry = typeRegistry;
64     }
65
66     @Override
67     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
68         return types.values();
69     }
70
71     @Override
72     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
73         return types.get(channelTypeUID);
74     }
75
76     @Override
77     public @Nullable ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID,
78             @Nullable Locale locale) {
79         return groups.get(channelGroupTypeUID);
80     }
81
82     @Override
83     public Collection<ChannelGroupType> getChannelGroupTypes(@Nullable Locale locale) {
84         return groups.values();
85     }
86
87     @Override
88     public Collection<ThingType> getThingTypes(@Nullable Locale locale) {
89         return things.values();
90     }
91
92     public Set<ThingTypeUID> getThingTypeUIDs() {
93         return things.keySet();
94     }
95
96     @Override
97     public @Nullable ThingType getThingType(ThingTypeUID thingTypeUID, @Nullable Locale locale) {
98         return things.get(thingTypeUID);
99     }
100
101     public void removeChannelType(ChannelTypeUID uid) {
102         types.remove(uid);
103     }
104
105     public void removeChannelGroupType(ChannelGroupTypeUID uid) {
106         groups.remove(uid);
107     }
108
109     public void setChannelGroupType(ChannelGroupTypeUID uid, ChannelGroupType type) {
110         groups.put(uid, type);
111     }
112
113     public void setChannelType(ChannelTypeUID uid, ChannelType type) {
114         types.put(uid, type);
115     }
116
117     public void removeThingType(ThingTypeUID uid) {
118         things.remove(uid);
119     }
120
121     public void setThingType(ThingTypeUID uid, ThingType type) {
122         things.put(uid, type);
123     }
124
125     public void setThingTypeIfAbsent(ThingTypeUID uid, ThingType type) {
126         things.putIfAbsent(uid, type);
127     }
128
129     public ThingTypeBuilder derive(ThingTypeUID newTypeId, ThingTypeUID baseTypeId) {
130         ThingType baseType = typeRegistry.getThingType(baseTypeId);
131
132         ThingTypeBuilder result = ThingTypeBuilder.instance(newTypeId, baseType.getLabel())
133                 .withChannelGroupDefinitions(baseType.getChannelGroupDefinitions())
134                 .withChannelDefinitions(baseType.getChannelDefinitions())
135                 .withExtensibleChannelTypeIds(baseType.getExtensibleChannelTypeIds())
136                 .withSupportedBridgeTypeUIDs(baseType.getSupportedBridgeTypeUIDs())
137                 .withProperties(baseType.getProperties()).isListed(false);
138
139         String representationProperty = baseType.getRepresentationProperty();
140         if (representationProperty != null) {
141             result = result.withRepresentationProperty(representationProperty);
142         }
143         URI configDescriptionURI = baseType.getConfigDescriptionURI();
144         if (configDescriptionURI != null) {
145             result = result.withConfigDescriptionURI(configDescriptionURI);
146         }
147         String category = baseType.getCategory();
148         if (category != null) {
149             result = result.withCategory(category);
150         }
151         String description = baseType.getDescription();
152         if (description != null) {
153             result = result.withDescription(description);
154         }
155
156         return result;
157     }
158 }