Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ public class DeviceManagementModule : Module
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<BluetoothDeviceManager>().As<IBluetoothDeviceManager>().SingleInstance();
builder.RegisterType<InfraredDeviceManager>().As<IInfraredDeviceManager>().SingleInstance();

builder.RegisterType<DeviceRepository>().As<IDeviceRepository>().SingleInstance();
builder.RegisterType<DeviceManager>().As<IDeviceManager>().SingleInstance();
builder.RegisterType<ManualDeviceManager>().As<IManualDeviceManager>().SingleInstance();

builder.RegisterType<InfraredDevice>().Keyed<Device>(DeviceType.Infrared);
builder.RegisterType<CircuitCubeDevice>().Keyed<Device>(DeviceType.CircuitCubes);
builder.RegisterType<PfxBrickDevice>().Keyed<Device>(DeviceType.PfxBrick);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public DeviceFactoryData(TVendor vendor, string name, string address, byte[] dev

public DeviceType DeviceType => TDevice.Type;

public bool IsAvailable => Vendor.IsAvailable;
public TVendor Vendor { get; }
public string Name { get; }
public string Address { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BrickController2.Helpers;
using BrickController2.DeviceManagement.InfraredDevice;
using BrickController2.Helpers;
using BrickController2.UI.Services.MainThread;
using Microsoft.Extensions.Logging;
using System;
Expand All @@ -12,7 +13,6 @@ namespace BrickController2.DeviceManagement
internal class DeviceManager : NotifyPropertyChangedSource, IDeviceManager
{
private readonly IBluetoothDeviceManager _bluetoothDeviceManager;
private readonly IInfraredDeviceManager _infraredDeviceManager;
private readonly IDeviceRepository _deviceRepository;
private readonly DeviceFactory _deviceFactory;
private readonly IMainThreadService _uiThreadService;
Expand All @@ -25,14 +25,12 @@ internal class DeviceManager : NotifyPropertyChangedSource, IDeviceManager

public DeviceManager(
IBluetoothDeviceManager bluetoothDeviceManager,
IInfraredDeviceManager infraredDeviceManager,
IDeviceRepository deviceRepository,
DeviceFactory deviceFactory,
IMainThreadService uiThreadService,
ILogger<DeviceManager> logger)
{
_bluetoothDeviceManager = bluetoothDeviceManager;
_infraredDeviceManager = infraredDeviceManager;
_deviceRepository = deviceRepository;
_deviceFactory = deviceFactory;
_uiThreadService = uiThreadService;
Expand Down Expand Up @@ -88,12 +86,7 @@ public async Task<bool> ScanAsync(CancellationToken token)

try
{
var infraScan = _infraredDeviceManager.ScanAsync(CreateDeviceAsync!, token);
var bluetoothScan = _bluetoothDeviceManager.ScanAsync(CreateDeviceAsync!, token);

await Task.WhenAll(infraScan, bluetoothScan);

return infraScan.Result && bluetoothScan.Result;
return await _bluetoothDeviceManager.ScanAsync(CreateDeviceAsync!, token);
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace BrickController2.DeviceManagement
{
public interface IDeviceFactoryData
{
bool IsAvailable { get; }
DeviceType DeviceType { get; }
string Name { get; }
string Address { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Threading.Tasks;

namespace BrickController2.DeviceManagement
namespace BrickController2.DeviceManagement.InfraredDevice
{
internal interface IInfraredDeviceManager : IDeviceScanner
internal interface IInfraredDeviceManager
{
Task<DeviceConnectionResult> ConnectDevice(InfraredDevice device);
Task DisconnectDevice(InfraredDevice device);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Autofac;
using BrickController2.DeviceManagement.DI;
using BrickController2.DeviceManagement.Vendors;
using BrickController2.PlatformServices.Infrared;

namespace BrickController2.DeviceManagement.InfraredDevice;

/// <summary>
/// fake Vendor for infrared devices
/// </summary>
internal class InfraRedDeviceVendor : Vendor<InfraRedDeviceVendor>
{
private IInfraredService? _infraredService;

public override string VendorName => "IRDevice";

public override bool IsAvailable => _infraredService != null && _infraredService.IsInfraredSupported && _infraredService.IsCarrierFrequencySupported(InfraredDeviceManager.IR_FREQUENCY);

protected override void Register(VendorBuilder<InfraRedDeviceVendor> builder)
{
// device manager
builder.ContainerBuilder.RegisterType<InfraredDeviceManager>().As<IInfraredDeviceManager>().SingleInstance();

builder.ContainerBuilder.RegisterBuildCallback(scope =>
{
_infraredService = scope.Resolve<IInfraredService>();
});

// manually added devices
builder.RegisterDevice<InfraredDevice>()
.WithDeviceFactory("0", "PF Infra 1")
.WithDeviceFactory("1", "PF Infra 2")
.WithDeviceFactory("2", "PF Infra 3")
.WithDeviceFactory("3", "PF Infra 4");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Threading;
using System.Threading.Tasks;

namespace BrickController2.DeviceManagement
namespace BrickController2.DeviceManagement.InfraredDevice
{
internal class InfraredDevice : Device
internal class InfraredDevice : Device, IDeviceType<InfraredDevice>
{
private readonly IInfraredDeviceManager _infraredDeviceManager;

Expand All @@ -15,7 +15,10 @@ public InfraredDevice(string name, string address, byte[] deviceData, IInfraredD
_infraredDeviceManager = infraredDeviceManager;
}

public override DeviceType DeviceType => DeviceType.Infrared;
public static DeviceType Type => DeviceType.Infrared;

public static string TypeName => "InfraredDevice";
public override DeviceType DeviceType => Type;
public override int NumberOfChannels => 2;

public override async Task<DeviceConnectionResult> ConnectAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
using BrickController2.PlatformServices.Infrared;
using BrickController2.Helpers;

namespace BrickController2.DeviceManagement
namespace BrickController2.DeviceManagement.InfraredDevice
{
internal class InfraredDeviceManager : IInfraredDeviceManager
{
private const int IR_FREQUENCY = 38000;
public const int IR_FREQUENCY = 38000;

private const int IR_MARK_US = 158;
private const int IR_START_GAP_US = 1026;
Expand All @@ -34,27 +34,6 @@ public InfraredDeviceManager(IInfraredService infraredService)
_infraredService = infraredService;
}

public async Task<bool> ScanAsync(Func<DeviceType, string, string, byte[]?, Task> deviceFoundCallback, CancellationToken token)
{
using (await _asyncLock.LockAsync())
{
if (_infraredService.IsInfraredSupported && _infraredService.IsCarrierFrequencySupported(IR_FREQUENCY))
{
for (int i = 0; i < 4; i++)
{
if (token.IsCancellationRequested)
{
break;
}

await deviceFoundCallback(DeviceType.Infrared, $"PF Infra {i + 1}", $"{i}", null);
}
}
}

return true;
}

public async Task<DeviceConnectionResult> ConnectDevice(InfraredDevice device)
{
using (await _asyncLock.LockAsync())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public abstract class Vendor<TVendor> : Module, IVendorModule
{
public abstract string VendorName { get; }

public virtual bool IsAvailable => true;

protected abstract void Register(VendorBuilder<TVendor> builder);

protected sealed override void Load(ContainerBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public ManualDeviceListPageViewModel(

var groups = manualDeviceManager.FactoryDataList
// apply ordering per vendor and device type
.Where(o => o.IsAvailable)
.OrderBy(o => o.VendorName)
.ThenBy(o => o.DeviceTypeName)
.GroupBy(o => (o.VendorName, o.DeviceType, o.DeviceTypeName), x => new DeviceEntry(x, GetDeviceInstance(x)));
Expand Down