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.cbus.internal;
15 import java.util.Hashtable;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.cbus.CBusBindingConstants;
20 import org.openhab.binding.cbus.handler.CBusCGateHandler;
21 import org.openhab.binding.cbus.handler.CBusDaliHandler;
22 import org.openhab.binding.cbus.handler.CBusLightHandler;
23 import org.openhab.binding.cbus.handler.CBusNetworkHandler;
24 import org.openhab.binding.cbus.handler.CBusTemperatureHandler;
25 import org.openhab.binding.cbus.handler.CBusTriggerHandler;
26 import org.openhab.binding.cbus.internal.discovery.CBusGroupDiscovery;
27 import org.openhab.binding.cbus.internal.discovery.CBusNetworkDiscovery;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.osgi.framework.ServiceRegistration;
36 import org.osgi.service.component.annotations.Component;
39 * The {@link CBusHandlerFactory} is responsible for creating things and thing
42 * @author Scott Linton - Initial contribution
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.cbus")
46 public class CBusHandlerFactory extends BaseThingHandlerFactory {
48 private @Nullable ServiceRegistration<?> cbusCGateHandlerServiceReg = null;
49 private @Nullable ServiceRegistration<?> cbusNetworkHandlerServiceReg = null;
52 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
53 return CBusBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
57 protected @Nullable ThingHandler createHandler(Thing thing) {
58 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
59 if (CBusBindingConstants.BRIDGE_TYPE_CGATE.equals(thingTypeUID)) {
60 CBusCGateHandler handler = new CBusCGateHandler((Bridge) thing);
61 registerDeviceDiscoveryService(handler);
64 if (CBusBindingConstants.BRIDGE_TYPE_NETWORK.equals(thingTypeUID)) {
65 CBusNetworkHandler handler = new CBusNetworkHandler((Bridge) thing);
66 registerDeviceDiscoveryService(handler);
69 if (CBusBindingConstants.THING_TYPE_LIGHT.equals(thingTypeUID)) {
70 return new CBusLightHandler(thing);
72 if (CBusBindingConstants.THING_TYPE_TEMPERATURE.equals(thingTypeUID)) {
73 return new CBusTemperatureHandler(thing);
75 if (CBusBindingConstants.THING_TYPE_TRIGGER.equals(thingTypeUID)) {
76 return new CBusTriggerHandler(thing);
78 if (CBusBindingConstants.THING_TYPE_DALI.equals(thingTypeUID)) {
79 return new CBusDaliHandler(thing);
85 protected void removeHandler(ThingHandler thingHandler) {
86 if (thingHandler instanceof CBusCGateHandler) {
87 ServiceRegistration<?> serviceReg = this.cbusCGateHandlerServiceReg;
88 if (serviceReg != null) {
89 serviceReg.unregister();
91 } else if (thingHandler instanceof CBusNetworkHandler) {
92 ServiceRegistration<?> serviceReg = this.cbusNetworkHandlerServiceReg;
93 if (serviceReg != null) {
94 serviceReg.unregister();
97 super.removeHandler(thingHandler);
100 private void registerDeviceDiscoveryService(CBusCGateHandler cbusCgateHandler) {
101 CBusNetworkDiscovery discoveryService = new CBusNetworkDiscovery(cbusCgateHandler);
102 cbusCGateHandlerServiceReg = super.bundleContext.registerService(DiscoveryService.class.getName(),
103 discoveryService, new Hashtable<String, Object>());
106 private void registerDeviceDiscoveryService(CBusNetworkHandler cbusNetworkHandler) {
107 CBusGroupDiscovery discoveryService = new CBusGroupDiscovery(cbusNetworkHandler);
108 cbusNetworkHandlerServiceReg = bundleContext.registerService(DiscoveryService.class.getName(), discoveryService,
109 new Hashtable<String, Object>());