From 67343ad842f1fa50c67d75374ff918fe8d7ff008 Mon Sep 17 00:00:00 2001 From: Noe Date: Fri, 15 Mar 2024 17:01:12 +0100 Subject: [PATCH] Update controller functionality to support calibration charge date selection --- .../SaliMax/src/DataTypes/Configuration.cs | 8 +-- csharp/App/SaliMax/src/Ess/Controller.cs | 71 ++++++------------- .../src/MiddlewareClasses/MiddlewareAgent.cs | 2 +- csharp/App/SaliMax/src/Program.cs | 12 +++- csharp/App/SaliMax/src/SystemConfig/Config.cs | 45 ++++++------ 5 files changed, 63 insertions(+), 75 deletions(-) diff --git a/csharp/App/SaliMax/src/DataTypes/Configuration.cs b/csharp/App/SaliMax/src/DataTypes/Configuration.cs index 0069d052c..43146a0bb 100644 --- a/csharp/App/SaliMax/src/DataTypes/Configuration.cs +++ b/csharp/App/SaliMax/src/DataTypes/Configuration.cs @@ -4,9 +4,9 @@ namespace InnovEnergy.App.SaliMax.DataTypes; public class Configuration { - public Double MinimumSoC { get; set; } - public Double GridSetPoint { get; set; } - public CalibrationChargeType ForceCalibrationCharge { get; set; } - public DateTime CalibrationChargeDate { get; set; } + public Double MinimumSoC { get; set; } + public Double GridSetPoint { get; set; } + public CalibrationChargeType ForceCalibrationChargeState { get; set; } + public DateTime CalibrationChargeDate { get; set; } } diff --git a/csharp/App/SaliMax/src/Ess/Controller.cs b/csharp/App/SaliMax/src/Ess/Controller.cs index b85601d1c..273456d8f 100644 --- a/csharp/App/SaliMax/src/Ess/Controller.cs +++ b/csharp/App/SaliMax/src/Ess/Controller.cs @@ -10,7 +10,7 @@ namespace InnovEnergy.App.SaliMax.Ess; public static class Controller { private static readonly Double BatteryHeatingPower = 200.0; // TODO: move to config - private static Boolean _hasOriginalNextDayAt10AmBeenChecked = false; + private static Boolean _hasCalibrationChargeChecked = false; private static DateTime _nextDayAt10Am = DateTime.Now; public static EssMode SelectControlMode(this StatusRecord s) @@ -187,74 +187,49 @@ public static class Controller private static Boolean MustDoCalibrationCharge(this StatusRecord statusRecord) { - var calibrationChargeForced = statusRecord.Config.ForceCalibrationCharge; + var calibrationChargeForced = statusRecord.Config.ForceCalibrationChargeState; var batteryCalibrationChargeRequested = statusRecord.Battery is { CalibrationChargeRequested: true };//BatteryCalibrationChargeRequested(statusRecord.Battery?.CalibrationChargeRequested?? false) ; - var mustDoCalibrationCharge = batteryCalibrationChargeRequested || calibrationChargeForced == CalibrationChargeType.ChargePermanently || calibrationChargeForced == CalibrationChargeType.AdditionallyOnce ; + var mustDoCalibrationCharge = calibrationChargeForced == CalibrationChargeType.ChargePermanently || + (calibrationChargeForced == CalibrationChargeType.AdditionallyOnce + && CalibrationDateHasBeenPassed(statusRecord.Config.DayAndTimeForAdditionalCalibration)) || + (calibrationChargeForced == CalibrationChargeType.RepetitivelyEvery && + CalibrationDateHasBeenPassed(statusRecord.Config.DayAndTimeForRepetitiveCalibration)); if (statusRecord.Battery is not null) { if (calibrationChargeForced == CalibrationChargeType.AdditionallyOnce && statusRecord.Battery.Eoc ) { - statusRecord.Config.ForceCalibrationCharge = CalibrationChargeType.RepetitivelyEvery; + statusRecord.Config.ForceCalibrationChargeState = CalibrationChargeType.RepetitivelyEvery; + _hasCalibrationChargeChecked = false; + + } + else if (calibrationChargeForced == CalibrationChargeType.RepetitivelyEvery && statusRecord.Battery.Eoc) + { + statusRecord.Config.DayAndTimeForRepetitiveCalibration = statusRecord.Config.DayAndTimeForRepetitiveCalibration.AddDays(7); + _hasCalibrationChargeChecked = false; } } return mustDoCalibrationCharge; } - private static Boolean BatteryCalibrationChargeRequested(Boolean calibrationChargeRequested) + private static Boolean CalibrationDateHasBeenPassed(DateTime calibrationChargeDate) { - if (calibrationChargeRequested) - { - if (!_hasOriginalNextDayAt10AmBeenChecked) - { - _nextDayAt10Am = CalculateNextCalibrationTime(); - _hasOriginalNextDayAt10AmBeenChecked = true; - } - if (CheckNextCalibrationTime(_nextDayAt10Am)) + if (!_hasCalibrationChargeChecked) + { + if (DateTime.Now >= calibrationChargeDate ) { + _hasCalibrationChargeChecked = true; return true; } - } - else - { - _hasOriginalNextDayAt10AmBeenChecked = false; return false; + } + return true; - return false; } - - private static Boolean CheckNextCalibrationTime(DateTime nextCalibrationTime) - { - var currentDateAndTime = DateTime.Now; - Console.WriteLine(" next X am is: " + nextCalibrationTime); - Console.WriteLine(" currentDateAndTime: " + currentDateAndTime); - - // Check if the current time is greater than or equal to the original nextCalibrationTime - var x = currentDateAndTime >= nextCalibrationTime; - Console.WriteLine(" we are checking next X am : " + x); - - return x; - } - - private static DateTime CalculateNextCalibrationTime() - { - var currentDateAndTime = DateTime.Now; - - // Calculate the next 10 AM within the same day - var nextCalibrationTime = new DateTime(currentDateAndTime.Year, currentDateAndTime.Month, currentDateAndTime.Day, 9, 0, 0); // next 9am - - // If the current time is already past 9 AM, move to the next day - if (currentDateAndTime.Hour >= 9) - { - nextCalibrationTime = nextCalibrationTime.AddDays(1); - } - - return nextCalibrationTime; - } - + private static Double ControlGridPower(this StatusRecord status, Double targetPower) { return ControlPower diff --git a/csharp/App/SaliMax/src/MiddlewareClasses/MiddlewareAgent.cs b/csharp/App/SaliMax/src/MiddlewareClasses/MiddlewareAgent.cs index 4bf6ca91c..b79d9c28e 100644 --- a/csharp/App/SaliMax/src/MiddlewareClasses/MiddlewareAgent.cs +++ b/csharp/App/SaliMax/src/MiddlewareClasses/MiddlewareAgent.cs @@ -64,7 +64,7 @@ public static class MiddlewareAgent Configuration? config = JsonSerializer.Deserialize(message); - Console.WriteLine($"Received a configuration message: GridSetPoint is " + config.GridSetPoint + ", MinimumSoC is " + config.MinimumSoC + " and ForceCalibrationCharge is " + config.ForceCalibrationCharge+ " and CalibrationChargeDate is " + config.CalibrationChargeDate); + Console.WriteLine($"Received a configuration message: GridSetPoint is " + config.GridSetPoint + ", MinimumSoC is " + config.MinimumSoC + " and ForceCalibrationCharge is " + config.ForceCalibrationChargeState+ " and CalibrationChargeDate is " + config.CalibrationChargeDate); // Send the reply to the sender's endpoint UdpListener.Send(replyData, replyData.Length, serverEndpoint); diff --git a/csharp/App/SaliMax/src/Program.cs b/csharp/App/SaliMax/src/Program.cs index 56268308b..c004a34e6 100644 --- a/csharp/App/SaliMax/src/Program.cs +++ b/csharp/App/SaliMax/src/Program.cs @@ -690,7 +690,17 @@ internal static class Program { status.Config.MinSoc = config.MinimumSoC; status.Config.GridSetPoint = config.GridSetPoint * 1000; // converted from kW to W - status.Config.ForceCalibrationCharge = config.ForceCalibrationCharge; + status.Config.ForceCalibrationChargeState = config.ForceCalibrationChargeState; + + if (config.ForceCalibrationChargeState == CalibrationChargeType.RepetitivelyEvery) + { + status.Config.DayAndTimeForRepetitiveCalibration = config.CalibrationChargeDate; + } + else if (config.ForceCalibrationChargeState == CalibrationChargeType.AdditionallyOnce) + { + status.Config.DayAndTimeForAdditionalCalibration = config.CalibrationChargeDate; + } + } } \ No newline at end of file diff --git a/csharp/App/SaliMax/src/SystemConfig/Config.cs b/csharp/App/SaliMax/src/SystemConfig/Config.cs index 713bc3a0d..668016fc8 100644 --- a/csharp/App/SaliMax/src/SystemConfig/Config.cs +++ b/csharp/App/SaliMax/src/SystemConfig/Config.cs @@ -15,26 +15,27 @@ public class Config //TODO: let IE choose from config files (Json) and connect t private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true }; - public required Double MinSoc { get; set; } - public required CalibrationChargeType ForceCalibrationCharge { get; set; } - public required DateTime DayAndTimeForCalibration { get; set; } - public required Boolean DisplayIndividualBatteries { get; set; } - public required Double PConstant { get; set; } - public required Double GridSetPoint { get; set; } - public required Double BatterySelfDischargePower { get; set; } - public required Double HoldSocZone { get; set; } - public required DevicesConfig IslandMode { get; set; } - public required DevicesConfig GridTie { get; set; } + public required Double MinSoc { get; set; } + public required CalibrationChargeType ForceCalibrationChargeState { get; set; } + public required DateTime DayAndTimeForRepetitiveCalibration { get; set; } + public required DateTime DayAndTimeForAdditionalCalibration { get; set; } + public required Boolean DisplayIndividualBatteries { get; set; } + public required Double PConstant { get; set; } + public required Double GridSetPoint { get; set; } + public required Double BatterySelfDischargePower { get; set; } + public required Double HoldSocZone { get; set; } + public required DevicesConfig IslandMode { get; set; } + public required DevicesConfig GridTie { get; set; } - public required Double MaxBatteryChargingCurrent { get; set; } - public required Double MaxBatteryDischargingCurrent { get; set; } - public required Double MaxDcPower { get; set; } + public required Double MaxBatteryChargingCurrent { get; set; } + public required Double MaxBatteryDischargingCurrent { get; set; } + public required Double MaxDcPower { get; set; } - public required Double MaxChargeBatteryVoltage { get; set; } - public required Double MinDischargeBatteryVoltage { get; set; } + public required Double MaxChargeBatteryVoltage { get; set; } + public required Double MinDischargeBatteryVoltage { get; set; } - public required DeviceConfig Devices { get; set; } - public required S3Config? S3 { get; set; } + public required DeviceConfig Devices { get; set; } + public required S3Config? S3 { get; set; } private static String? LastSavedData { get; set; } @@ -42,8 +43,9 @@ public class Config //TODO: let IE choose from config files (Json) and connect t public static Config Default => new() { MinSoc = 20, - ForceCalibrationCharge = CalibrationChargeType.RepetitivelyEvery, - DayAndTimeForCalibration = DefaultDatetime, + ForceCalibrationChargeState = CalibrationChargeType.RepetitivelyEvery, + DayAndTimeForRepetitiveCalibration = DefaultDatetime, + DayAndTimeForAdditionalCalibration = DefaultDatetime, DisplayIndividualBatteries = false, PConstant = .5, GridSetPoint = 0, @@ -120,8 +122,9 @@ public class Config //TODO: let IE choose from config files (Json) and connect t public static Config Default => new() { MinSoc = 20, - ForceCalibrationCharge = CalibrationChargeType.RepetitivelyEvery, - DayAndTimeForCalibration = DefaultDatetime, + ForceCalibrationChargeState = CalibrationChargeType.RepetitivelyEvery, + DayAndTimeForRepetitiveCalibration = DefaultDatetime, + DayAndTimeForAdditionalCalibration = DefaultDatetime, DisplayIndividualBatteries = false, PConstant = .5, GridSetPoint = 0,