]> git.basschouten.com Git - openhab-addons.git/blob
f07593596412f614a417c02ba4a94ab8222fe6ea
[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.knx.internal.factory;
14
15 import static org.openhab.binding.knx.internal.KNXBindingConstants.*;
16
17 import java.util.Collection;
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.knx.internal.client.SerialTransportAdapter;
23 import org.openhab.binding.knx.internal.handler.DeviceThingHandler;
24 import org.openhab.binding.knx.internal.handler.IPBridgeThingHandler;
25 import org.openhab.binding.knx.internal.handler.SerialBridgeThingHandler;
26 import org.openhab.binding.knx.internal.i18n.KNXTranslationProvider;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.i18n.LocaleProvider;
29 import org.openhab.core.i18n.TranslationProvider;
30 import org.openhab.core.io.transport.serial.SerialPortManager;
31 import org.openhab.core.net.NetworkAddressService;
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.ThingUID;
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.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42
43 /**
44  * The {@link KNXHandlerFactory} is responsible for creating things and thing
45  * handlers.
46  *
47  * @author Simon Kaufmann - Initial contribution and API
48  */
49 @NonNullByDefault
50 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.knx")
51 public class KNXHandlerFactory extends BaseThingHandlerFactory {
52
53     public static final Collection<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_DEVICE,
54             THING_TYPE_IP_BRIDGE, THING_TYPE_SERIAL_BRIDGE);
55
56     @Nullable
57     private NetworkAddressService networkAddressService;
58     private final SerialPortManager serialPortManager;
59
60     @Activate
61     public KNXHandlerFactory(final @Reference NetworkAddressService networkAddressService,
62             final @Reference TranslationProvider translationProvider, final @Reference LocaleProvider localeProvider,
63             final @Reference SerialPortManager serialPortManager) {
64         KNXTranslationProvider.I18N.setProvider(localeProvider, translationProvider);
65         this.networkAddressService = networkAddressService;
66         this.serialPortManager = serialPortManager;
67         SerialTransportAdapter.setSerialPortManager(serialPortManager);
68     }
69
70     @Override
71     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
73     }
74
75     @Override
76     public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
77             @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
78         if (THING_TYPE_IP_BRIDGE.equals(thingTypeUID)) {
79             ThingUID ipBridgeUID = getIPBridgeThingUID(thingTypeUID, thingUID, configuration);
80             return super.createThing(thingTypeUID, configuration, ipBridgeUID, null);
81         }
82         if (THING_TYPE_SERIAL_BRIDGE.equals(thingTypeUID)) {
83             ThingUID serialBridgeUID = getSerialBridgeThingUID(thingTypeUID, thingUID, configuration);
84             return super.createThing(thingTypeUID, configuration, serialBridgeUID, null);
85         }
86         if (THING_TYPE_DEVICE.equals(thingTypeUID)) {
87             return super.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
88         }
89         return null;
90     }
91
92     @Override
93     protected @Nullable ThingHandler createHandler(Thing thing) {
94         if (thing.getThingTypeUID().equals(THING_TYPE_IP_BRIDGE)) {
95             return new IPBridgeThingHandler((Bridge) thing, networkAddressService);
96         } else if (thing.getThingTypeUID().equals(THING_TYPE_SERIAL_BRIDGE)) {
97             return new SerialBridgeThingHandler((Bridge) thing, serialPortManager);
98         } else if (thing.getThingTypeUID().equals(THING_TYPE_DEVICE)) {
99             return new DeviceThingHandler(thing);
100         }
101         return null;
102     }
103
104     private ThingUID getIPBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
105             Configuration configuration) {
106         if (thingUID != null) {
107             return thingUID;
108         }
109         String ipAddress = (String) configuration.get(IP_ADDRESS);
110         return new ThingUID(thingTypeUID, ipAddress);
111     }
112
113     private ThingUID getSerialBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
114             Configuration configuration) {
115         if (thingUID != null) {
116             return thingUID;
117         }
118         String serialPort = (String) configuration.get(SERIAL_PORT);
119         return new ThingUID(thingTypeUID, serialPort);
120     }
121 }