]> git.basschouten.com Git - openhab-addons.git/blob
9b11c790d12df9cd821d9027dd8172511ae56e22
[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.paradoxalarm.internal.handlers;
14
15 import static org.openhab.binding.paradoxalarm.internal.handlers.ParadoxAlarmBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.paradoxalarm.internal.discovery.ParadoxDiscoveryService;
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 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * The {@link ParadoxAlarmHandlerFactory} is responsible for creating things and thing
39  * handlers.
40  *
41  * @author Konstantin Polihronov - Initial contribution
42  */
43 @NonNullByDefault
44 @Component(configurationPid = "binding.paradoxalarm", service = ThingHandlerFactory.class)
45 public class ParadoxAlarmHandlerFactory extends BaseThingHandlerFactory {
46
47     private final Logger logger = LoggerFactory.getLogger(ParadoxAlarmHandlerFactory.class);
48
49     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
50
51     @Override
52     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
53         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
54     }
55
56     @Override
57     protected @Nullable ThingHandler createHandler(Thing thing) {
58         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
59         ThingUID thingUID = thing.getUID();
60         if (COMMUNICATOR_THING_TYPE_UID.equals(thingTypeUID)) {
61             logger.debug("createHandler(): ThingHandler created for {}", thingUID);
62
63             ParadoxIP150BridgeHandler paradoxIP150BridgeHandler = new ParadoxIP150BridgeHandler((Bridge) thing);
64             registerDiscoveryService(paradoxIP150BridgeHandler);
65
66             return paradoxIP150BridgeHandler;
67         } else if (PANEL_THING_TYPE_UID.equals(thingTypeUID)) {
68             logger.debug("createHandler(): ThingHandler created for {}", thingUID);
69             return new ParadoxPanelHandler(thing);
70         } else if (PARTITION_THING_TYPE_UID.equals(thingTypeUID)) {
71             logger.debug("createHandler(): ThingHandler created for {}", thingUID);
72             return new ParadoxPartitionHandler(thing);
73         } else if (ZONE_THING_TYPE_UID.equals(thingTypeUID)) {
74             logger.debug("createHandler(): ThingHandler created for {}", thingUID);
75             return new ParadoxZoneHandler(thing);
76         } else {
77             logger.warn("Handler implementation not found for Thing: {}", thingUID);
78         }
79         return null;
80     }
81
82     private void registerDiscoveryService(ParadoxIP150BridgeHandler paradoxIP150BridgeHandler) {
83         ParadoxDiscoveryService discoveryService = new ParadoxDiscoveryService(paradoxIP150BridgeHandler);
84         ServiceRegistration<?> serviceRegistration = bundleContext.registerService(DiscoveryService.class.getName(),
85                 discoveryService, new Hashtable<>());
86         this.discoveryServiceRegs.put(paradoxIP150BridgeHandler.getThing().getUID(), serviceRegistration);
87     }
88
89     @Override
90     protected void removeHandler(ThingHandler thingHandler) {
91         if (thingHandler instanceof ParadoxIP150BridgeHandler) {
92             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
93             if (serviceReg != null) {
94                 serviceReg.unregister();
95             }
96         }
97     }
98 }