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;
|
||||
|
||||
|
@ -47,40 +48,87 @@ public static class Aggregator
|
|||
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();
|
||||
if (csvFile == "LogDirectory/log.csv")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var average1HPvPower = validFiles
|
||||
.Select(csvFile => csvFile.ExtractValue("/PvOnDc/Dc/Power"))
|
||||
.NotNull()
|
||||
.Average();
|
||||
if (IsFileWithinTimeRange(csvFile, oneHourBefore, currentTimestamp))
|
||||
{
|
||||
using var reader = new StreamReader(csvFile);
|
||||
|
||||
var average1HBatteryPower = validFiles
|
||||
.Select(csvFile => csvFile.ExtractValue("/Battery/Dc/Power"))
|
||||
.NotNull()
|
||||
.Average();
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
|
||||
var line = reader.ReadLine();
|
||||
var lines = line?.Split(';');
|
||||
|
||||
Console.WriteLine($"SOC: {average1HSoc}");
|
||||
Console.WriteLine($"PvPower: {average1HPvPower}");
|
||||
Console.WriteLine($"Battery: {average1HBatteryPower}");
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
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.");
|
||||
//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
|
||||
.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)
|
||||
|
|
Loading…
Reference in New Issue