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.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.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;
44 * The {@link AutomowerHandlerFactory} is responsible for creating things and thing
47 * @author Markus Pfleger - Initial contribution
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()));
56 private final OAuthFactory oAuthFactory;
57 protected final @NonNullByDefault({}) HttpClient httpClient;
58 private @Nullable ServiceRegistration<?> automowerDiscoveryServiceRegistration;
59 private final TimeZoneProvider timeZoneProvider;
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;
70 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
71 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
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);
82 if (AutomowerHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
83 return new AutomowerHandler(thing, timeZoneProvider);
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();
99 private void registerAutomowerDiscoveryService(AutomowerBridgeHandler handler) {
100 AutomowerDiscoveryService discoveryService = new AutomowerDiscoveryService(handler);
101 this.automowerDiscoveryServiceRegistration = bundleContext.registerService(DiscoveryService.class.getName(),
102 discoveryService, new Hashtable<>());