use "required"

This commit is contained in:
ig 2023-08-30 16:47:28 +02:00
parent 71ba569085
commit d09c53ad92
2 changed files with 36 additions and 45 deletions

View File

@ -8,24 +8,14 @@ public class Battery48TlDevices
public Battery48TlDevices(IReadOnlyList<Battery48TlDevice> devices) => _Devices = devices; public Battery48TlDevices(IReadOnlyList<Battery48TlDevice> devices) => _Devices = devices;
public Battery48TlRecords Read() public Battery48TlRecords? Read()
{ {
try var records = _Devices
{ .Select(TryRead)
var records = _Devices .NotNull()
.Select(TryRead) .ToList();
.NotNull()
.ToArray(_Devices.Count);
return new Battery48TlRecords(records); return Battery48TlRecords.FromBatteries(records);
}
catch (Exception e)
{
Console.WriteLine("Failed to read Battery data \n" + e.Message);
// TODO: log
return Battery48TlRecords.Null;
}
} }
private static Battery48TlRecord? TryRead(Battery48TlDevice d) private static Battery48TlRecord? TryRead(Battery48TlDevice d)

View File

@ -6,38 +6,39 @@ namespace InnovEnergy.Lib.Devices.Battery48TL;
public class Battery48TlRecords public class Battery48TlRecords
{ {
public Battery48TlRecords(IReadOnlyList<Battery48TlRecord> records) public required DcBus Dc { get; init; }
public required Boolean Eoc { get; init; }
public required IReadOnlyList<String> Warnings { get; init; }
public required IReadOnlyList<String> 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<Battery48TlRecord> Devices { get; init; }
public static Battery48TlRecords? FromBatteries(IReadOnlyList<Battery48TlRecord>? records)
{ {
var empty = records.Count == 0; if (records is null || records.Count == 0)
return null;
Devices = records; return new Battery48TlRecords
Eoc = !empty && records.All(r => r.Eoc); {
Warnings = records.SelectMany(r => r.Warnings).Distinct().ToList(); Devices = records,
Alarms = records.SelectMany(r => r.Alarms) .Distinct().ToList(); Eoc = records.All(r => r.Eoc),
Soc = empty ? 0 : records.Average(r => r.Soc.Value); Warnings = records.SelectMany(r => r.Warnings).Distinct().ToList(),
CurrentMinSoc = empty ? 0 : records.Min(r => r.Soc.Value); Alarms = records.SelectMany(r => r.Alarms).Distinct().ToList(),
Temperature = records.Any() ? records.Average(b => b.Temperatures.Cells.Average.Value) : 0; Soc = records.Average(r => r.Soc.Value),
HeatingPower = records.Any() ? records.Sum(b => b.HeatingPower) : 0; CurrentMinSoc = records.Min(r => r.Soc.Value),
Temperature = records.Average(b => b.Temperatures.Cells.Average.Value),
HeatingPower = records.Sum(b => b.HeatingPower),
Dc = empty Dc = DcBus.FromVoltageCurrent
? DcBus.Null (
: DcBus.FromVoltageCurrent records.Average(r => r.Dc.Voltage),
( records.Sum(r => r.Dc.Current)
records.Average(r => r.Dc.Voltage), )
records.Sum(r => r.Dc.Current) };
);
} }
public DcBus Dc { get; init; }
public Boolean Eoc { get; init; }
public IReadOnlyList<String> Warnings { get; init; }
public IReadOnlyList<String> 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<Battery48TlRecord> Devices { get; init; }
public static Battery48TlRecords Null => new Battery48TlRecords(Array.Empty<Battery48TlRecord>());
} }