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