]> git.basschouten.com Git - openhab-addons.git/blob
a7a4f22884bd0a130674ae31763e3e658714f347
[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.vitotronic.internal;
14
15 import java.util.Hashtable;
16
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;
34
35 /**
36  * The {@link VitotronicHandlerFactory} is responsible for creating things and thing
37  * handlers.
38  *
39  * @author Stefan Andres - Initial contribution
40  */
41 @NonNullByDefault
42 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.vitotronic")
43 public class VitotronicHandlerFactory extends BaseThingHandlerFactory {
44
45     private final Logger logger = LoggerFactory.getLogger(VitotronicHandlerFactory.class);
46
47     @Override
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);
52     }
53
54     @Override
55     protected @Nullable ThingHandler createHandler(Thing thing) {
56         logger.trace("Install Handler for Thing {}", thing.getUID());
57
58         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
59
60         if (thingTypeUID.equals(VitotronicBindingConstants.THING_TYPE_UID_BRIDGE)) {
61             VitotronicBridgeHandler handler = new VitotronicBridgeHandler((Bridge) thing);
62             registerThingDiscovery(handler);
63             return handler;
64         }
65
66         if (supportsThingType(thingTypeUID)) {
67             return new VitotronicThingHandler(thing);
68         }
69
70         return null;
71     }
72
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();
79     }
80
81     @Override
82     public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
83             @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
84         logger.trace("Create Thing for Type {}", thingUID);
85
86         String adapterID = (String) configuration.get(VitotronicBindingConstants.ADAPTER_ID);
87
88         if (VitotronicBindingConstants.THING_TYPE_UID_BRIDGE.equals(thingTypeUID)) {
89             logger.trace("Create Bridge: {}", adapterID);
90             return super.createThing(thingTypeUID, configuration, thingUID, null);
91         } else {
92             if (supportsThingType(thingTypeUID)) {
93                 logger.trace("Create Thing: {}", adapterID);
94                 return super.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
95             }
96         }
97
98         return null;
99     }
100 }