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