]> git.basschouten.com Git - openhab-addons.git/blob
693fd5fa0db11c77dc916bc2d9531562b0a19a1b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.internal;
14
15 import java.util.Set;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.mqtt.generic.MqttChannelStateDescriptionProvider;
22 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
23 import org.openhab.binding.mqtt.generic.internal.handler.GenericMQTTThingHandler;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.thing.binding.ThingHandlerFactory;
29 import org.openhab.core.transform.TransformationHelper;
30 import org.openhab.core.transform.TransformationService;
31 import org.osgi.service.component.ComponentContext;
32 import org.osgi.service.component.annotations.Activate;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Deactivate;
35 import org.osgi.service.component.annotations.Reference;
36
37 /**
38  * The {@link MqttThingHandlerFactory} is responsible for creating things and thing
39  * handlers.
40  *
41  * @author David Graeff - Initial contribution
42  */
43 @Component(service = ThingHandlerFactory.class)
44 @NonNullByDefault
45 public class MqttThingHandlerFactory extends BaseThingHandlerFactory implements TransformationServiceProvider {
46     private @NonNullByDefault({}) MqttChannelStateDescriptionProvider stateDescriptionProvider;
47     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
48             .of(MqttBindingConstants.GENERIC_MQTT_THING).collect(Collectors.toSet());
49
50     @Override
51     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
52         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
53     }
54
55     @Activate
56     @Override
57     protected void activate(ComponentContext componentContext) {
58         super.activate(componentContext);
59     }
60
61     @Deactivate
62     @Override
63     protected void deactivate(ComponentContext componentContext) {
64         super.deactivate(componentContext);
65     }
66
67     @Reference
68     protected void setStateDescriptionProvider(MqttChannelStateDescriptionProvider stateDescription) {
69         this.stateDescriptionProvider = stateDescription;
70     }
71
72     protected void unsetStateDescriptionProvider(MqttChannelStateDescriptionProvider stateDescription) {
73         this.stateDescriptionProvider = null;
74     }
75
76     @Override
77     protected @Nullable ThingHandler createHandler(Thing thing) {
78         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
79
80         if (thingTypeUID.equals(MqttBindingConstants.GENERIC_MQTT_THING)) {
81             return new GenericMQTTThingHandler(thing, stateDescriptionProvider, this, 1500);
82         }
83         return null;
84     }
85
86     @Override
87     public @Nullable TransformationService getTransformationService(String type) {
88         return TransformationHelper.getTransformationService(bundleContext, type);
89     }
90 }