]> git.basschouten.com Git - openhab-addons.git/blob
ae057eab7c41e08cfb00b6bc9593a4d9f94e1f88
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.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;
46
47 /**
48  * The {@link WeatherCompanyHandlerFactory} is responsible for creating thing handlers.
49  *
50  * @author Mark Hilbush - Initial contribution
51  */
52 @NonNullByDefault
53 @Component(configurationPid = "binding.weathercompany", service = ThingHandlerFactory.class)
54 public class WeatherCompanyHandlerFactory extends BaseThingHandlerFactory {
55     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
56
57     private TimeZoneProvider timeZoneProvider;
58     private UnitProvider unitProvider;
59     private HttpClient httpClient;
60     private LocationProvider locationProvider;
61     private LocaleProvider localeProvider;
62     private TranslationProvider i18nProvider;
63
64     @Activate
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;
75     }
76
77     @Override
78     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
79         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
80     }
81
82     @Override
83     protected @Nullable ThingHandler createHandler(Thing thing) {
84         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
85
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,
90                     localeProvider);
91         } else if (SUPPORTED_BRIDGE_THING_TYPES_UIDS.contains(thingTypeUID)) {
92             WeatherCompanyBridgeHandler handler = new WeatherCompanyBridgeHandler((Bridge) thing);
93             registerDeviceDiscoveryService(handler);
94             return handler;
95         }
96         return null;
97     }
98
99     @Override
100     protected synchronized void removeHandler(ThingHandler thingHandler) {
101         if (thingHandler instanceof WeatherCompanyBridgeHandler) {
102             ThingUID thingUID = thingHandler.getThing().getUID();
103             unregisterDeviceDiscoveryService(thingUID);
104         }
105         super.removeHandler(thingHandler);
106     }
107
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<>()));
114     }
115
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();
124             }
125         }
126     }
127 }