2 * Copyright (c) 2010-2020 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.automower.internal;
15 import java.util.Collections;
16 import java.util.Hashtable;
18 import java.util.function.Function;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
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;
43 * The {@link AutomowerHandlerFactory} is responsible for creating things and thing
46 * @author Markus Pfleger - Initial contribution
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()));
55 private final OAuthFactory oAuthFactory;
56 protected final @NonNullByDefault({}) HttpClient httpClient;
57 private @Nullable ServiceRegistration<?> automowerDiscoveryServiceRegistration;
60 public AutomowerHandlerFactory(@Reference OAuthFactory oAuthFactory,
61 @Reference HttpClientFactory httpClientFactory) {
62 this.oAuthFactory = oAuthFactory;
63 this.httpClient = httpClientFactory.getCommonHttpClient();
67 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
68 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
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);
79 if (AutomowerHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
80 return new AutomowerHandler(thing);
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();
96 private void registerAutomowerDiscoveryService(AutomowerBridgeHandler handler) {
97 AutomowerDiscoveryService discoveryService = new AutomowerDiscoveryService(handler);
98 this.automowerDiscoveryServiceRegistration = bundleContext.registerService(DiscoveryService.class.getName(),
99 discoveryService, new Hashtable<>());