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.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.core.thing.Bridge;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
27 import org.openhab.core.thing.UID;
28 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerFactory;
31 import org.osgi.framework.ServiceRegistration;
32 import org.osgi.service.component.annotations.Component;
35 * The {@link RoamingHandlerFactory} is responsible for creating things and thing
38 * @author Connor Petty - Initial contribution
41 @Component(configurationPid = "binding.roaming", service = ThingHandlerFactory.class)
42 public class RoamingHandlerFactory extends BaseThingHandlerFactory {
44 private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
47 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
48 return RoamingBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
52 protected @Nullable ThingHandler createHandler(Thing thing) {
53 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
55 if (THING_TYPE_ROAMING.equals(thingTypeUID)) {
56 RoamingBridgeHandler handler = new RoamingBridgeHandler((Bridge) thing);
57 registerRoamingBluetoothAdapter(handler);
64 private synchronized void registerRoamingBluetoothAdapter(RoamingBluetoothAdapter adapter) {
65 this.serviceRegs.put(adapter.getUID(),
66 bundleContext.registerService(RoamingBluetoothAdapter.class.getName(), adapter, new Hashtable<>()));
70 protected synchronized void removeHandler(ThingHandler thingHandler) {
71 if (thingHandler instanceof RoamingBluetoothAdapter bluetoothAdapter) {
72 UID uid = bluetoothAdapter.getUID();
73 ServiceRegistration<?> serviceReg = this.serviceRegs.remove(uid);
74 if (serviceReg != null) {
75 serviceReg.unregister();