2 * Copyright (c) 2010-2020 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.openhab.binding.wemo.internal.discovery.WemoLinkDiscoveryService;
23 import org.openhab.binding.wemo.internal.handler.WemoBridgeHandler;
24 import org.openhab.binding.wemo.internal.handler.WemoCoffeeHandler;
25 import org.openhab.binding.wemo.internal.handler.WemoCrockpotHandler;
26 import org.openhab.binding.wemo.internal.handler.WemoDimmerHandler;
27 import org.openhab.binding.wemo.internal.handler.WemoHandler;
28 import org.openhab.binding.wemo.internal.handler.WemoHolmesHandler;
29 import org.openhab.binding.wemo.internal.handler.WemoLightHandler;
30 import org.openhab.binding.wemo.internal.handler.WemoMakerHandler;
31 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
32 import org.openhab.core.config.discovery.DiscoveryService;
33 import org.openhab.core.io.transport.upnp.UpnpIOService;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.ThingUID;
38 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
39 import org.openhab.core.thing.binding.ThingHandler;
40 import org.openhab.core.thing.binding.ThingHandlerFactory;
41 import org.osgi.framework.ServiceRegistration;
42 import org.osgi.service.component.annotations.Component;
43 import org.osgi.service.component.annotations.Reference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * The {@link WemoHandlerFactory} is responsible for creating things and thing
51 * @author Hans-Jörg Merk - Initial contribution
52 * @author Kai Kreuzer - some refactoring for performance and simplification
54 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.wemo")
55 public class WemoHandlerFactory extends BaseThingHandlerFactory {
57 private final Logger logger = LoggerFactory.getLogger(WemoHandlerFactory.class);
59 private UpnpIOService upnpIOService;
61 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = WemoBindingConstants.SUPPORTED_THING_TYPES;
64 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
68 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
70 @SuppressWarnings({ "null", "unused" })
72 protected ThingHandler createHandler(Thing thing) {
73 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
74 if (thingTypeUID != null) {
75 logger.debug("Trying to create a handler for ThingType '{}", thingTypeUID);
77 WemoHttpCall wemoHttpcaller = new WemoHttpCall();
79 if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_BRIDGE)) {
80 logger.debug("Creating a WemoBridgeHandler for thing '{}' with UDN '{}'", thing.getUID(),
81 thing.getConfiguration().get(UDN));
82 WemoBridgeHandler handler = new WemoBridgeHandler((Bridge) thing);
83 registerDeviceDiscoveryService(handler, wemoHttpcaller);
85 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_MAKER)) {
86 logger.debug("Creating a WemoMakerHandler for thing '{}' with UDN '{}'", thing.getUID(),
87 thing.getConfiguration().get(UDN));
88 return new WemoMakerHandler(thing, upnpIOService, wemoHttpcaller);
89 } else if (WemoBindingConstants.SUPPORTED_DEVICE_THING_TYPES.contains(thing.getThingTypeUID())) {
90 logger.debug("Creating a WemoHandler for thing '{}' with UDN '{}'", thing.getUID(),
91 thing.getConfiguration().get(UDN));
92 return new WemoHandler(thing, upnpIOService, wemoHttpcaller);
93 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_COFFEE)) {
94 logger.debug("Creating a WemoCoffeeHandler for thing '{}' with UDN '{}'", thing.getUID(),
95 thing.getConfiguration().get(UDN));
96 return new WemoCoffeeHandler(thing, upnpIOService, wemoHttpcaller);
97 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_DIMMER)) {
98 logger.debug("Creating a WemoDimmerHandler for thing '{}' with UDN '{}'", thing.getUID(),
99 thing.getConfiguration().get("udn"));
100 return new WemoDimmerHandler(thing, upnpIOService, wemoHttpcaller);
101 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_CROCKPOT)) {
102 logger.debug("Creating a WemoCockpotHandler for thing '{}' with UDN '{}'", thing.getUID(),
103 thing.getConfiguration().get("udn"));
104 return new WemoCrockpotHandler(thing, upnpIOService, wemoHttpcaller);
105 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_PURIFIER)) {
106 logger.debug("Creating a WemoHolmesHandler for thing '{}' with UDN '{}'", thing.getUID(),
107 thing.getConfiguration().get("udn"));
108 return new WemoHolmesHandler(thing, upnpIOService, wemoHttpcaller);
109 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_HUMIDIFIER)) {
110 logger.debug("Creating a WemoHolmesHandler for thing '{}' with UDN '{}'", thing.getUID(),
111 thing.getConfiguration().get("udn"));
112 return new WemoHolmesHandler(thing, upnpIOService, wemoHttpcaller);
113 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_HEATER)) {
114 logger.debug("Creating a WemoHolmesHandler for thing '{}' with UDN '{}'", thing.getUID(),
115 thing.getConfiguration().get("udn"));
116 return new WemoHolmesHandler(thing, upnpIOService, wemoHttpcaller);
117 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_MZ100)) {
118 return new WemoLightHandler(thing, upnpIOService, wemoHttpcaller);
120 logger.warn("ThingHandler not found for {}", thingTypeUID);
128 protected void setUpnpIOService(UpnpIOService upnpIOService) {
129 this.upnpIOService = upnpIOService;
132 protected void unsetUpnpIOService(UpnpIOService upnpIOService) {
133 this.upnpIOService = null;
137 protected synchronized void removeHandler(ThingHandler thingHandler) {
138 if (thingHandler instanceof WemoBridgeHandler) {
139 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
140 if (serviceReg != null) {
141 serviceReg.unregister();
146 private synchronized void registerDeviceDiscoveryService(WemoBridgeHandler wemoBridgeHandler,
147 WemoHttpCall wemoHttpCaller) {
148 WemoLinkDiscoveryService discoveryService = new WemoLinkDiscoveryService(wemoBridgeHandler, upnpIOService,
150 this.discoveryServiceRegs.put(wemoBridgeHandler.getThing().getUID(),
151 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));