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