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