2 * Copyright (c) 2010-2022 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.darksky.internal.factory;
15 import static org.openhab.binding.darksky.internal.DarkSkyBindingConstants.*;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Hashtable;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.eclipse.jetty.client.HttpClient;
28 import org.openhab.binding.darksky.internal.discovery.DarkSkyDiscoveryService;
29 import org.openhab.binding.darksky.internal.handler.DarkSkyAPIHandler;
30 import org.openhab.binding.darksky.internal.handler.DarkSkyWeatherAndForecastHandler;
31 import org.openhab.core.config.discovery.DiscoveryService;
32 import org.openhab.core.i18n.LocaleProvider;
33 import org.openhab.core.i18n.LocationProvider;
34 import org.openhab.core.i18n.TranslationProvider;
35 import org.openhab.core.io.net.http.HttpClientFactory;
36 import org.openhab.core.thing.Bridge;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.thing.ThingUID;
40 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
41 import org.openhab.core.thing.binding.ThingHandler;
42 import org.openhab.core.thing.binding.ThingHandlerFactory;
43 import org.osgi.framework.ServiceRegistration;
44 import org.osgi.service.component.annotations.Activate;
45 import org.osgi.service.component.annotations.Component;
46 import org.osgi.service.component.annotations.Reference;
49 * The {@link DarkSkyHandlerFactory} is responsible for creating things and thing handlers.
51 * @author Christoph Weitkamp - Initial contribution
54 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.darksky")
55 public class DarkSkyHandlerFactory extends BaseThingHandlerFactory {
57 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
58 .unmodifiableSet(Stream
59 .concat(DarkSkyAPIHandler.SUPPORTED_THING_TYPES.stream(),
60 DarkSkyWeatherAndForecastHandler.SUPPORTED_THING_TYPES.stream())
61 .collect(Collectors.toSet()));
63 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
64 private final HttpClient httpClient;
65 private final LocaleProvider localeProvider;
66 private final LocationProvider locationProvider;
67 private final TranslationProvider i18nProvider;
70 public DarkSkyHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
71 final @Reference LocaleProvider localeProvider, final @Reference LocationProvider locationProvider,
72 final @Reference TranslationProvider i18nProvider) {
73 this.httpClient = httpClientFactory.getCommonHttpClient();
74 this.localeProvider = localeProvider;
75 this.locationProvider = locationProvider;
76 this.i18nProvider = i18nProvider;
80 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
81 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
85 protected @Nullable ThingHandler createHandler(Thing thing) {
86 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
88 if (THING_TYPE_WEATHER_API.equals(thingTypeUID)) {
89 DarkSkyAPIHandler handler = new DarkSkyAPIHandler((Bridge) thing, httpClient, localeProvider);
90 // register discovery service
91 DarkSkyDiscoveryService discoveryService = new DarkSkyDiscoveryService(handler, locationProvider,
92 localeProvider, i18nProvider);
93 discoveryServiceRegs.put(handler.getThing().getUID(), bundleContext
94 .registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
96 } else if (THING_TYPE_WEATHER_AND_FORECAST.equals(thingTypeUID)) {
97 return new DarkSkyWeatherAndForecastHandler(thing);
104 protected synchronized void removeHandler(ThingHandler thingHandler) {
105 if (thingHandler instanceof DarkSkyAPIHandler) {
106 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thingHandler.getThing().getUID());
107 if (serviceReg != null) {
108 // remove discovery service, if bridge handler is removed
109 DarkSkyDiscoveryService discoveryService = (DarkSkyDiscoveryService) bundleContext
110 .getService(serviceReg.getReference());
111 serviceReg.unregister();
112 if (discoveryService != null) {
113 discoveryService.deactivate();