Innovenergy_trunk/csharp/App/SaliMax/src/Controller/AvgBatteriesStatus.cs

56 lines
2.0 KiB
C#

using InnovEnergy.Lib.Devices.Battery48TL;
using InnovEnergy.Lib.StatusApi;
using InnovEnergy.Lib.Units;
using InnovEnergy.Lib.Units.Composite;
using static InnovEnergy.Lib.Devices.Battery48TL.TemperatureState;
namespace InnovEnergy.App.SaliMax.Controller;
public static class AvgBatteriesStatus
{
public static CombinedStatus<Battery48TLStatus>? Combine(this IReadOnlyList<Battery48TLStatus> stati)
{
var combined = stati.Count == 0
? null
: new Battery48TLStatus
{
Soc = stati.Min(b => b.Soc),
Temperature = stati.Average(b => b.Temperature),
Dc = new DcBus
{
Voltage = stati.Average(b => b.Dc.Voltage),
Current = stati.Sum(b => b.Dc.Current),
},
Alarms = stati.SelectMany(b => b.Alarms).Distinct().ToList(),
Warnings = stati.SelectMany(b => b.Warnings).Distinct().ToList(),
MaxChargingPower = stati.Sum(b => b.MaxChargingPower),
MaxDischargingPower = stati.Sum(b => b.MaxDischargingPower),
Heating = stati.Any(b => b.Heating),
AmberLed = LedState.Off, // not used for combined battery
BlueLed = LedState.Off,
RedLed = LedState.Off,
GreenLed = LedState.Off,
CellsVoltage = stati.Average(b => b.CellsVoltage),
ConnectedToDc = stati.Any(b => b.ConnectedToDc),
TemperatureState = stati.Any(b => b.TemperatureState == OperatingTemperature) // TODO: revisit when we have the overheated state
? OperatingTemperature
: Cold,
TotalCurrent = stati.Average(b => b.TotalCurrent),
EocReached = stati.All(b => b.EocReached),
};
return new CombinedStatus<Battery48TLStatus>
{
Combined = combined!,
Children = stati
};
}
}