]> git.basschouten.com Git - openhab-addons.git/blob
93bc189ce3746d73954b5d750728bd3e37abcd45
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.salus.internal;
14
15 import static org.openhab.binding.salus.internal.SalusBindingConstants.SALUS_DEVICE_TYPE;
16 import static org.openhab.binding.salus.internal.SalusBindingConstants.SALUS_IT600_DEVICE_TYPE;
17 import static org.openhab.binding.salus.internal.SalusBindingConstants.SALUS_SERVER_TYPE;
18 import static org.openhab.binding.salus.internal.SalusBindingConstants.SUPPORTED_THING_TYPES_UIDS;
19
20 import java.util.Hashtable;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.salus.internal.discovery.CloudDiscovery;
25 import org.openhab.binding.salus.internal.handler.CloudBridgeHandler;
26 import org.openhab.binding.salus.internal.handler.DeviceHandler;
27 import org.openhab.binding.salus.internal.handler.It600Handler;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.io.net.http.HttpClientFactory;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * The {@link SalusHandlerFactory} is responsible for creating things and thing
44  * handlers.
45  *
46  * @author Martin Grzeslowski - Initial contribution
47  */
48 @NonNullByDefault
49 @Component(configurationPid = "binding.salus", service = ThingHandlerFactory.class)
50 public class SalusHandlerFactory extends BaseThingHandlerFactory {
51
52     private final Logger logger = LoggerFactory.getLogger(SalusHandlerFactory.class);
53     protected final @NonNullByDefault({}) HttpClientFactory httpClientFactory;
54
55     @Activate
56     public SalusHandlerFactory(@Reference HttpClientFactory httpClientFactory) {
57         this.httpClientFactory = httpClientFactory;
58     }
59
60     @Override
61     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
63     }
64
65     @Override
66     protected @Nullable ThingHandler createHandler(Thing thing) {
67         var thingTypeUID = thing.getThingTypeUID();
68
69         if (SALUS_DEVICE_TYPE.equals(thingTypeUID)) {
70             return newSalusDevice(thing);
71         }
72         if (SALUS_IT600_DEVICE_TYPE.equals(thingTypeUID)) {
73             return newIt600(thing);
74         }
75         if (SALUS_SERVER_TYPE.equals(thingTypeUID)) {
76             return newSalusCloudBridge(thing);
77         }
78
79         return null;
80     }
81
82     private ThingHandler newSalusDevice(Thing thing) {
83         logger.debug("New Salus Device {}", thing.getUID().getId());
84         return new DeviceHandler(thing);
85     }
86
87     private ThingHandler newIt600(Thing thing) {
88         logger.debug("Registering IT600");
89         return new It600Handler(thing);
90     }
91
92     private ThingHandler newSalusCloudBridge(Thing thing) {
93         var handler = new CloudBridgeHandler((Bridge) thing, httpClientFactory);
94         var cloudDiscovery = new CloudDiscovery(handler, handler, handler.getThing().getUID());
95         registerThingDiscovery(cloudDiscovery);
96         return handler;
97     }
98
99     private synchronized void registerThingDiscovery(DiscoveryService discoveryService) {
100         bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>());
101     }
102 }