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