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

TechnicMotor TestScript (on master) #168

Merged
merged 3 commits into from
Apr 8, 2021
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
17 changes: 16 additions & 1 deletion powered-up.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31112.23
MinimumVisualStudioVersion = 15.0.26124.0
Expand All @@ -22,6 +22,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpBrick.PoweredUp.Mobile
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpBrick.PoweredUp.BlueGigaBLE", "src\SharpBrick.PoweredUp.BlueGigaBLE\SharpBrick.PoweredUp.BlueGigaBLE.csproj", "{E0DC0096-D7F1-4995-833D-9A6C0C3F8F98}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpBrick.PoweredUp.TestScript", "test\SharpBrick.PoweredUp.TestScript\SharpBrick.PoweredUp.TestScript.csproj", "{2A100817-6E86-4E58-8183-0EA7F49C0848}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -116,6 +118,18 @@ Global
{E0DC0096-D7F1-4995-833D-9A6C0C3F8F98}.Release|x64.Build.0 = Release|Any CPU
{E0DC0096-D7F1-4995-833D-9A6C0C3F8F98}.Release|x86.ActiveCfg = Release|Any CPU
{E0DC0096-D7F1-4995-833D-9A6C0C3F8F98}.Release|x86.Build.0 = Release|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Debug|x64.ActiveCfg = Debug|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Debug|x64.Build.0 = Debug|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Debug|x86.ActiveCfg = Debug|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Debug|x86.Build.0 = Debug|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Release|Any CPU.Build.0 = Release|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Release|x64.ActiveCfg = Release|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Release|x64.Build.0 = Release|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Release|x86.ActiveCfg = Release|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -128,6 +142,7 @@ Global
{E2D7D98D-9F20-4761-B507-D379A530E77D} = {62C31C3D-8ACF-4ED3-A3D8-225536F3AC6D}
{F66A2B09-84B6-477D-9B15-926E771C7D80} = {62C31C3D-8ACF-4ED3-A3D8-225536F3AC6D}
{E0DC0096-D7F1-4995-833D-9A6C0C3F8F98} = {62C31C3D-8ACF-4ED3-A3D8-225536F3AC6D}
{2A100817-6E86-4E58-8183-0EA7F49C0848} = {39B30145-497F-4AEB-A014-BBF27DA0651A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {34AA1641-ACED-43ED-A0DD-BA88E43A67A8}
Expand Down
12 changes: 12 additions & 0 deletions src/SharpBrick.PoweredUp/Deployment/HubExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ public static async Task VerifyDeploymentModelAsync(this Hub self, Action<Deploy

var model = BuildModel(configure);

await VerifyDeploymentModelAsync(self, model);
}

/// <summary>
/// Verifies the deployment model and waits till it reaches zero deployment errors.
/// </summary>
/// <param name="self"></param>
/// <param name="configure">Builder infrastructure for the deployment model</param>
/// <returns></returns>
public static async Task VerifyDeploymentModelAsync(this Hub self, DeploymentModel model)
{

var awaitable = self.VerifyObservable(model)
.Do(LogErrors(self))
.Where(x => x.Length == 0)
Expand Down
19 changes: 13 additions & 6 deletions src/SharpBrick.PoweredUp/PoweredUpHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,31 @@ public void Discover(Func<Hub, Task> onDiscovery, CancellationToken token = defa
}, token);
}

public async Task<THub> DiscoverAsync<THub>(CancellationToken token = default)
public async Task<THub> DiscoverAsync<THub>(CancellationToken token = default) where THub : class
=> await DiscoverInternalAsync(typeof(THub), token) as THub;

public async Task<Hub> DiscoverAsync(Type hubType, CancellationToken token = default)
=> await DiscoverInternalAsync(hubType, token);

private async Task<Hub> DiscoverInternalAsync(Type hubType, CancellationToken token)
{
var tcs = new TaskCompletionSource<THub>();
var tcs = new TaskCompletionSource<Hub>();

Discover(hub =>
{
if (hub is THub tHub)
var currentHubType = hub.GetType();

if (currentHubType == hubType)
{
tcs.SetResult(tHub);
tcs.SetResult(hub);
}

return Task.CompletedTask;
}, token);

var hub = await tcs.Task;

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

_logger.LogInformation($"End DiscoveryAsync for {hubType.Name}");
return hub;
}

Expand Down
11 changes: 11 additions & 0 deletions test/SharpBrick.PoweredUp.TestScript/ITestScript.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;
using SharpBrick.PoweredUp.Deployment;

namespace SharpBrick.PoweredUp.TestScript
{
public interface ITestScript
{
void DefineDeploymentModel(DeploymentModelBuilder builder);
Task ExecuteScriptAsync(Hub hub, TestScriptExecutionContext context);
}
}
91 changes: 91 additions & 0 deletions test/SharpBrick.PoweredUp.TestScript/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SharpBrick.PoweredUp.Deployment;
using SharpBrick.PoweredUp.Hubs;

namespace SharpBrick.PoweredUp.TestScript
{
class Program
{
static async Task Main(string[] args)
{
var serviceProvider = new ServiceCollection()
.AddLogging(builder =>
{
builder.AddConsole();
})
.AddPoweredUp()
.AddWinRTBluetooth() // using WinRT Bluetooth on Windows (separate NuGet SharpBrick.PoweredUp.WinRT)
.BuildServiceProvider();

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

IEnumerable<ITestScript> scripts = new ITestScript[] {
new TechnicMotorTestScript<TechnicLargeLinearMotor>(),
};

var context = new TestScriptExecutionContext(serviceProvider.GetService<ILogger<TestScriptExecutionContext>>());

foreach (var script in scripts)
{
// Test Script
context.Log.LogInformation($"Execute Script {script.GetType().Name}");

// build deployment model
var model = BbuildDeploymentModel(script);
PrintModel(context, model);

// Accept to execute Test Script
var executeTest = await context.ConfirmAsync("> Confirm to execute Test Script");

if (executeTest)
{
context.Log.LogInformation("> Discovering & Connecting Hub");
var hubType = HubFactory.GetTypeFromSystemType(model.Hubs[0].HubType ?? throw new InvalidOperationException("Specify the hub type in the test script."));
using var hub = await host.DiscoverAsync(hubType);
await hub.ConnectAsync();

context.Log.LogInformation("> Verifying Deployment Model (fix it if necessary)");
await hub.VerifyDeploymentModelAsync(model);

context.Log.LogInformation("> Start Test Script");
await script.ExecuteScriptAsync(hub, context);

context.Log.LogInformation("> Switch Off Hub");
await hub.SwitchOffAsync();
}
else
{
context.Log.LogWarning($"> User decided not to execute Test Script");
}
}

}

private static void PrintModel(TestScriptExecutionContext context, DeploymentModel model)
{
context.Log.LogInformation($"> Deployment Model of Test Script");

foreach (var hub in model.Hubs)
{
context.Log.LogInformation($" > Hub: {hub.HubType}");

foreach (var device in hub.Devices)
{
context.Log.LogInformation($" > Device: {device.DeviceType} @ {device.PortId}");
}
}
}

private static DeploymentModel BbuildDeploymentModel(ITestScript script)
{
var builder = new DeploymentModelBuilder();
script.DefineDeploymentModel(builder);
var model = builder.Build();
return model;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\SharpBrick.PoweredUp\SharpBrick.PoweredUp.csproj" />
<ProjectReference Include="..\..\src\SharpBrick.PoweredUp.WinRT\SharpBrick.PoweredUp.WinRT.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
</ItemGroup>

</Project>
Loading