From cba19ee1eddffb70e5db81a17d3d9f2a9876be9b Mon Sep 17 00:00:00 2001 From: atef Date: Thu, 23 Feb 2023 16:32:06 +0100 Subject: [PATCH] Restructure EmuMeter Status and add AvgBatteries file --- csharp/app/SaliMax/SaliMax.csproj | 4 - csharp/app/SaliMax/src/AsciiArt.cs | 35 +---- .../src/Controller/AvgBatteriesStatus.cs | 51 +++++++ csharp/global.json | 1 + csharp/lib/Devices/EmuMeter/EmuMeterDevice.cs | 125 +++++++++++------- csharp/lib/Devices/EmuMeter/EmuMeterStatus.cs | 63 +++------ 6 files changed, 153 insertions(+), 126 deletions(-) create mode 100644 csharp/app/SaliMax/src/Controller/AvgBatteriesStatus.cs diff --git a/csharp/app/SaliMax/SaliMax.csproj b/csharp/app/SaliMax/SaliMax.csproj index 52f0b084f..0893e2a24 100644 --- a/csharp/app/SaliMax/SaliMax.csproj +++ b/csharp/app/SaliMax/SaliMax.csproj @@ -22,9 +22,5 @@ - - - - \ No newline at end of file diff --git a/csharp/app/SaliMax/src/AsciiArt.cs b/csharp/app/SaliMax/src/AsciiArt.cs index c23fe66b4..c8ffc4596 100644 --- a/csharp/app/SaliMax/src/AsciiArt.cs +++ b/csharp/app/SaliMax/src/AsciiArt.cs @@ -27,46 +27,13 @@ public static class AsciiArt .JoinLines(); } - public static String CreateBox(String title, String value) - { - if (value == "0") - { - value = ""; - } - - var contentWidth = Math.Max(title.Length, value.Length); - - var horizontal = "".PadRight(contentWidth + 2, '-').SurroundWith("+"); - - var t = title.PadRight(contentWidth).SurroundWith(" ").SurroundWith("|"); - - var x = value.SplitLines().ToArray(); - var values = ""; - - foreach (var line in x) - { - var temp = values; - values = line.PadRight(contentWidth).SurroundWith(" ").SurroundWith("|") + "\n"; - values += temp; - } - - values = values.Remove(values.LastIndexOf(Environment.NewLine)); - - return StringUtils.JoinLines( - horizontal, - t, - values, - horizontal - ); - } - public static String CreateHorizontalArrow(Decimal value, String separator) { var valueToString = " " + value.W(); if (value == 0) { - valueToString = "it's 0"; + valueToString = ""; } var contentWidth = separator.Length; diff --git a/csharp/app/SaliMax/src/Controller/AvgBatteriesStatus.cs b/csharp/app/SaliMax/src/Controller/AvgBatteriesStatus.cs new file mode 100644 index 000000000..7b90a6640 --- /dev/null +++ b/csharp/app/SaliMax/src/Controller/AvgBatteriesStatus.cs @@ -0,0 +1,51 @@ +using InnovEnergy.Lib.Devices.Battery48TL; + +namespace InnovEnergy.SaliMax.Controller; + +public class AvgBatteriesStatus +{ + public Decimal Soc { get; set; } + public Decimal Current { get; set; } + public Decimal Voltage { get; set; } + public Decimal Power { get; set; } + public Decimal BusVoltage { get; set; } + public Decimal BatteryTemperature { get; set; } + public IReadOnlyList Warnings { get; set; } + public IReadOnlyList Alarms { get; set; } + public Boolean HeaterOn { get; set; } + public Boolean EocReached { get; set; } + public Boolean BatteryCold { get; set; } + public Decimal MaxChargingPower { get; set; } + public Decimal MaxDischargingPower { get; set; } + + public static AvgBatteriesStatus ReadBatteriesStatus(IReadOnlyList batteriesStatus) + { + var soc = batteriesStatus.Any() ? batteriesStatus.Average(b => b.Soc) : 0; + var current = batteriesStatus.Select(b => b.Dc.Current).Aggregate(0m,(a, b) => a + b); + var voltage = batteriesStatus.Any() ? batteriesStatus.Average(b => b.Dc.Voltage) : 0; + var power = batteriesStatus.Select(b => b.Dc.Power).Aggregate(0m,(a, b) => a + b); + var busVoltage = batteriesStatus.Any() ? batteriesStatus.Average(b => b.BusVoltage): 0; + var batteryTemperature = batteriesStatus.Any() ? batteriesStatus.Average(b => b.Temperature): 0; + var heaterOn = batteriesStatus.Any() && batteriesStatus.Select(b => b.HeaterOn).Aggregate((a, b) => a | b); + var eocReached = batteriesStatus.All(b => b.EocReached); + var batteryCold = batteriesStatus.Any(b => b.BatteryCold); + var maxChargingPower = batteriesStatus.Select(b => b.MaxChargingPower).Aggregate(0m, (a, b) => a + b); + var maxDischargingPower = batteriesStatus.Select(b => b.MaxDischargingPower).Aggregate(0m, (a, b) => a + b); + + + return new AvgBatteriesStatus + { + Soc = soc, + Current = current, + Voltage = voltage, + Power = power, + BusVoltage = busVoltage, + BatteryTemperature = batteryTemperature, + HeaterOn = heaterOn, + EocReached = eocReached, + BatteryCold = batteryCold, + MaxChargingPower = maxChargingPower, + MaxDischargingPower = maxDischargingPower + }; + } +} \ No newline at end of file diff --git a/csharp/global.json b/csharp/global.json index 87932f4e8..9e5e1fd1d 100644 --- a/csharp/global.json +++ b/csharp/global.json @@ -1,5 +1,6 @@ { "sdk": { + "version": "6.0.0", "rollForward": "latestMajor", "allowPrerelease": true } diff --git a/csharp/lib/Devices/EmuMeter/EmuMeterDevice.cs b/csharp/lib/Devices/EmuMeter/EmuMeterDevice.cs index 845ab3f9b..c219755b3 100644 --- a/csharp/lib/Devices/EmuMeter/EmuMeterDevice.cs +++ b/csharp/lib/Devices/EmuMeter/EmuMeterDevice.cs @@ -1,5 +1,9 @@ using InnovEnergy.Lib.Protocols.Modbus.Clients; using InnovEnergy.Lib.Protocols.Modbus.Connections; +using InnovEnergy.Lib.StatusApi.Connections; +using InnovEnergy.Lib.StatusApi.Phases; +using InnovEnergy.Lib.Utils; +using static DecimalMath.DecimalEx; namespace InnovEnergy.Lib.Devices.EmuMeter; @@ -25,7 +29,7 @@ public class EmuMeterDevice return null; } } - + private static Decimal GetPhi(Decimal cosPhi) => cosPhi.Clamp(-1m, 1m).Apply(ACos); private EmuMeterStatus TryReadStatus() { @@ -35,54 +39,81 @@ public class EmuMeterDevice var voltageFreq = Modbus.ReadHoldingRegisters(9200, 112).ToDecimals(); // To check with Ivo var energyTotal = Modbus.ReadHoldingRegisters(6000, 24) .ToUInt64s(); var energyPhases = Modbus.ReadHoldingRegisters(6100, 104).ToUInt64s(); + + var activePowerL123 = powerCurrent[0]; + var activePowerL1 = powerCurrent[1]; + var activePowerL2 = powerCurrent[2]; + var activePowerL3 = powerCurrent[3]; + var reactivePowerL123 = powerCurrent[5]; + var reactivePowerL1 = powerCurrent[6]; + var reactivePowerL2 = powerCurrent[7]; + var reactivePowerL3 = powerCurrent[8]; + var apparentPowerL123 = powerCurrent[10]; + var apparentPowerL1 = powerCurrent[11]; + var apparentPowerL2 = powerCurrent[12]; + var apparentPowerL3 = powerCurrent[13]; + var currentL123 = powerCurrent[50]; + var currentL1 = powerCurrent[51]; + var currentL2 = powerCurrent[52]; + var currentL3 = powerCurrent[53]; + var voltageL1N = voltageFreq[0]; + var voltageL2N = voltageFreq[1]; + var voltageL3N = voltageFreq[2]; + var voltageL1L2 = voltageFreq[3]; + var voltageL2L3 = voltageFreq[4]; + var voltageL3L1 = voltageFreq[5]; + var powerFactorL1 = voltageFreq[50]; + var powerFactorL2 = voltageFreq[51]; + var powerFactorL3 = voltageFreq[52]; + var frequency = voltageFreq[55]; + var energyImportL123 = energyTotal[0 / 4] / 1000.0m; + var energyExportL123 = energyTotal[20 / 4] / 1000.0m; + var energyImportL1 = energyPhases[0 / 4] / 1000.0m; + var energyExportL1 = energyPhases[20 / 4] / 1000.0m; + var energyImportL2 = energyPhases[40 / 4] / 1000.0m; + var energyExportL2 = energyPhases[60 / 4] / 1000.0m; + var energyImportL3 = energyPhases[80 / 4] / 1000.0m; + var energyExportL3 = energyPhases[100 / 4] / 1000.0m; return new EmuMeterStatus - { - ActivePowerL123 = powerCurrent[0], - ActivePowerL1 = powerCurrent[1], - ActivePowerL2 = powerCurrent[2], - ActivePowerL3 = powerCurrent[3], - - ReactivePowerL123 = powerCurrent[5], - ReactivePowerL1 = powerCurrent[6], - ReactivePowerL2 = powerCurrent[7], - ReactivePowerL3 = powerCurrent[8], - - ApparentPowerL123 = powerCurrent[10], - ApparentPowerL1 = powerCurrent[11], - ApparentPowerL2 = powerCurrent[12], - ApparentPowerL3 = powerCurrent[13], - - CurrentL123 = powerCurrent[50], - CurrentL1 = powerCurrent[51], - CurrentL2 = powerCurrent[52], - CurrentL3 = powerCurrent[53], - - VoltageL1N = voltageFreq[0], - VoltageL2N = voltageFreq[1], - VoltageL3N = voltageFreq[2], - VoltageL1L2 = voltageFreq[3], - VoltageL2L3 = voltageFreq[4], - VoltageL3L1 = voltageFreq[5], - - PowerFactorL1 = voltageFreq[50], - PowerFactorL2 = voltageFreq[51], - PowerFactorL3 = voltageFreq[52], - - Frequency = voltageFreq[55], - - EnergyImportL123 = energyTotal[0 / 4] / 1000.0m, - EnergyExportL123 = energyTotal[20 / 4] / 1000.0m, - - EnergyImportL1 = energyPhases[0 / 4] / 1000.0m, - EnergyExportL1 = energyPhases[20 / 4] / 1000.0m, - - EnergyImportL2 = energyPhases[40 / 4] / 1000.0m, - EnergyExportL2 = energyPhases[60 / 4] / 1000.0m, - - EnergyImportL3 = energyPhases[80 / 4] / 1000.0m, - EnergyExportL3 = energyPhases[100 / 4] / 1000.0m, - }; + ( + Ac: new ThreePhaseAcConnection + ( + new AcPhase( + voltageL1N, + currentL1, + GetPhi(powerFactorL1) + ), + + new AcPhase( + voltageL2N, + currentL2, + GetPhi(powerFactorL2) + ), + + new AcPhase( + voltageL3N, + currentL3, + GetPhi(powerFactorL3) + ), + frequency + ), + activePowerL123, + reactivePowerL123, + apparentPowerL123, + currentL123, + voltageL1L2, + voltageL2L3, + voltageL3L1, + energyImportL123, + energyImportL1, + energyImportL2, + energyImportL3, + energyExportL123, + energyExportL1, + energyExportL2, + energyExportL3 + ); } } \ No newline at end of file diff --git a/csharp/lib/Devices/EmuMeter/EmuMeterStatus.cs b/csharp/lib/Devices/EmuMeter/EmuMeterStatus.cs index d08a7b284..713a4b2de 100644 --- a/csharp/lib/Devices/EmuMeter/EmuMeterStatus.cs +++ b/csharp/lib/Devices/EmuMeter/EmuMeterStatus.cs @@ -1,45 +1,26 @@ +using InnovEnergy.Lib.StatusApi.Connections; +using InnovEnergy.Lib.StatusApi.Devices; + namespace InnovEnergy.Lib.Devices.EmuMeter; public record EmuMeterStatus -{ - public Decimal ActivePowerL123 { get; init; } - public Decimal ActivePowerL1 { get; init; } - public Decimal ActivePowerL2 { get; init; } - public Decimal ActivePowerL3 { get; init; } - public Decimal ReactivePowerL123 { get; init; } - public Decimal ReactivePowerL1 { get; init; } - public Decimal ReactivePowerL2 { get; init; } - public Decimal ReactivePowerL3 { get; init; } - public Decimal ApparentPowerL123 { get; init; } - public Decimal ApparentPowerL1 { get; init; } - public Decimal ApparentPowerL2 { get; init; } - public Decimal ApparentPowerL3 { get; init; } - - public Decimal CurrentL123 { get; init; } - public Decimal CurrentL1 { get; init; } - public Decimal CurrentL2 { get; init; } - public Decimal CurrentL3 { get; init; } - - public Decimal VoltageL1N { get; init; } - public Decimal VoltageL2N { get; init; } - public Decimal VoltageL3N { get; init; } - public Decimal VoltageL1L2 { get; init; } - public Decimal VoltageL2L3 { get; init; } - public Decimal VoltageL3L1 { get; init; } - - public Decimal PowerFactorL1 { get; init; } - public Decimal PowerFactorL2 { get; init; } - public Decimal PowerFactorL3 { get; init; } - - public Decimal EnergyImportL123 { get; init; } - public Decimal EnergyImportL1 { get; init; } - public Decimal EnergyImportL2 { get; init; } - public Decimal EnergyImportL3 { get; init; } - - public Decimal EnergyExportL123 { get; init; } - public Decimal EnergyExportL1 { get; init; } - public Decimal EnergyExportL2 { get; init; } - public Decimal EnergyExportL3 { get; init; } +( + ThreePhaseAcConnection Ac, + Decimal ActivePowerL123, + Decimal ReactivePowerL123, + Decimal ApparentPowerL123, + Decimal CurrentL123, + Decimal VoltageL1L2, + Decimal VoltageL2L3, + Decimal VoltageL3L1, + Decimal EnergyImportL123, + Decimal EnergyImportL1, + Decimal EnergyImportL2, + Decimal EnergyImportL3, + Decimal EnergyExportL123, + Decimal EnergyExportL1, + Decimal EnergyExportL2, + Decimal EnergyExportL3 +):GridMeter(Ac) +{} - public Decimal Frequency { get; init; } -} \ No newline at end of file