]> git.basschouten.com Git - openhab-addons.git/blob
47359a89a9d7d9c806e7d1da0fba9f55ae6f113d
[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.hdpowerview.internal;
14
15 import java.util.Hashtable;
16
17 import javax.ws.rs.client.ClientBuilder;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.openhab.binding.hdpowerview.internal.discovery.HDPowerViewDeviceDiscoveryService;
23 import org.openhab.binding.hdpowerview.internal.discovery.ShadeDiscoveryService;
24 import org.openhab.binding.hdpowerview.internal.handler.GatewayBridgeHandler;
25 import org.openhab.binding.hdpowerview.internal.handler.HDPowerViewHubHandler;
26 import org.openhab.binding.hdpowerview.internal.handler.HDPowerViewRepeaterHandler;
27 import org.openhab.binding.hdpowerview.internal.handler.HDPowerViewShadeHandler;
28 import org.openhab.binding.hdpowerview.internal.handler.ShadeThingHandler;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.i18n.LocaleProvider;
31 import org.openhab.core.i18n.TranslationProvider;
32 import org.openhab.core.io.net.http.HttpClientFactory;
33 import org.openhab.core.thing.Bridge;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingTypeUID;
36 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
37 import org.openhab.core.thing.binding.ThingHandler;
38 import org.openhab.core.thing.binding.ThingHandlerFactory;
39 import org.osgi.service.component.ComponentContext;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Reference;
43 import org.osgi.service.jaxrs.client.SseEventSourceFactory;
44
45 /**
46  * The {@link HDPowerViewHandlerFactory} is responsible for creating things and thing
47  * handlers.
48  *
49  * @author Andy Lintner - Initial contribution
50  */
51 @NonNullByDefault
52 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.hdpowerview")
53 public class HDPowerViewHandlerFactory extends BaseThingHandlerFactory {
54
55     private final HttpClient httpClient;
56     private final HDPowerViewTranslationProvider translationProvider;
57     private final ClientBuilder clientBuilder;
58     private final SseEventSourceFactory eventSourceFactory;
59
60     @Activate
61     public HDPowerViewHandlerFactory(@Reference HttpClientFactory httpClientFactory,
62             final @Reference TranslationProvider i18nProvider, final @Reference LocaleProvider localeProvider,
63             final @Reference ClientBuilder clientBuilder, final @Reference SseEventSourceFactory eventSourceFactory,
64             ComponentContext componentContext) {
65         super.activate(componentContext);
66         this.httpClient = httpClientFactory.getCommonHttpClient();
67         this.translationProvider = new HDPowerViewTranslationProvider(getBundleContext().getBundle(), i18nProvider,
68                 localeProvider);
69         this.clientBuilder = clientBuilder;
70         this.eventSourceFactory = eventSourceFactory;
71     }
72
73     @Override
74     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
75         return HDPowerViewBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
76     }
77
78     @Override
79     protected @Nullable ThingHandler createHandler(Thing thing) {
80         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
81         if (HDPowerViewBindingConstants.THING_TYPE_GATEWAY.equals(thingTypeUID)) {
82             GatewayBridgeHandler handler = new GatewayBridgeHandler((Bridge) thing, httpClient, translationProvider,
83                     clientBuilder, eventSourceFactory);
84             registerService(new ShadeDiscoveryService(handler));
85             return handler;
86         } else if (HDPowerViewBindingConstants.THING_TYPE_SHADE3.equals(thingTypeUID)) {
87             return new ShadeThingHandler(thing);
88         } else if (HDPowerViewBindingConstants.THING_TYPE_HUB.equals(thingTypeUID)) {
89             HDPowerViewHubHandler handler = new HDPowerViewHubHandler((Bridge) thing, httpClient, translationProvider);
90             registerService(new HDPowerViewDeviceDiscoveryService(handler));
91             return handler;
92         } else if (HDPowerViewBindingConstants.THING_TYPE_SHADE.equals(thingTypeUID)) {
93             return new HDPowerViewShadeHandler(thing);
94         } else if (HDPowerViewBindingConstants.THING_TYPE_REPEATER.equals(thingTypeUID)) {
95             return new HDPowerViewRepeaterHandler(thing);
96         }
97         return null;
98     }
99
100     private void registerService(DiscoveryService discoveryService) {
101         bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>());
102     }
103 }