Skip to content

Commit

Permalink
Fallback to DynamicDevice for unknown devices
Browse files Browse the repository at this point in the history
- Fixed DeviceFactory to return DynamicDevice if no typed device present

#37 non-breaking
  • Loading branch information
tthiery committed Jul 8, 2020
1 parent 1b1cda8 commit 5331409
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
37 changes: 23 additions & 14 deletions examples/SharpBrick.PoweredUp.Examples/ExampleDynamicDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using SharpBrick.PoweredUp;
using SharpBrick.PoweredUp.Deployment;
using SharpBrick.PoweredUp.Devices;
using SharpBrick.PoweredUp.Protocol;

Expand All @@ -16,13 +15,14 @@ public IPoweredUpDevice Create(DeviceType deviceType)
=> null;

public IPoweredUpDevice CreateConnected(DeviceType deviceType, IPoweredUpProtocol protocol, byte hubId, byte portId)
=> null;
=> new DynamicDevice(protocol, hubId, portId);
}
public class ExampleDynamicDevice : BaseExample
{
public override void Configure(IServiceCollection serviceCollection)
public override void Configure(IServiceCollection services)
{
serviceCollection.AddSingleton<IDeviceFactory, EmptyDeviceFactory>();
// pretend we do not know the device and use only dynamic ones
services.AddSingleton<IDeviceFactory, EmptyDeviceFactory>();
}

public override async Task ExecuteAsync()
Expand All @@ -31,31 +31,40 @@ public override async Task ExecuteAsync()

using (var technicMediumHub = host.FindByType<TechnicMediumHub>())
{
var model = new DeploymentModelBuilder()
// deployment model verification with unknown devices
await technicMediumHub.VerifyDeploymentModelAsync(mb => mb
.AddAnyHub(hubBuilder => hubBuilder
.AddAnyDevice(0))
.Build();
);

model.Verify(technicMediumHub.Protocol);
var dynamicDeviceWhichIsAMotor = technicMediumHub.Port(0).GetDevice<DynamicDevice>();

var dynamicDeviceWhichIsAMotor = new DynamicDevice(technicMediumHub.Protocol, technicMediumHub.HubId, 0);
await dynamicDeviceWhichIsAMotor.DiscoverAsync();
// or also direct from a protocol
//var dynamicDeviceWhichIsAMotor = new DynamicDevice(technicMediumHub.Protocol, technicMediumHub.HubId, 0);

// discover the unknown device using the LWP
await dynamicDeviceWhichIsAMotor.DiscoverAsync();
logger.LogInformation("Discovery completed");

// use combined mode values from the device
await dynamicDeviceWhichIsAMotor.TryLockDeviceForCombinedModeNotificationSetupAsync(2, 3);
await dynamicDeviceWhichIsAMotor.SetupNotificationAsync(2, true);
await dynamicDeviceWhichIsAMotor.SetupNotificationAsync(3, true);
await dynamicDeviceWhichIsAMotor.UnlockFromCombinedModeNotificationSetupAsync(true);

using var disposable = dynamicDeviceWhichIsAMotor.SingleValueMode<int>(2).Observable.Subscribe(x => logger.LogWarning($"Position: {x.SI} / {x.Pct}"));
using var disposable2 = dynamicDeviceWhichIsAMotor.SingleValueMode<short>(3).Observable.Subscribe(x => logger.LogWarning($"Absolute Position: {x.SI} / {x.Pct}"));
// get the individual modes for input and output
var powerMode = dynamicDeviceWhichIsAMotor.SingleValueMode<sbyte>(0);
var posMode = dynamicDeviceWhichIsAMotor.SingleValueMode<int>(2);
var aposMode = dynamicDeviceWhichIsAMotor.SingleValueMode<short>(3);

await dynamicDeviceWhichIsAMotor.SingleValueMode<sbyte>(0).WriteDirectModeDataAsync(0x64); // That is StartPower on a motor
// use their observables to report values
using var disposable = posMode.Observable.Subscribe(x => logger.LogWarning($"Position: {x.SI} / {x.Pct}"));
using var disposable2 = aposMode.Observable.Subscribe(x => logger.LogWarning($"Absolute Position: {x.SI} / {x.Pct}"));

// or even write to them
await powerMode.WriteDirectModeDataAsync(0x64); // That is StartPower on a motor
await Task.Delay(2_000);

await dynamicDeviceWhichIsAMotor.SingleValueMode<sbyte>(0).WriteDirectModeDataAsync(0x00); // That is Stop on a motor
await powerMode.WriteDirectModeDataAsync(0x00); // That is Stop on a motor

await Task.Delay(10_000);

Expand Down
2 changes: 1 addition & 1 deletion src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public IPoweredUpDevice CreateConnected(DeviceType deviceType, IPoweredUpProtoco
{
var type = GetTypeFromDeviceType(deviceType);

return (type == null) ? null : (IPoweredUpDevice)Activator.CreateInstance(type, protocol, hubId, portId);
return (type == null) ? new DynamicDevice(protocol, hubId, portId) : (IPoweredUpDevice)Activator.CreateInstance(type, protocol, hubId, portId);
}

public Type GetTypeFromDeviceType(DeviceType deviceType)
Expand Down

0 comments on commit 5331409

Please sign in to comment.