]> git.basschouten.com Git - openhab-addons.git/blob
ebd63177d47dd293d6432bede570c78653a55ca7
[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.rfxcom.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.rfxcom.internal.discovery.RFXComDeviceDiscoveryService;
25 import org.openhab.binding.rfxcom.internal.handler.RFXComBridgeHandler;
26 import org.openhab.binding.rfxcom.internal.handler.RFXComHandler;
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.osgi.framework.ServiceRegistration;
37 import org.osgi.service.component.annotations.Activate;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Reference;
40
41 /**
42  * The {@link RFXComHandlerFactory} is responsible for creating things and thing
43  * handlers.
44  *
45  * @author Pauli Anttila - Initial contribution
46  */
47 @NonNullByDefault
48 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.rfxcom")
49 public class RFXComHandlerFactory extends BaseThingHandlerFactory {
50
51     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Stream
52             .concat(RFXComBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS.stream(),
53                     RFXComBindingConstants.SUPPORTED_BRIDGE_THING_TYPES_UIDS.stream())
54             .collect(Collectors.toSet());
55
56     /**
57      * Service registration map
58      */
59     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
60
61     private final SerialPortManager serialPortManager;
62
63     @Activate
64     public RFXComHandlerFactory(final @Reference SerialPortManager serialPortManager) {
65         this.serialPortManager = serialPortManager;
66     }
67
68     @Override
69     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
70         return SUPPORTED_THING_TYPES.contains(thingTypeUID);
71     }
72
73     @Override
74     protected @Nullable ThingHandler createHandler(Thing thing) {
75         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
76
77         if (RFXComBindingConstants.SUPPORTED_BRIDGE_THING_TYPES_UIDS.contains(thingTypeUID)) {
78             RFXComBridgeHandler handler = new RFXComBridgeHandler((Bridge) thing, serialPortManager);
79             registerDeviceDiscoveryService(handler);
80             return handler;
81         } else if (supportsThingType(thingTypeUID)) {
82             return new RFXComHandler(thing);
83         }
84
85         return null;
86     }
87
88     @Override
89     protected void removeHandler(ThingHandler thingHandler) {
90         if (thingHandler instanceof RFXComBridgeHandler) {
91             unregisterDeviceDiscoveryService(thingHandler.getThing());
92         }
93     }
94
95     private synchronized void registerDeviceDiscoveryService(RFXComBridgeHandler handler) {
96         RFXComDeviceDiscoveryService discoveryService = new RFXComDeviceDiscoveryService(handler);
97         discoveryService.activate();
98         this.discoveryServiceRegs.put(handler.getThing().getUID(),
99                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
100     }
101
102     private synchronized void unregisterDeviceDiscoveryService(Thing thing) {
103         ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thing.getUID());
104         if (serviceReg != null) {
105             RFXComDeviceDiscoveryService service = (RFXComDeviceDiscoveryService) bundleContext
106                     .getService(serviceReg.getReference());
107             serviceReg.unregister();
108             if (service != null) {
109                 service.deactivate();
110             }
111         }
112     }
113 }