Update Create Hour function

This commit is contained in:
atef 2023-12-14 16:39:27 +01:00
parent baa1b930d9
commit 137e9711c2
1 changed files with 84 additions and 43 deletions

View File

@ -1,4 +1,5 @@
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using static System.Double;
namespace InnovEnergy.App.SaliMax.AggregationService; namespace InnovEnergy.App.SaliMax.AggregationService;
@ -47,40 +48,87 @@ public static class Aggregator
var currentTimestamp = DateTime.Now.ToUnixTime(); var currentTimestamp = DateTime.Now.ToUnixTime();
var oneHourBefore = DateTime.Now.AddHours(-1).ToUnixTime(); var oneHourBefore = DateTime.Now.AddHours(-1).ToUnixTime();
var socAverage = new List<Double>();
var pvPowerAverage = new List<Double>();
var batteryPowerAverage = new List<Double>();
Console.WriteLine("-----------------------------------------------------------------------------------------------------------------"); Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
Console.WriteLine("File timestamp should start after "+ DateTime.Now.AddHours(-1)); Console.WriteLine("File timestamp should start after "+ DateTime.Now.AddHours(-1));
var validFiles = csvFiles foreach (var csvFile in csvFiles)
.Where(csvFile => IsFileWithinTimeRange(csvFile, oneHourBefore, currentTimestamp))
.ToList();
try
{ {
var average1HSoc = validFiles if (csvFile == "LogDirectory/log.csv")
.Select(csvFile => csvFile.ExtractValue("/Battery/Soc")) {
.NotNull() continue;
.Average(); }
var average1HPvPower = validFiles if (IsFileWithinTimeRange(csvFile, oneHourBefore, currentTimestamp))
.Select(csvFile => csvFile.ExtractValue("/PvOnDc/Dc/Power")) {
.NotNull() using var reader = new StreamReader(csvFile);
.Average();
var average1HBatteryPower = validFiles while (!reader.EndOfStream)
.Select(csvFile => csvFile.ExtractValue("/Battery/Dc/Power")) {
.NotNull()
.Average();
var line = reader.ReadLine();
var lines = line?.Split(';');
Console.WriteLine($"SOC: {average1HSoc}"); // Assuming there are always three columns (variable name and its value)
Console.WriteLine($"PvPower: {average1HPvPower}"); if (lines is { Length: 3 })
Console.WriteLine($"Battery: {average1HBatteryPower}"); {
var variableName = lines[0].Trim();
if (TryParse(lines[1].Trim(), out var value))
{
switch (variableName)
{
case var _ when GetVariable(variableName,"/Battery/Soc"):
// Code to execute when variableName matches IsSoc condition
socAverage.Add(value);
break;
case var _ when GetVariable(variableName, "/PvOnDc/Dc/Power"):
// Code to execute when variableName matches IsPvPower condition
pvPowerAverage.Add(value);
break;
case var _ when GetVariable(variableName, "/Battery/Dc/Power"):
// Code to execute when variableName matches IsBatteryPower condition
batteryPowerAverage.Add(value);
break;
// Add more cases as needed
default:
// Code to execute when variableName doesn't match any condition
break;
}
} }
catch (Exception e) else
{ {
e.WriteLine(); // Handle cases where variableValue is not a valid number
Console.WriteLine(
$"Invalid numeric value for variable {variableName}:{lines[1].Trim()}");
} }
}
else
{
// Handle invalid column format
Console.WriteLine("Invalid format in column");
}
}
}
}
// Calculate the average of values
var avgSoc = socAverage.Any() ? socAverage.Average() : 0.0;
var avgPvPower = pvPowerAverage.Any() ? pvPowerAverage.Average() : 0.0;
var avgBatteryPower = batteryPowerAverage.Any() ? batteryPowerAverage.Average() : 0.0;
// Print the stored CSV data for verification
Console.WriteLine($"SOC: {avgSoc}");
Console.WriteLine($"PvPower: {avgPvPower}");
Console.WriteLine($"Battery: {avgBatteryPower}");
Console.WriteLine("CSV data reading and storage completed."); Console.WriteLine("CSV data reading and storage completed.");
//Create a new file to folder "Hourly Aggregated Data and push it to S3" //Create a new file to folder "Hourly Aggregated Data and push it to S3"
@ -88,17 +136,10 @@ public static class Aggregator
} }
private static Double? ExtractValue(this String csvFile, String path) // Custom method to check if a string is numeric
private static Boolean GetVariable(String value, String path)
{ {
return File return value == path;
.ReadAllLines(csvFile)
.Select(l => l.Split(';'))
.Where(l => l.Length == 3)
.Where(line => line[0].Trim() == path)
.Select(l => Double.TryParse(l[1].Trim(), out var value)
? value
: (Double?) null)
.FirstOrDefault();
} }
private static Boolean IsFileWithinTimeRange(string filePath, long startTime, long endTime) private static Boolean IsFileWithinTimeRange(string filePath, long startTime, long endTime)