SSPC-Tester/SSPCTester.Devices/Config/DaqChannelOptionsNormalizer.cs

39 lines
1.4 KiB
C#
Raw Permalink Normal View History

using SSPCTester.Devices.Interfaces;
namespace SSPCTester.Devices.Config;
public static class DaqChannelOptionsNormalizer
{
public const int ChannelSlots = 8;
public static List<DaqChannelOptions> Normalize(IEnumerable<DaqChannelOptions>? channels)
{
var byPhysicalChannel = (channels ?? Enumerable.Empty<DaqChannelOptions>())
.Where(x => x.PhysicalChannel is >= 0 and < ChannelSlots)
.GroupBy(x => x.PhysicalChannel)
.ToDictionary(x => x.Key, x => x.First());
var normalized = new List<DaqChannelOptions>(ChannelSlots);
for (int physicalChannel = 0; physicalChannel < ChannelSlots; physicalChannel++)
{
byPhysicalChannel.TryGetValue(physicalChannel, out var source);
normalized.Add(new DaqChannelOptions
{
PhysicalChannel = physicalChannel,
Role = NormalizeRole(source?.Role),
Name = string.IsNullOrWhiteSpace(source?.Name) ? $"CH{physicalChannel}" : source.Name,
Scale = source?.Scale ?? 1,
Offset = source?.Offset ?? 0,
IsCalibrated = source?.IsCalibrated ?? false
});
}
return normalized;
}
public static string NormalizeRole(string? role) =>
Enum.TryParse<DaqChannelRole>(role, ignoreCase: true, out var parsed)
? parsed.ToString()
: nameof(DaqChannelRole.Unconfigured);
}