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.russound.internal;
15 import static org.openhab.binding.russound.internal.rio.RioConstants.*;
17 import java.util.Collections;
18 import java.util.Hashtable;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
23 import org.openhab.binding.russound.internal.discovery.RioSystemDeviceDiscoveryService;
24 import org.openhab.binding.russound.internal.rio.controller.RioControllerHandler;
25 import org.openhab.binding.russound.internal.rio.source.RioSourceHandler;
26 import org.openhab.binding.russound.internal.rio.system.RioSystemHandler;
27 import org.openhab.binding.russound.internal.rio.zone.RioZoneHandler;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
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.service.component.annotations.Component;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link RussoundHandlerFactory} is responsible for creating bridge and thing
43 * @author Tim Roberts - Initial contribution
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.russound")
46 public class RussoundHandlerFactory extends BaseThingHandlerFactory {
47 private final Logger logger = LoggerFactory.getLogger(RussoundHandlerFactory.class);
49 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
50 .unmodifiableSet(Stream.of(BRIDGE_TYPE_RIO, BRIDGE_TYPE_CONTROLLER, THING_TYPE_SOURCE, THING_TYPE_ZONE)
51 .collect(Collectors.toSet()));
54 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
55 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
59 protected ThingHandler createHandler(Thing thing) {
60 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
62 if (thingTypeUID.equals(BRIDGE_TYPE_RIO)) {
63 final RioSystemHandler sysHandler = new RioSystemHandler((Bridge) thing);
64 registerThingDiscovery(sysHandler);
66 } else if (thingTypeUID.equals(BRIDGE_TYPE_CONTROLLER)) {
67 return new RioControllerHandler((Bridge) thing);
68 } else if (thingTypeUID.equals(THING_TYPE_SOURCE)) {
69 return new RioSourceHandler(thing);
70 } else if (thingTypeUID.equals(THING_TYPE_ZONE)) {
71 return new RioZoneHandler(thing);
78 * Registers a {@link RioSystemDeviceDiscoveryService} from the passed {@link RioSystemHandler} and activates it.
80 * @param bridgeHandler the {@link RioSystemHandler} for discovery services
82 private synchronized void registerThingDiscovery(RioSystemHandler bridgeHandler) {
83 RioSystemDeviceDiscoveryService discoveryService = new RioSystemDeviceDiscoveryService(bridgeHandler);
84 logger.trace("Try to register Discovery service on BundleID: {} Service: {}",
85 bundleContext.getBundle().getBundleId(), DiscoveryService.class.getName());
87 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>());
88 discoveryService.activate();