Update daily data functions for SodistoreMax

This commit is contained in:
Noe 2025-04-09 00:02:23 +02:00
parent 5849507ad1
commit 41ca486edb
4 changed files with 43 additions and 34 deletions

View File

@ -1,4 +1,4 @@
#define Amax #undef Amax
#undef GridLimit #undef GridLimit
using System.IO.Compression; using System.IO.Compression;
@ -701,10 +701,10 @@ internal static class Program
switch (record.EssControl.Mode) switch (record.EssControl.Mode)
{ {
case EssMode.CalibrationCharge: case EssMode.CalibrationCharge:
record.Relays?.PerformSlowFlashingGreenLed(); //record.Relays?.PerformSlowFlashingGreenLed();
break; break;
case EssMode.OptimizeSelfConsumption when !_curtailFlag: case EssMode.OptimizeSelfConsumption when !_curtailFlag:
record.Relays?.PerformSolidGreenLed(); //record.Relays?.PerformSolidGreenLed();
break; break;
case EssMode.Off: case EssMode.Off:
break; break;
@ -720,7 +720,7 @@ internal static class Program
{ {
if (_curtailFlag) if (_curtailFlag)
{ {
record.Relays?.PerformFastFlashingGreenLed(); //record.Relays?.PerformFastFlashingGreenLed();
} }
break; break;
@ -729,22 +729,22 @@ internal static class Program
} }
else if (record.Battery?.Soc != null && record.StateMachine.State != 23 && record.Battery.Soc > 50) else if (record.Battery?.Soc != null && record.StateMachine.State != 23 && record.Battery.Soc > 50)
{ {
record.Relays?.PerformSolidOrangeLed(); //record.Relays?.PerformSolidOrangeLed();
} }
else if (record.Battery?.Soc != null && record.StateMachine.State != 23 && record.Battery.Soc < 50 && record.Battery.Soc > 20) else if (record.Battery?.Soc != null && record.StateMachine.State != 23 && record.Battery.Soc < 50 && record.Battery.Soc > 20)
{ {
record.Relays?.PerformSlowFlashingOrangeLed(); //record.Relays?.PerformSlowFlashingOrangeLed();
} }
else if (record.Battery?.Soc != null && record.StateMachine.State != 23 && record.Battery.Soc < 20) else if (record.Battery?.Soc != null && record.StateMachine.State != 23 && record.Battery.Soc < 20)
{ {
record.Relays?.PerformFastFlashingOrangeLed(); //record.Relays?.PerformFastFlashingOrangeLed();
} }
var criticalAlarm = record.DetectAlarmStates(); var criticalAlarm = record.DetectAlarmStates();
if (criticalAlarm is not null) if (criticalAlarm is not null)
{ {
record.Relays?.PerformFastFlashingRedLed(); //record.Relays?.PerformFastFlashingRedLed();
} }
} }

View File

@ -60,7 +60,8 @@ public interface IRelaysRecord
UInt16 PulseOut2HighTime { get; set; } UInt16 PulseOut2HighTime { get; set; }
UInt16 PulseOut3HighTime { get; set; } UInt16 PulseOut3HighTime { get; set; }
UInt16 PulseOut4HighTime { get; set; } UInt16 PulseOut4HighTime { get; set; }
UInt16 PulseOut5HighTime { get; set; } UInt16 PulseOut5HighTime { get; set; }
void PerformSolidGreenLed(); void PerformSolidGreenLed();
void PerformSlowFlashingGreenLed(); void PerformSlowFlashingGreenLed();
@ -73,6 +74,7 @@ public interface IRelaysRecord
void PerformSolidRedLed(); void PerformSolidRedLed();
void PerformSlowFlashingRedLed(); void PerformSlowFlashingRedLed();
void PerformFastFlashingRedLed();*/ void PerformFastFlashingRedLed();
*/
} }

View File

