2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.sleepiq.internal;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
23 import javax.ws.rs.client.ClientBuilder;
25 import org.openhab.binding.sleepiq.internal.discovery.SleepIQBedDiscoveryService;
26 import org.openhab.binding.sleepiq.internal.handler.SleepIQCloudHandler;
27 import org.openhab.binding.sleepiq.internal.handler.SleepIQDualBedHandler;
28 import org.openhab.core.config.discovery.DiscoveryService;
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 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * The {@link SleepIQHandlerFactory} is responsible for creating thing handlers.
46 * @author Gregory Moyer - Initial contribution
48 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.sleepiq")
49 public class SleepIQHandlerFactory extends BaseThingHandlerFactory {
50 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPE_UIDS = Collections
51 .unmodifiableSet(Stream.concat(SleepIQCloudHandler.SUPPORTED_THING_TYPE_UIDS.stream(),
52 SleepIQDualBedHandler.SUPPORTED_THING_TYPE_UIDS.stream()).collect(Collectors.toSet()));
54 private final Logger logger = LoggerFactory.getLogger(SleepIQHandlerFactory.class);
56 private final ClientBuilder clientBuilder;
58 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceReg = new HashMap<>();
61 public SleepIQHandlerFactory(@Reference ClientBuilder clientBuilder) {
62 this.clientBuilder = clientBuilder;
66 public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
67 return SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID);
71 protected ThingHandler createHandler(final Thing thing) {
72 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
74 if (SleepIQCloudHandler.SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID)) {
75 logger.debug("Creating SleepIQ cloud thing handler");
76 SleepIQCloudHandler cloudHandler = new SleepIQCloudHandler((Bridge) thing, clientBuilder);
77 registerBedDiscoveryService(cloudHandler);
79 } else if (SleepIQDualBedHandler.SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID)) {
80 logger.debug("Creating SleepIQ dual bed thing handler");
81 return new SleepIQDualBedHandler(thing);
88 protected void removeHandler(final ThingHandler thingHandler) {
89 logger.debug("Removing SleepIQ thing handler");
91 if (thingHandler instanceof SleepIQCloudHandler) {
92 unregisterBedDiscoveryService((SleepIQCloudHandler) thingHandler);
97 * Register the given cloud handler to participate in discovery of new beds.
99 * @param cloudHandler the cloud handler to register (must not be <code>null</code>)
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<>()));
109 * Unregister the given cloud handler from participating in discovery of new beds.
111 * @param cloudHandler the cloud handler to unregister (must not be <code>null</code>)
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();