2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.flicbutton.internal;
15 import java.util.HashMap;
16 import java.util.Hashtable;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
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;
40 * The {@link FlicButtonHandlerFactory} is responsible for creating things and thing
43 * @author Patrick Fink - Initial contribution
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.flicbutton")
47 public class FlicButtonHandlerFactory extends BaseThingHandlerFactory {
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<>();
56 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
57 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
62 protected ThingHandler createHandler(Thing thing) {
63 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
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());
79 protected synchronized void removeHandler(ThingHandler thingHandler) {
80 if (thingHandler instanceof FlicDaemonBridgeHandler) {
81 unregisterDiscoveryService(thingHandler.getThing().getUID());
83 super.removeHandler(thingHandler);
86 private synchronized void registerDiscoveryService(FlicButtonDiscoveryService discoveryService,
88 this.discoveryServiceRegs.put(bridgeUID, getBundleContext().registerService(DiscoveryService.class.getName(),
89 discoveryService, new Hashtable<String, Object>()));
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) {
100 serviceReg.unregister();
101 discoveryServiceRegs.remove(bridgeUID);