2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.knx.internal.factory;
15 import static org.openhab.binding.knx.internal.KNXBindingConstants.*;
17 import java.util.Collection;
20 import java.util.concurrent.ConcurrentHashMap;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.knx.internal.client.SerialTransportAdapter;
25 import org.openhab.binding.knx.internal.handler.DeviceThingHandler;
26 import org.openhab.binding.knx.internal.handler.IPBridgeThingHandler;
27 import org.openhab.binding.knx.internal.handler.KNXBridgeBaseThingHandler;
28 import org.openhab.binding.knx.internal.handler.SerialBridgeThingHandler;
29 import org.openhab.binding.knx.internal.i18n.KNXTranslationProvider;
30 import org.openhab.core.config.core.Configuration;
31 import org.openhab.core.i18n.LocaleProvider;
32 import org.openhab.core.i18n.TranslationProvider;
33 import org.openhab.core.io.transport.serial.SerialPortManager;
34 import org.openhab.core.net.NetworkAddressService;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
40 import org.openhab.core.thing.binding.ThingHandler;
41 import org.openhab.core.thing.binding.ThingHandlerFactory;
42 import org.osgi.service.component.annotations.Activate;
43 import org.osgi.service.component.annotations.Component;
44 import org.osgi.service.component.annotations.Modified;
45 import org.osgi.service.component.annotations.Reference;
48 * The {@link KNXHandlerFactory} is responsible for creating things and thing
51 * @author Simon Kaufmann - Initial contribution and API
54 @Component(service = { ThingHandlerFactory.class, KNXHandlerFactory.class }, configurationPid = "binding.knx")
55 public class KNXHandlerFactory extends BaseThingHandlerFactory {
57 public static final Collection<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_DEVICE,
58 THING_TYPE_IP_BRIDGE, THING_TYPE_SERIAL_BRIDGE);
61 private final NetworkAddressService networkAddressService;
62 private final SerialPortManager serialPortManager;
63 private final Map<ThingUID, KNXBridgeBaseThingHandler> bridges = new ConcurrentHashMap<>();
66 public KNXHandlerFactory(final @Reference NetworkAddressService networkAddressService, Map<String, Object> config,
67 final @Reference TranslationProvider translationProvider, final @Reference LocaleProvider localeProvider,
68 final @Reference SerialPortManager serialPortManager) {
69 KNXTranslationProvider.I18N.setProvider(localeProvider, translationProvider);
70 this.networkAddressService = networkAddressService;
71 this.serialPortManager = serialPortManager;
72 SerialTransportAdapter.setSerialPortManager(serialPortManager);
77 protected void modified(Map<String, Object> config) {
78 disableUoM = (boolean) config.getOrDefault(CONFIG_DISABLE_UOM, false);
82 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
83 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
87 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
88 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
89 if (THING_TYPE_IP_BRIDGE.equals(thingTypeUID)) {
90 ThingUID ipBridgeUID = getIPBridgeThingUID(thingTypeUID, thingUID, configuration);
91 return super.createThing(thingTypeUID, configuration, ipBridgeUID, null);
93 if (THING_TYPE_SERIAL_BRIDGE.equals(thingTypeUID)) {
94 ThingUID serialBridgeUID = getSerialBridgeThingUID(thingTypeUID, thingUID, configuration);
95 return super.createThing(thingTypeUID, configuration, serialBridgeUID, null);
97 if (THING_TYPE_DEVICE.equals(thingTypeUID)) {
98 return super.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
104 protected @Nullable ThingHandler createHandler(Thing thing) {
105 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
106 if (thingTypeUID.equals(THING_TYPE_IP_BRIDGE)) {
107 KNXBridgeBaseThingHandler bridgeHandler = new IPBridgeThingHandler((Bridge) thing, networkAddressService);
108 bridges.put(thing.getUID(), bridgeHandler);
109 return bridgeHandler;
110 } else if (thingTypeUID.equals(THING_TYPE_SERIAL_BRIDGE)) {
111 KNXBridgeBaseThingHandler bridgeHandler = new SerialBridgeThingHandler((Bridge) thing, serialPortManager);
112 bridges.put(thing.getUID(), bridgeHandler);
113 return bridgeHandler;
114 } else if (thingTypeUID.equals(THING_TYPE_DEVICE)) {
115 return new DeviceThingHandler(thing);
121 public void unregisterHandler(Thing thing) {
122 bridges.remove(thing.getUID());
123 super.unregisterHandler(thing);
126 private ThingUID getIPBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
127 Configuration configuration) {
128 if (thingUID != null) {
131 String ipAddress = (String) configuration.get(IP_ADDRESS);
132 return new ThingUID(thingTypeUID, ipAddress);
135 private ThingUID getSerialBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
136 Configuration configuration) {
137 if (thingUID != null) {
140 String serialPort = (String) configuration.get(SERIAL_PORT);
141 return new ThingUID(thingTypeUID, serialPort);
144 public Collection<KNXBridgeBaseThingHandler> getBridges() {
145 return Set.copyOf(bridges.values());