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.weatherunderground.internal;
15 import static org.openhab.binding.weatherunderground.internal.WeatherUndergroundBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.weatherunderground.internal.discovery.WeatherUndergroundDiscoveryService;
27 import org.openhab.binding.weatherunderground.internal.handler.WeatherUndergroundBridgeHandler;
28 import org.openhab.binding.weatherunderground.internal.handler.WeatherUndergroundHandler;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.i18n.LocaleProvider;
31 import org.openhab.core.i18n.LocationProvider;
32 import org.openhab.core.i18n.TimeZoneProvider;
33 import org.openhab.core.i18n.UnitProvider;
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 WeatherUndergroundHandlerFactory} is responsible for creating things and thing
50 * @author Laurent Garnier - Initial contribution
51 * @author Theo Giovanna - Added a bridge for the API key
52 * @author Laurent Garnier - Registration of the discovery service updated
55 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.weatherunderground")
56 public class WeatherUndergroundHandlerFactory extends BaseThingHandlerFactory {
58 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
59 .of(BRIDGE_THING_TYPES_UIDS, WeatherUndergroundBindingConstants.SUPPORTED_THING_TYPES_UIDS)
60 .flatMap(x -> x.stream()).collect(Collectors.toSet());
62 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
64 private final LocaleProvider localeProvider;
65 private final LocationProvider locationProvider;
66 private final UnitProvider unitProvider;
67 private final TimeZoneProvider timeZoneProvider;
70 public WeatherUndergroundHandlerFactory(final @Reference LocaleProvider localeProvider,
71 final @Reference LocationProvider locationProvider, final @Reference UnitProvider unitProvider,
72 final @Reference TimeZoneProvider timeZoneProvider) {
73 this.localeProvider = localeProvider;
74 this.locationProvider = locationProvider;
75 this.unitProvider = unitProvider;
76 this.timeZoneProvider = timeZoneProvider;
80 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
81 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
85 protected @Nullable ThingHandler createHandler(Thing thing) {
86 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
88 if (thingTypeUID.equals(THING_TYPE_WEATHER)) {
89 return new WeatherUndergroundHandler(thing, localeProvider, unitProvider, timeZoneProvider);
92 if (thingTypeUID.equals(THING_TYPE_BRIDGE)) {
93 WeatherUndergroundBridgeHandler handler = new WeatherUndergroundBridgeHandler((Bridge) thing);
94 registerDiscoveryService(handler.getThing().getUID());
102 protected void removeHandler(ThingHandler thingHandler) {
103 if (thingHandler instanceof WeatherUndergroundBridgeHandler) {
104 unregisterDiscoveryService(thingHandler.getThing().getUID());
108 private synchronized void registerDiscoveryService(ThingUID bridgeUID) {
109 WeatherUndergroundDiscoveryService discoveryService = new WeatherUndergroundDiscoveryService(bridgeUID,
110 localeProvider, locationProvider);
111 discoveryService.activate(null);
112 discoveryServiceRegs.put(bridgeUID,
113 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
116 private synchronized void unregisterDiscoveryService(ThingUID bridgeUID) {
117 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(bridgeUID);
118 if (serviceReg != null) {
119 WeatherUndergroundDiscoveryService service = (WeatherUndergroundDiscoveryService) bundleContext
120 .getService(serviceReg.getReference());
121 serviceReg.unregister();
122 if (service != null) {
123 service.deactivate();