2 * Copyright (c) 2010-2022 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.roaming.internal;
15 import static org.openhab.binding.bluetooth.roaming.internal.RoamingBindingConstants.THING_TYPE_ROAMING;
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.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.Component;
36 * The {@link RoamingHandlerFactory} is responsible for creating things and thing
39 * @author Connor Petty - Initial contribution
42 @Component(configurationPid = "binding.roaming", service = ThingHandlerFactory.class)
43 public class RoamingHandlerFactory extends BaseThingHandlerFactory {
45 private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
48 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
49 return RoamingBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
53 protected @Nullable ThingHandler createHandler(Thing thing) {
54 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
56 if (THING_TYPE_ROAMING.equals(thingTypeUID)) {
57 RoamingBridgeHandler handler = new RoamingBridgeHandler((Bridge) thing);
58 registerRoamingBluetoothAdapter(handler);
65 private synchronized void registerRoamingBluetoothAdapter(RoamingBluetoothAdapter adapter) {
66 this.serviceRegs.put(adapter.getUID(),
67 bundleContext.registerService(RoamingBluetoothAdapter.class.getName(), adapter, new Hashtable<>()));
71 protected synchronized void removeHandler(ThingHandler thingHandler) {
72 if (thingHandler instanceof RoamingBluetoothAdapter) {
73 UID uid = ((BluetoothAdapter) thingHandler).getUID();
74 ServiceRegistration<?> serviceReg = this.serviceRegs.remove(uid);
75 if (serviceReg != null) {
76 serviceReg.unregister();