@ -96,11 +96,10 @@ public static class Aggregator
} }
} }
} }
// this for test
private static AggregatedData CreateHourlyData(String myDirectory, Int64 afterTimestamp, Int64 beforeTimestamp) private static AggregatedData CreateHourlyData(String myDirectory, Int64 afterTimestamp, Int64 beforeTimestamp)
{ {
// Get all CSV files in the specified directory // Get all json files in the specified directory
var jsonFiles = Directory.GetFiles(myDirectory, "*.json"); var jsonFiles = Directory.GetFiles(myDirectory, "*.json");
var batterySoc = new List<Double>(); var batterySoc = new List<Double>();
var pvPowerSum = new List<Double>(); var pvPowerSum = new List<Double>();
@ -136,6 +135,9 @@ public static class Aggregator
} }
var jsonObject = JObject.Parse(jsonData); var jsonObject = JObject.Parse(jsonData);
//Console.WriteLine(jsonObject);
// Console.WriteLine(jsonObject["GridMeter"]);
if (jsonObject["Battery"] != null && jsonObject["Battery"]["Soc"] != null) if (jsonObject["Battery"] != null && jsonObject["Battery"]["Soc"] != null)
@ -146,9 +148,9 @@ public static class Aggregator
{ {
pvPowerSum.Add((double)jsonObject["PvOnDc"]["DcWh"]); pvPowerSum.Add((double)jsonObject["PvOnDc"]["DcWh"]);
} }
if (jsonObject["Battery"] != null && jsonObject["Battery"]["Dc"]["Power"] != null) if (jsonObject["Battery"] != null && jsonObject["Battery"]["Power"] != null)
{ {
double batteryPower = (double)jsonObject["Battery"]["Dc"]["Power"]; double batteryPower = (double)jsonObject["Battery"]["Power"];
if (batteryPower < 0) if (batteryPower < 0)
batteryDischargePower.Add(batteryPower); batteryDischargePower.Add(batteryPower);
else else
@ -156,10 +158,12 @@ public static class Aggregator
} }
if (jsonObject["GridMeter"] != null && jsonObject["GridMeter"]["ActivePowerExportT3"] != null) if (jsonObject["GridMeter"] != null && jsonObject["GridMeter"]["ActivePowerExportT3"] != null)
{ {
Console.WriteLine("power export is "+jsonObject["GridMeter"]["ActivePowerExportT3"]);
gridPowerExport.Add((double)jsonObject["GridMeter"]["ActivePowerExportT3"]); gridPowerExport.Add((double)jsonObject["GridMeter"]["ActivePowerExportT3"]);
} }
if (jsonObject["GridMeter"] != null && jsonObject["GridMeter"]["ActivePowerImportT3"] != null) if (jsonObject["GridMeter"] != null && jsonObject["GridMeter"]["ActivePowerImportT3"] != null)
{ {
Console.WriteLine("power import is "+jsonObject["GridMeter"]["ActivePowerImportT3"]);
gridPowerImport.Add((double)jsonObject["GridMeter"]["ActivePowerImportT3"]); gridPowerImport.Add((double)jsonObject["GridMeter"]["ActivePowerImportT3"]);
} }
if (jsonObject["Battery"] != null && jsonObject["Battery"]["HeatingPower"] != null) if (jsonObject["Battery"] != null && jsonObject["Battery"]["HeatingPower"] != null)
@ -252,6 +256,9 @@ public static class Aggregator
//Energy (kWh)= (Average Power / 3600) × Time (1 seconds) //Energy (kWh)= (Average Power / 3600) × Time (1 seconds)
// //
// Dividing the Average power readings by 3600 converts the result from watt-seconds to kilowatt-hours. // Dividing the Average power readings by 3600 converts the result from watt-seconds to kilowatt-hours.
//Console.WriteLine("MAX IS "+gridPowerExport.Max());
// Console.WriteLine("MIN IS "+gridPowerExport.Min());
var dischargingEnergy = (batteryDischargePower.Any() ? batteryDischargePower.Average() : 0.0) / 3600; var dischargingEnergy = (batteryDischargePower.Any() ? batteryDischargePower.Average() : 0.0) / 3600;
var chargingEnergy = (batteryChargePower.Any() ? batteryChargePower.Average() : 0.0) / 3600; var chargingEnergy = (batteryChargePower.Any() ? batteryChargePower.Average() : 0.0) / 3600;
@ -276,7 +283,7 @@ public static class Aggregator
HeatingPower = heatingPowerAvg HeatingPower = heatingPowerAvg
}; };
// Print the stored CSV data for verification // Print the stored JSON data for verification
Console.WriteLine($"Max SOC: {aggregatedData.MaxSoc}"); Console.WriteLine($"Max SOC: {aggregatedData.MaxSoc}");
Console.WriteLine($"Min SOC: {aggregatedData.MinSoc}"); Console.WriteLine($"Min SOC: {aggregatedData.MinSoc}");
@ -289,7 +296,7 @@ public static class Aggregator
Console.WriteLine($"Min SOC: {aggregatedData.MinSoc}"); Console.WriteLine($"Min SOC: {aggregatedData.MinSoc}");
Console.WriteLine("CSV data reading and storage completed."); Console.WriteLine("JSON data reading and storage completed.");
Console.WriteLine("-----------------------------------------------------------------------------------------------------------------"); Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
return aggregatedData; return aggregatedData;
@ -453,7 +460,7 @@ public static class Aggregator
HeatingPower = heatingPowerAvg.Any() ? heatingPowerAvg.Average() : 0.0, HeatingPower = heatingPowerAvg.Any() ? heatingPowerAvg.Average() : 0.0,
}; };
// Print the stored CSV data for verification // Print the stored JSON data for verification
Console.WriteLine($"Pv Power: {aggregatedData.PvPower}"); Console.WriteLine($"Pv Power: {aggregatedData.PvPower}");
Console.WriteLine($"Heating Power: {aggregatedData.HeatingPower}"); Console.WriteLine($"Heating Power: {aggregatedData.HeatingPower}");
Console.WriteLine($"Max SOC: {aggregatedData.MaxSoc}"); Console.WriteLine($"Max SOC: {aggregatedData.MaxSoc}");
@ -467,8 +474,7 @@ public static class Aggregator
Console.WriteLine("CSV data reading and storage completed."); Console.WriteLine("JSON data reading and storage completed.");
Console.WriteLine("CSV data reading and storage completed.");
Console.WriteLine("-----------------------------------------------------------------------------------------------------------------"); Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
return aggregatedData; return aggregatedData;

