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