2 * Copyright (c) 2010-2021 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.Arrays;
18 import java.util.Collection;
20 import org.openhab.binding.knx.internal.handler.DeviceThingHandler;
21 import org.openhab.binding.knx.internal.handler.IPBridgeThingHandler;
22 import org.openhab.binding.knx.internal.handler.SerialBridgeThingHandler;
23 import org.openhab.core.config.core.Configuration;
24 import org.openhab.core.net.NetworkAddressService;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerFactory;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Reference;
36 * The {@link KNXHandlerFactory} is responsible for creating things and thing
39 * @author Simon Kaufmann - Initial contribution and API
41 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.knx")
42 public class KNXHandlerFactory extends BaseThingHandlerFactory {
44 public static final Collection<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Arrays.asList(THING_TYPE_DEVICE,
45 THING_TYPE_IP_BRIDGE, THING_TYPE_SERIAL_BRIDGE);
47 private NetworkAddressService networkAddressService;
50 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
51 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
55 public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
57 if (THING_TYPE_IP_BRIDGE.equals(thingTypeUID)) {
58 ThingUID IPBridgeUID = getIPBridgeThingUID(thingTypeUID, thingUID, configuration);
59 return super.createThing(thingTypeUID, configuration, IPBridgeUID, null);
61 if (THING_TYPE_SERIAL_BRIDGE.equals(thingTypeUID)) {
62 ThingUID serialBridgeUID = getSerialBridgeThingUID(thingTypeUID, thingUID, configuration);
63 return super.createThing(thingTypeUID, configuration, serialBridgeUID, null);
65 if (THING_TYPE_DEVICE.equals(thingTypeUID)) {
66 return super.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
68 throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the KNX binding.");
72 protected ThingHandler createHandler(Thing thing) {
73 if (thing.getThingTypeUID().equals(THING_TYPE_IP_BRIDGE)) {
74 return new IPBridgeThingHandler((Bridge) thing, networkAddressService);
75 } else if (thing.getThingTypeUID().equals(THING_TYPE_SERIAL_BRIDGE)) {
76 return new SerialBridgeThingHandler((Bridge) thing);
77 } else if (thing.getThingTypeUID().equals(THING_TYPE_DEVICE)) {
78 return new DeviceThingHandler(thing);
83 private ThingUID getIPBridgeThingUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration) {
84 if (thingUID != null) {
87 String ipAddress = (String) configuration.get(IP_ADDRESS);
88 return new ThingUID(thingTypeUID, ipAddress);
91 private ThingUID getSerialBridgeThingUID(ThingTypeUID thingTypeUID, ThingUID thingUID,
92 Configuration configuration) {
93 if (thingUID != null) {
96 String serialPort = (String) configuration.get(SERIAL_PORT);
97 return new ThingUID(thingTypeUID, serialPort);
101 protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
102 this.networkAddressService = networkAddressService;
105 protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
106 this.networkAddressService = null;