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