Skip to content

Commit

Permalink
Prevent multiple watchers for same device
Browse files Browse the repository at this point in the history
When a Bluetooth device is found, the watcher for the device is now added to a dictionary keyed by MAC address, to prevent the possibility of duplicate watchers for the same device.
  • Loading branch information
wazzamatazz committed May 30, 2021
1 parent b3d56dc commit 1ecf0eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions build/version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Major": 0,
"Minor": 4,
"Minor": 5,
"Patch": 0,
"PreRelease": "alpha"
"PreRelease": ""
}
13 changes: 9 additions & 4 deletions src/NRuuviTag.Listener.Linux/BlueZListener.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -105,7 +106,7 @@ CancellationToken cancellationToken
using (var @lock = new SemaphoreSlim(1, 1)) {

// Registrations for devices that we are observing.
var watchers = new HashSet<IDisposable>();
var watchers = new Dictionary<string, IDisposable>(StringComparer.OrdinalIgnoreCase);

// Adds a watcher for the specified device so that we can emit new samples when the
// device properties change.
Expand All @@ -116,9 +117,13 @@ CancellationToken cancellationToken

await @lock.WaitAsync(cancellationToken).ConfigureAwait(false);
try {
watchers.Add(await device.WatchPropertiesAsync(changes => {
if (watchers.ContainsKey(properties.Address)) {
return;
}

watchers[properties.Address] = await device.WatchPropertiesAsync(changes => {
UpdateDeviceProperties(properties, changes);
}).ConfigureAwait(false));
}).ConfigureAwait(false);
}
finally {
@lock.Release();
Expand Down Expand Up @@ -169,7 +174,7 @@ CancellationToken cancellationToken
// Dispose of the watcher registrations.
await @lock.WaitAsync().ConfigureAwait(false);
try {
foreach (var item in watchers) {
foreach (var item in watchers.Values) {
item.Dispose();
}
watchers.Clear();
Expand Down

0 comments on commit 1ecf0eb

Please sign in to comment.