-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathDeviceFactory.cs
More file actions
69 lines (62 loc) · 3.33 KB
/
DeviceFactory.cs
File metadata and controls
69 lines (62 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Device.Net
{
public sealed class DeviceFactory : IDeviceFactory
{
#region Fields
#pragma warning disable IDE0052 // Remove unread private members
private readonly ILogger _logger;
#pragma warning restore IDE0052 // Remove unread private members
private readonly ILoggerFactory _loggerFactory;
private readonly GetConnectedDeviceDefinitionsAsync _getConnectedDevicesAsync;
private readonly GetDeviceAsync _getDevice;
private readonly Func<ConnectedDeviceDefinition, CancellationToken, Task<bool>> _supportsDevice;
#endregion
#region Constructor
/// <summary>
/// Constructs a DeviceFactory
/// </summary>
/// <param name="loggerFactory">The factory for creating new loggers for each device</param>
/// <param name="getConnectedDevicesAsync">A delegate that returns matching connected device definitions</param>
/// <param name="getDevice">A delegate to construct the device based on the specified connected device definition</param>
/// <param name="supportsDevice">A delegate that returns whether or not this factory supports the connected device</param>
public DeviceFactory(
ILoggerFactory loggerFactory,
GetConnectedDeviceDefinitionsAsync getConnectedDevicesAsync,
GetDeviceAsync getDevice,
Func<ConnectedDeviceDefinition, CancellationToken, Task<bool>> supportsDevice
)
{
_getConnectedDevicesAsync = getConnectedDevicesAsync ?? throw new ArgumentNullException(nameof(getConnectedDevicesAsync));
_loggerFactory = loggerFactory ?? NullLoggerFactory.Instance;
_logger = _loggerFactory.CreateLogger<DeviceFactory>();
_getDevice = getDevice;
_supportsDevice = supportsDevice ?? throw new ArgumentNullException(nameof(supportsDevice));
}
#endregion
#region Public Methods
public Task<bool> SupportsDeviceAsync(ConnectedDeviceDefinition connectedDeviceDefinition, CancellationToken cancellationToken = default)
{
_logger.LogInformation("Checking if the factory supports this connected device. Device Definition: {deviceDefinition}", connectedDeviceDefinition);
return _supportsDevice(connectedDeviceDefinition, cancellationToken);
}
public Task<IEnumerable<ConnectedDeviceDefinition>> GetConnectedDeviceDefinitionsAsync(CancellationToken cancellationToken = default)
{
_logger.LogInformation("Getting connected device definitions...");
return _getConnectedDevicesAsync(cancellationToken);
}
public Task<IDevice> GetDeviceAsync(ConnectedDeviceDefinition connectedDeviceDefinition, CancellationToken cancellationToken = default)
{
_logger.LogInformation("Getting device for connected device {deviceDefinition}", connectedDeviceDefinition);
return connectedDeviceDefinition == null ?
throw new ArgumentNullException(nameof(connectedDeviceDefinition)) :
_getDevice(connectedDeviceDefinition, cancellationToken);
}
#endregion
}
}