]> git.basschouten.com Git - openhab-addons.git/blob
be0874ac78a325fa573b6566c14ed3a37a93b648
[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.flicbutton.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.flicbutton.internal.discovery.FlicButtonDiscoveryService;
25 import org.openhab.binding.flicbutton.internal.discovery.FlicSimpleclientDiscoveryServiceImpl;
26 import org.openhab.binding.flicbutton.internal.handler.FlicButtonHandler;
27 import org.openhab.binding.flicbutton.internal.handler.FlicDaemonBridgeHandler;
28 import org.openhab.core.config.discovery.DiscoveryService;
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.Component;
38
39 /**
40  * The {@link FlicButtonHandlerFactory} is responsible for creating things and thing
41  * handlers.
42  *
43  * @author Patrick Fink - Initial contribution
44  */
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.flicbutton")
46 @NonNullByDefault
47 public class FlicButtonHandlerFactory extends BaseThingHandlerFactory {
48
49     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
50             .concat(FlicButtonBindingConstants.BRIDGE_THING_TYPES_UIDS.stream(),
51                     FlicButtonBindingConstants.SUPPORTED_THING_TYPES_UIDS.stream())
52             .collect(Collectors.toSet());
53     private final Map<ThingUID, @Nullable ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
54
55     @Override
56     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
57         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
58     }
59
60     @Override
61     @Nullable
62     protected ThingHandler createHandler(Thing thing) {
63         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
64
65         if (thingTypeUID.equals(FlicButtonBindingConstants.FLICBUTTON_THING_TYPE)) {
66             return new FlicButtonHandler(thing);
67         } else if (thingTypeUID.equals(FlicButtonBindingConstants.BRIDGE_THING_TYPE)) {
68             FlicButtonDiscoveryService discoveryService = new FlicSimpleclientDiscoveryServiceImpl(thing.getUID());
69             FlicDaemonBridgeHandler bridgeHandler = new FlicDaemonBridgeHandler((Bridge) thing, discoveryService);
70             registerDiscoveryService(discoveryService, thing.getUID());
71
72             return bridgeHandler;
73         }
74
75         return null;
76     }
77
78     @Override
79     protected synchronized void removeHandler(ThingHandler thingHandler) {
80         if (thingHandler instanceof FlicDaemonBridgeHandler) {
81             unregisterDiscoveryService(thingHandler.getThing().getUID());
82         }
83         super.removeHandler(thingHandler);
84     }
85
86     private synchronized void registerDiscoveryService(FlicButtonDiscoveryService discoveryService,
87             ThingUID bridgeUID) {
88         this.discoveryServiceRegs.put(bridgeUID, getBundleContext().registerService(DiscoveryService.class.getName(),
89                 discoveryService, new Hashtable<String, Object>()));
90     }
91
92     private synchronized void unregisterDiscoveryService(ThingUID bridgeUID) {
93         ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(bridgeUID);
94         if (serviceReg != null) {
95             FlicButtonDiscoveryService service = (FlicButtonDiscoveryService) getBundleContext()
96                     .getService(serviceReg.getReference());
97             if (service != null) {
98                 service.deactivate();
99             }
100             serviceReg.unregister();
101             discoveryServiceRegs.remove(bridgeUID);
102         }
103     }
104 }