]> git.basschouten.com Git - openhab-addons.git/blob
f158d7d23487bd78c052579e45339bebad6d6bc4
[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.zway.internal;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.zway.internal.discovery.ZWayDeviceDiscoveryService;
26 import org.openhab.binding.zway.internal.handler.ZWayBridgeHandler;
27 import org.openhab.binding.zway.internal.handler.ZWayZAutomationDeviceHandler;
28 import org.openhab.binding.zway.internal.handler.ZWayZWaveDeviceHandler;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.osgi.framework.ServiceRegistration;
38 import org.osgi.service.component.annotations.Component;
39
40 /**
41  * The {@link ZWayHandlerFactory} is responsible for creating things and thing
42  * handlers.
43  *
44  * @author Patrick Hecker - Initial contribution
45  */
46 @NonNullByDefault
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.zway")
48 public class ZWayHandlerFactory extends BaseThingHandlerFactory {
49
50     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.unmodifiableSet(
51             Stream.of(ZWayBridgeHandler.SUPPORTED_THING_TYPE, ZWayZAutomationDeviceHandler.SUPPORTED_THING_TYPE,
52                     ZWayZWaveDeviceHandler.SUPPORTED_THING_TYPE).collect(Collectors.toSet()));
53
54     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
55
56     @Override
57     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
58         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
59     }
60
61     @Override
62     protected @Nullable ThingHandler createHandler(Thing thing) {
63         if (ZWayBridgeHandler.SUPPORTED_THING_TYPE.equals(thing.getThingTypeUID())) {
64             ZWayBridgeHandler handler = new ZWayBridgeHandler((Bridge) thing);
65             registerDeviceDiscoveryService(handler);
66
67             return handler;
68         } else if (ZWayZAutomationDeviceHandler.SUPPORTED_THING_TYPE.equals(thing.getThingTypeUID())) {
69             return new ZWayZAutomationDeviceHandler(thing);
70         } else if (ZWayZWaveDeviceHandler.SUPPORTED_THING_TYPE.equals(thing.getThingTypeUID())) {
71             return new ZWayZWaveDeviceHandler(thing);
72         } else {
73             return null;
74         }
75     }
76
77     @Override
78     protected synchronized void removeHandler(ThingHandler thingHandler) {
79         if (thingHandler instanceof ZWayBridgeHandler) {
80             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
81             if (serviceReg != null) {
82                 serviceReg.unregister();
83             }
84         }
85     }
86
87     private synchronized void registerDeviceDiscoveryService(ZWayBridgeHandler handler) {
88         ZWayDeviceDiscoveryService discoveryService = new ZWayDeviceDiscoveryService(handler);
89         this.discoveryServiceRegs.put(handler.getThing().getUID(),
90                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
91     }
92 }