Skip to content

Commit

Permalink
Merge pull request #67 from sharpbrick/issue-65-simpler-discovery
Browse files Browse the repository at this point in the history
Make DiscoveryAsync more user friendly

#65 non-breaking
  • Loading branch information
tthiery authored Jul 30, 2020
2 parents 988b2f8 + 3607f65 commit 5dd4123
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 18 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,23 @@ var serviceProvider = new ServiceCollection()

var host = serviceProvider.GetService<PoweredUpHost>();

var hub = await host.DiscoverAsync<TechnicMediumHub>(); // starting version 2.1
await hub.ConnectAsync();
````

## Discovering Hubs for UI
````csharp
var host = serviceProvider.GetService<PoweredUpHost>();

var cts = new CancellationTokenSource();
host.Discover(async hub =>
{
await hub.ConnectAsync();

Console.WriteLine(hub.AdvertisingName);
Console.WriteLine(hub.SystemType.ToString());
await hub.ConnectAsync(); // to get some more properties from it
cts.Cancel();
Console.WriteLine("Press RETURN to continue");
// show in UI
}, cts.Token);

Console.WriteLine("Press RETURN to cancel Scanning");
Console.ReadLine();

cts.Cancel();
// Cancel Button => cts.Cancel();
````

## Sending Commands to Ports and Devices of a Hub
Expand All @@ -56,7 +57,7 @@ See source code in `examples/SharpBrick.PoweredUp.Examples` for more examples.
````csharp
// do hub discovery before
using (var technicMediumHub = host.FindByType<TechnicMediumHub>())
using (var technicMediumHub = hub as TechnicMediumHub)
{
// optionally verify if everything is wired up correctly (v2.0 onwards)
await technicMediumHub.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
Expand Down
8 changes: 5 additions & 3 deletions examples/SharpBrick.PoweredUp.Examples/BaseExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public virtual void Configure(IServiceCollection serviceCollection)
.AddPoweredUp();
}

public void InitHostAndDiscover(bool enableTrace)
public async Task InitHostAndDiscoverAsync(bool enableTrace)
{
InitHost(enableTrace);
Discover(enableTrace);
await DiscoverAsync(enableTrace);
}

public virtual void Discover(bool enableTrace)
public virtual Task DiscoverAsync(bool enableTrace)
{
var logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger("Main");

Expand Down Expand Up @@ -67,6 +67,8 @@ public virtual void Discover(bool enableTrace)
cts.Cancel();

selectedHub = result;

return Task.CompletedTask;
}

public void InitHost(bool enableTrace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public class ExampleBluetoothByKnownAddress : BaseExample
public TechnicMediumHub DirectlyConnectedHub { get; private set; }

// device needs to be switched on!
public override void Discover(bool enableTrace)
public override async Task DiscoverAsync(bool enableTrace)
{
var hub = host.Create<TechnicMediumHub>(ChangeMe_BluetoothAddress);

selectedHub = DirectlyConnectedHub = hub;

hub.ConnectAsync().Wait();
await hub.ConnectAsync();
}

public override async Task ExecuteAsync()
Expand Down
41 changes: 41 additions & 0 deletions examples/SharpBrick.PoweredUp.Examples/ExampleDiscoverByType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Threading.Tasks;
using SharpBrick.PoweredUp;

namespace Example
{
public class ExampleDiscoverByType : BaseExample
{
public TechnicMediumHub DirectlyConnectedHub { get; private set; }

// device needs to be switched on!
public override async Task DiscoverAsync(bool enableTrace)
{
var hub = await host.DiscoverAsync<TechnicMediumHub>();

selectedHub = DirectlyConnectedHub = hub;

await hub.ConnectAsync();
}

public override async Task ExecuteAsync()
{
using (var technicMediumHub = DirectlyConnectedHub)
{
await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0x00, 0x00);

await Task.Delay(2000);

await technicMediumHub.RgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);

await Task.Delay(2000);

await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0xff, 0x00);

await Task.Delay(2000);

await technicMediumHub.SwitchOffAsync();
}
}
}
}
5 changes: 3 additions & 2 deletions examples/SharpBrick.PoweredUp.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ static async Task Main(string[] args)
//example = new Example.ExampleBluetoothByKnownAddress();
//example = new Example.ExampleBluetoothByName();
//example = new Example.ExampleSetHubProperty();
example = new Example.ExampleHubPropertyObserving();
//example = new Example.ExampleHubPropertyObserving();
example = new Example.ExampleDiscoverByType();

example.InitHostAndDiscover(enableTrace);
await example.InitHostAndDiscoverAsync(enableTrace);

if (example.selectedHub != null)
{
Expand Down
23 changes: 23 additions & 0 deletions src/SharpBrick.PoweredUp/PoweredUpHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,34 @@ public void Discover(Func<Hub, Task> onDiscovery, CancellationToken token = defa
_hubs.Add((deviceInfo, hub));
_logger.LogInformation($"Discovered log of type {hub.GetType().Name} with name '{deviceInfo.Name}' on Bluetooth Address '{deviceInfo.BluetoothAddress}'");
onDiscovery(hub).Wait();
}
}, token);
}

public async Task<THub> DiscoverAsync<THub>(CancellationToken token = default)
{
var tcs = new TaskCompletionSource<THub>();

Discover(hub =>
{
if (hub is THub tHub)
{
tcs.SetResult(tHub);
}
return Task.CompletedTask;
}, token);

var hub = await tcs.Task;

_logger.LogInformation($"End DiscoveryAsync for {typeof(THub).Name}");

return hub;
}

public THub Create<THub>(ulong bluetoothAddress) where THub : Hub
{
var hub = _hubFactory.Create<THub>();
Expand Down

0 comments on commit 5dd4123

Please sign in to comment.