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.paradoxalarm.internal.handlers;
15 import static org.openhab.binding.paradoxalarm.internal.handlers.ParadoxAlarmBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
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;
38 * The {@link ParadoxAlarmHandlerFactory} is responsible for creating things and thing
41 * @author Konstantin Polihronov - Initial contribution
44 @Component(configurationPid = "binding.paradoxalarm", service = ThingHandlerFactory.class)
45 public class ParadoxAlarmHandlerFactory extends BaseThingHandlerFactory {
47 private final Logger logger = LoggerFactory.getLogger(ParadoxAlarmHandlerFactory.class);
49 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
52 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
53 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
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);
63 ParadoxIP150BridgeHandler paradoxIP150BridgeHandler = new ParadoxIP150BridgeHandler((Bridge) thing);
64 registerDiscoveryService(paradoxIP150BridgeHandler);
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);
77 logger.warn("Handler implementation not found for Thing: {}", thingUID);
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);
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();