]> git.basschouten.com Git - openhab-addons.git/blob
93f34134b01eaa15fbbdaef5378954e156608b74
[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.enocean.internal;
14
15 import java.util.HashMap;
16 import java.util.Hashtable;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
22 import org.openhab.binding.enocean.internal.discovery.EnOceanDeviceDiscoveryService;
23 import org.openhab.binding.enocean.internal.handler.EnOceanBaseActuatorHandler;
24 import org.openhab.binding.enocean.internal.handler.EnOceanBaseSensorHandler;
25 import org.openhab.binding.enocean.internal.handler.EnOceanBridgeHandler;
26 import org.openhab.binding.enocean.internal.handler.EnOceanClassicDeviceHandler;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.io.transport.serial.SerialPortManager;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingManager;
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.openhab.core.thing.link.ItemChannelLinkRegistry;
38 import org.osgi.framework.ServiceRegistration;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
41
42 /**
43  * The {@link EnOceanHandlerFactory} is responsible for creating things and thing
44  * handlers.
45  *
46  * @author Daniel Weber - Initial contribution
47  */
48 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.enocean")
49 public class EnOceanHandlerFactory extends BaseThingHandlerFactory {
50
51     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
52             .concat(EnOceanBridgeHandler.SUPPORTED_THING_TYPES.stream(),
53                     EnOceanBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS.stream())
54             .collect(Collectors.toSet());
55
56     private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
57
58     @Reference
59     SerialPortManager serialPortManager;
60
61     @Reference
62     ItemChannelLinkRegistry itemChannelLinkRegistry;
63
64     @Reference
65     ThingManager thingManager;
66
67     @Override
68     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
69         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
70     }
71
72     @Override
73     protected ThingHandler createHandler(Thing thing) {
74         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
75
76         if (EnOceanBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
77             EnOceanBridgeHandler bridgeHandler = new EnOceanBridgeHandler((Bridge) thing, serialPortManager);
78             registerDeviceDiscoveryService(bridgeHandler);
79             return bridgeHandler;
80         } else if (EnOceanBaseActuatorHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
81             return new EnOceanBaseActuatorHandler(thing, itemChannelLinkRegistry);
82         } else if (EnOceanBaseSensorHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
83             return new EnOceanBaseSensorHandler(thing, itemChannelLinkRegistry);
84         } else if (EnOceanClassicDeviceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
85             return new EnOceanClassicDeviceHandler(thing, itemChannelLinkRegistry);
86         }
87
88         return null;
89     }
90
91     @Override
92     protected void removeHandler(ThingHandler thingHandler) {
93         if (this.discoveryServiceRegs != null) {
94             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(thingHandler.getThing().getUID());
95             if (serviceReg != null) {
96                 serviceReg.unregister();
97                 discoveryServiceRegs.remove(thingHandler.getThing().getUID());
98             }
99         }
100     }
101
102     private void registerDeviceDiscoveryService(EnOceanBridgeHandler handler) {
103         EnOceanDeviceDiscoveryService discoveryService = new EnOceanDeviceDiscoveryService(handler, thingManager);
104         discoveryService.activate();
105         this.discoveryServiceRegs.put(handler.getThing().getUID(),
106                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
107     }
108 }