update the Create Avreage function

This commit is contained in:
atef 2024-01-10 16:28:25 +01:00
parent 91da191874
commit 5245df237f
1 changed files with 59 additions and 17 deletions

View File

@ -98,10 +98,15 @@ public static class Aggregator
private static AggregatedData CreateAverage(String myDirectory, Int64 afterTimestamp, Int64 beforeTimestamp)
{
// Get all CSV files in the specified directory
var csvFiles = Directory.GetFiles(myDirectory, "*.csv");
var socAverage = new List<Double>();
var pvPowerAverage = new List<Double>();
var batteryPowerAverage = new List<Double>();
var csvFiles = Directory.GetFiles(myDirectory, "*.csv");
var batterySoc = new List<Double>();
var batteryAvgSoc = new List<Double>(); // this is only used for the daily data.
var pvPowerAverage = new List<Double>();
var gridPowerImport = new List<Double>();
var gridPowerExport = new List<Double>();
var batteryDischargePower = new List<Double>();
var batteryChargePower = new List<Double>();
Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
Console.WriteLine("File timestamp should start after "+ afterTimestamp);
@ -132,18 +137,41 @@ public static class Aggregator
{
switch (variableName)
{
case "/Battery/Soc" or "/AvgSoc":
socAverage.Add(value);
case "/Battery/Soc" or "/MinSoc" or "/MaxSoc":
batterySoc.Add(value);
break;
case "/PvOnDc/Dc/Power" or "/AvgPvPower":
case "/AvgSoc":
batteryAvgSoc.Add(value);
break;
case "/PvOnDc/Dc/Power" or "/SumPvPower":
pvPowerAverage.Add(value);
break;
case "/Battery/Dc/Power" or "/BatteryPowerAverage":
batteryPowerAverage.Add(value);
break;
case "/Battery/Dc/Power" or "/SumDischargingBatteryPower" or "/SumChargingBatteryPower":
if (value < 0)
{
batteryDischargePower.Add(value);
}
else
{
batteryChargePower.Add(value);
}
break;
case "/GridMeter/Ac/Power/Active" or "/SumGridExportPower" or "/SumGridImportPower":
if (value < 0)
{
gridPowerExport.Add(value);
}
else
{
gridPowerImport.Add(value);
}
break;
// Add more cases as needed
default:
// Code to execute when variableName doesn't match any condition
@ -169,15 +197,29 @@ public static class Aggregator
AggregatedData aggregatedData = new AggregatedData
{
AvgSoc = socAverage.Any() ? socAverage.Average() : 0.0,
AvgPvPower = pvPowerAverage.Any() ? pvPowerAverage.Average() : 0.0,
BatteryPowerAverage = batteryPowerAverage.Any() ? batteryPowerAverage.Average() : 0.0
MaxSoc = batterySoc.Any() ? batterySoc.Max() : 0.0,
MinSoc = batterySoc.Any() ? batterySoc.Min() : 0.0,
AvgSoc = batteryAvgSoc.Any() ? batteryAvgSoc.Average() : 0.0,
SumDischargingBatteryPower = batteryDischargePower.Any() ? batteryDischargePower.Sum() : 0.0,
SumChargingBatteryPower = batteryChargePower.Any() ? batteryChargePower.Sum() : 0.0,
SumGridExportPower = gridPowerExport.Any() ? gridPowerExport.Sum() : 0.0,
SumGridImportPower = gridPowerImport.Any() ? gridPowerImport.Sum() : 0.0,
SumPvPower = pvPowerAverage.Any() ? pvPowerAverage.Sum() : 0.0,
};
// Print the stored CSV data for verification
Console.WriteLine($"SOC: {aggregatedData.AvgSoc}");
Console.WriteLine($"PvPower: {aggregatedData.AvgPvPower}");
Console.WriteLine($"Battery: {aggregatedData.BatteryPowerAverage}");
Console.WriteLine($"Max SOC: {aggregatedData.MaxSoc}");
Console.WriteLine($"Min SOC: {aggregatedData.MinSoc}");
Console.WriteLine($"ChargingBatteryPower: {aggregatedData.SumDischargingBatteryPower}");
Console.WriteLine($"DischargingBatteryBattery: {aggregatedData.SumChargingBatteryPower}");
Console.WriteLine($"SumGridExportPower: {aggregatedData.SumGridExportPower}");
Console.WriteLine($"SumGridImportPower: {aggregatedData.SumGridImportPower}");
Console.WriteLine($"Min SOC: {aggregatedData.MinSoc}");
Console.WriteLine("CSV data reading and storage completed.");
Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");