2 * Copyright (c) 2010-2020 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.AbstractOpenWeatherMapHandler;
30 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapAPIHandler;
31 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapUVIndexHandler;
32 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapWeatherAndForecastHandler;
33 import org.openhab.core.config.discovery.DiscoveryService;
34 import org.openhab.core.i18n.LocaleProvider;
35 import org.openhab.core.i18n.LocationProvider;
36 import org.openhab.core.i18n.TimeZoneProvider;
37 import org.openhab.core.i18n.TranslationProvider;
38 import org.openhab.core.io.net.http.HttpClientFactory;
39 import org.openhab.core.thing.Bridge;
40 import org.openhab.core.thing.Thing;
41 import org.openhab.core.thing.ThingTypeUID;
42 import org.openhab.core.thing.ThingUID;
43 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
44 import org.openhab.core.thing.binding.ThingHandler;
45 import org.openhab.core.thing.binding.ThingHandlerFactory;
46 import org.osgi.framework.ServiceRegistration;
47 import org.osgi.service.component.annotations.Activate;
48 import org.osgi.service.component.annotations.Component;
49 import org.osgi.service.component.annotations.Reference;
52 * The {@link OpenWeatherMapHandlerFactory} is responsible for creating things and thing handlers.
54 * @author Christoph Weitkamp - Initial contribution
57 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.openweathermap")
58 public class OpenWeatherMapHandlerFactory extends BaseThingHandlerFactory {
60 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
61 .unmodifiableSet(Stream.concat(OpenWeatherMapAPIHandler.SUPPORTED_THING_TYPES.stream(),
62 AbstractOpenWeatherMapHandler.SUPPORTED_THING_TYPES.stream()).collect(Collectors.toSet()));
64 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
65 private final HttpClient httpClient;
66 private final LocaleProvider localeProvider;
67 private final LocationProvider locationProvider;
68 private final TranslationProvider i18nProvider;
69 private final TimeZoneProvider timeZoneProvider;
72 public OpenWeatherMapHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
73 final @Reference LocaleProvider localeProvider, final @Reference LocationProvider locationProvider,
74 final @Reference TranslationProvider i18nProvider, final @Reference TimeZoneProvider timeZoneProvider) {
75 this.httpClient = httpClientFactory.getCommonHttpClient();
76 this.localeProvider = localeProvider;
77 this.locationProvider = locationProvider;
78 this.i18nProvider = i18nProvider;
79 this.timeZoneProvider = timeZoneProvider;
83 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
84 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
88 protected @Nullable ThingHandler createHandler(Thing thing) {
89 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
91 if (THING_TYPE_WEATHER_API.equals(thingTypeUID)) {
92 OpenWeatherMapAPIHandler handler = new OpenWeatherMapAPIHandler((Bridge) thing, httpClient, localeProvider);
93 // register discovery service
94 OpenWeatherMapDiscoveryService discoveryService = new OpenWeatherMapDiscoveryService(handler,
95 locationProvider, localeProvider, i18nProvider);
96 discoveryServiceRegs.put(handler.getThing().getUID(), bundleContext
97 .registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
99 } else if (THING_TYPE_WEATHER_AND_FORECAST.equals(thingTypeUID)) {
100 return new OpenWeatherMapWeatherAndForecastHandler(thing, timeZoneProvider);
101 } else if (THING_TYPE_UVINDEX.equals(thingTypeUID)) {
102 return new OpenWeatherMapUVIndexHandler(thing, timeZoneProvider);
109 protected synchronized void removeHandler(ThingHandler thingHandler) {
110 if (thingHandler instanceof OpenWeatherMapAPIHandler) {
111 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thingHandler.getThing().getUID());
112 if (serviceReg != null) {
113 // remove discovery service, if bridge handler is removed
114 OpenWeatherMapDiscoveryService discoveryService = (OpenWeatherMapDiscoveryService) bundleContext
115 .getService(serviceReg.getReference());
116 serviceReg.unregister();
117 if (discoveryService != null) {
118 discoveryService.deactivate();