]> git.basschouten.com Git - openhab-addons.git/blob
af87c7cafd83f439ed91f0f64fe7f96d9d990ff8
[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.automower.internal;
14
15 import java.util.Collections;
16 import java.util.Hashtable;
17 import java.util.Set;
18 import java.util.function.Function;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.openhab.binding.automower.internal.bridge.AutomowerBridgeHandler;
26 import org.openhab.binding.automower.internal.discovery.AutomowerDiscoveryService;
27 import org.openhab.binding.automower.internal.things.AutomowerHandler;
28 import org.openhab.core.auth.client.oauth2.OAuthFactory;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.i18n.TimeZoneProvider;
31 import org.openhab.core.io.net.http.HttpClientFactory;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerFactory;
38 import org.osgi.framework.ServiceRegistration;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42
43 /**
44  * The {@link AutomowerHandlerFactory} is responsible for creating things and thing
45  * handlers.
46  *
47  * @author Markus Pfleger - Initial contribution
48  */
49 @NonNullByDefault
50 @Component(configurationPid = "binding.automower", service = ThingHandlerFactory.class)
51 public class AutomowerHandlerFactory extends BaseThingHandlerFactory {
52     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.unmodifiableSet(Stream
53             .of(AutomowerBridgeHandler.SUPPORTED_THING_TYPES.stream(), AutomowerHandler.SUPPORTED_THING_TYPES.stream())
54             .flatMap(Function.identity()).collect(Collectors.toSet()));
55
56     private final OAuthFactory oAuthFactory;
57     protected final @NonNullByDefault({}) HttpClient httpClient;
58     private @Nullable ServiceRegistration<?> automowerDiscoveryServiceRegistration;
59     private final TimeZoneProvider timeZoneProvider;
60
61     @Activate
62     public AutomowerHandlerFactory(@Reference OAuthFactory oAuthFactory, @Reference HttpClientFactory httpClientFactory,
63             @Reference TimeZoneProvider timeZoneProvider) {
64         this.oAuthFactory = oAuthFactory;
65         this.httpClient = httpClientFactory.getCommonHttpClient();
66         this.timeZoneProvider = timeZoneProvider;
67     }
68
69     @Override
70     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
71         return SUPPORTED_THING_TYPES.contains(thingTypeUID);
72     }
73
74     @Override
75     protected @Nullable ThingHandler createHandler(Thing thing) {
76         if (AutomowerBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
77             AutomowerBridgeHandler handler = new AutomowerBridgeHandler((Bridge) thing, oAuthFactory, httpClient);
78             registerAutomowerDiscoveryService(handler);
79             return handler;
80         }
81
82         if (AutomowerHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
83             return new AutomowerHandler(thing, timeZoneProvider);
84         }
85
86         return null;
87     }
88
89     @Override
90     protected synchronized void removeHandler(ThingHandler thingHandler) {
91         if (thingHandler instanceof AutomowerBridgeHandler) {
92             if (automowerDiscoveryServiceRegistration != null) {
93                 // remove discovery service, if bridge handler is removed
94                 automowerDiscoveryServiceRegistration.unregister();
95             }
96         }
97     }
98
99     private void registerAutomowerDiscoveryService(AutomowerBridgeHandler handler) {
100         AutomowerDiscoveryService discoveryService = new AutomowerDiscoveryService(handler);
101         this.automowerDiscoveryServiceRegistration = bundleContext.registerService(DiscoveryService.class.getName(),
102                 discoveryService, new Hashtable<>());
103     }
104 }