]> git.basschouten.com Git - openhab-addons.git/blob
c49693f62c0f52d1fef8da16e9a855932cf39e83
[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.binding.bluetooth.BluetoothAdapter;
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 RoamingHandlerFactory} is responsible for creating things and thing
37  * handlers.
38  *
39  * @author Connor Petty - Initial contribution
40  */
41 @NonNullByDefault
42 @Component(configurationPid = "binding.roaming", service = ThingHandlerFactory.class)
43 public class RoamingHandlerFactory extends BaseThingHandlerFactory {
44
45     private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
46
47     @Override
48     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
49         return RoamingBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
50     }
51
52     @Override
53     protected @Nullable ThingHandler createHandler(Thing thing) {
54         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
55
56         if (THING_TYPE_ROAMING.equals(thingTypeUID)) {
57             RoamingBridgeHandler handler = new RoamingBridgeHandler((Bridge) thing);
58             registerRoamingBluetoothAdapter(handler);
59             return handler;
60         }
61
62         return null;
63     }
64
65     private synchronized void registerRoamingBluetoothAdapter(RoamingBluetoothAdapter adapter) {
66         this.serviceRegs.put(adapter.getUID(),
67                 bundleContext.registerService(RoamingBluetoothAdapter.class.getName(), adapter, new Hashtable<>()));
68     }
69
70     @Override
71     protected synchronized void removeHandler(ThingHandler thingHandler) {
72         if (thingHandler instanceof RoamingBluetoothAdapter) {
73             UID uid = ((BluetoothAdapter) thingHandler).getUID();
74             ServiceRegistration<?> serviceReg = this.serviceRegs.remove(uid);
75             if (serviceReg != null) {
76                 serviceReg.unregister();
77             }
78         }
79     }
80 }