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.zway.internal;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
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;
39 * The {@link ZWayHandlerFactory} is responsible for creating things and thing
42 * @author Patrick Hecker - Initial contribution
44 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.zway")
45 public class ZWayHandlerFactory extends BaseThingHandlerFactory {
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()));
51 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
54 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
55 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
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);
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);
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();
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<>()));