]> git.basschouten.com Git - openhab-addons.git/blob
f543d5ebdcfb878fc8c4062d979c3071df6e4849
[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.neohub.internal;
14
15 import static org.openhab.binding.neohub.internal.NeoHubBindingConstants.*;
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.io.net.http.WebSocketFactory;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerFactory;
33 import org.osgi.framework.ServiceRegistration;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
37
38 /**
39  * The {@link NeoHubHandlerFactory} creates things and thing handlers
40  *
41  * @author Andrew Fiddian-Green - Initial contribution
42  */
43 @NonNullByDefault
44 @Component(configurationPid = "binding.neohub", service = ThingHandlerFactory.class)
45 public class NeoHubHandlerFactory extends BaseThingHandlerFactory {
46
47     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_NEOHUB, THING_TYPE_NEOSTAT,
48             THING_TYPE_NEOPLUG, THING_TYPE_NEOCONTACT, THING_TYPE_NEOTEMPERATURESENSOR);
49
50     private final WebSocketFactory webSocketFactory;
51     private final Map<ThingUID, ServiceRegistration<?>> discoServices = new HashMap<>();
52
53     @Activate
54     public NeoHubHandlerFactory(final @Reference WebSocketFactory webSocketFactory) {
55         this.webSocketFactory = webSocketFactory;
56     }
57
58     @Override
59     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
60         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
61     }
62
63     @Override
64     protected @Nullable ThingHandler createHandler(Thing thing) {
65         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
66
67         if ((thingTypeUID.equals(THING_TYPE_NEOHUB)) && (thing instanceof Bridge bridge)) {
68             NeoHubHandler handler = new NeoHubHandler(bridge, webSocketFactory);
69             createDiscoveryService(handler);
70             return handler;
71         }
72
73         if (thingTypeUID.equals(THING_TYPE_NEOSTAT)) {
74             return new NeoStatHandler(thing);
75         }
76
77         if (thingTypeUID.equals(THING_TYPE_NEOPLUG)) {
78             return new NeoPlugHandler(thing);
79         }
80
81         if (thingTypeUID.equals(THING_TYPE_NEOCONTACT)) {
82             return new NeoContactHandler(thing);
83         }
84
85         if (thingTypeUID.equals(THING_TYPE_NEOTEMPERATURESENSOR)) {
86             return new NeoTemperatureSensorHandler(thing);
87         }
88
89         return null;
90     }
91
92     @Override
93     protected synchronized void removeHandler(ThingHandler handler) {
94         if (handler instanceof NeoHubHandler neoHubHandler) {
95             destroyDiscoveryService(neoHubHandler);
96         }
97     }
98
99     /*
100      * create a discovery service so that a newly created hub will find the
101      * respective things tht are inside it
102      */
103     private synchronized void createDiscoveryService(NeoHubHandler handler) {
104         // create a new discovery service
105         NeoHubDiscoveryService ds = new NeoHubDiscoveryService(handler);
106
107         // activate the discovery service
108         ds.activate();
109
110         // register the discovery service
111         ServiceRegistration<?> serviceReg = bundleContext.registerService(DiscoveryService.class.getName(), ds,
112                 new Hashtable<>());
113
114         /*
115          * store service registration in a list so we can destroy it when the respective
116          * hub is destroyed
117          */
118         discoServices.put(handler.getThing().getUID(), serviceReg);
119     }
120
121     /*
122      * destroy the discovery service
123      */
124     private synchronized void destroyDiscoveryService(NeoHubHandler handler) {
125         // fetch the respective thing's service registration from our list
126         ServiceRegistration<?> serviceReg = discoServices.remove(handler.getThing().getUID());
127
128         if (serviceReg != null) {
129             // retrieve the respective discovery service
130             NeoHubDiscoveryService disco = (NeoHubDiscoveryService) bundleContext.getService(serviceReg.getReference());
131
132             // and unregister the service
133             serviceReg.unregister();
134
135             // deactivate the service
136             if (disco != null) {
137                 disco.deactivate();
138             }
139         }
140     }
141 }