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.alarmdecoder.internal;
15 import static org.openhab.binding.alarmdecoder.internal.AlarmDecoderBindingConstants.*;
17 import java.util.Collections;
18 import java.util.HashMap;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.alarmdecoder.internal.handler.ADBridgeHandler;
27 import org.openhab.binding.alarmdecoder.internal.handler.IPBridgeHandler;
28 import org.openhab.binding.alarmdecoder.internal.handler.KeypadHandler;
29 import org.openhab.binding.alarmdecoder.internal.handler.LRRHandler;
30 import org.openhab.binding.alarmdecoder.internal.handler.RFZoneHandler;
31 import org.openhab.binding.alarmdecoder.internal.handler.SerialBridgeHandler;
32 import org.openhab.binding.alarmdecoder.internal.handler.VZoneHandler;
33 import org.openhab.binding.alarmdecoder.internal.handler.ZoneHandler;
34 import org.openhab.core.config.discovery.DiscoveryService;
35 import org.openhab.core.io.transport.serial.SerialPortManager;
36 import org.openhab.core.thing.Bridge;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.thing.ThingUID;
40 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
41 import org.openhab.core.thing.binding.ThingHandler;
42 import org.openhab.core.thing.binding.ThingHandlerFactory;
43 import org.osgi.framework.ServiceRegistration;
44 import org.osgi.service.component.annotations.Activate;
45 import org.osgi.service.component.annotations.Component;
46 import org.osgi.service.component.annotations.Reference;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * The {@link AlarmDecoderHandlerFactory} is responsible for creating things and thing
54 * @author Bob Adair - Initial contribution
57 @Component(configurationPid = "binding.alarmdecoder", service = ThingHandlerFactory.class)
58 public class AlarmDecoderHandlerFactory extends BaseThingHandlerFactory {
60 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
61 .unmodifiableSet(Stream.of(THING_TYPE_IPBRIDGE, THING_TYPE_SERIALBRIDGE, THING_TYPE_ZONE, THING_TYPE_RFZONE,
62 THING_TYPE_VZONE, THING_TYPE_KEYPAD, THING_TYPE_LRR).collect(Collectors.toSet()));
64 private final Logger logger = LoggerFactory.getLogger(AlarmDecoderHandlerFactory.class);
66 private final SerialPortManager serialPortManager;
69 public AlarmDecoderHandlerFactory(final @Reference SerialPortManager serialPortManager) {
70 // Obtain the serial port manager service using an OSGi reference
71 this.serialPortManager = serialPortManager;
75 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
76 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
79 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegMap = new HashMap<>();
80 // Marked as Nullable only to fix incorrect redundant null check complaints from null annotations
83 protected @Nullable ThingHandler createHandler(Thing thing) {
84 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
86 if (THING_TYPE_IPBRIDGE.equals(thingTypeUID)) {
87 IPBridgeHandler bridgeHandler = new IPBridgeHandler((Bridge) thing);
88 registerDiscoveryService(bridgeHandler);
90 } else if (THING_TYPE_SERIALBRIDGE.equals(thingTypeUID)) {
91 SerialBridgeHandler bridgeHandler = new SerialBridgeHandler((Bridge) thing, serialPortManager);
92 registerDiscoveryService(bridgeHandler);
94 } else if (THING_TYPE_ZONE.equals(thingTypeUID)) {
95 return new ZoneHandler(thing);
96 } else if (THING_TYPE_RFZONE.equals(thingTypeUID)) {
97 return new RFZoneHandler(thing);
98 } else if (THING_TYPE_VZONE.equals(thingTypeUID)) {
99 return new VZoneHandler(thing);
100 } else if (THING_TYPE_KEYPAD.equals(thingTypeUID)) {
101 return new KeypadHandler(thing);
102 } else if (THING_TYPE_LRR.equals(thingTypeUID)) {
103 return new LRRHandler(thing);
110 protected synchronized void removeHandler(ThingHandler thingHandler) {
111 if (thingHandler instanceof ADBridgeHandler) {
112 ServiceRegistration<?> serviceReg = discoveryServiceRegMap.remove(thingHandler.getThing().getUID());
113 if (serviceReg != null) {
114 logger.debug("Unregistering discovery service.");
115 serviceReg.unregister();
121 * Register a discovery service for a bridge handler.
123 * @param bridgeHandler bridge handler for which to register the discovery service
125 private synchronized void registerDiscoveryService(ADBridgeHandler bridgeHandler) {
126 logger.debug("Registering discovery service.");
127 AlarmDecoderDiscoveryService discoveryService = new AlarmDecoderDiscoveryService(bridgeHandler);
128 bridgeHandler.setDiscoveryService(discoveryService);
129 discoveryServiceRegMap.put(bridgeHandler.getThing().getUID(),
130 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, null));