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