From 137e9711c214c36655edf0ab86156884306e9f78 Mon Sep 17 00:00:00 2001 From: atef Date: Thu, 14 Dec 2023 16:39:27 +0100 Subject: [PATCH] Update Create Hour function --- .../src/AggregationService/Aggregator.cs | 127 ++++++++++++------ 1 file changed, 84 insertions(+), 43 deletions(-) diff --git a/csharp/App/SaliMax/src/AggregationService/Aggregator.cs b/csharp/App/SaliMax/src/AggregationService/Aggregator.cs index 962c39d18..09b803969 100644 --- a/csharp/App/SaliMax/src/AggregationService/Aggregator.cs +++ b/csharp/App/SaliMax/src/AggregationService/Aggregator.cs @@ -1,4 +1,5 @@ using InnovEnergy.Lib.Utils; +using static System.Double; namespace InnovEnergy.App.SaliMax.AggregationService; @@ -43,64 +44,104 @@ public static class Aggregator // Get all CSV files in the specified directory var csvFiles = Directory.GetFiles(myDirectory, "*.csv"); - + var currentTimestamp = DateTime.Now.ToUnixTime(); var oneHourBefore = DateTime.Now.AddHours(-1).ToUnixTime(); + + var socAverage = new List(); + var pvPowerAverage = new List(); + var batteryPowerAverage = new List(); Console.WriteLine("-----------------------------------------------------------------------------------------------------------------"); Console.WriteLine("File timestamp should start after "+ DateTime.Now.AddHours(-1)); - - var validFiles = csvFiles - .Where(csvFile => IsFileWithinTimeRange(csvFile, oneHourBefore, currentTimestamp)) - .ToList(); - try + foreach (var csvFile in csvFiles) { - var average1HSoc = validFiles - .Select(csvFile => csvFile.ExtractValue("/Battery/Soc")) - .NotNull() - .Average(); - - var average1HPvPower = validFiles - .Select(csvFile => csvFile.ExtractValue("/PvOnDc/Dc/Power")) - .NotNull() - .Average(); - - var average1HBatteryPower = validFiles - .Select(csvFile => csvFile.ExtractValue("/Battery/Dc/Power")) - .NotNull() - .Average(); - - - Console.WriteLine($"SOC: {average1HSoc}"); - Console.WriteLine($"PvPower: {average1HPvPower}"); - Console.WriteLine($"Battery: {average1HBatteryPower}"); - + if (csvFile == "LogDirectory/log.csv") + { + continue; + } + + if (IsFileWithinTimeRange(csvFile, oneHourBefore, currentTimestamp)) + { + using var reader = new StreamReader(csvFile); + + while (!reader.EndOfStream) + { + + var line = reader.ReadLine(); + var lines = line?.Split(';'); + + // Assuming there are always three columns (variable name and its value) + if (lines is { Length: 3 }) + { + 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; + } + + } + else + { + // 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"); + } + } + } } - catch (Exception e) - { - e.WriteLine(); - } - + + // 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."); //Create a new file to folder "Hourly Aggregated Data and push it to S3" Console.WriteLine("-----------------------------------------------------------------------------------------------------------------"); } - - 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 - .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(); + return value == path; } - + private static Boolean IsFileWithinTimeRange(string filePath, long startTime, long endTime) { var fileTimestamp = long.TryParse(Path.GetFileNameWithoutExtension(filePath).Replace("log_", ""), out var fileTimestamp1) ? fileTimestamp1 : -1;