Update Create Hour function
This commit is contained in:
parent
baa1b930d9
commit
137e9711c2
|
@ -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<Double>();
|
||||
var pvPowerAverage = new List<Double>();
|
||||
var batteryPowerAverage = new List<Double>();
|
||||
|
||||
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;
|
||||
|
|
Loading…
Reference in New Issue