From eb62dc454d60a4f3219e2fb9ac1ea756874cb7c2 Mon Sep 17 00:00:00 2001 From: djnitehawk Date: Sat, 15 Mar 2025 20:16:59 +0530 Subject: [PATCH] improve inverter status serialization --- src/Client/Pages/Index.razor | 29 ++++++++----------- src/Server/InverterMon.Server.csproj | 2 +- .../InverterService/FelicityInverter.cs | 16 +++++----- src/Shared/Models/InverterStatus.cs | 12 ++++---- src/changelog.md | 3 +- src/global.json | 5 ++++ 6 files changed, 34 insertions(+), 33 deletions(-) create mode 100644 src/global.json diff --git a/src/Client/Pages/Index.razor b/src/Client/Pages/Index.razor index 8bd48b7..274c5d2 100644 --- a/src/Client/Pages/Index.razor +++ b/src/Client/Pages/Index.razor @@ -45,7 +45,7 @@
-
@@ -55,7 +55,7 @@
-
@RoundToWholeNumber(status?.OutputVoltage)
+
@status?.OutputVoltage
V @@ -68,7 +68,7 @@
-
@RoundToOneDecimal(status?.LoadCurrent)
+
@status?.LoadCurrent
A
@@ -97,13 +97,13 @@
-
@RoundToWholeNumber(status?.PVInputVoltage)
+
@status?.PVInputVoltage
V
-
@RoundToOneDecimal(status?.PVInputCurrent)
+
@status?.PVInputCurrent
A
@@ -119,8 +119,9 @@
-
@@ -137,7 +138,7 @@
@status?.BatteryChargeWatts
W
-
@RoundToOneDecimal(status?.BatteryChargeCurrent)
+
@status?.BatteryChargeCurrent
A
@@ -145,7 +146,7 @@
Voltage
-
@RoundToOneDecimal(status?.BatteryVoltage)
+
@status?.BatteryVoltage
V
@GetCRate() C
@@ -199,12 +200,6 @@ onStatusRetrievalError -= NullifyStatus; } - private static double RoundToWholeNumber(double? val) - => Math.Round(val ?? 0, 0); - - private static double RoundToOneDecimal(double? val) - => Math.Round(val ?? 0, 1); - public static async Task StartStatusStreaming(string basePath) { //note: only reason we have a full-time stream download is because there's a bug in @@ -255,10 +250,10 @@ private static double GetCRate() { if (status?.BatteryChargeCRate > 0) - return Math.Round(status.BatteryChargeCRate, 2); + return status.BatteryChargeCRate; if (status?.BatteryDischargeCRate > 0) - return Math.Round(status.BatteryDischargeCRate, 2); + return status.BatteryDischargeCRate; return 0; } diff --git a/src/Server/InverterMon.Server.csproj b/src/Server/InverterMon.Server.csproj index d9d09d1..59bcba5 100644 --- a/src/Server/InverterMon.Server.csproj +++ b/src/Server/InverterMon.Server.csproj @@ -7,7 +7,7 @@ CS8618;CA2016 MIT direct - 13 + latest diff --git a/src/Server/InverterService/FelicityInverter.cs b/src/Server/InverterService/FelicityInverter.cs index 9133b41..4c2c393 100644 --- a/src/Server/InverterService/FelicityInverter.cs +++ b/src/Server/InverterService/FelicityInverter.cs @@ -65,8 +65,8 @@ public sealed class FelicitySolarInverter // BatteryChargingStage = regs[1], // 0x1102: Battery charging stage (offset 1) - Status.WorkingMode = (WorkingMode)regs[0]; // 0x1101: Working mode (offset 0) - Status.BatteryVoltage = regs[7] / 100.0; // 0x1108: Battery voltage (offset 0x1108 - 0x1101 = 7) + Status.WorkingMode = (WorkingMode)regs[0]; // 0x1101: Working mode (offset 0) + Status.BatteryVoltage = Math.Round(regs[7] / 100.0, 1); // 0x1108: Battery voltage (offset 0x1108 - 0x1101 = 7) var disCur = ChargeStatus(regs[8]); // 0x1109: Battery current (offset 8) -- signed value Status.BatteryDischargeCurrent = disCur.IsDischarge ? disCur.PositiveValue : 0; @@ -76,12 +76,12 @@ public sealed class FelicitySolarInverter Status.BatteryDischargeWatts = disPow.IsDischarge ? disPow.PositiveValue : 0; Status.BatteryChargeWatts = disPow.IsDischarge is false ? disPow.PositiveValue : 0; - Status.OutputVoltage = regs[16] / 10.0; // 0x1111: AC output voltage (offset 0x1111 - 0x1101 = 16) - Status.GridVoltage = regs[22] / 10.0; // 0x1111: AC output voltage (offset 0x1117 - 0x1101 = 22) - Status.LoadWatts = regs[29]; // 0x111E: AC output active power (offset 0x111E - 0x1101 = 29) - Status.LoadPercentage = regs[31]; // 0x1120: Load percentage (offset 0x1120 - 0x1101 = 31) - Status.PVInputVoltage = regs[37] / 10.0; // 0x1126: PV input voltage (offset 0x1126 - 0x1101 = 37) - Status.PVInputWatt = regs[41]; // 0x112A: PV input power (offset 0x112A - 0x1101 = 41) -- signed value + Status.OutputVoltage = Math.Round(regs[16] / 10.0, 0); // 0x1111: AC output voltage (offset 0x1111 - 0x1101 = 16) + Status.GridVoltage = Convert.ToInt16(regs[22] / 10.0); // 0x1111: AC output voltage (offset 0x1117 - 0x1101 = 22) + Status.LoadWatts = regs[29]; // 0x111E: AC output active power (offset 0x111E - 0x1101 = 29) + Status.LoadPercentage = regs[31]; // 0x1120: Load percentage (offset 0x1120 - 0x1101 = 31) + Status.PVInputVoltage = Math.Round(regs[37] / 10.0, 0); // 0x1126: PV input voltage (offset 0x1126 - 0x1101 = 37) + Status.PVInputWatt = regs[41]; // 0x112A: PV input power (offset 0x112A - 0x1101 = 41) -- signed value static (bool IsDischarge, short PositiveValue) ChargeStatus(short value) { diff --git a/src/Shared/Models/InverterStatus.cs b/src/Shared/Models/InverterStatus.cs index 525b038..ab04631 100644 --- a/src/Shared/Models/InverterStatus.cs +++ b/src/Shared/Models/InverterStatus.cs @@ -8,7 +8,7 @@ public class InverterStatus public int BatteryCapacity { get; set; } = 100; [JsonPropertyName("b")] - public double BatteryChargeCRate => BatteryChargeCurrent == 0 ? 0 : Convert.ToDouble(BatteryChargeCurrent) / BatteryCapacity; + public double BatteryChargeCRate => BatteryChargeCurrent == 0 ? 0 : Math.Round(Convert.ToDouble(BatteryChargeCurrent) / BatteryCapacity, 2); [JsonPropertyName("c")] public int BatteryChargeCurrent { get; set; } @@ -17,7 +17,7 @@ public class InverterStatus public int BatteryChargeWatts { get; set; } [JsonPropertyName("e")] - public double BatteryDischargeCRate => BatteryDischargeCurrent == 0 ? 0 : Convert.ToDouble(BatteryDischargeCurrent) / BatteryCapacity; + public double BatteryDischargeCRate => BatteryDischargeCurrent == 0 ? 0 : Math.Round(Convert.ToDouble(BatteryDischargeCurrent) / BatteryCapacity, 2); [JsonPropertyName("f")] public int BatteryDischargeCurrent { get; set; } @@ -35,13 +35,13 @@ public class InverterStatus public int GridUsageWatts => GridVoltage < 10 ? 0 : LoadWatts + BatteryChargeWatts - (PVInputWatt + BatteryDischargeWatts); [JsonPropertyName("k")] - public double GridVoltage { get; set; } + public int GridVoltage { get; set; } [JsonPropertyName("l")] public WorkingMode WorkingMode { get; set; } [JsonPropertyName("m")] - public double LoadCurrent => LoadWatts == 0 ? 0 : LoadWatts / OutputVoltage; + public double LoadCurrent => LoadWatts == 0 ? 0 : Math.Round(LoadWatts / OutputVoltage, 1); [JsonPropertyName("n")] public int LoadPercentage { get; set; } @@ -53,7 +53,7 @@ public class InverterStatus public double OutputVoltage { get; set; } [JsonPropertyName("q")] - public double PVInputCurrent => PVInputWatt == 0 ? 0 : PVInputWatt / PVInputVoltage; + public double PVInputCurrent => PVInputWatt == 0 ? 0 : Math.Round(PVInputWatt / PVInputVoltage, 1); [JsonPropertyName("r")] public double PVInputVoltage { get; set; } @@ -69,7 +69,7 @@ public class InverterStatus pvInputWatt = value; var interval = (DateTime.Now - pvInputWattHourLastComputed).TotalSeconds; - PVInputWattHour += value / (3600 / interval); + PVInputWattHour += Math.Round(value / (3600 / interval), 2); pvInputWattHourLastComputed = DateTime.Now; } } diff --git a/src/changelog.md b/src/changelog.md index 79dc415..a3a0059 100644 --- a/src/changelog.md +++ b/src/changelog.md @@ -1,3 +1,4 @@ ## changelog -- fix pv potential progress bar not showing \ No newline at end of file +- prevent serialization errors for inverter status +- round floating point numbers on serverside before sending data to client \ No newline at end of file diff --git a/src/global.json b/src/global.json new file mode 100644 index 0000000..66a9a84 --- /dev/null +++ b/src/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "9.0.200" + } +} \ No newline at end of file