]> git.basschouten.com Git - openhab-addons.git/blob
a350bb37ae9025bbc9920331ddaecc838bc4bee2
[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.lirc.internal;
14
15 import static org.openhab.binding.lirc.internal.LIRCBindingConstants.THING_TYPE_REMOTE;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20
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;
34
35 /**
36  * The {@link LIRCHandlerFactory} is responsible for creating things and thing
37  * handlers.
38  *
39  * @author Andrew Nagle - Initial contribution
40  */
41 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.lirc")
42 public class LIRCHandlerFactory extends BaseThingHandlerFactory {
43
44     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
45
46     @Override
47     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
48         return LIRCBindingConstants.SUPPORTED_THING_TYPES.contains(thingTypeUID);
49     }
50
51     @Override
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);
57             return handler;
58         } else if (thingTypeUID.equals(THING_TYPE_REMOTE)) {
59             return new LIRCRemoteHandler(thing);
60         }
61         return null;
62     }
63
64     @Override
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();
70             }
71         }
72     }
73
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<>()));
78     }
79 }