2 * Copyright (c) 2010-2023 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;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.eclipse.jetty.client.HttpClient;
27 import org.openhab.binding.openweathermap.internal.discovery.OpenWeatherMapDiscoveryService;
28 import org.openhab.binding.openweathermap.internal.handler.AbstractOpenWeatherMapHandler;
29 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapAPIHandler;
30 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapAirPollutionHandler;
31 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapOneCallHandler;
32 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapOneCallHistoryHandler;
33 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapUVIndexHandler;
34 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapWeatherAndForecastHandler;
35 import org.openhab.core.config.discovery.DiscoveryService;
36 import org.openhab.core.i18n.LocaleProvider;
37 import org.openhab.core.i18n.LocationProvider;
38 import org.openhab.core.i18n.TimeZoneProvider;
39 import org.openhab.core.i18n.TranslationProvider;
40 import org.openhab.core.io.net.http.HttpClientFactory;
41 import org.openhab.core.thing.Bridge;
42 import org.openhab.core.thing.Thing;
43 import org.openhab.core.thing.ThingTypeUID;
44 import org.openhab.core.thing.ThingUID;
45 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
46 import org.openhab.core.thing.binding.ThingHandler;
47 import org.openhab.core.thing.binding.ThingHandlerFactory;
48 import org.osgi.framework.ServiceRegistration;
49 import org.osgi.service.component.annotations.Activate;
50 import org.osgi.service.component.annotations.Component;
51 import org.osgi.service.component.annotations.Reference;
54 * The {@link OpenWeatherMapHandlerFactory} is responsible for creating things and thing handlers.
56 * @author Christoph Weitkamp - Initial contribution
59 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.openweathermap")
60 public class OpenWeatherMapHandlerFactory extends BaseThingHandlerFactory {
62 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
63 .unmodifiableSet(Stream.concat(OpenWeatherMapAPIHandler.SUPPORTED_THING_TYPES.stream(),
64 AbstractOpenWeatherMapHandler.SUPPORTED_THING_TYPES.stream()).collect(Collectors.toSet()));
66 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
67 private final HttpClient httpClient;
68 private final LocaleProvider localeProvider;
69 private final LocationProvider locationProvider;
70 private final TranslationProvider i18nProvider;
71 private final TimeZoneProvider timeZoneProvider;
74 public OpenWeatherMapHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
75 final @Reference LocaleProvider localeProvider, final @Reference LocationProvider locationProvider,
76 final @Reference TranslationProvider i18nProvider, final @Reference TimeZoneProvider timeZoneProvider) {
77 this.httpClient = httpClientFactory.getCommonHttpClient();
78 this.localeProvider = localeProvider;
79 this.locationProvider = locationProvider;
80 this.i18nProvider = i18nProvider;
81 this.timeZoneProvider = timeZoneProvider;
85 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
86 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
90 protected @Nullable ThingHandler createHandler(Thing thing) {
91 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
93 if (THING_TYPE_WEATHER_API.equals(thingTypeUID)) {
94 OpenWeatherMapAPIHandler handler = new OpenWeatherMapAPIHandler((Bridge) thing, httpClient, localeProvider);
95 // register discovery service
96 OpenWeatherMapDiscoveryService discoveryService = new OpenWeatherMapDiscoveryService(handler,
97 locationProvider, localeProvider, i18nProvider);
98 discoveryServiceRegs.put(handler.getThing().getUID(),
99 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, null));
101 } else if (THING_TYPE_WEATHER_AND_FORECAST.equals(thingTypeUID)) {
102 return new OpenWeatherMapWeatherAndForecastHandler(thing, timeZoneProvider);
103 } else if (THING_TYPE_UVINDEX.equals(thingTypeUID)) {
104 return new OpenWeatherMapUVIndexHandler(thing, timeZoneProvider);
105 } else if (THING_TYPE_AIR_POLLUTION.equals(thingTypeUID)) {
106 return new OpenWeatherMapAirPollutionHandler(thing, timeZoneProvider);
107 } else if (THING_TYPE_ONECALL_WEATHER_AND_FORECAST.equals(thingTypeUID)) {
108 return new OpenWeatherMapOneCallHandler(thing, timeZoneProvider);
109 } else if (THING_TYPE_ONECALL_HISTORY.equals(thingTypeUID)) {
110 return new OpenWeatherMapOneCallHistoryHandler(thing, timeZoneProvider);
117 protected synchronized void removeHandler(ThingHandler thingHandler) {
118 if (thingHandler instanceof OpenWeatherMapAPIHandler) {
119 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thingHandler.getThing().getUID());
120 if (serviceReg != null) {
121 // remove discovery service, if bridge handler is removed
122 OpenWeatherMapDiscoveryService discoveryService = (OpenWeatherMapDiscoveryService) bundleContext
123 .getService(serviceReg.getReference());
124 serviceReg.unregister();
125 if (discoveryService != null) {
126 discoveryService.deactivate();