|
19 | 19 | import java.util.Map;
|
20 | 20 | import java.util.Set;
|
21 | 21 |
|
| 22 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 23 | +import org.eclipse.jdt.annotation.Nullable; |
22 | 24 | import org.openhab.binding.wemo.internal.discovery.WemoLinkDiscoveryService;
|
23 | 25 | import org.openhab.binding.wemo.internal.handler.WemoBridgeHandler;
|
24 | 26 | import org.openhab.binding.wemo.internal.handler.WemoCoffeeHandler;
|
|
39 | 41 | import org.openhab.core.thing.binding.ThingHandler;
|
40 | 42 | import org.openhab.core.thing.binding.ThingHandlerFactory;
|
41 | 43 | import org.osgi.framework.ServiceRegistration;
|
| 44 | +import org.osgi.service.component.annotations.Activate; |
42 | 45 | import org.osgi.service.component.annotations.Component;
|
43 | 46 | import org.osgi.service.component.annotations.Reference;
|
| 47 | +import org.osgi.service.component.annotations.ReferenceCardinality; |
| 48 | +import org.osgi.service.component.annotations.ReferencePolicy; |
44 | 49 | import org.slf4j.Logger;
|
45 | 50 | import org.slf4j.LoggerFactory;
|
46 | 51 |
|
|
51 | 56 | * @author Hans-Jörg Merk - Initial contribution
|
52 | 57 | * @author Kai Kreuzer - some refactoring for performance and simplification
|
53 | 58 | */
|
| 59 | +@NonNullByDefault |
54 | 60 | @Component(service = ThingHandlerFactory.class, configurationPid = "binding.wemo")
|
55 | 61 | public class WemoHandlerFactory extends BaseThingHandlerFactory {
|
56 | 62 |
|
57 | 63 | private final Logger logger = LoggerFactory.getLogger(WemoHandlerFactory.class);
|
58 | 64 |
|
59 |
| - private UpnpIOService upnpIOService; |
60 |
| - |
61 | 65 | public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = WemoBindingConstants.SUPPORTED_THING_TYPES;
|
62 | 66 |
|
| 67 | + private UpnpIOService upnpIOService; |
| 68 | + private @Nullable WemoHttpCallFactory wemoHttpCallFactory; |
| 69 | + |
63 | 70 | @Override
|
64 | 71 | public boolean supportsThingType(ThingTypeUID thingTypeUID) {
|
65 | 72 | return SUPPORTED_THING_TYPES.contains(thingTypeUID);
|
66 | 73 | }
|
67 | 74 |
|
68 | 75 | private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
|
69 | 76 |
|
70 |
| - @SuppressWarnings({ "null", "unused" }) |
71 |
| - @Override |
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); |
76 |
| - |
77 |
| - WemoHttpCall wemoHttpcaller = new WemoHttpCall(); |
78 |
| - |
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); |
84 |
| - return handler; |
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); |
119 |
| - } else { |
120 |
| - logger.warn("ThingHandler not found for {}", thingTypeUID); |
121 |
| - return null; |
122 |
| - } |
123 |
| - } |
124 |
| - return null; |
| 77 | + @Activate |
| 78 | + public WemoHandlerFactory(final @Reference UpnpIOService upnpIOService) { |
| 79 | + this.upnpIOService = upnpIOService; |
125 | 80 | }
|
126 | 81 |
|
127 |
| - @Reference |
128 |
| - protected void setUpnpIOService(UpnpIOService upnpIOService) { |
129 |
| - this.upnpIOService = upnpIOService; |
| 82 | + @Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC) |
| 83 | + public void setWemoHttpCallFactory(WemoHttpCallFactory wemoHttpCallFactory) { |
| 84 | + this.wemoHttpCallFactory = wemoHttpCallFactory; |
| 85 | + } |
| 86 | + |
| 87 | + public void unsetWemoHttpCallFactory(WemoHttpCallFactory wemoHttpCallFactory) { |
| 88 | + this.wemoHttpCallFactory = null; |
130 | 89 | }
|
131 | 90 |
|
132 |
| - protected void unsetUpnpIOService(UpnpIOService upnpIOService) { |
133 |
| - this.upnpIOService = null; |
| 91 | + @Override |
| 92 | + protected @Nullable ThingHandler createHandler(Thing thing) { |
| 93 | + ThingTypeUID thingTypeUID = thing.getThingTypeUID(); |
| 94 | + logger.debug("Trying to create a handler for ThingType '{}", thingTypeUID); |
| 95 | + |
| 96 | + WemoHttpCallFactory wemoHttpCallFactory = this.wemoHttpCallFactory; |
| 97 | + WemoHttpCall wemoHttpcaller = wemoHttpCallFactory == null ? new WemoHttpCall() |
| 98 | + : wemoHttpCallFactory.createHttpCall(); |
| 99 | + |
| 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); |
| 105 | + return handler; |
| 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); |
| 140 | + } else { |
| 141 | + logger.warn("ThingHandler not found for {}", thingTypeUID); |
| 142 | + return null; |
| 143 | + } |
134 | 144 | }
|
135 | 145 |
|
136 | 146 | @Override
|
|
0 commit comments