]> git.basschouten.com Git - openhab-addons.git/blob
935e202f1298b1071371fcd45cae7d7def36bb35
[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.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
18 import java.util.Map;
19 import java.util.Optional;
20 import java.util.Set;
21
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;
39
40 /**
41  * The {@link BlueGigaHandlerFactory} is responsible for creating things and thing
42  * handlers.
43  *
44  * @author Chris Jackson - Initial contribution
45  * @author Kai Kreuzer - added support for adapter service registration
46  */
47 @NonNullByDefault
48 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.bluegiga")
49 public class BlueGigaHandlerFactory extends BaseThingHandlerFactory {
50
51     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
52             .singleton(BlueGigaAdapterConstants.THING_TYPE_BLUEGIGA);
53
54     private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
55
56     private Optional<SerialPortManager> serialPortManager = Optional.empty();
57
58     @Reference
59     protected void setSerialPortManager(final SerialPortManager serialPortManager) {
60         this.serialPortManager = Optional.of(serialPortManager);
61     }
62
63     protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
64         this.serialPortManager = Optional.empty();
65     }
66
67     @Override
68     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
69         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
70     }
71
72     @Override
73     @Nullable
74     protected ThingHandler createHandler(Thing thing) {
75         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
76
77         if (thingTypeUID.equals(BlueGigaAdapterConstants.THING_TYPE_BLUEGIGA)) {
78             BlueGigaBridgeHandler handler = new BlueGigaBridgeHandler((Bridge) thing, serialPortManager.get());
79             registerBluetoothAdapter(handler);
80             return handler;
81         } else {
82             return null;
83         }
84     }
85
86     private synchronized void registerBluetoothAdapter(BluetoothAdapter adapter) {
87         this.serviceRegs.put(adapter.getUID(),
88                 bundleContext.registerService(BluetoothAdapter.class.getName(), adapter, new Hashtable<>()));
89     }
90
91     @SuppressWarnings("null")
92     @Override
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();
99             }
100         }
101     }
102 }