]> git.basschouten.com Git - openhab-addons.git/blob
2681ed1aefe88e2f019e88de924f4a0123f387d8
[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.roaming.internal;
14
15 import static org.openhab.binding.bluetooth.roaming.internal.RoamingBindingConstants.THING_TYPE_ROAMING;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
27 import org.openhab.core.thing.UID;
28 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerFactory;
31 import org.osgi.framework.ServiceRegistration;
32 import org.osgi.service.component.annotations.Component;
33
34 /**
35  * The {@link RoamingHandlerFactory} is responsible for creating things and thing
36  * handlers.
37  *
38  * @author Connor Petty - Initial contribution
39  */
40 @NonNullByDefault
41 @Component(configurationPid = "binding.roaming", service = ThingHandlerFactory.class)
42 public class RoamingHandlerFactory extends BaseThingHandlerFactory {
43
44     private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
45
46     @Override
47     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
48         return RoamingBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
49     }
50
51     @Override
52     protected @Nullable ThingHandler createHandler(Thing thing) {
53         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
54
55         if (THING_TYPE_ROAMING.equals(thingTypeUID)) {
56             RoamingBridgeHandler handler = new RoamingBridgeHandler((Bridge) thing);
57             registerRoamingBluetoothAdapter(handler);
58             return handler;
59         }
60
61         return null;
62     }
63
64     private synchronized void registerRoamingBluetoothAdapter(RoamingBluetoothAdapter adapter) {
65         this.serviceRegs.put(adapter.getUID(),
66                 bundleContext.registerService(RoamingBluetoothAdapter.class.getName(), adapter, new Hashtable<>()));
67     }
68
69     @Override
70     protected synchronized void removeHandler(ThingHandler thingHandler) {
71         if (thingHandler instanceof RoamingBluetoothAdapter bluetoothAdapter) {
72             UID uid = bluetoothAdapter.getUID();
73             ServiceRegistration<?> serviceReg = this.serviceRegs.remove(uid);
74             if (serviceReg != null) {
75                 serviceReg.unregister();
76             }
77         }
78     }
79 }