]> git.basschouten.com Git - openhab-addons.git/blob
d8df3fd816d60817432bbef94a35f41a47ddf541
[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.weathercompany.internal;
14
15 import static org.openhab.binding.weathercompany.internal.WeatherCompanyBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.openhab.binding.weathercompany.internal.discovery.WeatherCompanyDiscoveryService;
25 import org.openhab.binding.weathercompany.internal.handler.WeatherCompanyBridgeHandler;
26 import org.openhab.binding.weathercompany.internal.handler.WeatherCompanyForecastHandler;
27 import org.openhab.binding.weathercompany.internal.handler.WeatherCompanyObservationsHandler;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.i18n.LocaleProvider;
30 import org.openhab.core.i18n.LocationProvider;
31 import org.openhab.core.i18n.TimeZoneProvider;
32 import org.openhab.core.i18n.UnitProvider;
33 import org.openhab.core.io.net.http.HttpClientFactory;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.ThingUID;
38 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
39 import org.openhab.core.thing.binding.ThingHandler;
40 import org.openhab.core.thing.binding.ThingHandlerFactory;
41 import org.osgi.framework.ServiceRegistration;
42 import org.osgi.service.component.annotations.Activate;
43 import org.osgi.service.component.annotations.Component;
44 import org.osgi.service.component.annotations.Reference;
45
46 /**
47  * The {@link WeatherCompanyHandlerFactory} is responsible for creating thing handlers.
48  *
49  * @author Mark Hilbush - Initial contribution
50  */
51 @NonNullByDefault
52 @Component(configurationPid = "binding.weathercompany", service = ThingHandlerFactory.class)
53 public class WeatherCompanyHandlerFactory extends BaseThingHandlerFactory {
54     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
55
56     private TimeZoneProvider timeZoneProvider;
57     private UnitProvider unitProvider;
58     private HttpClient httpClient;
59     private LocationProvider locationProvider;
60     private LocaleProvider localeProvider;
61
62     @Activate
63     public WeatherCompanyHandlerFactory(@Reference TimeZoneProvider timeZoneProvider,
64             @Reference UnitProvider unitProvider, @Reference HttpClientFactory httpClientFactory,
65             @Reference LocationProvider locationProvider, @Reference LocaleProvider localeProvider) {
66         this.timeZoneProvider = timeZoneProvider;
67         this.unitProvider = unitProvider;
68         this.httpClient = httpClientFactory.getCommonHttpClient();
69         this.locationProvider = locationProvider;
70         this.localeProvider = localeProvider;
71     }
72
73     @Override
74     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
75         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
76     }
77
78     @Override
79     protected @Nullable ThingHandler createHandler(Thing thing) {
80         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
81
82         if (THING_TYPE_WEATHER_FORECAST.equals(thingTypeUID)) {
83             return new WeatherCompanyForecastHandler(thing, timeZoneProvider, httpClient, unitProvider, localeProvider);
84         } else if (THING_TYPE_WEATHER_OBSERVATIONS.equals(thingTypeUID)) {
85             return new WeatherCompanyObservationsHandler(thing, timeZoneProvider, httpClient, unitProvider,
86                     localeProvider);
87         } else if (SUPPORTED_BRIDGE_THING_TYPES_UIDS.contains(thingTypeUID)) {
88             WeatherCompanyBridgeHandler handler = new WeatherCompanyBridgeHandler((Bridge) thing);
89             registerDeviceDiscoveryService(handler);
90             return handler;
91         }
92         return null;
93     }
94
95     @Override
96     protected synchronized void removeHandler(ThingHandler thingHandler) {
97         if (thingHandler instanceof WeatherCompanyBridgeHandler) {
98             ThingUID thingUID = thingHandler.getThing().getUID();
99             unregisterDeviceDiscoveryService(thingUID);
100         }
101         super.removeHandler(thingHandler);
102     }
103
104     private synchronized void registerDeviceDiscoveryService(WeatherCompanyBridgeHandler bridgeHandler) {
105         WeatherCompanyDiscoveryService discoveryService = new WeatherCompanyDiscoveryService(bridgeHandler,
106                 locationProvider, localeProvider);
107         discoveryService.activate(null);
108         this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
109                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
110     }
111
112     private void unregisterDeviceDiscoveryService(ThingUID bridgeUID) {
113         ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(bridgeUID);
114         if (serviceReg != null) {
115             WeatherCompanyDiscoveryService discoveryService = (WeatherCompanyDiscoveryService) bundleContext
116                     .getService(serviceReg.getReference());
117             serviceReg.unregister();
118             if (discoveryService != null) {
119                 discoveryService.deactivate();
120             }
121         }
122     }
123 }