-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathBaseExample.cs
More file actions
78 lines (57 loc) · 2.16 KB
/
Copy pathBaseExample.cs
File metadata and controls
78 lines (57 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SharpBrick.PoweredUp;
using SharpBrick.PoweredUp.Functions;
namespace Example;
public abstract class BaseExample
{
protected PoweredUpHost Host { get; set; }
protected IServiceProvider ServiceProvider { get; set; }
public Hub SelectedHub { get; set; }
public ILogger Log { get; private set; }
public abstract Task ExecuteAsync();
public virtual void Configure(IServiceCollection serviceCollection)
{
serviceCollection
.AddPoweredUp();
}
public async Task InitExampleAndDiscoverAsync(IServiceProvider serviceProvider, IConfiguration configuration)
{
ServiceProvider = serviceProvider;
Host = serviceProvider.GetService<PoweredUpHost>();
Log = serviceProvider.GetRequiredService<ILoggerFactory>().CreateLogger("Example");
var enableTrace = bool.TryParse(configuration["EnableTrace"], out var x) && x;
await DiscoverAsync(enableTrace);
}
public virtual Task DiscoverAsync(bool enableTrace)
{
Hub result = null;
Log.LogInformation("Finding Service");
var cts = new CancellationTokenSource();
Host.Discover(async hub =>
{
// add this when you are interested in a tracing of the message ("human readable")
if (enableTrace)
{
var tracer = hub.ServiceProvider.GetRequiredService<TraceMessages>();
await tracer.ExecuteAsync();
}
Log.LogInformation("Connecting to Hub");
await hub.ConnectAsync();
result = hub;
Log.LogInformation(hub.AdvertisingName);
Log.LogInformation(hub.SystemType.ToString());
cts.Cancel();
Log.LogInformation("Press RETURN to continue to the action");
}, cts.Token);
Log.LogInformation("Press RETURN to cancel Scanning");
Console.ReadLine();
cts.Cancel();
SelectedHub = result;
return Task.CompletedTask;
}
}