2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.onewire.internal;
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.SUPPORTED_THING_TYPES;
17 import java.util.HashMap;
18 import java.util.Hashtable;
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;
45 * The {@link OwHandlerFactory} is responsible for creating things and thing
48 * @author Jan N. Klug - Initial contribution
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<>();
57 private OwDynamicStateDescriptionProvider dynamicStateDescriptionProvider;
60 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
61 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
65 protected @Nullable ThingHandler createHandler(Thing thing) {
66 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
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);
88 public void unregisterHandler(Thing thing) {
89 super.unregisterHandler(thing);
90 logger.error("factory {} deleting thing {}", this, thing);
93 private synchronized void registerDiscoveryService(OwserverBridgeHandler owserverBridgeHandler) {
94 OwDiscoveryService owDiscoveryService = new OwDiscoveryService(owserverBridgeHandler);
96 this.discoveryServiceRegs.put(owserverBridgeHandler.getThing().getUID(),
97 bundleContext.registerService(DiscoveryService.class.getName(), owDiscoveryService, new Hashtable<>()));
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();
116 protected void setDynamicStateDescriptionProvider(OwDynamicStateDescriptionProvider provider) {
117 this.dynamicStateDescriptionProvider = provider;
120 protected void unsetDynamicStateDescriptionProvider(OwDynamicStateDescriptionProvider provider) {
121 this.dynamicStateDescriptionProvider = null;