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