2 * Copyright (c) 2010-2022 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.miele.internal;
15 import static org.openhab.binding.miele.internal.MieleBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.eclipse.jetty.client.HttpClient;
27 import org.openhab.binding.miele.internal.discovery.MieleApplianceDiscoveryService;
28 import org.openhab.binding.miele.internal.handler.CoffeeMachineHandler;
29 import org.openhab.binding.miele.internal.handler.DishWasherHandler;
30 import org.openhab.binding.miele.internal.handler.FridgeFreezerHandler;
31 import org.openhab.binding.miele.internal.handler.FridgeHandler;
32 import org.openhab.binding.miele.internal.handler.HobHandler;
33 import org.openhab.binding.miele.internal.handler.HoodHandler;
34 import org.openhab.binding.miele.internal.handler.MieleApplianceHandler;
35 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler;
36 import org.openhab.binding.miele.internal.handler.OvenHandler;
37 import org.openhab.binding.miele.internal.handler.TumbleDryerHandler;
38 import org.openhab.binding.miele.internal.handler.WashingMachineHandler;
39 import org.openhab.core.config.core.Configuration;
40 import org.openhab.core.config.discovery.DiscoveryService;
41 import org.openhab.core.i18n.LocaleProvider;
42 import org.openhab.core.i18n.TranslationProvider;
43 import org.openhab.core.io.net.http.HttpClientFactory;
44 import org.openhab.core.thing.Bridge;
45 import org.openhab.core.thing.Thing;
46 import org.openhab.core.thing.ThingTypeUID;
47 import org.openhab.core.thing.ThingUID;
48 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
49 import org.openhab.core.thing.binding.ThingHandler;
50 import org.openhab.core.thing.binding.ThingHandlerFactory;
51 import org.osgi.framework.ServiceRegistration;
52 import org.osgi.service.component.ComponentContext;
53 import org.osgi.service.component.annotations.Activate;
54 import org.osgi.service.component.annotations.Component;
55 import org.osgi.service.component.annotations.Reference;
58 * The {@link MieleHandlerFactory} is responsible for creating things and thing
61 * @author Karel Goderis - Initial contribution
62 * @author Jacob Laursen - Refactored to use framework's HTTP client
65 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miele")
66 public class MieleHandlerFactory extends BaseThingHandlerFactory {
68 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
69 .concat(MieleBridgeHandler.SUPPORTED_THING_TYPES.stream(),
70 MieleApplianceHandler.SUPPORTED_THING_TYPES.stream())
71 .collect(Collectors.toSet());
73 private final HttpClient httpClient;
74 private final TranslationProvider i18nProvider;
75 private final LocaleProvider localeProvider;
77 private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
80 public MieleHandlerFactory(@Reference final HttpClientFactory httpClientFactory,
81 final @Reference TranslationProvider i18nProvider, final @Reference LocaleProvider localeProvider,
82 ComponentContext componentContext) {
83 this.httpClient = httpClientFactory.getCommonHttpClient();
84 this.i18nProvider = i18nProvider;
85 this.localeProvider = localeProvider;
89 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
90 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
94 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
95 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
96 if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
97 ThingUID mieleBridgeUID = getBridgeThingUID(thingTypeUID, thingUID, configuration);
98 return super.createThing(thingTypeUID, configuration, mieleBridgeUID, null);
100 if (MieleApplianceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
101 ThingUID mieleApplianceUID = getApplianceUID(thingTypeUID, thingUID, configuration, bridgeUID);
102 return super.createThing(thingTypeUID, configuration, mieleApplianceUID, bridgeUID);
104 throw new IllegalArgumentException(
105 "The thing type " + thingTypeUID + " is not supported by the miele binding.");
109 protected @Nullable ThingHandler createHandler(Thing thing) {
110 if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
111 MieleBridgeHandler handler = new MieleBridgeHandler((Bridge) thing, httpClient);
112 registerApplianceDiscoveryService(handler);
114 } else if (MieleApplianceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
115 if (thing.getThingTypeUID().equals(THING_TYPE_HOOD)) {
116 return new HoodHandler(thing, i18nProvider, localeProvider);
118 if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGEFREEZER)) {
119 return new FridgeFreezerHandler(thing, i18nProvider, localeProvider);
121 if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGE)) {
122 return new FridgeHandler(thing, i18nProvider, localeProvider);
124 if (thing.getThingTypeUID().equals(THING_TYPE_OVEN)) {
125 return new OvenHandler(thing, i18nProvider, localeProvider);
127 if (thing.getThingTypeUID().equals(THING_TYPE_HOB)) {
128 return new HobHandler(thing, i18nProvider, localeProvider);
130 if (thing.getThingTypeUID().equals(THING_TYPE_WASHINGMACHINE)) {
131 return new WashingMachineHandler(thing, i18nProvider, localeProvider);
133 if (thing.getThingTypeUID().equals(THING_TYPE_DRYER)) {
134 return new TumbleDryerHandler(thing, i18nProvider, localeProvider);
136 if (thing.getThingTypeUID().equals(THING_TYPE_DISHWASHER)) {
137 return new DishWasherHandler(thing, i18nProvider, localeProvider);
139 if (thing.getThingTypeUID().equals(THING_TYPE_COFFEEMACHINE)) {
140 return new CoffeeMachineHandler(thing, i18nProvider, localeProvider);
147 private ThingUID getBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
148 Configuration configuration) {
149 if (thingUID == null) {
150 String hostID = (String) configuration.get(HOST);
151 thingUID = new ThingUID(thingTypeUID, hostID);
156 private ThingUID getApplianceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
157 Configuration configuration, @Nullable ThingUID bridgeUID) {
158 String applianceId = (String) configuration.get(APPLIANCE_ID);
160 if (thingUID == null) {
161 if (bridgeUID == null) {
162 thingUID = new ThingUID(thingTypeUID, applianceId);
164 thingUID = new ThingUID(thingTypeUID, bridgeUID, applianceId);
170 private synchronized void registerApplianceDiscoveryService(MieleBridgeHandler bridgeHandler) {
171 MieleApplianceDiscoveryService discoveryService = new MieleApplianceDiscoveryService(bridgeHandler);
172 discoveryService.activate();
173 this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
174 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
178 protected synchronized void removeHandler(ThingHandler thingHandler) {
179 if (thingHandler instanceof MieleBridgeHandler) {
180 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
181 if (serviceReg != null) {
182 // remove discovery service, if bridge handler is removed
183 MieleApplianceDiscoveryService service = (MieleApplianceDiscoveryService) bundleContext
184 .getService(serviceReg.getReference());
185 serviceReg.unregister();
186 if (service != null) {
187 service.deactivate();