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.bluegiga.internal.factory;
15 import java.util.HashMap;
16 import java.util.Hashtable;
18 import java.util.Optional;
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.binding.bluetooth.bluegiga.BlueGigaAdapterConstants;
25 import org.openhab.binding.bluetooth.bluegiga.handler.BlueGigaBridgeHandler;
26 import org.openhab.core.io.transport.serial.SerialPortManager;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.UID;
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 import org.osgi.service.component.annotations.Reference;
40 * The {@link BlueGigaHandlerFactory} is responsible for creating things and thing
43 * @author Chris Jackson - Initial contribution
44 * @author Kai Kreuzer - added support for adapter service registration
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.bluegiga")
48 public class BlueGigaHandlerFactory extends BaseThingHandlerFactory {
50 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set
51 .of(BlueGigaAdapterConstants.THING_TYPE_BLUEGIGA);
53 private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
55 private Optional<SerialPortManager> serialPortManager = Optional.empty();
58 protected void setSerialPortManager(final SerialPortManager serialPortManager) {
59 this.serialPortManager = Optional.of(serialPortManager);
62 protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
63 this.serialPortManager = Optional.empty();
67 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
68 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
73 protected ThingHandler createHandler(Thing thing) {
74 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
76 if (thingTypeUID.equals(BlueGigaAdapterConstants.THING_TYPE_BLUEGIGA)) {
77 BlueGigaBridgeHandler handler = new BlueGigaBridgeHandler((Bridge) thing, serialPortManager.get());
78 registerBluetoothAdapter(handler);
85 private synchronized void registerBluetoothAdapter(BluetoothAdapter adapter) {
86 this.serviceRegs.put(adapter.getUID(),
87 bundleContext.registerService(BluetoothAdapter.class.getName(), adapter, new Hashtable<>()));
91 protected synchronized void removeHandler(ThingHandler thingHandler) {
92 if (thingHandler instanceof BluetoothAdapter bluetoothAdapter) {
93 UID uid = bluetoothAdapter.getUID();
94 ServiceRegistration<?> serviceReg = this.serviceRegs.remove(uid);
95 if (serviceReg != null) {
96 serviceReg.unregister();