Update controller functionality to support calibration charge date selection

This commit is contained in:
Noe 2024-03-15 17:01:12 +01:00
parent 183f8cfc2a
commit 67343ad842
5 changed files with 63 additions and 75 deletions

View File

@ -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; }
}

View File

@ -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

View File

@ -64,7 +64,7 @@ public static class MiddlewareAgent
Configuration? config = JsonSerializer.Deserialize<Configuration>(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);

View File

@ -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;
}
}
}

View File

@ -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,