]> git.basschouten.com Git - openhab-addons.git/blob
3539144e945f14e88ddbed9acbffb62fc2925527
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.Arrays;
18 import java.util.Collection;
19
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;
34
35 /**
36  * The {@link KNXHandlerFactory} is responsible for creating things and thing
37  * handlers.
38  *
39  * @author Simon Kaufmann - Initial contribution and API
40  */
41 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.knx")
42 public class KNXHandlerFactory extends BaseThingHandlerFactory {
43
44     public static final Collection<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Arrays.asList(THING_TYPE_DEVICE,
45             THING_TYPE_IP_BRIDGE, THING_TYPE_SERIAL_BRIDGE);
46
47     private NetworkAddressService networkAddressService;
48
49     @Override
50     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
51         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
52     }
53
54     @Override
55     public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
56             ThingUID bridgeUID) {
57         if (THING_TYPE_IP_BRIDGE.equals(thingTypeUID)) {
58             ThingUID ipBridgeUID = getIPBridgeThingUID(thingTypeUID, thingUID, configuration);
59             return super.createThing(thingTypeUID, configuration, ipBridgeUID, null);
60         }
61         if (THING_TYPE_SERIAL_BRIDGE.equals(thingTypeUID)) {
62             ThingUID serialBridgeUID = getSerialBridgeThingUID(thingTypeUID, thingUID, configuration);
63             return super.createThing(thingTypeUID, configuration, serialBridgeUID, null);
64         }
65         if (THING_TYPE_DEVICE.equals(thingTypeUID)) {
66             return super.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
67         }
68         throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the KNX binding.");
69     }
70
71     @Override
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);
79         }
80         return null;
81     }
82
83     private ThingUID getIPBridgeThingUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration) {
84         if (thingUID != null) {
85             return thingUID;
86         }
87         String ipAddress = (String) configuration.get(IP_ADDRESS);
88         return new ThingUID(thingTypeUID, ipAddress);
89     }
90
91     private ThingUID getSerialBridgeThingUID(ThingTypeUID thingTypeUID, ThingUID thingUID,
92             Configuration configuration) {
93         if (thingUID != null) {
94             return thingUID;
95         }
96         String serialPort = (String) configuration.get(SERIAL_PORT);
97         return new ThingUID(thingTypeUID, serialPort);
98     }
99
100     @Reference
101     protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
102         this.networkAddressService = networkAddressService;
103     }
104
105     protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
106         this.networkAddressService = null;
107     }
108 }