]> git.basschouten.com Git - openhab-addons.git/blob
773886e80be6eb84bb708cb66852fff70592d177
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.bluetooth.bluegiga.internal.factory;
14
15 import java.util.HashMap;
16 import java.util.Hashtable;
17 import java.util.Map;
18 import java.util.Optional;
19 import java.util.Set;
20
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;
38
39 /**
40  * The {@link BlueGigaHandlerFactory} is responsible for creating things and thing
41  * handlers.
42  *
43  * @author Chris Jackson - Initial contribution
44  * @author Kai Kreuzer - added support for adapter service registration
45  */
46 @NonNullByDefault
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.bluegiga")
48 public class BlueGigaHandlerFactory extends BaseThingHandlerFactory {
49
50     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set
51             .of(BlueGigaAdapterConstants.THING_TYPE_BLUEGIGA);
52
53     private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
54
55     private Optional<SerialPortManager> serialPortManager = Optional.empty();
56
57     @Reference
58     protected void setSerialPortManager(final SerialPortManager serialPortManager) {
59         this.serialPortManager = Optional.of(serialPortManager);
60     }
61
62     protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
63         this.serialPortManager = Optional.empty();
64     }
65
66     @Override
67     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
68         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
69     }
70
71     @Override
72     @Nullable
73     protected ThingHandler createHandler(Thing thing) {
74         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
75
76         if (thingTypeUID.equals(BlueGigaAdapterConstants.THING_TYPE_BLUEGIGA)) {
77             BlueGigaBridgeHandler handler = new BlueGigaBridgeHandler((Bridge) thing, serialPortManager.get());
78             registerBluetoothAdapter(handler);
79             return handler;
80         } else {
81             return null;
82         }
83     }
84
85     private synchronized void registerBluetoothAdapter(BluetoothAdapter adapter) {
86         this.serviceRegs.put(adapter.getUID(),
87                 bundleContext.registerService(BluetoothAdapter.class.getName(), adapter, new Hashtable<>()));
88     }
89
90     @Override
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();
97             }
98         }
99     }
100 }