]> git.basschouten.com Git - openhab-addons.git/blob
0d77d14a58de5f65f8c0433ad563af04b7b3aab8
[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.cbus.internal;
14
15 import java.util.Hashtable;
16
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;
37
38 /**
39  * The {@link CBusHandlerFactory} is responsible for creating things and thing
40  * handlers.
41  *
42  * @author Scott Linton - Initial contribution
43  */
44 @NonNullByDefault
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.cbus")
46 public class CBusHandlerFactory extends BaseThingHandlerFactory {
47
48     private @Nullable ServiceRegistration<?> cbusCGateHandlerServiceReg = null;
49     private @Nullable ServiceRegistration<?> cbusNetworkHandlerServiceReg = null;
50
51     @Override
52     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
53         return CBusBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
54     }
55
56     @Override
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);
62             return handler;
63         }
64         if (CBusBindingConstants.BRIDGE_TYPE_NETWORK.equals(thingTypeUID)) {
65             CBusNetworkHandler handler = new CBusNetworkHandler((Bridge) thing);
66             registerDeviceDiscoveryService(handler);
67             return handler;
68         }
69         if (CBusBindingConstants.THING_TYPE_LIGHT.equals(thingTypeUID)) {
70             return new CBusLightHandler(thing);
71         }
72         if (CBusBindingConstants.THING_TYPE_TEMPERATURE.equals(thingTypeUID)) {
73             return new CBusTemperatureHandler(thing);
74         }
75         if (CBusBindingConstants.THING_TYPE_TRIGGER.equals(thingTypeUID)) {
76             return new CBusTriggerHandler(thing);
77         }
78         if (CBusBindingConstants.THING_TYPE_DALI.equals(thingTypeUID)) {
79             return new CBusDaliHandler(thing);
80         }
81         return null;
82     }
83
84     @Override
85     protected void removeHandler(ThingHandler thingHandler) {
86         if (thingHandler instanceof CBusCGateHandler) {
87             ServiceRegistration<?> serviceReg = this.cbusCGateHandlerServiceReg;
88             if (serviceReg != null) {
89                 serviceReg.unregister();
90             }
91         } else if (thingHandler instanceof CBusNetworkHandler) {
92             ServiceRegistration<?> serviceReg = this.cbusNetworkHandlerServiceReg;
93             if (serviceReg != null) {
94                 serviceReg.unregister();
95             }
96         }
97         super.removeHandler(thingHandler);
98     }
99
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>());
104     }
105
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>());
110     }
111 }