Skip to content

Commit

Permalink
Allow direct connection to known device
Browse files Browse the repository at this point in the history
- Extended PoweredUpHost with Create method.
- Add example
- Refactored BaseExample to allow alternatives to discovery

#52
  • Loading branch information
tthiery committed Jul 22, 2020
1 parent 4326d0e commit 97ef072
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 29 deletions.
51 changes: 30 additions & 21 deletions examples/SharpBrick.PoweredUp.Examples/BaseExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,16 @@ public virtual void Configure(IServiceCollection serviceCollection)
.AddPoweredUp();
}

public void CreateHostAndDiscover(bool enableTrace)
public void InitHostAndDiscover(bool enableTrace)
{
var serviceCollection = new ServiceCollection()
// configure your favourite level of logging.
.AddLogging(builder =>
{
builder
.AddConsole();
if (enableTrace)
{
builder.AddFilter("SharpBrick.PoweredUp.Bluetooth.BluetoothKernel", LogLevel.Debug);
}
})
.AddSingleton<IPoweredUpBluetoothAdapter, WinRTPoweredUpBluetoothAdapter>()
;

Configure(serviceCollection);

serviceProvider = serviceCollection.BuildServiceProvider();
InitHost(enableTrace);
Discover(enableTrace);
}

public virtual void Discover(bool enableTrace)
{
var logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger("Main");

host = serviceProvider.GetService<PoweredUpHost>();

Hub result = null;

logger.LogInformation("Finding Service");
Expand Down Expand Up @@ -83,5 +68,29 @@ public void CreateHostAndDiscover(bool enableTrace)

selectedHub = result;
}

public void InitHost(bool enableTrace)
{
var serviceCollection = new ServiceCollection()
// configure your favourite level of logging.
.AddLogging(builder =>
{
builder
.AddConsole();
if (enableTrace)
{
builder.AddFilter("SharpBrick.PoweredUp.Bluetooth.BluetoothKernel", LogLevel.Debug);
}
})
.AddSingleton<IPoweredUpBluetoothAdapter, WinRTPoweredUpBluetoothAdapter>()
;

Configure(serviceCollection);

serviceProvider = serviceCollection.BuildServiceProvider();

host = serviceProvider.GetService<PoweredUpHost>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Threading.Tasks;
using SharpBrick.PoweredUp;

namespace Example
{
public class ExampleBluetoothByKnownAddress : BaseExample
{
public const ulong ChangeMe_BluetoothAddress = 158897336311065;
public TechnicMediumHub DirectlyConnectedHub { get; private set; }

// device needs to be switched on!
public override void Discover(bool enableTrace)
{
var hub = host.Create<TechnicMediumHub>(ChangeMe_BluetoothAddress);

selectedHub = DirectlyConnectedHub = hub;

hub.ConnectAsync().Wait();
}

public override async Task ExecuteAsync()
{
using (var technicMediumHub = DirectlyConnectedHub)
{
await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0x00, 0x00);

await Task.Delay(2000);

await technicMediumHub.RgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);

await Task.Delay(2000);

await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0xff, 0x00);

await Task.Delay(2000);

await technicMediumHub.SwitchOffAsync();
}
}
}
}
5 changes: 3 additions & 2 deletions examples/SharpBrick.PoweredUp.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ static async Task Main(string[] args)
//example = new Example.ExampleHubAlert();
//example = new Example.ExampleTechnicMediumHubTiltSensor();
//example = new Example.ExampleTechnicMediumHubTiltSensorImpacts();
example = new Example.ExampleDynamicDevice();
//example = new Example.ExampleDynamicDevice();
example = new Example.ExampleBluetoothByKnownAddress();

example.CreateHostAndDiscover(enableTrace);
example.InitHostAndDiscover(enableTrace);

if (example.selectedHub != null)
{
Expand Down
17 changes: 13 additions & 4 deletions src/SharpBrick.PoweredUp/Hubs/HubFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@ public HubFactory(IServiceProvider serviceProvider)
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
}

public Hub CreateByBluetoothManufacturerData(byte[] manufacturerData, IServiceProvider serviceProvider)
=> (manufacturerData == null || manufacturerData.Length < 3) ? null : (PoweredUpHubManufacturerData)manufacturerData[1] switch
public Hub CreateByBluetoothManufacturerData(byte[] manufacturerData)
=> (manufacturerData == null || manufacturerData.Length < 3) ? null : Create(GetSystemTypeFromManufacturerData((PoweredUpHubManufacturerData)manufacturerData[1]));

private SystemType GetSystemTypeFromManufacturerData(PoweredUpHubManufacturerData poweredUpHubManufacturerData)
=> (SystemType)poweredUpHubManufacturerData;

public Hub Create(SystemType hubType)
=> hubType switch
{
PoweredUpHubManufacturerData.TechnicMediumHub => ActivatorUtilities.CreateInstance<TechnicMediumHub>(_serviceProvider, (byte)0x00),
_ => throw new NotSupportedException($"Hub with type {(PoweredUpHubManufacturerData)manufacturerData[1]} not yet supported."),
SystemType.LegoTechnic_MediumHub => ActivatorUtilities.CreateInstance<TechnicMediumHub>(_serviceProvider, (byte)0x00),
_ => throw new NotSupportedException($"Hub with type {hubType} not yet supported."),
};

public THub Create<THub>() where THub : class
=> Create(GetSystemTypeFromType(typeof(THub))) as THub;

public static SystemType GetSystemTypeFromType(Type type)
=> type.Name switch
{
Expand Down
3 changes: 2 additions & 1 deletion src/SharpBrick.PoweredUp/Hubs/IHubFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace SharpBrick.PoweredUp.Hubs
{
public interface IHubFactory
{
Hub CreateByBluetoothManufacturerData(byte[] manufacturerData, IServiceProvider serviceProvider);
Hub CreateByBluetoothManufacturerData(byte[] manufacturerData);
THub Create<THub>() where THub : class;
}
}
12 changes: 11 additions & 1 deletion src/SharpBrick.PoweredUp/PoweredUpHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void Discover(Func<Hub, Task> onDiscovery, CancellationToken token = defa
{
if (!_hubs.ContainsKey(deviceInfo.BluetoothAddress))
{
var hub = _hubFactory.CreateByBluetoothManufacturerData(deviceInfo.ManufacturerData, ServiceProvider);
var hub = _hubFactory.CreateByBluetoothManufacturerData(deviceInfo.ManufacturerData);
hub.ConnectWithBluetoothAdapter(_bluetoothAdapter, deviceInfo.BluetoothAddress);
_hubs.TryAdd(deviceInfo.BluetoothAddress, hub);
Expand All @@ -55,5 +55,15 @@ public void Discover(Func<Hub, Task> onDiscovery, CancellationToken token = defa
}
}, token);
}

public THub Create<THub>(ulong bluetoothAddress) where THub : Hub
{
var hub = _hubFactory.Create<THub>();
hub.ConnectWithBluetoothAdapter(_bluetoothAdapter, bluetoothAddress);

_hubs.TryAdd(bluetoothAddress, hub);

return hub;
}
}
}

0 comments on commit 97ef072

Please sign in to comment.