Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initialization of protocol in CLI #100

Merged
merged 1 commit into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}