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