2 * Copyright (c) 2010-2021 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.openweathermap.internal.factory;
15 import static org.openhab.binding.openweathermap.internal.OpenWeatherMapBindingConstants.*;
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.openweathermap.internal.discovery.OpenWeatherMapDiscoveryService;
29 import org.openhab.binding.openweathermap.internal.handler.*;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.i18n.LocaleProvider;
32 import org.openhab.core.i18n.LocationProvider;
33 import org.openhab.core.i18n.TimeZoneProvider;
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 OpenWeatherMapHandlerFactory} is responsible for creating things and thing handlers.
51 * @author Christoph Weitkamp - Initial contribution
54 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.openweathermap")
55 public class OpenWeatherMapHandlerFactory extends BaseThingHandlerFactory {
57 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
58 .unmodifiableSet(Stream.concat(OpenWeatherMapAPIHandler.SUPPORTED_THING_TYPES.stream(),
59 AbstractOpenWeatherMapHandler.SUPPORTED_THING_TYPES.stream()).collect(Collectors.toSet()));
61 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
62 private final HttpClient httpClient;
63 private final LocaleProvider localeProvider;
64 private final LocationProvider locationProvider;
65 private final TranslationProvider i18nProvider;
66 private final TimeZoneProvider timeZoneProvider;
69 public OpenWeatherMapHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
70 final @Reference LocaleProvider localeProvider, final @Reference LocationProvider locationProvider,
71 final @Reference TranslationProvider i18nProvider, final @Reference TimeZoneProvider timeZoneProvider) {
72 this.httpClient = httpClientFactory.getCommonHttpClient();
73 this.localeProvider = localeProvider;
74 this.locationProvider = locationProvider;
75 this.i18nProvider = i18nProvider;
76 this.timeZoneProvider = timeZoneProvider;
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 OpenWeatherMapAPIHandler handler = new OpenWeatherMapAPIHandler((Bridge) thing, httpClient, localeProvider);
90 // register discovery service
91 OpenWeatherMapDiscoveryService discoveryService = new OpenWeatherMapDiscoveryService(handler,
92 locationProvider, 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 OpenWeatherMapWeatherAndForecastHandler(thing, timeZoneProvider);
98 } else if (THING_TYPE_UVINDEX.equals(thingTypeUID)) {
99 return new OpenWeatherMapUVIndexHandler(thing, timeZoneProvider);
100 } else if (THING_TYPE_ONECALL_WEATHER_AND_FORECAST.equals(thingTypeUID)) {
101 return new OpenWeatherMapOneCallHandler(thing, timeZoneProvider);
102 } else if (THING_TYPE_ONECALL_HISTORY.equals(thingTypeUID)) {
103 return new OpenWeatherMapOneCallHistoryHandler(thing, timeZoneProvider);
110 protected synchronized void removeHandler(ThingHandler thingHandler) {
111 if (thingHandler instanceof OpenWeatherMapAPIHandler) {
112 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thingHandler.getThing().getUID());
113 if (serviceReg != null) {
114 // remove discovery service, if bridge handler is removed
115 OpenWeatherMapDiscoveryService discoveryService = (OpenWeatherMapDiscoveryService) bundleContext
116 .getService(serviceReg.getReference());
117 serviceReg.unregister();
118 if (discoveryService != null) {
119 discoveryService.deactivate();