2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.sinope.internal;
15 import java.util.HashMap;
16 import java.util.Hashtable;
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;
36 * {@link SinopeHandlerFactory} is a factory for {@link SinopeThermostatHandler}s and {@link SinopeGatewayHandler}s
38 * @author Pascal Larin - Initial contribution
40 @Component(service = ThingHandlerFactory.class)
41 public class SinopeHandlerFactory extends BaseThingHandlerFactory {
43 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = SinopeBindingConstants.SUPPORTED_THING_TYPES_UIDS;
44 private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
47 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
48 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
52 protected ThingHandler createHandler(Thing thing) {
53 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
55 if (SinopeBindingConstants.THING_TYPE_GATEWAY.equals(thingTypeUID)) {
56 SinopeGatewayHandler bridge = new SinopeGatewayHandler((Bridge) thing);
57 registerDiscoveryService(bridge);
59 } else if (SinopeBindingConstants.THING_TYPE_THERMO.equals(thingTypeUID)) {
60 return new SinopeThermostatHandler(thing);
65 private synchronized void registerDiscoveryService(SinopeGatewayHandler bridge) {
66 SinopeThingsDiscoveryService discoveryService = new SinopeThingsDiscoveryService(bridge);
68 this.discoveryServiceRegs.put(bridge.getThing().getUID(),
69 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
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());