update createHourlyAvreage function

This commit is contained in:
atef 2023-12-11 18:03:01 +01:00
parent 7bc70f49f6
commit 9a60bdb4ae
1 changed files with 63 additions and 43 deletions

View File

@ -27,6 +27,7 @@ using InnovEnergy.Lib.Units;
using InnovEnergy.Lib.Utils;
using System.Text.Json;
using RabbitMQ.Client;
using static System.Double;
using static InnovEnergy.Lib.Devices.Trumpf.SystemControl.DataTypes.SystemConfig;
using DeviceState = InnovEnergy.App.SaliMax.Devices.DeviceState;
@ -671,17 +672,19 @@ internal static class Program
private static void CreateHourlyAverage()
{
string myDirectory = "LogDirectory";
var myDirectory = "LogDirectory";
// Get all CSV files in the specified directory
string[] csvFiles = Directory.GetFiles(myDirectory, "*.csv");
var csvFiles = Directory.GetFiles(myDirectory, "*.csv");
var currentTimestamp = DateTime.Now.ToUnixTime();
Double socAverage = 0;
Double pvPowerAverage = 0;
Double batteryPowerAverage = 0;
Int64 currentTimestamp = DateTime.Now.ToUnixTime();
Double socAverage=0;
Double pvPowerAverage=0;
Double batteryPowerAverage=0;
Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
Console.WriteLine("File timestamp should start after "+DateTime.Now.AddHours(-1));
Console.WriteLine("File timestamp should start after "+ DateTime.Now.AddHours(-1));
foreach (var csvFile in csvFiles)
{
@ -689,57 +692,74 @@ internal static class Program
{
continue;
}
Int64 fileTimestamp = Int64.Parse(Path.GetFileNameWithoutExtension(csvFile).Replace("log_", ""));
Int64 oneHourBefore = DateTime.Now.AddHours(-1).ToUnixTime();
var fileTimestamp = Int64.Parse(Path.GetFileNameWithoutExtension(csvFile).Replace("log_", ""));
var oneHourBefore = DateTime.Now.AddHours(-1).ToUnixTime();
if (fileTimestamp >= oneHourBefore && fileTimestamp <= currentTimestamp)
{
Console.WriteLine("Check file created at "+DateTimeOffset.FromUnixTimeSeconds(fileTimestamp).DateTime);
using (var reader = new StreamReader(csvFile))
Console.WriteLine("Check file created at "+ DateTimeOffset.FromUnixTimeSeconds(fileTimestamp).DateTime);
using var reader = new StreamReader(csvFile);
while (!reader.EndOfStream)
{
while (!reader.EndOfStream)
var line = reader.ReadLine();
var values = line?.Split(';');
// Assuming there are always three columns (variable name and its value)
if (values is { Length: 3 })
{
var line = reader.ReadLine();
var values = line?.Split(';');
var variableName = values[0].Trim();
var value = TryParse(values[1].Trim(), out var v) ? v : 0;
// Assuming there are always three columns (variable name and its value)
if (values is { Length: 3 })
// Check if variableValue is a valid number
if (IsSoc(variableName))
{
String variableName = values[0].Trim();
String variableValue = values[1].Trim();
// Check if variableValue is a valid number
if (IsSoc(variableName))
if (socAverage == 0)
{
if (socAverage == 0){socAverage = double.TryParse(variableValue, out double v) ? v : 0;}
else{socAverage = (socAverage + (double.TryParse(variableValue, out double v) ? v : 0)) / 2;}
}
if (IsPvPower(variableName))
{
if (pvPowerAverage == 0){pvPowerAverage = double.TryParse(variableValue, out double v) ? v : 0;}
else{pvPowerAverage = (pvPowerAverage + (double.TryParse(variableValue, out double v) ? v : 0)) / 2;}
}
if (IsBatteryPower(variableName))
{
if (batteryPowerAverage == 0){batteryPowerAverage = double.TryParse(variableValue, out double v) ? v : 0;}
else{batteryPowerAverage = (batteryPowerAverage + (double.TryParse(variableValue, out double v) ? v : 0)) / 2;}
socAverage = value;
}
else
{
// Handle cases where variableValue is not a valid number
// Console.WriteLine($"Invalid numeric value for variable {variableName}: {variableValue}");
socAverage = (socAverage + value) / 2;
}
}
if (IsPvPower(variableName))
{
if (pvPowerAverage == 0)
{
pvPowerAverage = value;
}
else
{
pvPowerAverage = (pvPowerAverage + value) / 2;
}
}
if (IsBatteryPower(variableName))
{
if (batteryPowerAverage == 0)
{
batteryPowerAverage = value;
}
else
{
batteryPowerAverage = (batteryPowerAverage + value) / 2;
}
}
else
{
// Handle invalid CSV format
//Console.WriteLine($"Invalid format in file: {csvFile}");
//break;
// Handle cases where variableValue is not a valid number
// Console.WriteLine($"Invalid numeric value for variable {variableName}: {variableValue}");
}
}
else
{
// Handle invalid CSV format
//Console.WriteLine($"Invalid format in file: {csvFile}");
//break;
}
}
}
}
@ -774,8 +794,8 @@ internal static class Program
private static void ApplyConfigFile(this StatusRecord status, Configuration config)
{
status.Config.MinSoc = config.MinimumSoC;
status.Config.GridSetPoint = config.GridSetPoint*1000;
status.Config.MinSoc = config.MinimumSoC;
status.Config.GridSetPoint = config.GridSetPoint * 1000; // converted from kW to W
status.Config.ForceCalibrationCharge = config.ForceCalibrationCharge;
}