2 * Copyright (c) 2010-2023 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.TranslationProvider;
33 import org.openhab.core.i18n.UnitProvider;
34 import org.openhab.core.io.net.http.HttpClientFactory;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
40 import org.openhab.core.thing.binding.ThingHandler;
41 import org.openhab.core.thing.binding.ThingHandlerFactory;
42 import org.osgi.framework.ServiceRegistration;
43 import org.osgi.service.component.annotations.Activate;
44 import org.osgi.service.component.annotations.Component;
45 import org.osgi.service.component.annotations.Reference;
48 * The {@link WeatherCompanyHandlerFactory} is responsible for creating thing handlers.
50 * @author Mark Hilbush - Initial contribution
53 @Component(configurationPid = "binding.weathercompany", service = ThingHandlerFactory.class)
54 public class WeatherCompanyHandlerFactory extends BaseThingHandlerFactory {
55 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
57 private TimeZoneProvider timeZoneProvider;
58 private UnitProvider unitProvider;
59 private HttpClient httpClient;
60 private LocationProvider locationProvider;
61 private LocaleProvider localeProvider;
62 private TranslationProvider i18nProvider;
65 public WeatherCompanyHandlerFactory(@Reference TimeZoneProvider timeZoneProvider,
66 @Reference UnitProvider unitProvider, @Reference HttpClientFactory httpClientFactory,
67 @Reference LocationProvider locationProvider, @Reference LocaleProvider localeProvider,
68 @Reference TranslationProvider i18nProvider) {
69 this.timeZoneProvider = timeZoneProvider;
70 this.unitProvider = unitProvider;
71 this.httpClient = httpClientFactory.getCommonHttpClient();
72 this.locationProvider = locationProvider;
73 this.localeProvider = localeProvider;
74 this.i18nProvider = i18nProvider;
78 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
79 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
83 protected @Nullable ThingHandler createHandler(Thing thing) {
84 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
86 if (THING_TYPE_WEATHER_FORECAST.equals(thingTypeUID)) {
87 return new WeatherCompanyForecastHandler(thing, timeZoneProvider, httpClient, unitProvider, localeProvider);
88 } else if (THING_TYPE_WEATHER_OBSERVATIONS.equals(thingTypeUID)) {
89 return new WeatherCompanyObservationsHandler(thing, timeZoneProvider, httpClient, unitProvider,
91 } else if (SUPPORTED_BRIDGE_THING_TYPES_UIDS.contains(thingTypeUID)) {
92 WeatherCompanyBridgeHandler handler = new WeatherCompanyBridgeHandler((Bridge) thing);
93 registerDeviceDiscoveryService(handler);
100 protected synchronized void removeHandler(ThingHandler thingHandler) {
101 if (thingHandler instanceof WeatherCompanyBridgeHandler) {
102 ThingUID thingUID = thingHandler.getThing().getUID();
103 unregisterDeviceDiscoveryService(thingUID);
105 super.removeHandler(thingHandler);
108 private synchronized void registerDeviceDiscoveryService(WeatherCompanyBridgeHandler bridgeHandler) {
109 WeatherCompanyDiscoveryService discoveryService = new WeatherCompanyDiscoveryService(bridgeHandler,
110 locationProvider, localeProvider, i18nProvider);
111 discoveryService.activate(null);
112 this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
113 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
116 private void unregisterDeviceDiscoveryService(ThingUID bridgeUID) {
117 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(bridgeUID);
118 if (serviceReg != null) {
119 WeatherCompanyDiscoveryService discoveryService = (WeatherCompanyDiscoveryService) bundleContext
120 .getService(serviceReg.getReference());
121 serviceReg.unregister();
122 if (discoveryService != null) {
123 discoveryService.deactivate();