]> git.basschouten.com Git - openhab-addons.git/blob
5277461e79ec9a4f3fe2d96c95031827e57d9160
[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.russound.internal;
14
15 import static org.openhab.binding.russound.internal.rio.RioConstants.*;
16
17 import java.util.Collections;
18 import java.util.Hashtable;
19 import java.util.Set;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
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;
38
39 /**
40  * The {@link RussoundHandlerFactory} is responsible for creating bridge and thing
41  * handlers.
42  *
43  * @author Tim Roberts - Initial contribution
44  */
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.russound")
46 public class RussoundHandlerFactory extends BaseThingHandlerFactory {
47     private final Logger logger = LoggerFactory.getLogger(RussoundHandlerFactory.class);
48
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()));
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         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
61
62         if (thingTypeUID.equals(BRIDGE_TYPE_RIO)) {
63             final RioSystemHandler sysHandler = new RioSystemHandler((Bridge) thing);
64             registerThingDiscovery(sysHandler);
65             return 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);
72         }
73
74         return null;
75     }
76
77     /**
78      * Registers a {@link RioSystemDeviceDiscoveryService} from the passed {@link RioSystemHandler} and activates it.
79      *
80      * @param bridgeHandler the {@link RioSystemHandler} for discovery services
81      */
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());
86
87         bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>());
88         discoveryService.activate();
89     }
90 }