]> git.basschouten.com Git - openhab-addons.git/blob
936da60cd95e1333ee5fe44148c23a1b00b87353
[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.elerotransmitterstick.internal;
14
15 import static org.openhab.binding.elerotransmitterstick.internal.EleroTransmitterStickBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Hashtable;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.elerotransmitterstick.internal.discovery.EleroChannelDiscoveryService;
28 import org.openhab.binding.elerotransmitterstick.internal.handler.EleroChannelHandler;
29 import org.openhab.binding.elerotransmitterstick.internal.handler.EleroTransmitterStickHandler;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.io.transport.serial.SerialPortManager;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
37 import org.openhab.core.thing.binding.ThingHandler;
38 import org.openhab.core.thing.binding.ThingHandlerFactory;
39 import org.osgi.framework.ServiceRegistration;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Reference;
43
44 /**
45  * The {@link EleroTransmitterStickHandlerFactory} is responsible for creating things and thing
46  * handlers.
47  *
48  * @author Volker Bier - Initial contribution
49  */
50 @NonNullByDefault
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.elerotransmitterstick")
52 public class EleroTransmitterStickHandlerFactory extends BaseThingHandlerFactory {
53
54     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
55             .unmodifiableSet(Stream.of(THING_TYPE_STICK, THING_TYPE_ELERO_CHANNEL).collect(Collectors.toSet()));
56
57     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
58
59     private final SerialPortManager serialPortManager;
60
61     @Activate
62     public EleroTransmitterStickHandlerFactory(final @Reference SerialPortManager serialPortManager) {
63         this.serialPortManager = serialPortManager;
64     }
65
66     @Override
67     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
68         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
69     }
70
71     @Override
72     protected @Nullable ThingHandler createHandler(Thing thing) {
73         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
74
75         if (thingTypeUID.equals(THING_TYPE_STICK)) {
76             EleroTransmitterStickHandler bridgeHandler = new EleroTransmitterStickHandler((Bridge) thing,
77                     serialPortManager);
78
79             EleroChannelDiscoveryService discoveryService = new EleroChannelDiscoveryService(bridgeHandler);
80             discoveryServiceRegs.put(bridgeHandler.getThing().getUID(), bundleContext
81                     .registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
82
83             return bridgeHandler;
84         } else if (thingTypeUID.equals(THING_TYPE_ELERO_CHANNEL)) {
85             return new EleroChannelHandler(thing);
86         }
87
88         return null;
89     }
90 }