]> git.basschouten.com Git - openhab-addons.git/blob
bc9a288ebdff367fd70bc416321df136b2f0d313
[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.Collection;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap;
21
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;
46
47 /**
48  * The {@link KNXHandlerFactory} is responsible for creating things and thing
49  * handlers.
50  *
51  * @author Simon Kaufmann - Initial contribution and API
52  */
53 @NonNullByDefault
54 @Component(service = { ThingHandlerFactory.class, KNXHandlerFactory.class }, configurationPid = "binding.knx")
55 public class KNXHandlerFactory extends BaseThingHandlerFactory {
56
57     public static final Collection<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_DEVICE,
58             THING_TYPE_IP_BRIDGE, THING_TYPE_SERIAL_BRIDGE);
59
60     @Nullable
61     private final NetworkAddressService networkAddressService;
62     private final SerialPortManager serialPortManager;
63     private final Map<ThingUID, KNXBridgeBaseThingHandler> bridges = new ConcurrentHashMap<>();
64
65     @Activate
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);
73         modified(config);
74     }
75
76     @Modified
77     protected void modified(Map<String, Object> config) {
78         disableUoM = (boolean) config.getOrDefault(CONFIG_DISABLE_UOM, false);
79     }
80
81     @Override
82     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
83         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
84     }
85
86     @Override
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);
92         }
93         if (THING_TYPE_SERIAL_BRIDGE.equals(thingTypeUID)) {
94             ThingUID serialBridgeUID = getSerialBridgeThingUID(thingTypeUID, thingUID, configuration);
95             return super.createThing(thingTypeUID, configuration, serialBridgeUID, null);
96         }
97         if (THING_TYPE_DEVICE.equals(thingTypeUID)) {
98             return super.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
99         }
100         return null;
101     }
102
103     @Override
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);
116         }
117         return null;
118     }
119
120     @Override
121     public void unregisterHandler(Thing thing) {
122         bridges.remove(thing.getUID());
123         super.unregisterHandler(thing);
124     }
125
126     private ThingUID getIPBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
127             Configuration configuration) {
128         if (thingUID != null) {
129             return thingUID;
130         }
131         String ipAddress = (String) configuration.get(IP_ADDRESS);
132         return new ThingUID(thingTypeUID, ipAddress);
133     }
134
135     private ThingUID getSerialBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
136             Configuration configuration) {
137         if (thingUID != null) {
138             return thingUID;
139         }
140         String serialPort = (String) configuration.get(SERIAL_PORT);
141         return new ThingUID(thingTypeUID, serialPort);
142     }
143
144     public Collection<KNXBridgeBaseThingHandler> getBridges() {
145         return Set.copyOf(bridges.values());
146     }
147 }