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