From d09c53ad9246a30ad25955eb6b8a6a160fa8e0e9 Mon Sep 17 00:00:00 2001 From: ig Date: Wed, 30 Aug 2023 16:47:28 +0200 Subject: [PATCH] use "required" --- .../Devices/Battery48TL/Battery48TlDevices.cs | 22 ++----- .../Devices/Battery48TL/Battery48TlRecords.cs | 59 ++++++++++--------- 2 files changed, 36 insertions(+), 45 deletions(-) diff --git a/csharp/Lib/Devices/Battery48TL/Battery48TlDevices.cs b/csharp/Lib/Devices/Battery48TL/Battery48TlDevices.cs index bdd25d8d8..586937929 100644 --- a/csharp/Lib/Devices/Battery48TL/Battery48TlDevices.cs +++ b/csharp/Lib/Devices/Battery48TL/Battery48TlDevices.cs @@ -8,24 +8,14 @@ public class Battery48TlDevices public Battery48TlDevices(IReadOnlyList devices) => _Devices = devices; - public Battery48TlRecords Read() + public Battery48TlRecords? Read() { - try - { - var records = _Devices - .Select(TryRead) - .NotNull() - .ToArray(_Devices.Count); + var records = _Devices + .Select(TryRead) + .NotNull() + .ToList(); - return new Battery48TlRecords(records); - } - catch (Exception e) - { - Console.WriteLine("Failed to read Battery data \n" + e.Message); - // TODO: log - - return Battery48TlRecords.Null; - } + return Battery48TlRecords.FromBatteries(records); } private static Battery48TlRecord? TryRead(Battery48TlDevice d) diff --git a/csharp/Lib/Devices/Battery48TL/Battery48TlRecords.cs b/csharp/Lib/Devices/Battery48TL/Battery48TlRecords.cs index d0adbe985..93d57fa32 100644 --- a/csharp/Lib/Devices/Battery48TL/Battery48TlRecords.cs +++ b/csharp/Lib/Devices/Battery48TL/Battery48TlRecords.cs @@ -6,38 +6,39 @@ namespace InnovEnergy.Lib.Devices.Battery48TL; public class Battery48TlRecords { - public Battery48TlRecords(IReadOnlyList records) + public required DcBus Dc { get; init; } + public required Boolean Eoc { get; init; } + public required IReadOnlyList Warnings { get; init; } + public required IReadOnlyList Alarms { get; init; } + public required Percent Soc { get; init; } + public required Percent CurrentMinSoc { get; init; } + public required Temperature Temperature { get; init; } + public required DcPower HeatingPower { get; init; } + + public required IReadOnlyList Devices { get; init; } + + public static Battery48TlRecords? FromBatteries(IReadOnlyList? records) { - var empty = records.Count == 0; + if (records is null || records.Count == 0) + return null; - Devices = records; - Eoc = !empty && records.All(r => r.Eoc); - Warnings = records.SelectMany(r => r.Warnings).Distinct().ToList(); - Alarms = records.SelectMany(r => r.Alarms) .Distinct().ToList(); - Soc = empty ? 0 : records.Average(r => r.Soc.Value); - CurrentMinSoc = empty ? 0 : records.Min(r => r.Soc.Value); - Temperature = records.Any() ? records.Average(b => b.Temperatures.Cells.Average.Value) : 0; - HeatingPower = records.Any() ? records.Sum(b => b.HeatingPower) : 0; + return new Battery48TlRecords + { + Devices = records, + Eoc = records.All(r => r.Eoc), + Warnings = records.SelectMany(r => r.Warnings).Distinct().ToList(), + Alarms = records.SelectMany(r => r.Alarms).Distinct().ToList(), + Soc = records.Average(r => r.Soc.Value), + CurrentMinSoc = records.Min(r => r.Soc.Value), + Temperature = records.Average(b => b.Temperatures.Cells.Average.Value), + HeatingPower = records.Sum(b => b.HeatingPower), - Dc = empty - ? DcBus.Null - : DcBus.FromVoltageCurrent - ( - records.Average(r => r.Dc.Voltage), - records.Sum(r => r.Dc.Current) - ); + Dc = DcBus.FromVoltageCurrent + ( + records.Average(r => r.Dc.Voltage), + records.Sum(r => r.Dc.Current) + ) + }; } - public DcBus Dc { get; init; } - public Boolean Eoc { get; init; } - public IReadOnlyList Warnings { get; init; } - public IReadOnlyList Alarms { get; init; } - public Percent Soc { get; init; } - public Percent CurrentMinSoc { get; init; } - public Temperature Temperature { get; init; } - public DcPower HeatingPower { get; init; } - - public IReadOnlyList Devices { get; init; } - - public static Battery48TlRecords Null => new Battery48TlRecords(Array.Empty()); } \ No newline at end of file