From 3da144d7037b983f98359517145b92f0c727872a Mon Sep 17 00:00:00 2001 From: "T. Thiery" Date: Thu, 22 Apr 2021 21:44:11 +0200 Subject: [PATCH] Added multiple target frameworks to all CLI projects - Fixed Examples, CLI, TestScript Projects - Fixed the documentation #155 breaking --- .vscode/launch.json | 11 +- README.md | 30 ++-- docs/bleadapters/BlueGigaBLED112.md | 11 +- .../BaseExample.cs | 60 +------ .../ExampleTwoHubsMotorControl.cs | 1 - .../SharpBrick.PoweredUp.Examples/Program.cs | 149 ++++++++++-------- .../SharpBrick.PoweredUp.Examples.csproj | 13 +- .../SharpBrick.PoweredUp.BlueGigaBLE.csproj | 8 +- src/SharpBrick.PoweredUp.Cli/Program.cs | 73 ++++++--- .../SharpBrick.PoweredUp.Cli.csproj | 17 +- .../Program.cs | 40 ++++- .../SharpBrick.PoweredUp.TestScript.csproj | 16 +- .../TestScriptExecutionContext.cs | 4 +- 13 files changed, 249 insertions(+), 184 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 7e2d1e2..1c15e68 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,9 +9,10 @@ "type": "coreclr", "request": "launch", "preLaunchTask": "build", - "program": "${workspaceFolder}/examples/SharpBrick.PoweredUp.Examples/bin/Debug/netcoreapp3.1/SharpBrick.PoweredUp.Examples.dll", + "program": "${workspaceFolder}/examples/SharpBrick.PoweredUp.Examples/bin/Debug/net5.0-windows10.0.19041.0/SharpBrick.PoweredUp.Examples.dll", "args": [ - "--trace" + "--TraceDebug", + "true" ], "cwd": "${workspaceFolder}/examples/SharpBrick.PoweredUp.Examples", "console": "integratedTerminal", @@ -22,7 +23,7 @@ "type": "coreclr", "request": "launch", "preLaunchTask": "build", - "program": "${workspaceFolder}/src/SharpBrick.PoweredUp.Cli/bin/Debug/netcoreapp3.1/SharpBrick.PoweredUp.Cli.dll", + "program": "${workspaceFolder}/src/SharpBrick.PoweredUp.Cli/bin/Debug/net5.0-windows10.0.19041.0/SharpBrick.PoweredUp.Cli.dll", "args": [ "device", "list" @@ -36,7 +37,7 @@ "type": "coreclr", "request": "launch", "preLaunchTask": "build", - "program": "${workspaceFolder}/src/SharpBrick.PoweredUp.Cli/bin/Debug/netcoreapp3.1/SharpBrick.PoweredUp.Cli.dll", + "program": "${workspaceFolder}/src/SharpBrick.PoweredUp.Cli/bin/Debug/net5.0-windows10.0.19041.0/SharpBrick.PoweredUp.Cli.dll", "args": [ "device", "dump-static-port", @@ -52,7 +53,7 @@ "type": "coreclr", "request": "launch", "preLaunchTask": "build", - "program": "${workspaceFolder}/src/SharpBrick.PoweredUp.Cli/bin/Debug/netcoreapp3.1/SharpBrick.PoweredUp.Cli.dll", + "program": "${workspaceFolder}/src/SharpBrick.PoweredUp.Cli/bin/Debug/net5.0-windows10.0.19041.0/SharpBrick.PoweredUp.Cli.dll", "args": [ "device", "pretty-print", diff --git a/README.md b/README.md index ea36a40..16d56a0 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ SharpBrick.PoweredUp is a .NET implementation of the Bluetooth Low Energy Protoc - **Virtual Port Creation**: Combine multiple devices of the same type into a virtual combined port. This allows synchronous access to multiple devices using the same message (e.g. using two motors for driving). - **Deployment Model Verification**: The SDK includes a model builder and a verification method to ensure that the wired devies are correctly reflecting the expectations in the program. - **Tools**: The `poweredup` CLI includes a device list feature, enumerating the metadata properties of the LEGO Wireless Protocol. -- **Support for multiple Bluetooth Stacks**: Use any of Microsoft WinRT, BlueGiga Bluetooth (Silicon's Lab BlueGiga-adapter (for example BLED112) and Xamarin using BLE.Plugin to talk to your Lego hubs. +- **Support for multiple Bluetooth Stacks**: Use either Microsoft WinRT, BlueGiga Bluetooth (Silicon's Lab BlueGiga-adapter, for example BLED112) or Xamarin using BLE.Plugin to talk to your Lego hubs. # Examples @@ -29,6 +29,9 @@ Additional to code fragments below, look into the `examples/SharpBrick.PoweredUp ````csharp using SharpBrick.PoweredUp; + +using Microsoft.Extensions.DependencyInjection; // SharpBrick.PoweredUp uses the DI system +using Microsoft.Extensions.Logging; // SharpBrick.PoweredUp also logs stuff ```` ## Discovering Hubs @@ -37,7 +40,7 @@ using SharpBrick.PoweredUp; var serviceProvider = new ServiceCollection() .AddLogging() .AddPoweredUp() - .AddWinRTBluetooth() // using WinRT Bluetooth on Windows (separate NuGet SharpBrick.PoweredUp.WinRT) + .AddWinRTBluetooth() // using WinRT Bluetooth on Windows (separate NuGet SharpBrick.PoweredUp.WinRT; others are available) .BuildServiceProvider(); var host = serviceProvider.GetService(); @@ -192,10 +195,9 @@ using (var scope = serviceProvider.CreateScope()) // create a scoped DI containe await motor.GotoPositionAsync(-45, 10, 100, PortOutputCommandSpecialSpeed.Brake); } ```` -## Connecting with a BlueGiga-Bluetooth-adapter - +## Connecting with other Bluetooth Adapters -For details on using BlueGiga-adapter, also on Raspberry Pi or Ubuntu, see [here.](docs/bleadapters/BlueGigaBLED112.md) +- **BlueGiga Adapter**: Find more details in the [docs](docs/bleadapters/BlueGigaBLED112.md) about the needed configuration and the setup on Windows, Ubuntu or the Raspberry Pi. # Command Line Experience @@ -211,16 +213,22 @@ The `poweredup` command line utility intends to allow the inspection of LEGO Wir ```` - **Pretty Print Binary Dumps**: Help to convert a binary dump in a nice representation. -- **Use of other Bluetooth LE stack** (default is WinRT on Windows) +- **Use of other Bluetooth LE stack** By default the CLI tools assumes the usage of WinRT. If used on another operating system or with another Bluetooth Adapter on Windows, the Bluetooth Adapter needs to be specified. Adapter might need additional configuration, see their details documentation. - - For BlueGiga-adapter, also on Raspberry Pi or Ubuntu, see [here.](docs/bleadapters/BlueGigaBLED112.md) + ```` + poweredup device list --BluetoothAdapter BlueGigaBLE + ```` ## Installation Instruction 1. Install the [latest .NET](https://dotnet.microsoft.com/download) on your machine (e.g. .NET 5). 2. Install the `poweredup` dotnet utility using the following instruction ```` - dotnet tool install -g SharpBrick.PoweredUp.Cli + // On Windows + dotnet tool install --framework net5.0-windows10.0.19041.0 -g SharpBrick.PoweredUp.Cli + + // On Linux + dotnet tool install --framework net5.0 -g SharpBrick.PoweredUp.Cli ```` 3. Start using the tool ```` @@ -269,9 +277,8 @@ DI Container Elements - Bluetooth Adapter - [X] .NET Core 3.1 (on Windows 10 using WinRT Bluetooth). Please use version v3.4.0 and consider upgrading to .NET 5 - [X] .NET 5 (on Windows 10 using WinRT Bluetooth) (⚠ v4.0 or later) - - [X] .NET 5 (on Windows 10 using BlueGiga-adapter) (⚠ v4.0 or later) - - [X] .NET 5 (on Raspberry Pi 3 or 4 using BlueGiga-adapter) (⚠ v4.0 or later) - - [X] .NET 5 (on Ubuntu 20.04 using BlueGiga-adapter) (⚠ v4.0 or later) + - [X] .NET 5 (on Windows 10 using BlueGiga Adapter) (⚠ v4.0 or later) + - [X] .NET 5 (on Linux using BlueGiga Adapter) (tested on Raspberry Pi 3, 4 and Ubuntu 20.04; ⚠ v4.0 or later) - [ ] UWP (most likely December 2021; UWP currently does not support .NET Standard 2.1 and C# 8.0+) - [ ] .NET Framework 4.8 (will never be supported; .NET Framework does not and will never support .NET Standard 2.1 and C# 8.0+) - [X] Xamarin 5 (on Android using BLE.Plugin) (⚠ v4.0 or later) @@ -358,7 +365,6 @@ DI Container Elements ## Resources - [Lego Wireless Protocol Specification](https://lego.github.io/lego-ble-wireless-protocol-docs) ([GitHub](https://github.com/lego/lego-ble-wireless-protocol-docs)) -- [BlueGiga BLE protcol](https://www.silabs.com/documents/public/reference-manuals/Bluetooth-LE-Software-API%20Reference-Manual-for-BLE-Version-1.10.pdf) ## Contribution diff --git a/docs/bleadapters/BlueGigaBLED112.md b/docs/bleadapters/BlueGigaBLED112.md index 5919270..5b6b569 100644 --- a/docs/bleadapters/BlueGigaBLED112.md +++ b/docs/bleadapters/BlueGigaBLED112.md @@ -52,11 +52,12 @@ On windows-systems you can debug or run from inside Visual Studio / your IDE wit ## Command Line Experience ## ```` - poweredup device list --usebluegiga COM4 --tracebluegiga + poweredup device list --BluetoothAdapter BlueGigaBLE --COMPortName COM4 --TraceDebug true ```` -``--usebluegiga`` is the parameter that tells the cli-tool to use the BleuGiga-implementation. It has to be followed by the name of the serial port to use. +`--BluetoothAdapter` switches the BluetoothAdapter to use BlueGigaBLE. +`--COMPortName` is the parameter that tells the cli-tool to use the given serial port. Use the name of the serial port as described above; so replace `COM4` with COMx whatever x is used on your Windows-system or e.g. /dev/ttyACM0 for Linux-based OS. -`--tracebluegiga` is an optional parameter; when used the cli emits **a lot** of additional trace-information of the BleuGiga-implementation. Default: no additional trace. +`--TraceDebug` is an optional parameter; when used the cli emits **a lot** of additional trace-information of the BlueGiga-implementation. Default: no additional trace. ## Using / Building for Linux-based OS ## At the time of this writing you've got to make sure that you don't include/depend on the SharpBrick.PoweredUp.WinRT-project, because this doesn't build for Linux-platforms. So change your project-dependencies accordingly. @@ -71,7 +72,7 @@ The following instructions assume that you've got installed at least the .NET 5 - Change the mod of the executable (the file which is named like your project) to "executable" by using ``chmod +x (filename)`` on the Raspberry Pi - Open a terminal-windows on the Pi and enter - ``cd ~/powered-up`` - - ``./SharpBrick.PoweredUp.Examples --usebluegiga /dev/ttyACM0`` + - ``./SharpBrick.PoweredUp.Examples --BluetoothAdapter BlueGigaBLE --COMPortName /dev/ttyACM0`` ### Build and run for Ubuntu 20.04 ### - open a command prompt on your dev-machine (Windows) @@ -81,7 +82,7 @@ The following instructions assume that you've got installed at least the .NET 5 - Change the mod of the executable (the file which is named like your project) to "executable" by using ``chmod +x (filename)`` on Ubuntu - Open a terminal-windows on Ubuntu and enter - ``cd ~/powered-up`` - - ``./SharpBrick.PoweredUp.Examples --usebluegiga /dev/ttyACM0`` + - ``./SharpBrick.PoweredUp.Examples --BluetoothAdapter BlueGigaBLE --COMPortName /dev/ttyACM0`` - In case you receive a permission-denied error for /dev/ttyACM0: - sudo chmod +rw /dev/ttyACM0 - sudo chmod o+rw /dev/ttyACM0 diff --git a/examples/SharpBrick.PoweredUp.Examples/BaseExample.cs b/examples/SharpBrick.PoweredUp.Examples/BaseExample.cs index 7d47a0b..66f8a95 100644 --- a/examples/SharpBrick.PoweredUp.Examples/BaseExample.cs +++ b/examples/SharpBrick.PoweredUp.Examples/BaseExample.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SharpBrick.PoweredUp; @@ -22,16 +23,17 @@ public virtual void Configure(IServiceCollection serviceCollection) { serviceCollection .AddPoweredUp(); - - - } - public async Task InitHostAndDiscoverAsync(bool enableTrace , string bluetoothStackPort="WINRT", bool enableTraceBlueGiga = false) + public async Task InitExampleAndDiscoverAsync(IServiceProvider serviceProvider, IConfiguration configuration) { - InitHost(enableTrace, bluetoothStackPort, enableTraceBlueGiga); + ServiceProvider = serviceProvider; + + Host = serviceProvider.GetService(); + + Log = serviceProvider.GetService().CreateLogger("Example"); - Log = ServiceProvider.GetService().CreateLogger("Example"); + var enableTrace = bool.TryParse(configuration["EnableTrace"], out var x) && x; await DiscoverAsync(enableTrace); } @@ -73,51 +75,5 @@ public virtual Task DiscoverAsync(bool enableTrace) return Task.CompletedTask; } - - public void InitHost(bool enableTrace, string bluetoothStackPort="WINRT", bool enableTraceBlueGiga=false) - { - var serviceCollection = new ServiceCollection() - // configure your favourite level of logging. - .AddLogging(builder => - { - builder - .AddConsole(); - - if (enableTrace) - { - builder.AddFilter("SharpBrick.PoweredUp.Bluetooth.BluetoothKernel", LogLevel.Debug); - } - if (enableTraceBlueGiga) - { - builder.AddFilter("SharpBrick.PoweredUp.BlueGigaBLE.BlueGigaBLEPoweredUpBluetoothAdapater", LogLevel.Debug); - } - }); - if (bluetoothStackPort.Equals("WINRT" , StringComparison.OrdinalIgnoreCase)) - { - serviceCollection.AddWinRTBluetooth(); - } - else - { - //this adds the BlueGiga-implementation instead of the WinRT-implementation - //the value of the parameter bluetoothStackPort is taken for the COM-Port on which the BlueGiga-adapter is connected - _ = serviceCollection.AddBlueGigaBLEBluetooth(options => - { - //enter the COMPort-Name here - //on Windows-PCs you can find it under Device Manager --> Ports (COM & LPT) --> Bleugiga Bluetooth Low Energy (COM#) (where # is a number) - options.COMPortName = bluetoothStackPort; - //setting this option to false supresses the complete LogDebug()-commands; so they will not generated at all - options.TraceDebug = enableTraceBlueGiga; - }); - } - //can be easily extended here by taking another implementation (for example BlueZ for Raspberry) into the BluetoothImplementation-enum and then - //do the needed Addxxx and options here: - - - Configure(serviceCollection); - - ServiceProvider = serviceCollection.BuildServiceProvider(); - - Host = ServiceProvider.GetService(); - } } } \ No newline at end of file diff --git a/examples/SharpBrick.PoweredUp.Examples/ExampleTwoHubsMotorControl.cs b/examples/SharpBrick.PoweredUp.Examples/ExampleTwoHubsMotorControl.cs index ee7f94d..a793a5d 100644 --- a/examples/SharpBrick.PoweredUp.Examples/ExampleTwoHubsMotorControl.cs +++ b/examples/SharpBrick.PoweredUp.Examples/ExampleTwoHubsMotorControl.cs @@ -2,7 +2,6 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using SharpBrick.PoweredUp; -using SharpBrick.PoweredUp.BlueGigaBLE; namespace Example { diff --git a/examples/SharpBrick.PoweredUp.Examples/Program.cs b/examples/SharpBrick.PoweredUp.Examples/Program.cs index 9db139a..894c71d 100644 --- a/examples/SharpBrick.PoweredUp.Examples/Program.cs +++ b/examples/SharpBrick.PoweredUp.Examples/Program.cs @@ -1,84 +1,107 @@ using System; using System.Linq; +using System.Reflection; using System.Threading.Tasks; - +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; namespace SharpBrick.PoweredUp.Examples { class Program { + // invoke (due to multi-targeting) with ... + // dotnet run -f net5.0 -- + // dotnet run -f net5.0-windows10.0.19041.0 -- + + // use command line parameters + // --EnableTrace true + // --BluetoothAdapter WinRT|BlueGigaBLE + // --COMPortName COM4 + // -- TraceDebug true + // or configure in poweredup.json static async Task Main(string[] args) { - var enableTrace = (args.Length > 0 && args[0] == "--trace"); - string bluetoothStackPort = "WINRT"; - bool enableTraceBlueGiga = false; - if (args.Any(x => x.Equals("--usebluegiga", StringComparison.OrdinalIgnoreCase))) + // (1) load a configuration + var configuration = new ConfigurationBuilder() + .SetBasePath(Environment.CurrentDirectory) + .AddJsonFile("poweredup.json", true) + .AddCommandLine(args) + .Build(); + + // Example Selection: select the example to execute + var exampleToExecute = configuration["Example"] ?? "Colors"; + + var typeToExecute = Assembly + .GetExecutingAssembly() + .GetTypes() + .FirstOrDefault(x => x.FullName.StartsWith($"Example.Example{exampleToExecute}")); + + if (typeToExecute is null) + { + Console.WriteLine("Could not find example."); + + return; + } + + var example = Activator.CreateInstance(typeToExecute) as Example.BaseExample; + + + // (2) build the DI container + var serviceCollection = new ServiceCollection(); + + // (2a) configure your favourite level of logging. + serviceCollection.AddLogging(builder => { - for (int i = 0; i < args.Length; i++) + builder + .AddConsole(); + + if (bool.TryParse(configuration["EnableTrace"], out var enableTrace) && enableTrace) + { + builder.AddFilter("SharpBrick.PoweredUp.Bluetooth.BluetoothKernel", LogLevel.Debug); + } + if (bool.TryParse(configuration["TraceDebug"], out var traceDebug) && traceDebug) { - if (args[i].Equals("--usebluegiga", StringComparison.OrdinalIgnoreCase)) - { - if (args.Length > i) - { - bluetoothStackPort = args[i + 1]; - } - break; - } + builder.AddFilter("SharpBrick.PoweredUp.BlueGigaBLE.BlueGigaBLEPoweredUpBluetoothAdapater", LogLevel.Debug); } + }); + + // (2b) add .AddPoweredUp() (we delegate this into the example because some examples need a different setup) + example.Configure(serviceCollection); + + + // (2c) add your favourite Bluetooth Adapter + var bluetoothAdapter = configuration["BluetoothAdapter"] ?? "WinRT"; + +#if WINDOWS + if (bluetoothAdapter == "WinRT") + { + serviceCollection.AddWinRTBluetooth(); } - if (args.Any(x => x.Equals("--tracebluegiga", StringComparison.OrdinalIgnoreCase))) +#endif + +#if NET5_0_OR_GREATER + if (bluetoothAdapter == "BlueGigaBLE") { - enableTraceBlueGiga = true; + // config for "COMPortName" and "TraceDebug" (either via command line or poweredup.json) + serviceCollection.AddBlueGigaBLEBluetooth(options => + { + // on Windows-PCs you can find it under Device Manager --> Ports (COM & LPT) --> Bleugiga Bluetooth Low Energy (COM#) (where # is a number) + // "COMPortName": "COM4", + + // setting this option to false supresses the complete LogDebug()-commands; so they will not generated at all + // "TraceDebug": true + configuration.Bind(options); + }); } +#endif + + var serviceProvider = serviceCollection.BuildServiceProvider(); - // NOTE: Examples are in their own root namespace to make namespace usage clear - Example.BaseExample example; - - //example = new Example.ExampleColors(); - //example = new Example.ExampleMotorControl(); - //example = new Example.ExampleMotorInputAbsolutePosition(); - //example = new Example.ExampleMotorVirtualPort(); - //example = new Example.ExampleHubActions(); - //example = new Example.ExampleTechnicMediumHubAccelerometer(); - //example = new Example.ExampleTechnicMediumHubGyroSensor(); - //example = new Example.ExampleVoltage(); - //example = new Example.ExampleTechnicMediumTemperatureSensor(); - //example = new Example.ExampleMotorInputCombinedMode(); - //example = new Example.ExampleMixedBag(); - //example = new Example.ExampleHubAlert(); - //example = new Example.ExampleTechnicMediumHubTiltSensor(); - //example = new Example.ExampleTechnicMediumHubTiltSensorImpacts(); - //example = new Example.ExampleDynamicDevice(); - //example = new Example.ExampleBluetoothByKnownAddress(); - //example = new Example.ExampleBluetoothByName(); - //example = new Example.ExampleSetHubProperty(); - //example = new Example.ExampleHubPropertyObserving(); - //example = new Example.ExampleDiscoverByType(); - //example = new Example.ExampleCalibrationSteering(); - //example = new Example.ExampleRampUp(); - //example = new Example.ExampleTechnicMediumHubGestSensor(); - //example = new Example.ExampleRemoteControlButton(); - //example = new Example.ExampleRemoteControlRssi(); - //example = new Example.ExampleTechnicMediumAngularMotorGrey(); - //example = new Example.ExampleMarioBarcode(); - //example = new Example.ExampleMarioPants(); - //example = new Example.ExampleMarioAccelerometer(); - //example = new Example.ExampleDuploTrainBase(); - //example = new Example.ExampleTechnicColorSensor(); - //example = new Example.ExampleTechnicDistanceSensor(); - //example = new Example.ExampleTechnicMediumHubGestSensor(); - //example = new Example.ExampleMoveHubInternalTachoMotorControl(); - //example = new Example.ExampleMoveHubExternalMediumLinearMotorControl(); - //example = new Example.ExampleMoveHubColors(); - //example = new Example.ExampleMoveHubTiltSensor(); - //example = new ExampleTwoHubsMotorControl(); - //example = new ExampleTwoPortHubMediumLinearMotor(); - example = new Example.ExampleColorDistanceSensor(); - - // NOTE: Examples are programmed object oriented style. Base class implements methods Configure, DiscoverAsync and ExecuteAsync to be overwriten on demand. - // InitHostAndDiscoverAsync uses the WinRT-bluetooth-implementation by default (bluetoothStackPort defaults to "WinRT" and enableTraceBlueGiga defaults to false) - await example.InitHostAndDiscoverAsync(enableTrace, bluetoothStackPort, enableTraceBlueGiga); + // Examples Initialization + await example.InitExampleAndDiscoverAsync(serviceProvider, configuration); + // Example Execution if (example.SelectedHub is not null) { await example.ExecuteAsync(); diff --git a/examples/SharpBrick.PoweredUp.Examples/SharpBrick.PoweredUp.Examples.csproj b/examples/SharpBrick.PoweredUp.Examples/SharpBrick.PoweredUp.Examples.csproj index fce54b1..33bb82f 100644 --- a/examples/SharpBrick.PoweredUp.Examples/SharpBrick.PoweredUp.Examples.csproj +++ b/examples/SharpBrick.PoweredUp.Examples/SharpBrick.PoweredUp.Examples.csproj @@ -2,17 +2,26 @@ Exe - net5.0-windows10.0.19041.0 + net5.0-windows10.0.19041.0;net5.0 false - + + + + + + + + + + diff --git a/src/SharpBrick.PoweredUp.BlueGigaBLE/SharpBrick.PoweredUp.BlueGigaBLE.csproj b/src/SharpBrick.PoweredUp.BlueGigaBLE/SharpBrick.PoweredUp.BlueGigaBLE.csproj index 80d2b8f..568ad7b 100644 --- a/src/SharpBrick.PoweredUp.BlueGigaBLE/SharpBrick.PoweredUp.BlueGigaBLE.csproj +++ b/src/SharpBrick.PoweredUp.BlueGigaBLE/SharpBrick.PoweredUp.BlueGigaBLE.csproj @@ -1,7 +1,7 @@ - net5.0-windows10.0.19041.0 + net5.0 true @@ -10,16 +10,12 @@ 9.0 - - - - - + diff --git a/src/SharpBrick.PoweredUp.Cli/Program.cs b/src/SharpBrick.PoweredUp.Cli/Program.cs index c5f6080..1b9442e 100644 --- a/src/SharpBrick.PoweredUp.Cli/Program.cs +++ b/src/SharpBrick.PoweredUp.Cli/Program.cs @@ -6,6 +6,7 @@ using System.Threading; using System.Threading.Tasks; using McMaster.Extensions.CommandLineUtils; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SharpBrick.PoweredUp.Bluetooth; @@ -22,6 +23,14 @@ class Program static async Task Main(string[] args) { + // load a configuration object + var configuration = new ConfigurationBuilder() + .AddJsonFile("poweredup.json", true) + .AddCommandLine(args) + .Build(); + + var enableTrace = bool.TryParse(configuration["EnableTrace"], out var enableTraceInConfig) && enableTraceInConfig; + var app = new CommandLineApplication { Name = "poweredup", @@ -51,18 +60,18 @@ static async Task Main(string[] args) { deviceListApp.Description = "Inspect all devices declared on the Hub by using information gathered using the LEGO Wireless Protocol"; deviceListApp.HelpOption(); - var traceOption = deviceListApp.Option("--trace", "Enable Tracing (default: no trace)", CommandOptionType.NoValue); - var blueGigaOption = deviceListApp.Option("--usebluegiga", "Use BlueGiga-Bluetooth adapter-stack (default is WindowsRT-Bluetooth-Stack) and give the port (for example 'COM4') as parameter", CommandOptionType.SingleValue); - var traceBlueGigaOption = deviceListApp.Option("--tracebluegiga", "Emmit extra trace from Bluegiga-communication (default: no extra trace)", CommandOptionType.NoValue); + + // pseudo option for configuration to create documentation + deviceListApp.Option("--EnableTrace", "Enable Tracing (default: no trace)", CommandOptionType.SingleValue); + deviceListApp.Option("--BluetoothAdapter", "Use a specified BLE adapter (e.g. WinRT or BlueGigaBLE). Defaults to WinRT. Some adapter might require additional parameters.", CommandOptionType.SingleValue); + deviceListApp.Option("--COMPortName", "Name of the COM port to connect the BluetoothAdapter (if applicable; e.g. BlueGigaBLE)", CommandOptionType.SingleValue); + deviceListApp.Option("--TraceDebug", "Create detailed tracing of the BluetoothAdapter (if applicable; e.g. BlueGigaBLE)", CommandOptionType.SingleValue); + deviceListApp.OnExecuteAsync(async cts => { try { - //for a boolean parameter it is just enough to test wether it has been given at all; HasValue() is true, if the parameter has been given: - var enableTrace = traceOption.HasValue(); - var bluetoothStackPort = blueGigaOption.HasValue() ? blueGigaOption.Value() : "WINRT"; - var enableBlueGigaTrace = traceBlueGigaOption.HasValue(); - var serviceProvider = CreateServiceProvider(enableTrace, bluetoothStackPort, enableBlueGigaTrace); + var serviceProvider = CreateServiceProvider(configuration); (ulong bluetoothAddress, SystemType systemType) = FindAndSelectHub(serviceProvider.GetService()); if (bluetoothAddress == 0) @@ -96,25 +105,23 @@ static async Task Main(string[] args) { deviceDumpStaticPortApp.Description = "Inspect a specific device on a Hub Port by using (non-dynamic) information gathered using the LEGO Wireless Protocol. Emits a binary dump (use list for human readable output)."; deviceDumpStaticPortApp.HelpOption(); - var traceOption = deviceDumpStaticPortApp.Option("--trace", "Enable Tracing (default: no trace)", CommandOptionType.NoValue); - var blueGigaOption = deviceDumpStaticPortApp.Option("--usebluegiga", "Use BlueGiga-Bluetooth adapter-stack (default is WindowsRT-Bluetooth-Stack) and give the port (for example 'COM4') as parameter", CommandOptionType.SingleValue); - var traceBlueGigaOption = deviceDumpStaticPortApp.Option("--tracebluegiga", "Emmit extra trace from Bluegiga-communication", CommandOptionType.NoValue); + // pseudo option for configuration to create documentation + deviceDumpStaticPortApp.Option("--EnableTrace", "Enable Tracing (default: no trace)", CommandOptionType.SingleValue); + deviceDumpStaticPortApp.Option("--BluetoothAdapter", "Use a specified BLE adapter (e.g. WinRT or BlueGigaBLE). Defaults to WinRT. Some adapter might require additional parameters.", CommandOptionType.SingleValue); + deviceDumpStaticPortApp.Option("--COMPortName", "Name of the COM port to connect the BluetoothAdapter (if applicable; e.g. BlueGigaBLE)", CommandOptionType.SingleValue); + deviceDumpStaticPortApp.Option("--TraceDebug", "Create detailed tracing of the BluetoothAdapter (if applicable; e.g. BlueGigaBLE)", CommandOptionType.SingleValue); var portOption = deviceDumpStaticPortApp.Option("-p", "Port to Dump", CommandOptionType.SingleValue); var headerOption = deviceDumpStaticPortApp.Option("-f", "Add Hub and IOType Header", CommandOptionType.NoValue); - + deviceDumpStaticPortApp.OnExecuteAsync(async cts => { try { - - var enableTrace = traceOption.HasValue(); var headerEnabled = headerOption.Values.Count > 0; - var bluetoothStackPort = blueGigaOption.HasValue() ? blueGigaOption.Value() : "WINRT"; - var enableBlueGigaTrace = traceBlueGigaOption.HasValue(); - var serviceProvider = CreateServiceProvider(enableTrace, bluetoothStackPort, enableBlueGigaTrace); + var serviceProvider = CreateServiceProvider(configuration); (ulong bluetoothAddress, SystemType systemType) = FindAndSelectHub(serviceProvider.GetService()); if (bluetoothAddress == 0) @@ -151,7 +158,9 @@ static async Task Main(string[] args) { prettyPrintApp.Description = "Pretty prints a previously recorded binary dump collected using dump-static-ports"; prettyPrintApp.HelpOption(); - var traceOption = prettyPrintApp.Option("--trace", "Enable Tracing (default: no trace) ", CommandOptionType.NoValue); + + prettyPrintApp.Option("--EnableTrace", "Enable Tracing (default: no trace)", CommandOptionType.SingleValue); + var systemTypeOption = prettyPrintApp.Option("--t", "System Type (parsable number)", CommandOptionType.SingleValue); var hubOption = prettyPrintApp.Option("--h", "Hub Id (decimal number)", CommandOptionType.SingleValue); var portOption = prettyPrintApp.Option("--p", "Port Id (decimal number)", CommandOptionType.SingleValue); @@ -164,7 +173,6 @@ static async Task Main(string[] args) { try { - var enableTrace = traceOption.HasValue(); var systemType = byte.TryParse(systemTypeOption.Value(), out var x1) ? x1 : (byte)0; var hubId = byte.TryParse(hubOption.Value(), out var x2) ? x2 : (byte)0; var portId = byte.TryParse(portOption.Value(), out var x3) ? x3 : (byte)0; @@ -233,22 +241,35 @@ private static IServiceProvider CreateServiceProviderWithMock(bool enableTrace) => CreateServiceProviderInternal(enableTrace) .AddMockBluetooth() .BuildServiceProvider(); - private static IServiceProvider CreateServiceProvider(bool enableTrace, string bluetoothStackPort = "WINRT", bool enableBlueGigaTrace=false) + private static IServiceProvider CreateServiceProvider(IConfiguration configuration) { + var enableTrace = bool.TryParse(configuration["EnableTrace"], out var v) && v; + var serviceCollection = CreateServiceProviderInternal(enableTrace); - if (bluetoothStackPort.Equals("WINRT", StringComparison.OrdinalIgnoreCase)) - { + var bluetoothAdapter = configuration["BluetoothAdapter"] ?? "WinRT"; +#if WINDOWS + if (bluetoothAdapter == "WinRT") + { serviceCollection.AddWinRTBluetooth(); - } - else + } +#endif + +#if NET5_0_OR_GREATER + if (bluetoothAdapter == "BlueGigaBLE") { + // config for "COMPortName" and "TraceDebug" (either via command line or poweredup.json) serviceCollection.AddBlueGigaBLEBluetooth(options => { - options.COMPortName = bluetoothStackPort; - options.TraceDebug = enableBlueGigaTrace; + // on Windows-PCs you can find it under Device Manager --> Ports (COM & LPT) --> Bleugiga Bluetooth Low Energy (COM#) (where # is a number) + // "COMPortName": "COM4", + + // setting this option to false supresses the complete LogDebug()-commands; so they will not generated at all + // "TraceDebug": true + configuration.Bind(options); }); } +#endif return serviceCollection.BuildServiceProvider(); } diff --git a/src/SharpBrick.PoweredUp.Cli/SharpBrick.PoweredUp.Cli.csproj b/src/SharpBrick.PoweredUp.Cli/SharpBrick.PoweredUp.Cli.csproj index 17548c5..2b4f3f8 100644 --- a/src/SharpBrick.PoweredUp.Cli/SharpBrick.PoweredUp.Cli.csproj +++ b/src/SharpBrick.PoweredUp.Cli/SharpBrick.PoweredUp.Cli.csproj @@ -2,18 +2,27 @@ Exe - net5.0-windows10.0.19041.0 + net5.0-windows10.0.19041.0;net5.0 true poweredup - - - + + + + + + + + + + + + diff --git a/test/SharpBrick.PoweredUp.TestScript/Program.cs b/test/SharpBrick.PoweredUp.TestScript/Program.cs index a6eeb70..26c1b6f 100644 --- a/test/SharpBrick.PoweredUp.TestScript/Program.cs +++ b/test/SharpBrick.PoweredUp.TestScript/Program.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SharpBrick.PoweredUp.Deployment; @@ -12,14 +13,45 @@ class Program { static async Task Main(string[] args) { - var serviceProvider = new ServiceCollection() + var configuration = new ConfigurationBuilder() + .SetBasePath(Environment.CurrentDirectory) + .AddJsonFile("poweredup.json", true) + .AddCommandLine(args) + .Build(); + + var serviceCollection = new ServiceCollection() .AddLogging(builder => { builder.AddConsole(); }) - .AddPoweredUp() - .AddWinRTBluetooth() // using WinRT Bluetooth on Windows (separate NuGet SharpBrick.PoweredUp.WinRT) - .BuildServiceProvider(); + .AddPoweredUp(); + + var bluetoothAdapter = configuration["BluetoothAdapter"] ?? "WinRT"; + +#if WINDOWS + if (bluetoothAdapter == "WinRT") + { + serviceCollection.AddWinRTBluetooth(); + } +#endif + +#if NET5_0_OR_GREATER + if (bluetoothAdapter == "BlueGigaBLE") + { + // config for "COMPortName" and "TraceDebug" (either via command line or poweredup.json) + serviceCollection.AddBlueGigaBLEBluetooth(options => + { + // on Windows-PCs you can find it under Device Manager --> Ports (COM & LPT) --> Bleugiga Bluetooth Low Energy (COM#) (where # is a number) + // "COMPortName": "COM4", + + // setting this option to false supresses the complete LogDebug()-commands; so they will not generated at all + // "TraceDebug": true + configuration.Bind(options); + }); + } +#endif + + var serviceProvider = serviceCollection.BuildServiceProvider(); var host = serviceProvider.GetService(); diff --git a/test/SharpBrick.PoweredUp.TestScript/SharpBrick.PoweredUp.TestScript.csproj b/test/SharpBrick.PoweredUp.TestScript/SharpBrick.PoweredUp.TestScript.csproj index 53509f9..56467c2 100644 --- a/test/SharpBrick.PoweredUp.TestScript/SharpBrick.PoweredUp.TestScript.csproj +++ b/test/SharpBrick.PoweredUp.TestScript/SharpBrick.PoweredUp.TestScript.csproj @@ -2,15 +2,27 @@ Exe - net5.0-windows10.0.19041.0 + net5.0;net5.0-windows10.0.19041.0 + + false + + + + + + + + - + + + diff --git a/test/SharpBrick.PoweredUp.TestScript/TestScriptExecutionContext.cs b/test/SharpBrick.PoweredUp.TestScript/TestScriptExecutionContext.cs index 260483f..9d89851 100644 --- a/test/SharpBrick.PoweredUp.TestScript/TestScriptExecutionContext.cs +++ b/test/SharpBrick.PoweredUp.TestScript/TestScriptExecutionContext.cs @@ -14,7 +14,7 @@ public TestScriptExecutionContext(ILogger log) public ILogger Log { get; } public Hub CurrentHub { get; set; } - public async Task ConfirmAsync(string question) + public Task ConfirmAsync(string question) { Console.Write(question + " (Y/n)"); @@ -29,7 +29,7 @@ public async Task ConfirmAsync(string question) Log.LogError($"[FAIL] by User {question}"); } - return confirm; + return Task.FromResult(confirm); } public void Assert(int value, int min, int max)