using System.Diagnostics.CodeAnalysis; using InnovEnergy.Lib.Devices.Battery48TL.DataTypes; using InnovEnergy.Lib.Protocols.Modbus.Conversions; using static InnovEnergy.Lib.Devices.Battery48TL.DataTypes.LedState; using static InnovEnergy.Lib.Devices.Battery48TL.DataTypes.TemperatureState; namespace InnovEnergy.Lib.Devices.Battery48TL; using R = Battery48TlRegisters; internal static class Parser { internal static Boolean ParseEoc(this R r) { return r.Leds is { Green: On, Amber: Off, Blue : Off }; } internal static Leds ParseLeds(this R r) { LedState ParseLed(LedColor led) => (LedState)((r._LedStates >> (Int32)led) & 3); return new() { Blue = ParseLed(LedColor.Blue), Green = ParseLed(LedColor.Green), Amber = ParseLed(LedColor.Amber), Red = ParseLed(LedColor.Red), }; } internal static Temperatures ParseBatteryTemperatures(this R r) => new() { Board = r._TemperatureBoard, Cells = r.ParseCells(), State = r.ParseTemperatureState() }; private static TemperatureState ParseTemperatureState(this R r) => r.Leds switch { { Green: >= Blinking, Blue: >= Blinking } => Cold, _ => Operation, // TODO: overheated }; private static CellTemperatures ParseCells(this R r) => new() { Left = r._TemperatureCellsLeft, Right = r._TemperatureCellsRight, Center = r._TemperatureCellsCenter }; internal static Boolean ParseConnectedToDcBus(this R r) => (r._IoStates & 1) == 0; // inverted internal static Boolean ParseHeating (this R r) => (r._IoStates & 64) != 0; [SuppressMessage("ReSharper", "StringLiteralTypo")] internal static IEnumerable ParseAlarms(this R data) { Boolean HasBit(Int16 bit) => (data._AlarmFlags & 1uL << bit) > 0; if (HasBit(0) ) yield return "Tam : BMS temperature too low"; if (HasBit(2) ) yield return "TaM2 : BMS temperature too high"; if (HasBit(3) ) yield return "Tbm : Battery temperature too low"; if (HasBit(5) ) yield return "TbM2 : Battery temperature too high"; if (HasBit(7) ) yield return "VBm2 : Bus voltage too low"; if (HasBit(9) ) yield return "VBM2 : Bus voltage too high"; if (HasBit(11)) yield return "IDM2 : Discharge current too high"; if (HasBit(12)) yield return "ISOB : Electrical insulation failure"; if (HasBit(13)) yield return "MSWE : Main switch failure"; if (HasBit(14)) yield return "FUSE : Main fuse blown"; if (HasBit(15)) yield return "HTRE : Battery failed to warm up"; if (HasBit(16)) yield return "TCPE : Temperature sensor failure"; if (HasBit(17)) yield return "STRE :"; if (HasBit(18)) yield return "CME : Current sensor failure"; if (HasBit(19)) yield return "HWFL : BMS hardware failure"; if (HasBit(20)) yield return "HWEM : Hardware protection tripped"; if (HasBit(21)) yield return "ThM : Heatsink temperature too high"; if (HasBit(22)) yield return "vsm1 : String voltage too low"; if (HasBit(23)) yield return "vsm2 : Low string voltage failure"; if (HasBit(25)) yield return "vsM2 : String voltage too high"; if (HasBit(27)) yield return "iCM2 : Charge current too high"; if (HasBit(29)) yield return "iDM2 : Discharge current too high"; if (HasBit(31)) yield return "MID2 : String voltage unbalance too high"; if (HasBit(33)) yield return "CCBF : Internal charger hardware failure"; if (HasBit(34)) yield return "AhFL :"; if (HasBit(36)) yield return "TbCM :"; if (HasBit(37)) yield return "BRNF :"; if (HasBit(42)) yield return "HTFS : If Heaters Fuse Blown"; if (HasBit(43)) yield return "DATA : Parameters out of range"; if (HasBit(45)) yield return "CELL2:"; } [SuppressMessage("ReSharper", "StringLiteralTypo")] internal static IEnumerable ParseWarnings(this R data) { Boolean HasBit(Int16 bit) => (data._WarningFlags & 1uL << bit) > 0; if (HasBit(1) ) yield return "TaM1: BMS temperature high"; if (HasBit(4) ) yield return "TbM1: Battery temperature high"; if (HasBit(6) ) yield return "VBm1: Bus voltage low"; if (HasBit(8) ) yield return "VBM1: Bus voltage high"; if (HasBit(10)) yield return "IDM1: Discharge current high"; if (HasBit(24)) yield return "vsM1: String voltage high"; if (HasBit(26)) yield return "iCM1: Charge current high"; if (HasBit(28)) yield return "iDM1: Discharge current high"; if (HasBit(30)) yield return "MID1: String voltages unbalanced"; if (HasBit(32)) yield return "BLPW: Not enough charging power on bus"; if (HasBit(35)) yield return "Ah_W: String SOC low"; if (HasBit(38)) yield return "MPMM: Midpoint wiring problem"; if (HasBit(39)) yield return "TCMM:"; if (HasBit(40)) yield return "TCdi: Temperature difference between strings high"; if (HasBit(41)) yield return "WMTO:"; if (HasBit(44)) yield return "bit44:"; if (HasBit(46)) yield return "CELL1:"; } }