]> git.basschouten.com Git - openhab-addons.git/blob
7b3ab897947dc3ecd221f65106d3cc9d48e94b6e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.miele.internal;
14
15 import static org.openhab.binding.miele.internal.MieleBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
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;
56
57 /**
58  * The {@link MieleHandlerFactory} is responsible for creating things and thing
59  * handlers.
60  *
61  * @author Karel Goderis - Initial contribution
62  * @author Jacob Laursen - Refactored to use framework's HTTP client
63  */
64 @NonNullByDefault
65 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miele")
66 public class MieleHandlerFactory extends BaseThingHandlerFactory {
67
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());
72
73     private final HttpClient httpClient;
74     private final TranslationProvider i18nProvider;
75     private final LocaleProvider localeProvider;
76
77     private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
78
79     @Activate
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;
86     }
87
88     @Override
89     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
90         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
91     }
92
93     @Override
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);
99         }
100         if (MieleApplianceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
101             ThingUID mieleApplianceUID = getApplianceUID(thingTypeUID, thingUID, configuration, bridgeUID);
102             return super.createThing(thingTypeUID, configuration, mieleApplianceUID, bridgeUID);
103         }
104         throw new IllegalArgumentException(
105                 "The thing type " + thingTypeUID + " is not supported by the miele binding.");
106     }
107
108     @Override
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);
113             return 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);
117             }
118             if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGEFREEZER)) {
119                 return new FridgeFreezerHandler(thing, i18nProvider, localeProvider);
120             }
121             if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGE)) {
122                 return new FridgeHandler(thing, i18nProvider, localeProvider);
123             }
124             if (thing.getThingTypeUID().equals(THING_TYPE_OVEN)) {
125                 return new OvenHandler(thing, i18nProvider, localeProvider);
126             }
127             if (thing.getThingTypeUID().equals(THING_TYPE_HOB)) {
128                 return new HobHandler(thing, i18nProvider, localeProvider);
129             }
130             if (thing.getThingTypeUID().equals(THING_TYPE_WASHINGMACHINE)) {
131                 return new WashingMachineHandler(thing, i18nProvider, localeProvider);
132             }
133             if (thing.getThingTypeUID().equals(THING_TYPE_DRYER)) {
134                 return new TumbleDryerHandler(thing, i18nProvider, localeProvider);
135             }
136             if (thing.getThingTypeUID().equals(THING_TYPE_DISHWASHER)) {
137                 return new DishWasherHandler(thing, i18nProvider, localeProvider);
138             }
139             if (thing.getThingTypeUID().equals(THING_TYPE_COFFEEMACHINE)) {
140                 return new CoffeeMachineHandler(thing, i18nProvider, localeProvider);
141             }
142         }
143
144         return null;
145     }
146
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);
152         }
153         return thingUID;
154     }
155
156     private ThingUID getApplianceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
157             Configuration configuration, @Nullable ThingUID bridgeUID) {
158         String applianceId = (String) configuration.get(APPLIANCE_ID);
159
160         if (thingUID == null) {
161             if (bridgeUID == null) {
162                 thingUID = new ThingUID(thingTypeUID, applianceId);
163             } else {
164                 thingUID = new ThingUID(thingTypeUID, bridgeUID, applianceId);
165             }
166         }
167         return thingUID;
168     }
169
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<>()));
175     }
176
177     @Override
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();
188                 }
189             }
190         }
191     }
192 }