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