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