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.siemensrds.internal;
15 import static org.openhab.binding.siemensrds.internal.RdsBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
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 * The {@link RdsHandlerFactory} creates things and thing handlers
38 * @author Andrew Fiddian-Green - Initial contribution
41 @Component(configurationPid = "binding.siemensrds", service = ThingHandlerFactory.class)
42 public class RdsHandlerFactory extends BaseThingHandlerFactory {
44 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_CLOUD, THING_TYPE_RDS);
46 private final Map<ThingUID, ServiceRegistration<?>> discos = new HashMap<>();
49 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
50 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
54 protected @Nullable ThingHandler createHandler(Thing thing) {
55 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
57 if ((thingTypeUID.equals(THING_TYPE_CLOUD)) && (thing instanceof Bridge bridge)) {
58 RdsCloudHandler handler = new RdsCloudHandler(bridge);
59 createDiscoveryService(handler);
63 if (thingTypeUID.equals(THING_TYPE_RDS)) {
64 return new RdsHandler(thing);
71 protected synchronized void removeHandler(ThingHandler handler) {
72 if (handler instanceof RdsCloudHandler cloudHandler) {
73 destroyDiscoveryService(cloudHandler);
78 * create a discovery service so that a newly created cloud account will find
79 * the things that it supports
81 private synchronized void createDiscoveryService(RdsCloudHandler handler) {
82 // create a new discovery service
83 RdsDiscoveryService ds = new RdsDiscoveryService(handler);
85 // register the discovery service
86 ServiceRegistration<?> serviceReg = bundleContext.registerService(DiscoveryService.class.getName(), ds,
90 * store service registration in a list so we can destroy it when the respective
93 discos.put(handler.getThing().getUID(), serviceReg);
95 // finally activate the discovery service
100 * destroy the discovery service
102 private synchronized void destroyDiscoveryService(RdsCloudHandler handler) {
103 // fetch the respective thing's service registration from our list
105 ServiceRegistration<?> serviceReg = discos.remove(handler.getThing().getUID());
107 // retrieve the respective discovery service
108 if (serviceReg != null) {
109 RdsDiscoveryService disco = (RdsDiscoveryService) bundleContext.getService(serviceReg.getReference());
111 // unregister the service
112 serviceReg.unregister();
114 // deactivate the service