]> git.basschouten.com Git - openhab-addons.git/blob
a8e726041233e08c96863fdafd1ef7b5cbc115c6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.openweathermap.internal.factory;
14
15 import static org.openhab.binding.openweathermap.internal.OpenWeatherMapBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Hashtable;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24
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;
50
51 /**
52  * The {@link OpenWeatherMapHandlerFactory} is responsible for creating things and thing handlers.
53  *
54  * @author Christoph Weitkamp - Initial contribution
55  */
56 @NonNullByDefault
57 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.openweathermap")
58 public class OpenWeatherMapHandlerFactory extends BaseThingHandlerFactory {
59
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()));
63
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;
70
71     @Activate
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;
80     }
81
82     @Override
83     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
84         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
85     }
86
87     @Override
88     protected @Nullable ThingHandler createHandler(Thing thing) {
89         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
90
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<>()));
98             return handler;
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);
103         }
104
105         return null;
106     }
107
108     @Override
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();
119                 }
120             }
121         }
122     }
123 }