2 * Copyright (c) 2010-2020 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.bluetooth.bluez.internal;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
21 import org.openhab.binding.bluetooth.BluetoothAdapter;
22 import org.openhab.binding.bluetooth.bluez.BlueZAdapterConstants;
23 import org.openhab.binding.bluetooth.bluez.handler.BlueZBridgeHandler;
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.Component;
36 * The {@link BlueZHandlerFactory} is responsible for creating things and thing
39 * @author Kai Kreuzer - Initial contribution and API
41 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.bluetooth.bluez")
42 public class BlueZHandlerFactory extends BaseThingHandlerFactory {
44 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
45 .singleton(BlueZAdapterConstants.THING_TYPE_BLUEZ);
47 private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
50 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
51 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
55 protected ThingHandler createHandler(Thing thing) {
56 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
58 if (thingTypeUID.equals(BlueZAdapterConstants.THING_TYPE_BLUEZ)) {
59 BlueZBridgeHandler handler = new BlueZBridgeHandler((Bridge) thing);
60 registerBluetoothAdapter(handler);
67 private synchronized void registerBluetoothAdapter(BluetoothAdapter adapter) {
68 this.serviceRegs.put(adapter.getUID(),
69 bundleContext.registerService(BluetoothAdapter.class.getName(), adapter, new Hashtable<>()));
73 protected synchronized void removeHandler(ThingHandler thingHandler) {
74 if (thingHandler instanceof BluetoothAdapter) {
75 UID uid = ((BluetoothAdapter) thingHandler).getUID();
76 ServiceRegistration<?> serviceReg = this.serviceRegs.remove(uid);
77 if (serviceReg != null) {
78 serviceReg.unregister();