2 * Copyright (c) 2010-2021 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.weathercompany.internal;
15 import static org.openhab.binding.weathercompany.internal.WeatherCompanyBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
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;
47 * The {@link WeatherCompanyHandlerFactory} is responsible for creating thing handlers.
49 * @author Mark Hilbush - Initial contribution
52 @Component(configurationPid = "binding.weathercompany", service = ThingHandlerFactory.class)
53 public class WeatherCompanyHandlerFactory extends BaseThingHandlerFactory {
54 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
56 private TimeZoneProvider timeZoneProvider;
57 private UnitProvider unitProvider;
58 private HttpClient httpClient;
59 private LocationProvider locationProvider;
60 private LocaleProvider localeProvider;
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;
74 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
75 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
79 protected @Nullable ThingHandler createHandler(Thing thing) {
80 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
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,
87 } else if (SUPPORTED_BRIDGE_THING_TYPES_UIDS.contains(thingTypeUID)) {
88 WeatherCompanyBridgeHandler handler = new WeatherCompanyBridgeHandler((Bridge) thing);
89 registerDeviceDiscoveryService(handler);
96 protected synchronized void removeHandler(ThingHandler thingHandler) {
97 if (thingHandler instanceof WeatherCompanyBridgeHandler) {
98 ThingUID thingUID = thingHandler.getThing().getUID();
99 unregisterDeviceDiscoveryService(thingUID);
101 super.removeHandler(thingHandler);
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<>()));
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();