]> git.basschouten.com Git - openhab-addons.git/blob
ad08f67ee16277a9cffde543d696df1d913323be
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.nikobus.internal;
14
15 import static org.openhab.binding.nikobus.internal.NikobusBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.nikobus.internal.handler.NikobusDimmerModuleHandler;
25 import org.openhab.binding.nikobus.internal.handler.NikobusPcLinkHandler;
26 import org.openhab.binding.nikobus.internal.handler.NikobusPushButtonHandler;
27 import org.openhab.binding.nikobus.internal.handler.NikobusRollershutterModuleHandler;
28 import org.openhab.binding.nikobus.internal.handler.NikobusSwitchModuleHandler;
29 import org.openhab.core.io.transport.serial.SerialPortManager;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
38
39 /**
40  * The {@link NikobusHandlerFactory} is responsible for creating things and thing
41  * handlers.
42  *
43  * @author Boris Krivonog - Initial contribution
44  */
45 @NonNullByDefault
46 @Component(configurationPid = "binding.nikobus", service = ThingHandlerFactory.class)
47 public class NikobusHandlerFactory extends BaseThingHandlerFactory {
48
49     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
50             .unmodifiableSet(Stream.of(BRIDGE_TYPE_PCLINK, THING_TYPE_PUSH_BUTTON, THING_TYPE_SWITCH_MODULE,
51                     THING_TYPE_DIMMER_MODULE, THING_TYPE_ROLLERSHUTTER_MODULE).collect(Collectors.toSet()));
52
53     private @NonNullByDefault({}) SerialPortManager serialPortManager;
54
55     @Override
56     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
57         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
58     }
59
60     @Override
61     protected @Nullable ThingHandler createHandler(Thing thing) {
62         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
63
64         if (BRIDGE_TYPE_PCLINK.equals(thingTypeUID)) {
65             return new NikobusPcLinkHandler((Bridge) thing, serialPortManager);
66         }
67
68         if (THING_TYPE_PUSH_BUTTON.equals(thingTypeUID)) {
69             return new NikobusPushButtonHandler(thing);
70         }
71
72         if (THING_TYPE_SWITCH_MODULE.equals(thingTypeUID)) {
73             return new NikobusSwitchModuleHandler(thing);
74         }
75
76         if (THING_TYPE_DIMMER_MODULE.equals(thingTypeUID)) {
77             return new NikobusDimmerModuleHandler(thing);
78         }
79
80         if (THING_TYPE_ROLLERSHUTTER_MODULE.equals(thingTypeUID)) {
81             return new NikobusRollershutterModuleHandler(thing);
82         }
83
84         return null;
85     }
86
87     @Reference
88     protected void setSerialPortManager(final SerialPortManager serialPortManager) {
89         this.serialPortManager = serialPortManager;
90     }
91
92     protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
93         this.serialPortManager = null;
94     }
95 }