]> git.basschouten.com Git - openhab-addons.git/blob
c802398866cb2947bb88a74696cb5eb8d1f65215
[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.openhab.binding.miele.internal.discovery.MieleApplianceDiscoveryService;
27 import org.openhab.binding.miele.internal.handler.CoffeeMachineHandler;
28 import org.openhab.binding.miele.internal.handler.DishWasherHandler;
29 import org.openhab.binding.miele.internal.handler.FridgeFreezerHandler;
30 import org.openhab.binding.miele.internal.handler.FridgeHandler;
31 import org.openhab.binding.miele.internal.handler.HobHandler;
32 import org.openhab.binding.miele.internal.handler.HoodHandler;
33 import org.openhab.binding.miele.internal.handler.MieleApplianceHandler;
34 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler;
35 import org.openhab.binding.miele.internal.handler.OvenHandler;
36 import org.openhab.binding.miele.internal.handler.TumbleDryerHandler;
37 import org.openhab.binding.miele.internal.handler.WashingMachineHandler;
38 import org.openhab.core.config.core.Configuration;
39 import org.openhab.core.config.discovery.DiscoveryService;
40 import org.openhab.core.i18n.LocaleProvider;
41 import org.openhab.core.i18n.TranslationProvider;
42 import org.openhab.core.thing.Bridge;
43 import org.openhab.core.thing.Thing;
44 import org.openhab.core.thing.ThingTypeUID;
45 import org.openhab.core.thing.ThingUID;
46 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
47 import org.openhab.core.thing.binding.ThingHandler;
48 import org.openhab.core.thing.binding.ThingHandlerFactory;
49 import org.osgi.framework.ServiceRegistration;
50 import org.osgi.service.component.ComponentContext;
51 import org.osgi.service.component.annotations.Activate;
52 import org.osgi.service.component.annotations.Component;
53 import org.osgi.service.component.annotations.Reference;
54
55 /**
56  * The {@link MieleHandlerFactory} is responsible for creating things and thing
57  * handlers.
58  *
59  * @author Karel Goderis - Initial contribution
60  */
61 @NonNullByDefault
62 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miele")
63 public class MieleHandlerFactory extends BaseThingHandlerFactory {
64
65     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
66             .concat(MieleBridgeHandler.SUPPORTED_THING_TYPES.stream(),
67                     MieleApplianceHandler.SUPPORTED_THING_TYPES.stream())
68             .collect(Collectors.toSet());
69
70     private final TranslationProvider i18nProvider;
71     private final LocaleProvider localeProvider;
72
73     private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
74
75     @Activate
76     public MieleHandlerFactory(final @Reference TranslationProvider i18nProvider,
77             final @Reference LocaleProvider localeProvider, ComponentContext componentContext) {
78         this.i18nProvider = i18nProvider;
79         this.localeProvider = localeProvider;
80     }
81
82     @Override
83     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
84         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
85     }
86
87     @Override
88     public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
89             @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
90         if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
91             ThingUID mieleBridgeUID = getBridgeThingUID(thingTypeUID, thingUID, configuration);
92             return super.createThing(thingTypeUID, configuration, mieleBridgeUID, null);
93         }
94         if (MieleApplianceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
95             ThingUID mieleApplianceUID = getApplianceUID(thingTypeUID, thingUID, configuration, bridgeUID);
96             return super.createThing(thingTypeUID, configuration, mieleApplianceUID, bridgeUID);
97         }
98         throw new IllegalArgumentException(
99                 "The thing type " + thingTypeUID + " is not supported by the miele binding.");
100     }
101
102     @Override
103     protected @Nullable ThingHandler createHandler(Thing thing) {
104         if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
105             MieleBridgeHandler handler = new MieleBridgeHandler((Bridge) thing);
106             registerApplianceDiscoveryService(handler);
107             return handler;
108         } else if (MieleApplianceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
109             if (thing.getThingTypeUID().equals(THING_TYPE_HOOD)) {
110                 return new HoodHandler(thing, i18nProvider, localeProvider);
111             }
112             if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGEFREEZER)) {
113                 return new FridgeFreezerHandler(thing, i18nProvider, localeProvider);
114             }
115             if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGE)) {
116                 return new FridgeHandler(thing, i18nProvider, localeProvider);
117             }
118             if (thing.getThingTypeUID().equals(THING_TYPE_OVEN)) {
119                 return new OvenHandler(thing, i18nProvider, localeProvider);
120             }
121             if (thing.getThingTypeUID().equals(THING_TYPE_HOB)) {
122                 return new HobHandler(thing, i18nProvider, localeProvider);
123             }
124             if (thing.getThingTypeUID().equals(THING_TYPE_WASHINGMACHINE)) {
125                 return new WashingMachineHandler(thing, i18nProvider, localeProvider);
126             }
127             if (thing.getThingTypeUID().equals(THING_TYPE_DRYER)) {
128                 return new TumbleDryerHandler(thing, i18nProvider, localeProvider);
129             }
130             if (thing.getThingTypeUID().equals(THING_TYPE_DISHWASHER)) {
131                 return new DishWasherHandler(thing, i18nProvider, localeProvider);
132             }
133             if (thing.getThingTypeUID().equals(THING_TYPE_COFFEEMACHINE)) {
134                 return new CoffeeMachineHandler(thing, i18nProvider, localeProvider);
135             }
136         }
137
138         return null;
139     }
140
141     private ThingUID getBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
142             Configuration configuration) {
143         if (thingUID == null) {
144             String hostID = (String) configuration.get(HOST);
145             thingUID = new ThingUID(thingTypeUID, hostID);
146         }
147         return thingUID;
148     }
149
150     private ThingUID getApplianceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
151             Configuration configuration, @Nullable ThingUID bridgeUID) {
152         String applianceId = (String) configuration.get(APPLIANCE_ID);
153
154         if (thingUID == null) {
155             if (bridgeUID == null) {
156                 thingUID = new ThingUID(thingTypeUID, applianceId);
157             } else {
158                 thingUID = new ThingUID(thingTypeUID, bridgeUID, applianceId);
159             }
160         }
161         return thingUID;
162     }
163
164     private synchronized void registerApplianceDiscoveryService(MieleBridgeHandler bridgeHandler) {
165         MieleApplianceDiscoveryService discoveryService = new MieleApplianceDiscoveryService(bridgeHandler);
166         discoveryService.activate();
167         this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
168                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
169     }
170
171     @Override
172     protected synchronized void removeHandler(ThingHandler thingHandler) {
173         if (thingHandler instanceof MieleBridgeHandler) {
174             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
175             if (serviceReg != null) {
176                 // remove discovery service, if bridge handler is removed
177                 MieleApplianceDiscoveryService service = (MieleApplianceDiscoveryService) bundleContext
178                         .getService(serviceReg.getReference());
179                 serviceReg.unregister();
180                 if (service != null) {
181                     service.deactivate();
182                 }
183             }
184         }
185     }
186 }