]> git.basschouten.com Git - openhab-addons.git/blob
1828ac10e0769da04a6992a15c7d6ba07ee7787a
[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.bluetooth.bluez.internal;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.bluetooth.BluetoothAdapter;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.thing.UID;
29 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerFactory;
32 import org.osgi.framework.ServiceRegistration;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
36
37 /**
38  * The {@link BlueZHandlerFactory} is responsible for creating things and thing
39  * handlers.
40  *
41  * @author Kai Kreuzer - Initial contribution and API
42  * @author Connor Petty - Added DeviceManagerFactory
43  */
44 @NonNullByDefault
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.bluetooth.bluez")
46 public class BlueZHandlerFactory extends BaseThingHandlerFactory {
47
48     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
49             .singleton(BlueZAdapterConstants.THING_TYPE_BLUEZ);
50
51     private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
52
53     private final DeviceManagerFactory deviceManagerFactory;
54
55     @Activate
56     public BlueZHandlerFactory(@Reference DeviceManagerFactory deviceManagerFactory) {
57         this.deviceManagerFactory = deviceManagerFactory;
58     }
59
60     @Override
61     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
63     }
64
65     @Override
66     protected @Nullable ThingHandler createHandler(Thing thing) {
67         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
68
69         if (thingTypeUID.equals(BlueZAdapterConstants.THING_TYPE_BLUEZ)) {
70             BlueZBridgeHandler handler = new BlueZBridgeHandler((Bridge) thing, deviceManagerFactory);
71             registerBluetoothAdapter(handler);
72             return handler;
73         } else {
74             return null;
75         }
76     }
77
78     private synchronized void registerBluetoothAdapter(BluetoothAdapter adapter) {
79         this.serviceRegs.put(adapter.getUID(),
80                 bundleContext.registerService(BluetoothAdapter.class.getName(), adapter, new Hashtable<>()));
81     }
82
83     @Override
84     protected synchronized void removeHandler(ThingHandler thingHandler) {
85         if (thingHandler instanceof BluetoothAdapter) {
86             UID uid = ((BluetoothAdapter) thingHandler).getUID();
87             ServiceRegistration<?> serviceReg = this.serviceRegs.remove(uid);
88             if (serviceReg != null) {
89                 serviceReg.unregister();
90             }
91         }
92     }
93 }