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.vitotronic.internal;
15 import java.util.Hashtable;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.vitotronic.internal.discovery.VitotronicDiscoveryService;
20 import org.openhab.binding.vitotronic.internal.handler.VitotronicBridgeHandler;
21 import org.openhab.binding.vitotronic.internal.handler.VitotronicThingHandler;
22 import org.openhab.core.config.core.Configuration;
23 import org.openhab.core.config.discovery.DiscoveryService;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerFactory;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link VitotronicHandlerFactory} is responsible for creating things and thing
39 * @author Stefan Andres - Initial contribution
42 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.vitotronic")
43 public class VitotronicHandlerFactory extends BaseThingHandlerFactory {
45 private final Logger logger = LoggerFactory.getLogger(VitotronicHandlerFactory.class);
48 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
49 logger.trace("Ask Handler for Supported Thing {}",
50 VitotronicBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID));
51 return VitotronicBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
55 protected @Nullable ThingHandler createHandler(Thing thing) {
56 logger.trace("Install Handler for Thing {}", thing.getUID());
58 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
60 if (thingTypeUID.equals(VitotronicBindingConstants.THING_TYPE_UID_BRIDGE)) {
61 VitotronicBridgeHandler handler = new VitotronicBridgeHandler((Bridge) thing);
62 registerThingDiscovery(handler);
66 if (supportsThingType(thingTypeUID)) {
67 return new VitotronicThingHandler(thing);
73 private synchronized void registerThingDiscovery(VitotronicBridgeHandler bridgeHandler) {
74 VitotronicDiscoveryService discoveryService = new VitotronicDiscoveryService(bridgeHandler);
75 logger.trace("Try to register Discovery service on BundleID: {} Service: {}",
76 bundleContext.getBundle().getBundleId(), DiscoveryService.class.getName());
77 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>());
78 discoveryService.activate();
82 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
83 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
84 logger.trace("Create Thing for Type {}", thingUID);
86 String adapterID = (String) configuration.get(VitotronicBindingConstants.ADAPTER_ID);
88 if (VitotronicBindingConstants.THING_TYPE_UID_BRIDGE.equals(thingTypeUID)) {
89 logger.trace("Create Bridge: {}", adapterID);
90 return super.createThing(thingTypeUID, configuration, thingUID, null);
92 if (supportsThingType(thingTypeUID)) {
93 logger.trace("Create Thing: {}", adapterID);
94 return super.createThing(thingTypeUID, configuration, thingUID, bridgeUID);