]> git.basschouten.com Git - openhab-addons.git/blob
966d2cf86a4a52322ff50fa13a6f2d050375e3a9
[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.irtrans.internal;
14
15 import static org.openhab.binding.irtrans.internal.IRtransBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
22 import org.openhab.binding.irtrans.internal.handler.BlasterHandler;
23 import org.openhab.binding.irtrans.internal.handler.EthernetBridgeHandler;
24 import org.openhab.core.config.core.Configuration;
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
34 /**
35  * The {@link IRtransHandlerFactory} is responsible for creating things and
36  * thing handlers.
37  *
38  * @author Karel Goderis - Initial contribution
39  *
40  */
41 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.irtrans")
42 public class IRtransHandlerFactory extends BaseThingHandlerFactory {
43
44     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
45             .unmodifiableSet(Stream.of(THING_TYPE_BLASTER, THING_TYPE_ETHERNET_BRIDGE).collect(Collectors.toSet()));
46
47     @Override
48     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
49         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
50     }
51
52     @Override
53     public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
54             ThingUID bridgeUID) {
55         if (IRtransBindingConstants.THING_TYPE_ETHERNET_BRIDGE.equals(thingTypeUID)) {
56             ThingUID ethernetBridgeUID = getEthernetBridgeThingUID(thingTypeUID, thingUID, configuration);
57             return super.createThing(thingTypeUID, configuration, ethernetBridgeUID, null);
58         }
59         if (IRtransBindingConstants.THING_TYPE_BLASTER.equals(thingTypeUID)) {
60             ThingUID blasterUID = getBlasterUID(thingTypeUID, thingUID, configuration, bridgeUID);
61             return super.createThing(thingTypeUID, configuration, blasterUID, bridgeUID);
62         }
63         throw new IllegalArgumentException(
64                 "The thing type " + thingTypeUID + " is not supported by the IRtrans binding.");
65     }
66
67     @Override
68     protected ThingHandler createHandler(Thing thing) {
69         if (thing.getThingTypeUID().equals(IRtransBindingConstants.THING_TYPE_ETHERNET_BRIDGE)) {
70             return new EthernetBridgeHandler((Bridge) thing);
71         } else if (thing.getThingTypeUID().equals(IRtransBindingConstants.THING_TYPE_BLASTER)) {
72             return new BlasterHandler(thing);
73         } else {
74             return null;
75         }
76     }
77
78     private ThingUID getEthernetBridgeThingUID(ThingTypeUID thingTypeUID, ThingUID thingUID,
79             Configuration configuration) {
80         if (thingUID == null) {
81             String ipAddress = (String) configuration.get(EthernetBridgeHandler.IP_ADDRESS);
82             return new ThingUID(thingTypeUID, ipAddress);
83         }
84         return thingUID;
85     }
86
87     private ThingUID getBlasterUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration,
88             ThingUID bridgeUID) {
89         String ledId = (String) configuration.get(BlasterHandler.LED);
90
91         if (thingUID == null) {
92             return new ThingUID(thingTypeUID, "Led" + ledId, bridgeUID.getId());
93         }
94         return thingUID;
95     }
96 }