]> git.basschouten.com Git - openhab-addons.git/blob
19a1e941982503415827b42519b8288b929f5004
[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.boschshc.internal.devices;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_CLIMATE_CONTROL;
16 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_INWALL_SWITCH;
17 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_MOTION_DETECTOR;
18 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_SHC;
19 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_SHUTTER_CONTROL;
20 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_THERMOSTAT;
21 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_TWINGUARD;
22 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_WINDOW_CONTACT;
23
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.function.Function;
27
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.openhab.binding.boschshc.internal.devices.bridge.BoschSHCBridgeHandler;
31 import org.openhab.binding.boschshc.internal.devices.climatecontrol.ClimateControlHandler;
32 import org.openhab.binding.boschshc.internal.devices.inwallswitch.BoschInWallSwitchHandler;
33 import org.openhab.binding.boschshc.internal.devices.motiondetector.MotionDetectorHandler;
34 import org.openhab.binding.boschshc.internal.devices.shuttercontrol.ShutterControlHandler;
35 import org.openhab.binding.boschshc.internal.devices.thermostat.ThermostatHandler;
36 import org.openhab.binding.boschshc.internal.devices.twinguard.BoschTwinguardHandler;
37 import org.openhab.binding.boschshc.internal.devices.windowcontact.WindowContactHandler;
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.binding.BaseThingHandler;
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.service.component.annotations.Component;
46
47 /**
48  * The {@link BoschSHCHandlerFactory} is responsible for creating things and
49  * thing handlers.
50  *
51  * @author Stefan Kästle - Initial contribution
52  * @author Christian Oeing - Added Shutter Control and ThermostatHandler; refactored handler mapping
53  */
54 @NonNullByDefault
55 @Component(configurationPid = "binding.boschshc", service = ThingHandlerFactory.class)
56 public class BoschSHCHandlerFactory extends BaseThingHandlerFactory {
57
58     private static class ThingTypeHandlerMapping {
59         public ThingTypeUID thingTypeUID;
60         public Function<Thing, BaseThingHandler> handlerSupplier;
61
62         public ThingTypeHandlerMapping(ThingTypeUID thingTypeUID, Function<Thing, BaseThingHandler> handlerSupplier) {
63             this.thingTypeUID = thingTypeUID;
64             this.handlerSupplier = handlerSupplier;
65         }
66     }
67
68     private static final Collection<ThingTypeHandlerMapping> SUPPORTED_THING_TYPES = List.of(
69             new ThingTypeHandlerMapping(THING_TYPE_SHC, thing -> new BoschSHCBridgeHandler((Bridge) thing)),
70             new ThingTypeHandlerMapping(THING_TYPE_INWALL_SWITCH, BoschInWallSwitchHandler::new),
71             new ThingTypeHandlerMapping(THING_TYPE_TWINGUARD, BoschTwinguardHandler::new),
72             new ThingTypeHandlerMapping(THING_TYPE_WINDOW_CONTACT, WindowContactHandler::new),
73             new ThingTypeHandlerMapping(THING_TYPE_MOTION_DETECTOR, MotionDetectorHandler::new),
74             new ThingTypeHandlerMapping(THING_TYPE_SHUTTER_CONTROL, ShutterControlHandler::new),
75             new ThingTypeHandlerMapping(THING_TYPE_THERMOSTAT, ThermostatHandler::new),
76             new ThingTypeHandlerMapping(THING_TYPE_CLIMATE_CONTROL, ClimateControlHandler::new));
77
78     @Override
79     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
80         return SUPPORTED_THING_TYPES.stream().anyMatch(mapping -> mapping.thingTypeUID.equals(thingTypeUID));
81     }
82
83     @Override
84     protected @Nullable ThingHandler createHandler(Thing thing) {
85         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
86
87         // Search for mapping for thing type and return handler for it if found. Otherwise return null.
88         return SUPPORTED_THING_TYPES.stream().filter(mapping -> mapping.thingTypeUID.equals(thingTypeUID)).findFirst()
89                 .<@Nullable BaseThingHandler> map(mapping -> mapping.handlerSupplier.apply(thing)).orElse(null);
90     }
91 }