]> git.basschouten.com Git - openhab-addons.git/blob
3e2b54f83ed6b86b0ca8b2ba7071d351209b51fc
[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.sinope.internal;
14
15 import java.util.HashMap;
16 import java.util.Hashtable;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.openhab.binding.sinope.SinopeBindingConstants;
21 import org.openhab.binding.sinope.handler.SinopeGatewayHandler;
22 import org.openhab.binding.sinope.handler.SinopeThermostatHandler;
23 import org.openhab.binding.sinope.internal.discovery.SinopeThingsDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryService;
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.annotations.Component;
34
35 /**
36  * {@link SinopeHandlerFactory} is a factory for {@link SinopeThermostatHandler}s and {@link SinopeGatewayHandler}s
37  *
38  * @author Pascal Larin - Initial contribution
39  */
40 @Component(service = ThingHandlerFactory.class)
41 public class SinopeHandlerFactory extends BaseThingHandlerFactory {
42
43     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = SinopeBindingConstants.SUPPORTED_THING_TYPES_UIDS;
44     private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
45
46     @Override
47     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
48         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
49     }
50
51     @Override
52     protected ThingHandler createHandler(Thing thing) {
53         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
54
55         if (SinopeBindingConstants.THING_TYPE_GATEWAY.equals(thingTypeUID)) {
56             SinopeGatewayHandler bridge = new SinopeGatewayHandler((Bridge) thing);
57             registerDiscoveryService(bridge);
58             return bridge;
59         } else if (SinopeBindingConstants.THING_TYPE_THERMO.equals(thingTypeUID)) {
60             return new SinopeThermostatHandler(thing);
61         }
62         return null;
63     }
64
65     private synchronized void registerDiscoveryService(SinopeGatewayHandler bridge) {
66         SinopeThingsDiscoveryService discoveryService = new SinopeThingsDiscoveryService(bridge);
67
68         this.discoveryServiceRegs.put(bridge.getThing().getUID(),
69                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
70     }
71
72     @Override
73     protected synchronized void removeHandler(ThingHandler thingHandler) {
74         if (thingHandler instanceof SinopeGatewayHandler) {
75             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(thingHandler.getThing().getUID());
76             if (serviceReg != null) {
77                 // remove discovery service, if bridge handler is removed
78                 serviceReg.unregister();
79                 discoveryServiceRegs.remove(thingHandler.getThing().getUID());
80             }
81         }
82     }
83 }