View File

@ -6,6 +6,7 @@ using InnovEnergy.App.SodiStoreMax.Devices;
using InnovEnergy.App.SodiStoreMax.SystemConfig; using InnovEnergy.App.SodiStoreMax.SystemConfig;
using InnovEnergy.Lib.Units; using InnovEnergy.Lib.Units;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using Newtonsoft.Json;
using static System.Text.Json.JsonSerializer; using static System.Text.Json.JsonSerializer;
namespace InnovEnergy.App.SodiStoreMax.AggregationService; namespace InnovEnergy.App.SodiStoreMax.AggregationService;
@ -30,7 +31,7 @@ public class AggregatedData
{ {
var date = DateTime.Now.ToUnixTime(); var date = DateTime.Now.ToUnixTime();
var defaultHDataPath = Environment.CurrentDirectory + "/" + directory + "/"; var defaultHDataPath = Environment.CurrentDirectory + "/" + directory + "/";
var dataFilePath = defaultHDataPath + date + ".csv"; var dataFilePath = defaultHDataPath + date + ".json";
if (!Directory.Exists(defaultHDataPath)) if (!Directory.Exists(defaultHDataPath))
{ {
@ -41,8 +42,11 @@ public class AggregatedData
try try
{ {
var csvString = this.ToCsv(); // Convert the object to a JSON string
File.WriteAllText(dataFilePath, csvString); var jsonString = JsonConvert.SerializeObject(this, Formatting.None);
// Write JSON to file
File.WriteAllText(dataFilePath, jsonString);
} }
catch (Exception e) catch (Exception e)
{ {
@ -54,21 +58,21 @@ public class AggregatedData
public static void DeleteDailyData(String directory) public static void DeleteDailyData(String directory)
{ {
var csvFiles = Directory.GetFiles(directory, "*.csv"); var jsonFiles = Directory.GetFiles(directory, "*.json");
foreach (var csvFile in csvFiles) foreach (var jsonFile in jsonFiles)
{ {
File.Delete(csvFile); File.Delete(jsonFile);
Console.WriteLine($"Deleted daily data file: {csvFile}"); Console.WriteLine($"Deleted daily data file: {jsonFile}");
} }
} }
public async Task<Boolean> PushToS3() public async Task<Boolean> PushToS3()
{ {
var csv = this.ToCsv(); var jsonString = JsonConvert.SerializeObject(this, Formatting.None);
if (_S3Config is null) if (_S3Config is null)
return false; return false;
var s3Path = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".csv"; var s3Path = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".json";
var request = _S3Config.CreatePutRequest(s3Path); var request = _S3Config.CreatePutRequest(s3Path);
// Compress CSV data to a byte array // Compress CSV data to a byte array
@ -78,11 +82,11 @@ public class AggregatedData
//Create a zip directory and put the compressed file inside //Create a zip directory and put the compressed file inside
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{ {
var entry = archive.CreateEntry("data.csv", CompressionLevel.SmallestSize); // Add CSV data to the ZIP archive var entry = archive.CreateEntry("data.json", CompressionLevel.SmallestSize); // Add CSV data to the ZIP archive
using (var entryStream = entry.Open()) using (var entryStream = entry.Open())
using (var writer = new StreamWriter(entryStream)) using (var writer = new StreamWriter(entryStream))
{ {
writer.Write(csv); writer.Write(jsonString);
} }
} }
@ -98,9 +102,6 @@ public class AggregatedData
// Upload the compressed data (ZIP archive) to S3 // Upload the compressed data (ZIP archive) to S3
var response = await request.PutAsync(stringContent); var response = await request.PutAsync(stringContent);
//
// var request = _S3Config.CreatePutRequest(s3Path);
// var response = await request.PutAsync(new StringContent(csv));
if (response.StatusCode != 200) if (response.StatusCode != 200)
{ {