76 lines
2.4 KiB
C#
76 lines
2.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
|
|
namespace InnovEnergy.App.GrowattCommunication.AggregationService;
|
|
|
|
|
|
public class HourlyEnergyData
|
|
{
|
|
public String Type { get; set; } = "Hourly";
|
|
public DateTime Timestamp { get; set; }
|
|
|
|
public double SelfGeneratedElectricity { get; set; }
|
|
public double ElectricityPurchased { get; set; }
|
|
public double ElectricityFed { get; set; }
|
|
public double BatteryChargeEnergy { get; set; }
|
|
public double BatteryDischargeEnergy { get; set; }
|
|
public double LoadPowerConsumption { get; set; }
|
|
}
|
|
|
|
public class DailyEnergyData
|
|
{
|
|
public String Type { get; set; } = "Daily";
|
|
public DateTime Timestamp { get; set; }
|
|
|
|
public double DailySelfGeneratedElectricity { get; set; }
|
|
public double DailyElectricityPurchased { get; set; }
|
|
public double DailyElectricityFed { get; set; }
|
|
public double BatteryDailyChargeEnergy { get; set; }
|
|
public double BatteryDailyDischargeEnergy { get; set; }
|
|
public double DailyLoadPowerConsumption { get; set; }
|
|
}
|
|
|
|
public static class AggregatedDataFileWriter
|
|
{
|
|
private static bool _folderCreated = false;
|
|
|
|
private static readonly JsonSerializerOptions JsonOptions = new JsonSerializerOptions
|
|
{
|
|
WriteIndented = false
|
|
};
|
|
|
|
public static void AppendHourlyData(HourlyEnergyData data, string baseFolder)
|
|
{
|
|
var filePath = GetDailyFilePath(data.Timestamp, baseFolder);
|
|
AppendJsonLine(filePath, data);
|
|
Console.WriteLine($"Hourly data appended to {filePath}");
|
|
}
|
|
|
|
public static void AppendDailyData(DailyEnergyData data, string baseFolder)
|
|
{
|
|
var filePath = GetDailyFilePath(data.Timestamp, baseFolder);
|
|
AppendJsonLine(filePath, data);
|
|
Console.WriteLine($"Daily data appended to {filePath}");
|
|
}
|
|
|
|
private static string GetDailyFilePath(DateTime timestamp, string baseFolder)
|
|
{
|
|
var folder = Path.Combine(baseFolder, "AggregatedData");
|
|
|
|
if (!_folderCreated)
|
|
{
|
|
Directory.CreateDirectory(folder);
|
|
_folderCreated = true;
|
|
}
|
|
|
|
var fileName = timestamp.ToString("ddMMyyyy") + ".json";
|
|
return Path.Combine(folder, fileName);
|
|
}
|
|
|
|
private static void AppendJsonLine<T>(string filePath, T data)
|
|
{
|
|
var json = JsonSerializer.Serialize(data, JsonOptions);
|
|
File.AppendAllText(filePath, json + Environment.NewLine);
|
|
}
|
|
} |