2 * Copyright (c) 2010-2023 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.jupnp.UpnpService;
25 import org.openhab.binding.wemo.internal.discovery.WemoLinkDiscoveryService;
26 import org.openhab.binding.wemo.internal.handler.WemoBridgeHandler;
27 import org.openhab.binding.wemo.internal.handler.WemoCoffeeHandler;
28 import org.openhab.binding.wemo.internal.handler.WemoCrockpotHandler;
29 import org.openhab.binding.wemo.internal.handler.WemoDimmerHandler;
30 import org.openhab.binding.wemo.internal.handler.WemoHolmesHandler;
31 import org.openhab.binding.wemo.internal.handler.WemoInsightHandler;
32 import org.openhab.binding.wemo.internal.handler.WemoLightHandler;
33 import org.openhab.binding.wemo.internal.handler.WemoMakerHandler;
34 import org.openhab.binding.wemo.internal.handler.WemoMotionHandler;
35 import org.openhab.binding.wemo.internal.handler.WemoSwitchHandler;
36 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
37 import org.openhab.core.config.discovery.DiscoveryService;
38 import org.openhab.core.io.transport.upnp.UpnpIOService;
39 import org.openhab.core.thing.Bridge;
40 import org.openhab.core.thing.Thing;
41 import org.openhab.core.thing.ThingTypeUID;
42 import org.openhab.core.thing.ThingUID;
43 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
44 import org.openhab.core.thing.binding.ThingHandler;
45 import org.openhab.core.thing.binding.ThingHandlerFactory;
46 import org.osgi.framework.ServiceRegistration;
47 import org.osgi.service.component.annotations.Activate;
48 import org.osgi.service.component.annotations.Component;
49 import org.osgi.service.component.annotations.Reference;
50 import org.osgi.service.component.annotations.ReferenceCardinality;
51 import org.osgi.service.component.annotations.ReferencePolicy;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * The {@link WemoHandlerFactory} is responsible for creating things and thing
59 * @author Hans-Jörg Merk - Initial contribution
60 * @author Kai Kreuzer - some refactoring for performance and simplification
63 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.wemo")
64 public class WemoHandlerFactory extends BaseThingHandlerFactory {
66 private final Logger logger = LoggerFactory.getLogger(WemoHandlerFactory.class);
68 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = WemoBindingConstants.SUPPORTED_THING_TYPES;
70 private final UpnpIOService upnpIOService;
71 private final UpnpService upnpService;
72 private @Nullable WemoHttpCallFactory wemoHttpCallFactory;
75 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
76 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
79 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
82 public WemoHandlerFactory(final @Reference UpnpIOService upnpIOService, @Reference UpnpService upnpService) {
83 this.upnpIOService = upnpIOService;
84 this.upnpService = upnpService;
87 @Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
88 public void setWemoHttpCallFactory(WemoHttpCallFactory wemoHttpCallFactory) {
89 this.wemoHttpCallFactory = wemoHttpCallFactory;
92 public void unsetWemoHttpCallFactory(WemoHttpCallFactory wemoHttpCallFactory) {
93 this.wemoHttpCallFactory = null;
97 protected @Nullable ThingHandler createHandler(Thing thing) {
98 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
99 logger.debug("Trying to create a handler for ThingType '{}", thingTypeUID);
101 WemoHttpCallFactory wemoHttpCallFactory = this.wemoHttpCallFactory;
102 WemoHttpCall wemoHttpcaller = wemoHttpCallFactory == null ? new WemoHttpCall()
103 : wemoHttpCallFactory.createHttpCall();
105 if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_BRIDGE)) {
106 logger.debug("Creating a WemoBridgeHandler for thing '{}' with UDN '{}'", thing.getUID(),
107 thing.getConfiguration().get(UDN));
108 WemoBridgeHandler handler = new WemoBridgeHandler((Bridge) thing);
109 registerDeviceDiscoveryService(handler, wemoHttpcaller);
111 } else if (WemoBindingConstants.THING_TYPE_INSIGHT.equals(thing.getThingTypeUID())) {
112 logger.debug("Creating a WemoInsightHandler for thing '{}' with UDN '{}'", thing.getUID(),
113 thing.getConfiguration().get(UDN));
114 return new WemoInsightHandler(thing, upnpIOService, upnpService, wemoHttpcaller);
115 } else if (WemoBindingConstants.THING_TYPE_SOCKET.equals(thing.getThingTypeUID())
116 || WemoBindingConstants.THING_TYPE_LIGHTSWITCH.equals(thing.getThingTypeUID())) {
117 logger.debug("Creating a WemoSwitchHandler for thing '{}' with UDN '{}'", thing.getUID(),
118 thing.getConfiguration().get(UDN));
119 return new WemoSwitchHandler(thing, upnpIOService, upnpService, wemoHttpcaller);
120 } else if (WemoBindingConstants.THING_TYPE_MOTION.equals(thing.getThingTypeUID())) {
121 logger.debug("Creating a WemoMotionHandler for thing '{}' with UDN '{}'", thing.getUID(),
122 thing.getConfiguration().get(UDN));
123 return new WemoMotionHandler(thing, upnpIOService, upnpService, wemoHttpcaller);
124 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_MAKER)) {
125 logger.debug("Creating a WemoMakerHandler for thing '{}' with UDN '{}'", thing.getUID(),
126 thing.getConfiguration().get(UDN));
127 return new WemoMakerHandler(thing, upnpIOService, upnpService, wemoHttpcaller);
128 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_COFFEE)) {
129 logger.debug("Creating a WemoCoffeeHandler for thing '{}' with UDN '{}'", thing.getUID(),
130 thing.getConfiguration().get(UDN));
131 return new WemoCoffeeHandler(thing, upnpIOService, upnpService, wemoHttpcaller);
132 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_DIMMER)) {
133 logger.debug("Creating a WemoDimmerHandler for thing '{}' with UDN '{}'", thing.getUID(),
134 thing.getConfiguration().get("udn"));
135 return new WemoDimmerHandler(thing, upnpIOService, upnpService, wemoHttpcaller);
136 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_CROCKPOT)) {
137 logger.debug("Creating a WemoCockpotHandler for thing '{}' with UDN '{}'", thing.getUID(),
138 thing.getConfiguration().get("udn"));
139 return new WemoCrockpotHandler(thing, upnpIOService, upnpService, wemoHttpcaller);
140 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_PURIFIER)) {
141 logger.debug("Creating a WemoHolmesHandler for thing '{}' with UDN '{}'", thing.getUID(),
142 thing.getConfiguration().get("udn"));
143 return new WemoHolmesHandler(thing, upnpIOService, upnpService, wemoHttpcaller);
144 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_HUMIDIFIER)) {
145 logger.debug("Creating a WemoHolmesHandler for thing '{}' with UDN '{}'", thing.getUID(),
146 thing.getConfiguration().get("udn"));
147 return new WemoHolmesHandler(thing, upnpIOService, upnpService, wemoHttpcaller);
148 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_HEATER)) {
149 logger.debug("Creating a WemoHolmesHandler for thing '{}' with UDN '{}'", thing.getUID(),
150 thing.getConfiguration().get("udn"));
151 return new WemoHolmesHandler(thing, upnpIOService, upnpService, wemoHttpcaller);
152 } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_MZ100)) {
153 return new WemoLightHandler(thing, upnpIOService, upnpService, wemoHttpcaller);
155 logger.warn("ThingHandler not found for {}", thingTypeUID);
161 protected synchronized void removeHandler(ThingHandler thingHandler) {
162 if (thingHandler instanceof WemoBridgeHandler) {
163 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
164 if (serviceReg != null) {
165 serviceReg.unregister();
170 private synchronized void registerDeviceDiscoveryService(WemoBridgeHandler wemoBridgeHandler,
171 WemoHttpCall wemoHttpCaller) {
172 WemoLinkDiscoveryService discoveryService = new WemoLinkDiscoveryService(wemoBridgeHandler, upnpIOService,
174 this.discoveryServiceRegs.put(wemoBridgeHandler.getThing().getUID(),
175 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));