]> git.basschouten.com Git - openhab-addons.git/blob
7228d48404f010c7f4f0ed8f44fa46fe8f6d9572
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.darksky.internal.factory;
14
15 import static org.openhab.binding.darksky.internal.DarkSkyBindingConstants.*;
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.darksky.internal.discovery.DarkSkyDiscoveryService;
29 import org.openhab.binding.darksky.internal.handler.DarkSkyAPIHandler;
30 import org.openhab.binding.darksky.internal.handler.DarkSkyWeatherAndForecastHandler;
31 import org.openhab.core.config.discovery.DiscoveryService;
32 import org.openhab.core.i18n.LocaleProvider;
33 import org.openhab.core.i18n.LocationProvider;
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 DarkSkyHandlerFactory} 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.darksky")
55 public class DarkSkyHandlerFactory extends BaseThingHandlerFactory {
56
57     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
58             .unmodifiableSet(Stream
59                     .concat(DarkSkyAPIHandler.SUPPORTED_THING_TYPES.stream(),
60                             DarkSkyWeatherAndForecastHandler.SUPPORTED_THING_TYPES.stream())
61                     .collect(Collectors.toSet()));
62
63     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
64     private final HttpClient httpClient;
65     private final LocaleProvider localeProvider;
66     private final LocationProvider locationProvider;
67     private final TranslationProvider i18nProvider;
68
69     @Activate
70     public DarkSkyHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
71             final @Reference LocaleProvider localeProvider, final @Reference LocationProvider locationProvider,
72             final @Reference TranslationProvider i18nProvider) {
73         this.httpClient = httpClientFactory.getCommonHttpClient();
74         this.localeProvider = localeProvider;
75         this.locationProvider = locationProvider;
76         this.i18nProvider = i18nProvider;
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             DarkSkyAPIHandler handler = new DarkSkyAPIHandler((Bridge) thing, httpClient, localeProvider);
90             // register discovery service
91             DarkSkyDiscoveryService discoveryService = new DarkSkyDiscoveryService(handler, locationProvider,
92                     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 DarkSkyWeatherAndForecastHandler(thing);
98         }
99
100         return null;
101     }
102
103     @Override
104     protected synchronized void removeHandler(ThingHandler thingHandler) {
105         if (thingHandler instanceof DarkSkyAPIHandler) {
106             ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thingHandler.getThing().getUID());
107             if (serviceReg != null) {
108                 // remove discovery service, if bridge handler is removed
109                 DarkSkyDiscoveryService discoveryService = (DarkSkyDiscoveryService) bundleContext
110                         .getService(serviceReg.getReference());
111                 serviceReg.unregister();
112                 if (discoveryService != null) {
113                     discoveryService.deactivate();
114                 }
115             }
116         }
117     }
118 }