From b9c5a0d158e92cef29335b301bba042684e371fd Mon Sep 17 00:00:00 2001 From: Connor Petty Date: Sun, 14 Feb 2021 10:52:18 -0800 Subject: [PATCH] [bluetooth] Expire discovery snapshots so that devices can be rediscovered (#10099) Signed-off-by: Connor Petty --- .../discovery/internal/BluetoothDiscoveryService.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryService.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryService.java index b1f4010b4b2..af31f910b25 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryService.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryService.java @@ -12,6 +12,7 @@ */ package org.openhab.binding.bluetooth.discovery.internal; +import java.time.Duration; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -30,6 +31,7 @@ import org.openhab.binding.bluetooth.BluetoothBindingConstants; import org.openhab.binding.bluetooth.BluetoothDevice; import org.openhab.binding.bluetooth.BluetoothDiscoveryListener; import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant; +import org.openhab.core.cache.ExpiringCache; import org.openhab.core.config.discovery.AbstractDiscoveryService; import org.openhab.core.config.discovery.DiscoveryResult; import org.openhab.core.config.discovery.DiscoveryResultBuilder; @@ -172,7 +174,8 @@ public class BluetoothDiscoveryService extends AbstractDiscoveryService implemen private final Map discoveryFutures = new HashMap<>(); private final Map> discoveryResults = new ConcurrentHashMap<>(); - private @Nullable BluetoothDeviceSnapshot latestSnapshot; + private ExpiringCache latestSnapshot = new ExpiringCache<>(Duration.ofMinutes(1), + () -> null); /** * This is meant to be used as part of a Map.compute function @@ -209,7 +212,7 @@ public class BluetoothDiscoveryService extends AbstractDiscoveryService implemen CompletableFuture future = null; BluetoothDeviceSnapshot snapshot = new BluetoothDeviceSnapshot(device); - BluetoothDeviceSnapshot latestSnapshot = this.latestSnapshot; + BluetoothDeviceSnapshot latestSnapshot = this.latestSnapshot.getValue(); if (latestSnapshot != null) { snapshot.merge(latestSnapshot); @@ -236,7 +239,7 @@ public class BluetoothDiscoveryService extends AbstractDiscoveryService implemen } } } - this.latestSnapshot = snapshot; + this.latestSnapshot.putValue(snapshot); if (future == null) { // we pass in the snapshot since it acts as a delegate for the device. It will also retain any new