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.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
19 import java.util.Optional;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.bluetooth.BluetoothAdapter;
25 import org.openhab.binding.bluetooth.bluegiga.BlueGigaAdapterConstants;
26 import org.openhab.binding.bluetooth.bluegiga.handler.BlueGigaBridgeHandler;
27 import org.openhab.core.io.transport.serial.SerialPortManager;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.UID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.framework.ServiceRegistration;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
41 * The {@link BlueGigaHandlerFactory} is responsible for creating things and thing
44 * @author Chris Jackson - Initial contribution
45 * @author Kai Kreuzer - added support for adapter service registration
48 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.bluegiga")
49 public class BlueGigaHandlerFactory extends BaseThingHandlerFactory {
51 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
52 .singleton(BlueGigaAdapterConstants.THING_TYPE_BLUEGIGA);
54 private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
56 private Optional<SerialPortManager> serialPortManager = Optional.empty();
59 protected void setSerialPortManager(final SerialPortManager serialPortManager) {
60 this.serialPortManager = Optional.of(serialPortManager);
63 protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
64 this.serialPortManager = Optional.empty();
68 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
69 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
74 protected ThingHandler createHandler(Thing thing) {
75 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
77 if (thingTypeUID.equals(BlueGigaAdapterConstants.THING_TYPE_BLUEGIGA)) {
78 BlueGigaBridgeHandler handler = new BlueGigaBridgeHandler((Bridge) thing, serialPortManager.get());
79 registerBluetoothAdapter(handler);
86 private synchronized void registerBluetoothAdapter(BluetoothAdapter adapter) {
87 this.serviceRegs.put(adapter.getUID(),
88 bundleContext.registerService(BluetoothAdapter.class.getName(), adapter, new Hashtable<>()));
91 @SuppressWarnings("null")
93 protected synchronized void removeHandler(ThingHandler thingHandler) {
94 if (thingHandler instanceof BluetoothAdapter) {
95 UID uid = ((BluetoothAdapter) thingHandler).getUID();
96 ServiceRegistration<?> serviceReg = this.serviceRegs.remove(uid);
97 if (serviceReg != null) {
98 serviceReg.unregister();