2 * Copyright (c) 2010-2023 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.lirc.internal;
15 import static org.openhab.binding.lirc.internal.LIRCBindingConstants.THING_TYPE_REMOTE;
17 import java.util.HashMap;
18 import java.util.Hashtable;
21 import org.openhab.binding.lirc.internal.discovery.LIRCRemoteDiscoveryService;
22 import org.openhab.binding.lirc.internal.handler.LIRCBridgeHandler;
23 import org.openhab.binding.lirc.internal.handler.LIRCRemoteHandler;
24 import org.openhab.core.config.discovery.DiscoveryService;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerFactory;
32 import org.osgi.framework.ServiceRegistration;
33 import org.osgi.service.component.annotations.Component;
36 * The {@link LIRCHandlerFactory} is responsible for creating things and thing
39 * @author Andrew Nagle - Initial contribution
41 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.lirc")
42 public class LIRCHandlerFactory extends BaseThingHandlerFactory {
44 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
47 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
48 return LIRCBindingConstants.SUPPORTED_THING_TYPES.contains(thingTypeUID);
52 protected ThingHandler createHandler(Thing thing) {
53 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
54 if (LIRCBindingConstants.SUPPORTED_BRIDGE_TYPES.contains(thingTypeUID)) {
55 LIRCBridgeHandler handler = new LIRCBridgeHandler((Bridge) thing);
56 registerDeviceDiscoveryService(handler);
58 } else if (thingTypeUID.equals(THING_TYPE_REMOTE)) {
59 return new LIRCRemoteHandler(thing);
65 protected synchronized void removeHandler(ThingHandler thingHandler) {
66 if (this.discoveryServiceRegs != null) {
67 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
68 if (serviceReg != null) {
69 serviceReg.unregister();
74 private synchronized void registerDeviceDiscoveryService(LIRCBridgeHandler handler) {
75 LIRCRemoteDiscoveryService discoveryService = new LIRCRemoteDiscoveryService(handler);
76 this.discoveryServiceRegs.put(handler.getThing().getUID(),
77 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));