]> git.basschouten.com Git - openhab-addons.git/blob
dbc49d4764c1df88af3c46729f734705cb162df5
[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.weatherunderground.internal;
14
15 import static org.openhab.binding.weatherunderground.internal.WeatherUndergroundBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
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;
45
46 /**
47  * The {@link WeatherUndergroundHandlerFactory} is responsible for creating things and thing
48  * handlers.
49  *
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
53  */
54 @NonNullByDefault
55 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.weatherunderground")
56 public class WeatherUndergroundHandlerFactory extends BaseThingHandlerFactory {
57
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());
61
62     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
63
64     private final LocaleProvider localeProvider;
65     private final LocationProvider locationProvider;
66     private final UnitProvider unitProvider;
67     private final TimeZoneProvider timeZoneProvider;
68
69     @Activate
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;
77     }
78
79     @Override
80     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
81         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
82     }
83
84     @Override
85     protected @Nullable ThingHandler createHandler(Thing thing) {
86         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
87
88         if (thingTypeUID.equals(THING_TYPE_WEATHER)) {
89             return new WeatherUndergroundHandler(thing, localeProvider, unitProvider, timeZoneProvider);
90         }
91
92         if (thingTypeUID.equals(THING_TYPE_BRIDGE)) {
93             WeatherUndergroundBridgeHandler handler = new WeatherUndergroundBridgeHandler((Bridge) thing);
94             registerDiscoveryService(handler.getThing().getUID());
95             return handler;
96         }
97
98         return null;
99     }
100
101     @Override
102     protected void removeHandler(ThingHandler thingHandler) {
103         if (thingHandler instanceof WeatherUndergroundBridgeHandler) {
104             unregisterDiscoveryService(thingHandler.getThing().getUID());
105         }
106     }
107
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<>()));
114     }
115
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();
124             }
125         }
126     }
127 }