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