]> git.basschouten.com Git - openhab-addons.git/blob
393c0638c87981ee5dd38527ced5078f6092bc76
[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.*;
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;
47
48 /**
49  * The {@link OpenWeatherMapHandlerFactory} is responsible for creating things and thing handlers.
50  *
51  * @author Christoph Weitkamp - Initial contribution
52  */
53 @NonNullByDefault
54 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.openweathermap")
55 public class OpenWeatherMapHandlerFactory extends BaseThingHandlerFactory {
56
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()));
60
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;
67
68     @Activate
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;
77     }
78
79     @Override
80     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
81         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
82     }
83
84     @Override
85     protected @Nullable ThingHandler createHandler(Thing thing) {
86         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
87
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<>()));
95             return handler;
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);
104         }
105
106         return null;
107     }
108
109     @Override
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();
120                 }
121             }
122         }
123     }
124 }