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