2 * Copyright (c) 2010-2021 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.wemo.internal;
15 import static org.openhab.binding.wemo.internal.WemoBindingConstants.UDN;
17 import java.util.HashMap;
18 import java.util.Hashtable;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.wemo.internal.discovery.WemoLinkDiscoveryService;
25 import org.openhab.binding.wemo.internal.handler.WemoBridgeHandler;
26 import org.openhab.binding.wemo.internal.handler.WemoCoffeeHandler;
27 import org.openhab.binding.wemo.internal.handler.WemoCrockpotHandler;
28 import org.openhab.binding.wemo.internal.handler.WemoDimmerHandler;
29 import org.openhab.binding.wemo.internal.handler.WemoHandler;
30 import org.openhab.binding.wemo.internal.handler.WemoHolmesHandler;
31 import org.openhab.binding.wemo.internal.handler.WemoLightHandler;
32 import org.openhab.binding.wemo.internal.handler.WemoMakerHandler;
33 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
34 import org.openhab.core.config.discovery.DiscoveryService;
35 import org.openhab.core.io.transport.upnp.UpnpIOService;
36 import org.openhab.core.thing.Bridge;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.thing.ThingUID;
40 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
41 import org.openhab.core.thing.binding.ThingHandler;
42 import org.openhab.core.thing.binding.ThingHandlerFactory;
43 import org.osgi.framework.ServiceRegistration;
44 import org.osgi.service.component.annotations.Activate;
45 import org.osgi.service.component.annotations.Component;
46 import org.osgi.service.component.annotations.Reference;
47 import org.osgi.service.component.annotations.ReferenceCardinality;
48 import org.osgi.service.component.annotations.ReferencePolicy;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * The {@link WemoHandlerFactory} is responsible for creating things and thing
56 * @author Hans-Jörg Merk - Initial contribution
57 * @author Kai Kreuzer - some refactoring for performance and simplification
60 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.wemo")
61 public class WemoHandlerFactory extends BaseThingHandlerFactory {
63 private final Logger logger = LoggerFactory.getLogger(WemoHandlerFactory.class);
65 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = WemoBindingConstants.SUPPORTED_THING_TYPES;
67 private UpnpIOService upnpIOService;
68 private @Nullable WemoHttpCallFactory wemoHttpCallFactory;
71 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
75 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
78 public WemoHandlerFactory(final @Reference UpnpIOService upnpIOService) {
79 this.upnpIOService = upnpIOService;
82 @Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
83 public void setWemoHttpCallFactory(WemoHttpCallFactory wemoHttpCallFactory) {
84 this.wemoHttpCallFactory = wemoHttpCallFactory;
87 public void unsetWemoHttpCallFactory(WemoHttpCallFactory wemoHttpCallFactory) {
88 this.wemoHttpCallFactory = null;
92 protected @Nullable ThingHandler createHandler(Thing thing) {
93 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
94 logger.debug("Trying to create a handler for ThingType '{}", thingTypeUID);
96 WemoHttpCallFactory wemoHttpCallFactory = this.wemoHttpCallFactory;
97 WemoHttpCall wemoHttpcaller = wemoHttpCallFactory == null ? new WemoHttpCall()
98 : wemoHttpCallFactory.createHttpCall();
100 if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_BRIDGE)) {
101 logger.debug("Creating a WemoBridgeHandler for thing '{}' with UDN '{}'", thing.getUID(),
102 thing.getConfiguration().get(UDN));
103 WemoBridgeHandler handler = new WemoBridgeHandler((Bridge) thing);
104 registerDeviceDiscoveryService(handler, wemoHttpcaller);
106 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_MAKER)) {
107 logger.debug("Creating a WemoMakerHandler for thing '{}' with UDN '{}'", thing.getUID(),
108 thing.getConfiguration().get(UDN));
109 return new WemoMakerHandler(thing, upnpIOService, wemoHttpcaller);
110 } else if (WemoBindingConstants.SUPPORTED_DEVICE_THING_TYPES.contains(thing.getThingTypeUID())) {
111 logger.debug("Creating a WemoHandler for thing '{}' with UDN '{}'", thing.getUID(),
112 thing.getConfiguration().get(UDN));
113 return new WemoHandler(thing, upnpIOService, wemoHttpcaller);
114 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_COFFEE)) {
115 logger.debug("Creating a WemoCoffeeHandler for thing '{}' with UDN '{}'", thing.getUID(),
116 thing.getConfiguration().get(UDN));
117 return new WemoCoffeeHandler(thing, upnpIOService, wemoHttpcaller);
118 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_DIMMER)) {
119 logger.debug("Creating a WemoDimmerHandler for thing '{}' with UDN '{}'", thing.getUID(),
120 thing.getConfiguration().get("udn"));
121 return new WemoDimmerHandler(thing, upnpIOService, wemoHttpcaller);
122 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_CROCKPOT)) {
123 logger.debug("Creating a WemoCockpotHandler for thing '{}' with UDN '{}'", thing.getUID(),
124 thing.getConfiguration().get("udn"));
125 return new WemoCrockpotHandler(thing, upnpIOService, wemoHttpcaller);
126 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_PURIFIER)) {
127 logger.debug("Creating a WemoHolmesHandler for thing '{}' with UDN '{}'", thing.getUID(),
128 thing.getConfiguration().get("udn"));
129 return new WemoHolmesHandler(thing, upnpIOService, wemoHttpcaller);
130 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_HUMIDIFIER)) {
131 logger.debug("Creating a WemoHolmesHandler for thing '{}' with UDN '{}'", thing.getUID(),
132 thing.getConfiguration().get("udn"));
133 return new WemoHolmesHandler(thing, upnpIOService, wemoHttpcaller);
134 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_HEATER)) {
135 logger.debug("Creating a WemoHolmesHandler for thing '{}' with UDN '{}'", thing.getUID(),
136 thing.getConfiguration().get("udn"));
137 return new WemoHolmesHandler(thing, upnpIOService, wemoHttpcaller);
138 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_MZ100)) {
139 return new WemoLightHandler(thing, upnpIOService, wemoHttpcaller);
141 logger.warn("ThingHandler not found for {}", thingTypeUID);
147 protected synchronized void removeHandler(ThingHandler thingHandler) {
148 if (thingHandler instanceof WemoBridgeHandler) {
149 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
150 if (serviceReg != null) {
151 serviceReg.unregister();
156 private synchronized void registerDeviceDiscoveryService(WemoBridgeHandler wemoBridgeHandler,
157 WemoHttpCall wemoHttpCaller) {
158 WemoLinkDiscoveryService discoveryService = new WemoLinkDiscoveryService(wemoBridgeHandler, upnpIOService,
160 this.discoveryServiceRegs.put(wemoBridgeHandler.getThing().getUID(),
161 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));