Update Create Hour function
This commit is contained in:
parent
baa1b930d9
commit
137e9711c2
|
@ -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;
|
||||||
|
|
||||||
|
@ -43,64 +44,104 @@ public static class Aggregator
|
||||||
|
|
||||||
// Get all CSV files in the specified directory
|
// Get all CSV files in the specified directory
|
||||||
var csvFiles = Directory.GetFiles(myDirectory, "*.csv");
|
var csvFiles = Directory.GetFiles(myDirectory, "*.csv");
|
||||||
|
|
||||||
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
|
|
||||||
.Where(csvFile => IsFileWithinTimeRange(csvFile, oneHourBefore, currentTimestamp))
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
try
|
foreach (var csvFile in csvFiles)
|
||||||
{
|
{
|
||||||
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();
|
|
||||||
|
while (!reader.EndOfStream)
|
||||||
var average1HBatteryPower = validFiles
|
{
|
||||||
.Select(csvFile => csvFile.ExtractValue("/Battery/Dc/Power"))
|
|
||||||
.NotNull()
|
var line = reader.ReadLine();
|
||||||
.Average();
|
var lines = line?.Split(';');
|
||||||
|
|
||||||
|
// Assuming there are always three columns (variable name and its value)
|
||||||
Console.WriteLine($"SOC: {average1HSoc}");
|
if (lines is { Length: 3 })
|
||||||
Console.WriteLine($"PvPower: {average1HPvPower}");
|
{
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
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)
|
|
||||||
{
|
// Calculate the average of values
|
||||||
e.WriteLine();
|
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"
|
||||||
Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
|
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
|
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)
|
||||||
{
|
{
|
||||||
var fileTimestamp = long.TryParse(Path.GetFileNameWithoutExtension(filePath).Replace("log_", ""), out var fileTimestamp1) ? fileTimestamp1 : -1;
|
var fileTimestamp = long.TryParse(Path.GetFileNameWithoutExtension(filePath).Replace("log_", ""), out var fileTimestamp1) ? fileTimestamp1 : -1;
|
||||||
|
|
Loading…
Reference in New Issue