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,15 +692,15 @@ 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 line = reader.ReadLine();
@ -706,26 +709,44 @@ internal static class Program
// Assuming there are always three columns (variable name and its value) // Assuming there are always three columns (variable name and its value)
if (values is { Length: 3 }) if (values is { Length: 3 })
{ {
String variableName = values[0].Trim(); var variableName = values[0].Trim();
String variableValue = values[1].Trim(); var value = TryParse(values[1].Trim(), out var v) ? v : 0;
// Check if variableValue is a valid number // Check if variableValue is a valid number
if (IsSoc(variableName)) if (IsSoc(variableName))
{ {
if (socAverage == 0){socAverage = double.TryParse(variableValue, out double v) ? v : 0;} if (socAverage == 0)
else{socAverage = (socAverage + (double.TryParse(variableValue, out double v) ? v : 0)) / 2;} {
socAverage = value;
}
else
{
socAverage = (socAverage + value) / 2;
}
} }
if (IsPvPower(variableName)) if (IsPvPower(variableName))
{ {
if (pvPowerAverage == 0){pvPowerAverage = double.TryParse(variableValue, out double v) ? v : 0;} if (pvPowerAverage == 0)
else{pvPowerAverage = (pvPowerAverage + (double.TryParse(variableValue, out double v) ? v : 0)) / 2;} {
pvPowerAverage = value;
}
else
{
pvPowerAverage = (pvPowerAverage + value) / 2;
}
} }
if (IsBatteryPower(variableName)) if (IsBatteryPower(variableName))
{ {
if (batteryPowerAverage == 0){batteryPowerAverage = double.TryParse(variableValue, out double v) ? v : 0;} if (batteryPowerAverage == 0)
else{batteryPowerAverage = (batteryPowerAverage + (double.TryParse(variableValue, out double v) ? v : 0)) / 2;} {
batteryPowerAverage = value;
}
else
{
batteryPowerAverage = (batteryPowerAverage + value) / 2;
}
} }
else else
{ {
@ -742,7 +763,6 @@ internal static class Program
} }
} }
} }
}
// Print the stored CSV data for verification // Print the stored CSV data for verification
@ -775,7 +795,7 @@ 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;
} }