Skip to content

Commit 454abe6

Browse files
committed
Add observable deployment monitoring
sharpbrick#36 non-breaking
1 parent f88e924 commit 454abe6

File tree

7 files changed

+72
-23
lines changed

7 files changed

+72
-23
lines changed

examples/SharpBrick.PoweredUp.Examples/ExampleMixedBag.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static async Task ExecuteAsync(PoweredUpHost host, IServiceProvider servi
1515

1616
using (var technicMediumHub = host.FindByType<TechnicMediumHub>())
1717
{
18-
await technicMediumHub.VerifyAsync(modelBuilder => modelBuilder
18+
await technicMediumHub.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
1919
.AddHub<TechnicMediumHub>(hubBuilder => hubBuilder
2020
.AddDevice<TechnicXLargeLinearMotor>(technicMediumHub.A)
2121
)

examples/SharpBrick.PoweredUp.Examples/ExampleMotorControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static async Task ExecuteAsync(PoweredUpHost host, IServiceProvider servi
1010
{
1111
using (var technicMediumHub = host.FindByType<TechnicMediumHub>())
1212
{
13-
await technicMediumHub.VerifyAsync(modelBuilder => modelBuilder
13+
await technicMediumHub.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
1414
.AddHub<TechnicMediumHub>(hubBuilder => hubBuilder
1515
.AddDevice<TechnicXLargeLinearMotor>(technicMediumHub.A)
1616
)

examples/SharpBrick.PoweredUp.Examples/ExampleMotorInputAbsolutePosition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static async Task ExecuteAsync(PoweredUpHost host, IServiceProvider servi
1515

1616
using (var technicMediumHub = host.FindByType<TechnicMediumHub>())
1717
{
18-
await technicMediumHub.VerifyAsync(modelBuilder => modelBuilder
18+
await technicMediumHub.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
1919
.AddHub<TechnicMediumHub>(hubBuilder => hubBuilder
2020
.AddDevice<TechnicXLargeLinearMotor>(technicMediumHub.A)
2121
)

examples/SharpBrick.PoweredUp.Examples/ExampleMotorInputCombinedMode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static async Task ExecuteAsync(PoweredUpHost host, IServiceProvider servi
1515

1616
using (var technicMediumHub = host.FindByType<TechnicMediumHub>())
1717
{
18-
await technicMediumHub.VerifyAsync(modelBuilder => modelBuilder
18+
await technicMediumHub.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
1919
.AddHub<TechnicMediumHub>(hubBuilder => hubBuilder
2020
.AddDevice<TechnicXLargeLinearMotor>(technicMediumHub.A)
2121
)

examples/SharpBrick.PoweredUp.Examples/ExampleMotorVirtualPort.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static async Task ExecuteAsync(PoweredUpHost host, IServiceProvider servi
1010
{
1111
using (var technicMediumHub = host.FindByType<TechnicMediumHub>())
1212
{
13-
await technicMediumHub.VerifyAsync(modelBuilder => modelBuilder
13+
await technicMediumHub.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
1414
.AddHub<TechnicMediumHub>(hubBuilder => hubBuilder
1515
.AddDevice<TechnicXLargeLinearMotor>(0)
1616
.AddDevice<TechnicXLargeLinearMotor>(2)

src/SharpBrick.PoweredUp/Deployment/DeploymentModel.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Threading.Tasks;
54
using SharpBrick.PoweredUp.Protocol;
65

76
namespace SharpBrick.PoweredUp.Deployment
@@ -20,14 +19,14 @@ public DeploymentModel(DeploymentHubModel[] hubs)
2019
Hubs = hubs;
2120
}
2221

23-
public Task<DeploymentModelError[]> VerifyAsync(params IPoweredUpProtocol[] protocols)
22+
public DeploymentModelError[] Verify(params IPoweredUpProtocol[] protocols)
2423
{
2524
//TODO: match best hub with best protocol
2625

27-
return VerifyAsync(protocols[0], 0x00, Hubs[0]);
26+
return Verify(protocols[0], 0x00, Hubs[0]);
2827
}
2928

30-
public Task<DeploymentModelError[]> VerifyAsync(IPoweredUpProtocol protocol, byte hubId, DeploymentHubModel hubModel)
29+
public DeploymentModelError[] Verify(IPoweredUpProtocol protocol, byte hubId, DeploymentHubModel hubModel)
3130
{
3231
if (protocol is null)
3332
{
@@ -75,7 +74,7 @@ public Task<DeploymentModelError[]> VerifyAsync(IPoweredUpProtocol protocol, byt
7574
}
7675
)));
7776

78-
return Task.FromResult(result.ToArray());
77+
return result.ToArray();
7978
}
8079
}
8180
}
Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Reactive.Linq;
23
using System.Threading.Tasks;
34
using Microsoft.Extensions.DependencyInjection;
45
using Microsoft.Extensions.Logging;
@@ -8,37 +9,86 @@ namespace SharpBrick.PoweredUp
89
{
910
public static class HubExtensions
1011
{
11-
public static async Task<DeploymentModelError[]> VerifyAsync(this Hub self, Action<DeploymentModelBuilder> configure)
12+
public static async Task VerifyDeploymentModelAsync(this Hub self, Action<DeploymentModelBuilder> configure)
1213
{
1314
if (self is null)
1415
{
1516
throw new ArgumentNullException(nameof(self));
1617
}
1718

18-
if (configure is null)
19-
{
20-
throw new ArgumentNullException(nameof(configure));
21-
}
19+
var model = BuildModel(configure);
2220

23-
var deploymentModelBuilder = new DeploymentModelBuilder();
21+
var awaitable = self.VerifyObservable(model)
22+
.Do(LogErrors(self))
23+
.Where(x => x.Length == 0)
24+
.FirstAsync()
25+
.GetAwaiter();
2426

25-
configure(deploymentModelBuilder);
27+
var firstErors = model.Verify(self.Protocol);
2628

27-
var model = deploymentModelBuilder.Build();
29+
if (firstErors.Length > 0)
30+
{
31+
LogErrors(self)(firstErors);
2832

29-
var errors = await model.VerifyAsync(self.Protocol);
33+
await awaitable;
34+
}
35+
}
3036

31-
if (errors.Length > 0)
37+
private static Action<DeploymentModelError[]> LogErrors(Hub self)
38+
{
39+
return errors =>
3240
{
3341
var logger = self.ServiceProvider.GetService<ILoggerFactory>().CreateLogger<Hub>();
42+
if (errors.Length > 0)
43+
{
3444

35-
foreach (var error in errors)
45+
foreach (var error in errors)
46+
{
47+
logger.LogError($"ERROR {error.ErrorCode} @ {error.HubId}-{error.PortId}: {error.Message}");
48+
}
49+
}
50+
else
3651
{
37-
logger.LogError($"ERROR {error.ErrorCode} @ {error.HubId}-{error.PortId}: {error.Message}");
52+
logger.LogInformation("Deployment Validation Successful");
3853
}
54+
};
55+
}
56+
57+
public static IObservable<DeploymentModelError[]> VerifyObservable(this Hub self, Action<DeploymentModelBuilder> configure)
58+
{
59+
if (self is null)
60+
{
61+
throw new ArgumentNullException(nameof(self));
3962
}
4063

41-
return errors;
64+
var model = BuildModel(configure);
65+
66+
return self.VerifyObservable(model);
67+
}
68+
69+
public static IObservable<DeploymentModelError[]> VerifyObservable(this Hub self, DeploymentModel model)
70+
{
71+
if (model is null)
72+
{
73+
throw new ArgumentNullException(nameof(model));
74+
}
75+
76+
return self.Protocol.UpstreamMessages.Select(msg => model.Verify(self.Protocol));
77+
}
78+
79+
private static DeploymentModel BuildModel(Action<DeploymentModelBuilder> configure)
80+
{
81+
if (configure is null)
82+
{
83+
throw new ArgumentNullException(nameof(configure));
84+
}
85+
86+
var deploymentModelBuilder = new DeploymentModelBuilder();
87+
88+
configure(deploymentModelBuilder);
89+
90+
var model = deploymentModelBuilder.Build();
91+
return model;
4292
}
4393
}
4494
}

0 commit comments

Comments
 (0)