]> git.basschouten.com Git - openhab-addons.git/blob
49bc75e557e8438723736fa32cc7d07883da82bf
[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.alarmdecoder.internal;
14
15 import static org.openhab.binding.alarmdecoder.internal.AlarmDecoderBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
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;
49
50 /**
51  * The {@link AlarmDecoderHandlerFactory} is responsible for creating things and thing
52  * handlers.
53  *
54  * @author Bob Adair - Initial contribution
55  */
56 @NonNullByDefault
57 @Component(configurationPid = "binding.alarmdecoder", service = ThingHandlerFactory.class)
58 public class AlarmDecoderHandlerFactory extends BaseThingHandlerFactory {
59
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()));
63
64     private final Logger logger = LoggerFactory.getLogger(AlarmDecoderHandlerFactory.class);
65
66     private final SerialPortManager serialPortManager;
67
68     @Activate
69     public AlarmDecoderHandlerFactory(final @Reference SerialPortManager serialPortManager) {
70         // Obtain the serial port manager service using an OSGi reference
71         this.serialPortManager = serialPortManager;
72     }
73
74     @Override
75     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
76         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
77     }
78
79     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegMap = new HashMap<>();
80     // Marked as Nullable only to fix incorrect redundant null check complaints from null annotations
81
82     @Override
83     protected @Nullable ThingHandler createHandler(Thing thing) {
84         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
85
86         if (THING_TYPE_IPBRIDGE.equals(thingTypeUID)) {
87             IPBridgeHandler bridgeHandler = new IPBridgeHandler((Bridge) thing);
88             registerDiscoveryService(bridgeHandler);
89             return bridgeHandler;
90         } else if (THING_TYPE_SERIALBRIDGE.equals(thingTypeUID)) {
91             SerialBridgeHandler bridgeHandler = new SerialBridgeHandler((Bridge) thing, serialPortManager);
92             registerDiscoveryService(bridgeHandler);
93             return 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);
104         }
105
106         return null;
107     }
108
109     @Override
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();
116             }
117         }
118     }
119
120     /**
121      * Register a discovery service for a bridge handler.
122      *
123      * @param bridgeHandler bridge handler for which to register the discovery service
124      */
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));
131     }
132 }