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