]> git.basschouten.com Git - openhab-addons.git/blob
729868a20f9b406e0fdfa12119c3378b76b0885c
[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.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 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 = Collections
48             .unmodifiableSet(new HashSet<>(Arrays.asList(THING_TYPE_NEOHUB, THING_TYPE_NEOSTAT, THING_TYPE_NEOPLUG,
49                     THING_TYPE_NEOCONTACT, THING_TYPE_NEOTEMPERATURESENSOR)));
50
51     private final Map<ThingUID, ServiceRegistration<?>> discoServices = new HashMap<>();
52
53     @Override
54     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
55         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
56     }
57
58     @Override
59     protected @Nullable ThingHandler createHandler(Thing thing) {
60         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
61
62         if ((thingTypeUID.equals(THING_TYPE_NEOHUB)) && (thing instanceof Bridge)) {
63             NeoHubHandler handler = new NeoHubHandler((Bridge) thing);
64             createDiscoveryService(handler);
65             return handler;
66         }
67
68         if (thingTypeUID.equals(THING_TYPE_NEOSTAT)) {
69             return new NeoStatHandler(thing);
70         }
71
72         if (thingTypeUID.equals(THING_TYPE_NEOPLUG)) {
73             return new NeoPlugHandler(thing);
74         }
75
76         if (thingTypeUID.equals(THING_TYPE_NEOCONTACT)) {
77             return new NeoContactHandler(thing);
78         }
79
80         if (thingTypeUID.equals(THING_TYPE_NEOTEMPERATURESENSOR)) {
81             return new NeoTemperatureSensorHandler(thing);
82         }
83
84         return null;
85     }
86
87     @Override
88     protected synchronized void removeHandler(ThingHandler handler) {
89         if (handler instanceof NeoHubHandler) {
90             destroyDiscoveryService((NeoHubHandler) handler);
91         }
92     }
93
94     /*
95      * create a discovery service so that a newly created hub will find the
96      * respective things tht are inside it
97      */
98     private synchronized void createDiscoveryService(NeoHubHandler handler) {
99         // create a new discovery service
100         NeoHubDiscoveryService ds = new NeoHubDiscoveryService(handler);
101
102         // activate the discovery service
103         ds.activate();
104
105         // register the discovery service
106         ServiceRegistration<?> serviceReg = bundleContext.registerService(DiscoveryService.class.getName(), ds,
107                 new Hashtable<>());
108
109         /*
110          * store service registration in a list so we can destroy it when the respective
111          * hub is destroyed
112          */
113         discoServices.put(handler.getThing().getUID(), serviceReg);
114     }
115
116     /*
117      * destroy the discovery service
118      */
119     private synchronized void destroyDiscoveryService(NeoHubHandler handler) {
120         // fetch the respective thing's service registration from our list
121         ServiceRegistration<?> serviceReg = discoServices.remove(handler.getThing().getUID());
122
123         if (serviceReg != null) {
124             // retrieve the respective discovery service
125             NeoHubDiscoveryService disco = (NeoHubDiscoveryService) bundleContext.getService(serviceReg.getReference());
126
127             // and unregister the service
128             serviceReg.unregister();
129
130             // deactivate the service
131             if (disco != null) {
132                 disco.deactivate();
133             }
134         }
135     }
136 }