]> git.basschouten.com Git - openhab-addons.git/blob
7bda6c0002ba6e320bfc6a9d460d76302ebb1308
[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.sleepiq.internal;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.jetty.client.HttpClient;
26 import org.openhab.binding.sleepiq.internal.discovery.SleepIQBedDiscoveryService;
27 import org.openhab.binding.sleepiq.internal.handler.SleepIQCloudHandler;
28 import org.openhab.binding.sleepiq.internal.handler.SleepIQDualBedHandler;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.io.net.http.HttpClientFactory;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerFactory;
38 import org.osgi.framework.ServiceRegistration;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * The {@link SleepIQHandlerFactory} is responsible for creating thing handlers.
47  *
48  * @author Gregory Moyer - Initial contribution
49  */
50 @NonNullByDefault
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.sleepiq")
52 public class SleepIQHandlerFactory extends BaseThingHandlerFactory {
53     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPE_UIDS = Collections
54             .unmodifiableSet(Stream.concat(SleepIQCloudHandler.SUPPORTED_THING_TYPE_UIDS.stream(),
55                     SleepIQDualBedHandler.SUPPORTED_THING_TYPE_UIDS.stream()).collect(Collectors.toSet()));
56
57     private final Logger logger = LoggerFactory.getLogger(SleepIQHandlerFactory.class);
58
59     private final HttpClient httpClient;
60
61     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceReg = new HashMap<>();
62
63     @Activate
64     public SleepIQHandlerFactory(@Reference HttpClientFactory httpClientFactory) {
65         this.httpClient = httpClientFactory.getCommonHttpClient();
66     }
67
68     @Override
69     public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
70         return SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID);
71     }
72
73     @Override
74     protected @Nullable ThingHandler createHandler(final Thing thing) {
75         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
76         if (SleepIQCloudHandler.SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID)) {
77             logger.debug("Creating SleepIQ cloud thing handler");
78             SleepIQCloudHandler cloudHandler = new SleepIQCloudHandler((Bridge) thing, httpClient);
79             registerBedDiscoveryService(cloudHandler);
80             return cloudHandler;
81         } else if (SleepIQDualBedHandler.SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID)) {
82             logger.debug("Creating SleepIQ dual bed thing handler");
83             return new SleepIQDualBedHandler(thing);
84         }
85         return null;
86     }
87
88     @Override
89     protected void removeHandler(final ThingHandler thingHandler) {
90         logger.debug("Removing SleepIQ thing handler");
91         if (thingHandler instanceof SleepIQCloudHandler) {
92             unregisterBedDiscoveryService((SleepIQCloudHandler) thingHandler);
93         }
94     }
95
96     /**
97      * Register the given cloud handler to participate in discovery of new beds.
98      *
99      * @param cloudHandler the cloud handler to register (must not be <code>null</code>)
100      */
101     private synchronized void registerBedDiscoveryService(final SleepIQCloudHandler cloudHandler) {
102         logger.debug("Registering bed discovery service");
103         SleepIQBedDiscoveryService discoveryService = new SleepIQBedDiscoveryService(cloudHandler);
104         discoveryServiceReg.put(cloudHandler.getThing().getUID(),
105                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
106     }
107
108     /**
109      * Unregister the given cloud handler from participating in discovery of new beds.
110      *
111      * @param cloudHandler the cloud handler to unregister (must not be <code>null</code>)
112      */
113     private synchronized void unregisterBedDiscoveryService(final SleepIQCloudHandler cloudHandler) {
114         ThingUID thingUID = cloudHandler.getThing().getUID();
115         ServiceRegistration<?> serviceReg = discoveryServiceReg.remove(thingUID);
116         if (serviceReg != null) {
117             logger.debug("Unregistering bed discovery service");
118             serviceReg.unregister();
119         }
120     }
121 }