]> git.basschouten.com Git - openhab-addons.git/blob
0eb0b5297e2fa0f29c5b44bdb59374d01dde1c1a
[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.bondhome.internal;
14
15 import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.bondhome.internal.handler.BondBridgeHandler;
23 import org.openhab.binding.bondhome.internal.handler.BondDeviceHandler;
24 import org.openhab.core.io.net.http.HttpClientFactory;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
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.ComponentContext;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
37
38 /**
39  * The {@link BondHomeHandlerFactory} is responsible for creating things and thing
40  * handlers.
41  *
42  * @author Sara Geleskie Damiano - Initial contribution
43  */
44 @NonNullByDefault
45 @Component(configurationPid = "binding.bondhome", service = ThingHandlerFactory.class)
46 public class BondHomeHandlerFactory extends BaseThingHandlerFactory {
47     private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
48     private final HttpClientFactory httpClientFactory;
49
50     @Activate
51     public BondHomeHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
52             ComponentContext componentContext) {
53         super.activate(componentContext);
54         this.httpClientFactory = httpClientFactory;
55     }
56
57     @Override
58     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
59         return SUPPORTED_BRIDGE_TYPES.contains(thingTypeUID) || SUPPORTED_THING_TYPES.contains(thingTypeUID);
60     }
61
62     @Override
63     protected @Nullable ThingHandler createHandler(Thing thing) {
64         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
65
66         if (THING_TYPE_BOND_BRIDGE.equals(thingTypeUID)) {
67             return new BondBridgeHandler((Bridge) thing, httpClientFactory);
68         } else if (SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
69             return new BondDeviceHandler(thing);
70         }
71
72         return null;
73     }
74
75     @Override
76     protected synchronized void removeHandler(ThingHandler thingHandler) {
77         if (thingHandler instanceof BondBridgeHandler) {
78             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
79             if (serviceReg != null) {
80                 serviceReg.unregister();
81             }
82         }
83     }
84 }