Skip to content

Commit 5dd4123

Browse files
authored
Merge pull request #67 from sharpbrick/issue-65-simpler-discovery
Make DiscoveryAsync more user friendly #65 non-breaking
2 parents 988b2f8 + 3607f65 commit 5dd4123

6 files changed

Lines changed: 86 additions & 18 deletions

File tree

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,23 @@ var serviceProvider = new ServiceCollection()
3131

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

34+
var hub = await host.DiscoverAsync<TechnicMediumHub>(); // starting version 2.1
35+
await hub.ConnectAsync();
36+
````
37+
38+
## Discovering Hubs for UI
39+
````csharp
40+
var host = serviceProvider.GetService<PoweredUpHost>();
41+
3442
var cts = new CancellationTokenSource();
3543
host.Discover(async hub =>
3644
{
37-
await hub.ConnectAsync();
38-
39-
Console.WriteLine(hub.AdvertisingName);
40-
Console.WriteLine(hub.SystemType.ToString());
45+
await hub.ConnectAsync(); // to get some more properties from it
4146
42-
cts.Cancel();
43-
Console.WriteLine("Press RETURN to continue");
47+
// show in UI
4448
}, cts.Token);
4549

46-
Console.WriteLine("Press RETURN to cancel Scanning");
47-
Console.ReadLine();
48-
49-
cts.Cancel();
50+
// Cancel Button => cts.Cancel();
5051
````
5152

5253
## Sending Commands to Ports and Devices of a Hub
@@ -56,7 +57,7 @@ See source code in `examples/SharpBrick.PoweredUp.Examples` for more examples.
5657
````csharp
5758
// do hub discovery before
5859
59-
using (var technicMediumHub = host.FindByType<TechnicMediumHub>())
60+
using (var technicMediumHub = hub as TechnicMediumHub)
6061
{
6162
// optionally verify if everything is wired up correctly (v2.0 onwards)
6263
await technicMediumHub.VerifyDeploymentModelAsync(modelBuilder => modelBuilder

examples/SharpBrick.PoweredUp.Examples/BaseExample.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ public virtual void Configure(IServiceCollection serviceCollection)
2424
.AddPoweredUp();
2525
}
2626

27-
public void InitHostAndDiscover(bool enableTrace)
27+
public async Task InitHostAndDiscoverAsync(bool enableTrace)
2828
{
2929
InitHost(enableTrace);
30-
Discover(enableTrace);
30+
await DiscoverAsync(enableTrace);
3131
}
3232

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

@@ -67,6 +67,8 @@ public virtual void Discover(bool enableTrace)
6767
cts.Cancel();
6868

6969
selectedHub = result;
70+
71+
return Task.CompletedTask;
7072
}
7173

7274
public void InitHost(bool enableTrace)

examples/SharpBrick.PoweredUp.Examples/ExampleBluetoothByKnownAddress.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ public class ExampleBluetoothByKnownAddress : BaseExample
1010
public TechnicMediumHub DirectlyConnectedHub { get; private set; }
1111

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

1717
selectedHub = DirectlyConnectedHub = hub;
1818

19-
hub.ConnectAsync().Wait();
19+
await hub.ConnectAsync();
2020
}
2121

2222
public override async Task ExecuteAsync()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using SharpBrick.PoweredUp;
4+
5+
namespace Example
6+
{
7+
public class ExampleDiscoverByType : BaseExample
8+
{
9+
public TechnicMediumHub DirectlyConnectedHub { get; private set; }
10+
11+
// device needs to be switched on!
12+
public override async Task DiscoverAsync(bool enableTrace)
13+
{
14+
var hub = await host.DiscoverAsync<TechnicMediumHub>();
15+
16+
selectedHub = DirectlyConnectedHub = hub;
17+
18+
await hub.ConnectAsync();
19+
}
20+
21+
public override async Task ExecuteAsync()
22+
{
23+
using (var technicMediumHub = DirectlyConnectedHub)
24+
{
25+
await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0x00, 0x00);
26+
27+
await Task.Delay(2000);
28+
29+
await technicMediumHub.RgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);
30+
31+
await Task.Delay(2000);
32+
33+
await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0xff, 0x00);
34+
35+
await Task.Delay(2000);
36+
37+
await technicMediumHub.SwitchOffAsync();
38+
}
39+
}
40+
}
41+
}

examples/SharpBrick.PoweredUp.Examples/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ static async Task Main(string[] args)
2929
//example = new Example.ExampleBluetoothByKnownAddress();
3030
//example = new Example.ExampleBluetoothByName();
3131
//example = new Example.ExampleSetHubProperty();
32-
example = new Example.ExampleHubPropertyObserving();
32+
//example = new Example.ExampleHubPropertyObserving();
33+
example = new Example.ExampleDiscoverByType();
3334

34-
example.InitHostAndDiscover(enableTrace);
35+
await example.InitHostAndDiscoverAsync(enableTrace);
3536

3637
if (example.selectedHub != null)
3738
{

src/SharpBrick.PoweredUp/PoweredUpHost.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,34 @@ public void Discover(Func<Hub, Task> onDiscovery, CancellationToken token = defa
5353

5454
_hubs.Add((deviceInfo, hub));
5555

56+
_logger.LogInformation($"Discovered log of type {hub.GetType().Name} with name '{deviceInfo.Name}' on Bluetooth Address '{deviceInfo.BluetoothAddress}'");
57+
5658
onDiscovery(hub).Wait();
5759
}
5860
}, token);
5961
}
6062

63+
public async Task<THub> DiscoverAsync<THub>(CancellationToken token = default)
64+
{
65+
var tcs = new TaskCompletionSource<THub>();
66+
67+
Discover(hub =>
68+
{
69+
if (hub is THub tHub)
70+
{
71+
tcs.SetResult(tHub);
72+
}
73+
74+
return Task.CompletedTask;
75+
}, token);
76+
77+
var hub = await tcs.Task;
78+
79+
_logger.LogInformation($"End DiscoveryAsync for {typeof(THub).Name}");
80+
81+
return hub;
82+
}
83+
6184
public THub Create<THub>(ulong bluetoothAddress) where THub : Hub
6285
{
6386
var hub = _hubFactory.Create<THub>();

0 commit comments

Comments
 (0)