]> git.basschouten.com Git - openhab-addons.git/blob
59adb713017006b57e60a2cb69f6a3ff1d314f8c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.bluez.internal;
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.Set;
20
21 import org.openhab.binding.bluetooth.BluetoothAdapter;
22 import org.openhab.binding.bluetooth.bluez.BlueZAdapterConstants;
23 import org.openhab.binding.bluetooth.bluez.handler.BlueZBridgeHandler;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.thing.UID;
29 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerFactory;
32 import org.osgi.framework.ServiceRegistration;
33 import org.osgi.service.component.annotations.Component;
34
35 /**
36  * The {@link BlueZHandlerFactory} is responsible for creating things and thing
37  * handlers.
38  *
39  * @author Kai Kreuzer - Initial contribution and API
40  */
41 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.bluetooth.bluez")
42 public class BlueZHandlerFactory extends BaseThingHandlerFactory {
43
44     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
45             .singleton(BlueZAdapterConstants.THING_TYPE_BLUEZ);
46
47     private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
48
49     @Override
50     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
51         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
52     }
53
54     @Override
55     protected ThingHandler createHandler(Thing thing) {
56         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
57
58         if (thingTypeUID.equals(BlueZAdapterConstants.THING_TYPE_BLUEZ)) {
59             BlueZBridgeHandler handler = new BlueZBridgeHandler((Bridge) thing);
60             registerBluetoothAdapter(handler);
61             return handler;
62         } else {
63             return null;
64         }
65     }
66
67     private synchronized void registerBluetoothAdapter(BluetoothAdapter adapter) {
68         this.serviceRegs.put(adapter.getUID(),
69                 bundleContext.registerService(BluetoothAdapter.class.getName(), adapter, new Hashtable<>()));
70     }
71
72     @Override
73     protected synchronized void removeHandler(ThingHandler thingHandler) {
74         if (thingHandler instanceof BluetoothAdapter) {
75             UID uid = ((BluetoothAdapter) thingHandler).getUID();
76             ServiceRegistration<?> serviceReg = this.serviceRegs.remove(uid);
77             if (serviceReg != null) {
78                 serviceReg.unregister();
79             }
80         }
81     }
82 }