]> git.basschouten.com Git - openhab-addons.git/blob
4c2f9981aa6e7aba6762498331f9ecb37fa140d4
[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.siemensrds.internal;
14
15 import static org.openhab.binding.siemensrds.internal.RdsBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20 import java.util.Set;
21
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;
34
35 /**
36  * The {@link RdsHandlerFactory} creates things and thing handlers
37  *
38  * @author Andrew Fiddian-Green - Initial contribution
39  */
40 @NonNullByDefault
41 @Component(configurationPid = "binding.siemensrds", service = ThingHandlerFactory.class)
42 public class RdsHandlerFactory extends BaseThingHandlerFactory {
43
44     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_CLOUD, THING_TYPE_RDS);
45
46     private final Map<ThingUID, ServiceRegistration<?>> discos = new HashMap<>();
47
48     @Override
49     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
50         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
51     }
52
53     @Override
54     protected @Nullable ThingHandler createHandler(Thing thing) {
55         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
56
57         if ((thingTypeUID.equals(THING_TYPE_CLOUD)) && (thing instanceof Bridge bridge)) {
58             RdsCloudHandler handler = new RdsCloudHandler(bridge);
59             createDiscoveryService(handler);
60             return handler;
61         }
62
63         if (thingTypeUID.equals(THING_TYPE_RDS)) {
64             return new RdsHandler(thing);
65         }
66
67         return null;
68     }
69
70     @Override
71     protected synchronized void removeHandler(ThingHandler handler) {
72         if (handler instanceof RdsCloudHandler cloudHandler) {
73             destroyDiscoveryService(cloudHandler);
74         }
75     }
76
77     /*
78      * create a discovery service so that a newly created cloud account will find
79      * the things that it supports
80      */
81     private synchronized void createDiscoveryService(RdsCloudHandler handler) {
82         // create a new discovery service
83         RdsDiscoveryService ds = new RdsDiscoveryService(handler);
84
85         // register the discovery service
86         ServiceRegistration<?> serviceReg = bundleContext.registerService(DiscoveryService.class.getName(), ds,
87                 new Hashtable<>());
88
89         /*
90          * store service registration in a list so we can destroy it when the respective
91          * hub is destroyed
92          */
93         discos.put(handler.getThing().getUID(), serviceReg);
94
95         // finally activate the discovery service
96         ds.activate();
97     }
98
99     /*
100      * destroy the discovery service
101      */
102     private synchronized void destroyDiscoveryService(RdsCloudHandler handler) {
103         // fetch the respective thing's service registration from our list
104         @Nullable
105         ServiceRegistration<?> serviceReg = discos.remove(handler.getThing().getUID());
106
107         // retrieve the respective discovery service
108         if (serviceReg != null) {
109             RdsDiscoveryService disco = (RdsDiscoveryService) bundleContext.getService(serviceReg.getReference());
110
111             // unregister the service
112             serviceReg.unregister();
113
114             // deactivate the service
115             if (disco != null) {
116                 disco.deactivate();
117             }
118         }
119     }
120 }