Skip to content

Commit

Permalink
Fix initialization of protocol in CLI (#100)
Browse files Browse the repository at this point in the history
#98 non-breaking
  • Loading branch information
tthiery authored Oct 5, 2020
1 parent 290a929 commit 0c7b07b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/SharpBrick.PoweredUp.Cli/Commands/DevicesList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public DevicesList(ILegoWirelessProtocol protocol, DiscoverPorts discoverPorts)
this.protocol = protocol ?? throw new ArgumentNullException(nameof(protocol));
this.discoverPorts = discoverPorts ?? throw new ArgumentNullException(nameof(discoverPorts));
}
public async Task ExecuteAsync()
public async Task ExecuteAsync(SystemType knownSystemType)
{
Console.WriteLine("Discover Ports. Receiving Messages ...");

await protocol.ConnectAsync(); // registering to bluetooth notification
await protocol.ConnectAsync(knownSystemType); // registering to bluetooth notification

await Task.Delay(2000); // await ports to be announced initially by device.

Expand Down
4 changes: 2 additions & 2 deletions src/SharpBrick.PoweredUp.Cli/Commands/DumpStaticPortInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public DumpStaticPortInfo(ILegoWirelessProtocol protocol, DiscoverPorts discover
this.protocol = protocol ?? throw new ArgumentNullException(nameof(protocol));
this.discoverPorts = discoverPorts ?? throw new ArgumentNullException(nameof(discoverPorts));
}
public async Task ExecuteAsync(byte portId)
public async Task ExecuteAsync(SystemType knownSystemType, byte portId)
{
Console.WriteLine($"Discover Port {portId}. Receiving Messages ...");

await protocol.ConnectAsync(); // registering to bluetooth notification
await protocol.ConnectAsync(knownSystemType); // registering to bluetooth notification

await Task.Delay(2000); // await ports to be announced initially by device.

Expand Down
20 changes: 11 additions & 9 deletions src/SharpBrick.PoweredUp.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static async Task Main(string[] args)
var enableTrace = bool.TryParse(traceOption.Value(), out var x) ? x : false;
var serviceProvider = CreateServiceProvider(enableTrace);
ulong bluetoothAddress = FindAndSelectHub(serviceProvider.GetService<IPoweredUpBluetoothAdapter>());
(ulong bluetoothAddress, SystemType systemType) = FindAndSelectHub(serviceProvider.GetService<IPoweredUpBluetoothAdapter>());
if (bluetoothAddress == 0)
return;
Expand All @@ -50,7 +50,7 @@ static async Task Main(string[] args)
var deviceListCli = scopedServiceProvider.GetService<DevicesList>(); // ServiceLocator ok: transient factory
await deviceListCli.ExecuteAsync();
await deviceListCli.ExecuteAsync(systemType);
}
});
});
Expand All @@ -67,7 +67,7 @@ static async Task Main(string[] args)
var enableTrace = bool.TryParse(traceOption.Value(), out var x) ? x : false;
var serviceProvider = CreateServiceProvider(enableTrace);
ulong bluetoothAddress = FindAndSelectHub(serviceProvider.GetService<IPoweredUpBluetoothAdapter>());
(ulong bluetoothAddress, SystemType systemType) = FindAndSelectHub(serviceProvider.GetService<IPoweredUpBluetoothAdapter>());
if (bluetoothAddress == 0)
return;
Expand All @@ -85,7 +85,7 @@ static async Task Main(string[] args)
var port = byte.Parse(portOption.Value());
await dumpStaticPortInfoCommand.ExecuteAsync(port);
await dumpStaticPortInfoCommand.ExecuteAsync(systemType, port);
}
});
});
Expand Down Expand Up @@ -125,9 +125,10 @@ public static async Task AddTraceWriterAsync(IServiceProvider serviceProvider, b
}
}

private static ulong FindAndSelectHub(IPoweredUpBluetoothAdapter poweredUpBluetoothAdapter)
private static (ulong bluetoothAddress, SystemType systemType) FindAndSelectHub(IPoweredUpBluetoothAdapter poweredUpBluetoothAdapter)
{
ulong result = 0;
ulong resultBluetooth = 0;
SystemType resultSystemType = default;
var devices = new ConcurrentBag<(int key, ulong bluetoothAddresss, PoweredUpHubManufacturerData deviceType)>();
var cts = new CancellationTokenSource();
int idx = 1;
Expand Down Expand Up @@ -155,15 +156,16 @@ private static ulong FindAndSelectHub(IPoweredUpBluetoothAdapter poweredUpBlueto
{
var selected = devices.FirstOrDefault(kv => kv.key == key);

result = selected.bluetoothAddresss; // default is 0
resultBluetooth = selected.bluetoothAddresss; // default is 0
resultSystemType = (SystemType)selected.deviceType;

if (result != default)
if (resultBluetooth != default)
{
Console.WriteLine($"Selected {selected.deviceType} with key {selected.key}");
}
}

return result;
return (resultBluetooth, resultSystemType);
}
}
}

0 comments on commit 0c7b07b

Please sign in to comment.