diff --git a/src/Device.Net/FactoryBuilder.cs b/src/Device.Net/FactoryBuilder.cs new file mode 100644 index 00000000..de4db2a2 --- /dev/null +++ b/src/Device.Net/FactoryBuilder.cs @@ -0,0 +1,36 @@ +using Microsoft.Extensions.Logging; +using System.Collections.Generic; +using System.Collections.ObjectModel; + +namespace Device.Net +{ + public class FactoryBuilder : IFactoryBuilder + { + public ILoggerFactory LoggerFactory { get; } + public ReadOnlyCollection Factories { get; } + + public FactoryBuilder(ILoggerFactory loggerFactory = null) + { + LoggerFactory = loggerFactory; + Factories = new List().AsReadOnly(); + } + + public FactoryBuilder(IDeviceFactory deviceFactory, ILoggerFactory loggerFactory = null) + { + LoggerFactory = loggerFactory; + Factories = deviceFactory == null + ? new List().AsReadOnly() + : new ReadOnlyCollection(new IDeviceFactory[] { deviceFactory }); + } + + public FactoryBuilder(IList deviceFactories, ILoggerFactory loggerFactory = null) + { + LoggerFactory = loggerFactory; + Factories = deviceFactories == null + ? new List().AsReadOnly() + : new ReadOnlyCollection(deviceFactories); + } + + public IDeviceFactory Build() => Factories.Aggregate(); + } +} diff --git a/src/Device.Net/IFactoryBuilder.cs b/src/Device.Net/IFactoryBuilder.cs new file mode 100644 index 00000000..fdc25ede --- /dev/null +++ b/src/Device.Net/IFactoryBuilder.cs @@ -0,0 +1,12 @@ +using Microsoft.Extensions.Logging; +using System.Collections.ObjectModel; + +namespace Device.Net +{ + public interface IFactoryBuilder + { + ILoggerFactory LoggerFactory { get; } + ReadOnlyCollection Factories { get; } + IDeviceFactory Build(); + } +} diff --git a/src/Hid.Net/Windows/WindowsHidDeviceFactoryExtensions.cs b/src/Hid.Net/Windows/WindowsHidDeviceFactoryExtensions.cs index 1d1a294e..6a9a5094 100644 --- a/src/Hid.Net/Windows/WindowsHidDeviceFactoryExtensions.cs +++ b/src/Hid.Net/Windows/WindowsHidDeviceFactoryExtensions.cs @@ -18,6 +18,78 @@ public static class WindowsHidDeviceFactoryExtensions { #region Public Methods + public static IFactoryBuilder CreateWindowsHidDeviceFactory( + this IFactoryBuilder builder, + FilterDeviceDefinition filterDeviceDefinition, + IHidApiService hidApiService = null, + Guid? classGuid = null, + ushort? readBufferSize = null, + ushort? writeBufferSize = null, + GetConnectedDeviceDefinitionsAsync getConnectedDeviceDefinitionsAsync = null, + Func readReportTransform = null, + Func readTransferTransform = null, + Func writeTransferTransform = null, + WriteReportTransform writeReportTransform = null) + { + _ = builder ?? throw new ArgumentNullException(nameof(builder)); + + var factories = new List(builder.Factories) + { + CreateWindowsHidDeviceFactory + ( + new ReadOnlyCollection(new List { filterDeviceDefinition }), + builder.LoggerFactory, + hidApiService, + classGuid, + readBufferSize, + writeBufferSize, + getConnectedDeviceDefinitionsAsync, + readReportTransform, + readTransferTransform, + writeTransferTransform, + writeReportTransform + ) + }; + + return new FactoryBuilder(factories, builder.LoggerFactory); + } + + public static IFactoryBuilder CreateWindowsHidDeviceFactory( + this IFactoryBuilder builder, + FilterDeviceDefinition[] filterDeviceDefinitions, + IHidApiService hidApiService = null, + Guid? classGuid = null, + ushort? readBufferSize = null, + ushort? writeBufferSize = null, + GetConnectedDeviceDefinitionsAsync getConnectedDeviceDefinitionsAsync = null, + Func readReportTransform = null, + Func readTransferTransform = null, + Func writeTransferTransform = null, + WriteReportTransform writeReportTransform = null) + { + _ = builder ?? throw new ArgumentNullException(nameof(builder)); + + var factories = new List(builder.Factories) + { + CreateWindowsHidDeviceFactory + ( + new ReadOnlyCollection(filterDeviceDefinitions), + builder.LoggerFactory, + hidApiService, + classGuid, + readBufferSize, + writeBufferSize, + getConnectedDeviceDefinitionsAsync, + readReportTransform, + readTransferTransform, + writeTransferTransform, + writeReportTransform + ) + }; + + return new FactoryBuilder(factories, builder.LoggerFactory); + } + /// /// Creates a for Windows Hid devices ///