2 * Copyright (c) 2010-2021 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.bluetooth.bluez.internal;
15 import java.util.concurrent.CompletableFuture;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.atomic.AtomicInteger;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.freedesktop.dbus.exceptions.DBusException;
23 import org.openhab.binding.bluetooth.util.RetryException;
24 import org.openhab.binding.bluetooth.util.RetryFuture;
25 import org.openhab.core.common.ThreadPoolManager;
26 import org.osgi.service.component.annotations.Activate;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Deactivate;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.github.hypfvieh.bluetooth.DeviceManager;
35 * This service handles the lifecycle of the {@link DeviceManager} singleton instance.
36 * In addition, this class is responsible for managing the BlueZPropertiesChangedHandler instance
37 * used by the binding for listening and dispatching dbus events from the DeviceManager.
39 * Creation of the DeviceManagerWrapper is asynchronous and thus attempts to retrieve the
40 * DeviceManagerWrapper through 'getDeviceManager' may initially fail.
42 * @author Connor Petty - Initial Contribution
46 @Component(service = DeviceManagerFactory.class)
47 public class DeviceManagerFactory {
49 private final Logger logger = LoggerFactory.getLogger(DeviceManagerFactory.class);
50 private final ScheduledExecutorService scheduler = ThreadPoolManager.getScheduledPool("bluetooth");
52 private final BlueZPropertiesChangedHandler changeHandler = new BlueZPropertiesChangedHandler();
54 private @Nullable CompletableFuture<DeviceManager> deviceManagerFuture;
55 private @Nullable CompletableFuture<DeviceManagerWrapper> deviceManagerWrapperFuture;
57 public BlueZPropertiesChangedHandler getPropertiesChangedHandler() {
61 public @Nullable DeviceManagerWrapper getDeviceManager() {
62 // we can cheat the null checker with casting here
63 var future = (CompletableFuture<@Nullable DeviceManagerWrapper>) deviceManagerWrapperFuture;
65 return future.getNow(null);
71 public void initialize() {
72 logger.debug("initializing DeviceManagerFactory");
74 var stage1 = this.deviceManagerFuture = RetryFuture.callWithRetry(() -> {
76 // if this is the first call to the library, this call
77 // should throw an exception (that we are catching)
78 return DeviceManager.getInstance();
79 // Experimental - seems reuse does not work
80 } catch (IllegalStateException e) {
81 // Exception caused by first call to the library
82 return DeviceManager.createInstance(false);
86 this.deviceManagerWrapperFuture = stage1.thenCompose(devManager -> {
87 // lambdas can't modify outside variables due to scoping, so instead we use an AtomicInteger.
88 AtomicInteger tryCount = new AtomicInteger();
89 return RetryFuture.callWithRetry(() -> {
90 int count = tryCount.incrementAndGet();
92 logger.debug("Registering property handler attempt: {}", count);
93 devManager.registerPropertyHandler(changeHandler);
94 logger.debug("Successfully registered property handler");
95 return new DeviceManagerWrapper(devManager);
96 } catch (DBusException e) {
98 throw new RetryException(5, TimeUnit.SECONDS);
104 }).whenComplete((devManagerWrapper, th) -> {
106 logger.warn("Failed to initialize DeviceManager: {}", th.getMessage());
112 public void dispose() {
113 var stage1 = this.deviceManagerFuture;
114 if (stage1 != null) {
115 if (!stage1.cancel(true)) {
116 // a failure to cancel means that the stage completed normally
117 stage1.thenAccept(DeviceManager::closeConnection);
120 this.deviceManagerFuture = null;
122 var stage2 = this.deviceManagerWrapperFuture;
123 if (stage2 != null) {
126 this.deviceManagerWrapperFuture = null;