Skip to content

Commit

Permalink
Change PoweredUpHost to initialize scope
Browse files Browse the repository at this point in the history
#46 non-breaking
  • Loading branch information
tthiery committed Aug 1, 2020
1 parent c2f5385 commit 0b0406e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/SharpBrick.PoweredUp/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public static class IServiceCollectionExtensions
{
public static IServiceCollection AddPoweredUp(this IServiceCollection self)
=> self
.AddSingleton<IHubFactory, HubFactory>()
.AddSingleton<IDeviceFactory, DeviceFactory>()
.AddSingleton<PoweredUpHost>()
.AddScoped<IHubFactory, HubFactory>()
.AddScoped<IDeviceFactory, DeviceFactory>()
.AddTransient<LinearMidCalibration>();
}
}
24 changes: 16 additions & 8 deletions src/SharpBrick.PoweredUp/PoweredUpHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ public class PoweredUpHost
private readonly IPoweredUpBluetoothAdapter _bluetoothAdapter;
public IServiceProvider ServiceProvider { get; }
private readonly ILogger<PoweredUpHost> _logger;
private readonly IHubFactory _hubFactory;
private ConcurrentBag<(PoweredUpBluetoothDeviceInfo Info, Hub Hub)> _hubs = new ConcurrentBag<(PoweredUpBluetoothDeviceInfo, Hub)>();

public IEnumerable<Hub> Hubs => _hubs.Select(i => i.Hub);

public PoweredUpHost(IPoweredUpBluetoothAdapter bluetoothAdapter, IServiceProvider serviceProvider)
public PoweredUpHost(IPoweredUpBluetoothAdapter bluetoothAdapter, IServiceProvider serviceProvider, ILogger<PoweredUpHost> logger)
{
_bluetoothAdapter = bluetoothAdapter;
ServiceProvider = serviceProvider;
_logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger<PoweredUpHost>();
_hubFactory = serviceProvider.GetService<IHubFactory>();
_logger = logger;
}

//ctr with auto-finding bt adapter
Expand All @@ -48,8 +46,7 @@ public void Discover(Func<Hub, Task> onDiscovery, CancellationToken token = defa
{
if (!_hubs.Any(i => i.Item1.BluetoothAddress == deviceInfo.BluetoothAddress))
{
var hub = _hubFactory.CreateByBluetoothManufacturerData(deviceInfo.ManufacturerData);
hub.ConnectWithBluetoothAdapter(_bluetoothAdapter, deviceInfo.BluetoothAddress);
var hub = CreateProtocolScope(deviceInfo.BluetoothAddress, hubFactory => hubFactory.CreateByBluetoothManufacturerData(deviceInfo.ManufacturerData));
_hubs.Add((deviceInfo, hub));
Expand Down Expand Up @@ -83,8 +80,7 @@ public async Task<THub> DiscoverAsync<THub>(CancellationToken token = default)

public THub Create<THub>(ulong bluetoothAddress) where THub : Hub
{
var hub = _hubFactory.Create<THub>();
hub.ConnectWithBluetoothAdapter(_bluetoothAdapter, bluetoothAddress);
var hub = CreateProtocolScope(bluetoothAddress, hubFactory => hubFactory.Create<THub>());

_hubs.Add((new PoweredUpBluetoothDeviceInfo()
{
Expand All @@ -95,5 +91,17 @@ public THub Create<THub>(ulong bluetoothAddress) where THub : Hub

return hub;
}

private THub CreateProtocolScope<THub>(ulong bluetoothAddress, Func<IHubFactory, THub> factory) where THub : Hub
{
var scopedServiceProvider = ServiceProvider.CreateScope().ServiceProvider;

var hubFactory = scopedServiceProvider.GetService<IHubFactory>();

var hub = factory(hubFactory);
hub.ConnectWithBluetoothAdapter(_bluetoothAdapter, bluetoothAddress);

return hub;
}
}
}

0 comments on commit 0b0406e

Please sign in to comment.