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 Battery48TlRecords Read()
{
try
public Battery48TlRecords? Read()
{
var records = _Devices
.Select(TryRead)
.NotNull()
.ToArray(_Devices.Count);
.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)

View File

@ -6,38 +6,39 @@ namespace InnovEnergy.Lib.Devices.Battery48TL;
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;
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
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<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>());
}