]> git.basschouten.com Git - openhab-addons.git/blob
b0f2216e74ba5de9ea4765a45ed6f48e45988474
[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.onewire.internal;
14
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.SUPPORTED_THING_TYPES;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.onewire.internal.discovery.OwDiscoveryService;
24 import org.openhab.binding.onewire.internal.handler.AdvancedMultisensorThingHandler;
25 import org.openhab.binding.onewire.internal.handler.BAE091xSensorThingHandler;
26 import org.openhab.binding.onewire.internal.handler.BasicMultisensorThingHandler;
27 import org.openhab.binding.onewire.internal.handler.BasicThingHandler;
28 import org.openhab.binding.onewire.internal.handler.EDSSensorThingHandler;
29 import org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerFactory;
38 import org.osgi.framework.ServiceRegistration;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * The {@link OwHandlerFactory} is responsible for creating things and thing
46  * handlers.
47  *
48  * @author Jan N. Klug - Initial contribution
49  */
50 @NonNullByDefault
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.onewire")
52 public class OwHandlerFactory extends BaseThingHandlerFactory {
53     Logger logger = LoggerFactory.getLogger(OwHandlerFactory.class);
54     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
55
56     @NonNullByDefault({})
57     private OwDynamicStateDescriptionProvider dynamicStateDescriptionProvider;
58
59     @Override
60     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
61         return SUPPORTED_THING_TYPES.contains(thingTypeUID);
62     }
63
64     @Override
65     protected @Nullable ThingHandler createHandler(Thing thing) {
66         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
67
68         if (OwserverBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
69             OwserverBridgeHandler owserverBridgeHandler = new OwserverBridgeHandler((Bridge) thing);
70             registerDiscoveryService(owserverBridgeHandler);
71             return owserverBridgeHandler;
72         } else if (BasicMultisensorThingHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
73             return new BasicMultisensorThingHandler(thing, dynamicStateDescriptionProvider);
74         } else if (AdvancedMultisensorThingHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
75             return new AdvancedMultisensorThingHandler(thing, dynamicStateDescriptionProvider);
76         } else if (BasicThingHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
77             return new BasicThingHandler(thing, dynamicStateDescriptionProvider);
78         } else if (EDSSensorThingHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
79             return new EDSSensorThingHandler(thing, dynamicStateDescriptionProvider);
80         } else if (BAE091xSensorThingHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
81             return new BAE091xSensorThingHandler(thing, dynamicStateDescriptionProvider);
82         }
83
84         return null;
85     }
86
87     @Override
88     public void unregisterHandler(Thing thing) {
89         super.unregisterHandler(thing);
90         logger.error("factory {} deleting thing {}", this, thing);
91     }
92
93     private synchronized void registerDiscoveryService(OwserverBridgeHandler owserverBridgeHandler) {
94         OwDiscoveryService owDiscoveryService = new OwDiscoveryService(owserverBridgeHandler);
95
96         this.discoveryServiceRegs.put(owserverBridgeHandler.getThing().getUID(),
97                 bundleContext.registerService(DiscoveryService.class.getName(), owDiscoveryService, new Hashtable<>()));
98     }
99
100     @Override
101     protected synchronized void removeHandler(ThingHandler thingHandler) {
102         if (thingHandler instanceof OwserverBridgeHandler) {
103             // remove discovery service, if bridge handler is removed
104             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
105             if (serviceReg != null) {
106                 OwDiscoveryService service = (OwDiscoveryService) bundleContext.getService(serviceReg.getReference());
107                 serviceReg.unregister();
108                 if (service != null) {
109                     service.deactivate();
110                 }
111             }
112         }
113     }
114
115     @Reference
116     protected void setDynamicStateDescriptionProvider(OwDynamicStateDescriptionProvider provider) {
117         this.dynamicStateDescriptionProvider = provider;
118     }
119
120     protected void unsetDynamicStateDescriptionProvider(OwDynamicStateDescriptionProvider provider) {
121         this.dynamicStateDescriptionProvider = null;
122     }
123 }