118 lines
4.0 KiB
C#
118 lines
4.0 KiB
C#
using System.IO.Compression;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using Flurl.Http;
|
|
using InnovEnergy.App.SaliMax.Devices;
|
|
using InnovEnergy.App.SaliMax.SystemConfig;
|
|
using InnovEnergy.Lib.Units;
|
|
using InnovEnergy.Lib.Utils;
|
|
using Newtonsoft.Json;
|
|
using static System.Text.Json.JsonSerializer;
|
|
|
|
namespace InnovEnergy.App.SaliMax.AggregationService;
|
|
// shut up trim warnings
|
|
#pragma warning disable IL2026
|
|
|
|
public class AggregatedData
|
|
{
|
|
public required Double MinSoc { get; set; }
|
|
public required Double MaxSoc { get; set; }
|
|
public required Double PvPower { get; set; }
|
|
public required Double DischargingBatteryPower { get; set; }
|
|
public required Double ChargingBatteryPower { get; set; }
|
|
public required Double GridExportPower { get; set; }
|
|
public required Double GridImportPower { get; set; }
|
|
public required Double HeatingPower { get; set; }
|
|
|
|
|
|
private readonly S3Config? _S3Config = Config.Load().S3;
|
|
|
|
public void Save(String directory)
|
|
{
|
|
var date = DateTime.Now.ToUnixTime();
|
|
var defaultHDataPath = Environment.CurrentDirectory + "/" + directory + "/";
|
|
var dataFilePath = defaultHDataPath + date + ".json";
|
|
|
|
if (!Directory.Exists(defaultHDataPath))
|
|
{
|
|
Directory.CreateDirectory(defaultHDataPath);
|
|
Console.WriteLine("Directory created successfully.");
|
|
}
|
|
Console.WriteLine("data file path is " + dataFilePath);
|
|
|
|
try
|
|
{
|
|
// Convert the object to a JSON string
|
|
var jsonString = JsonConvert.SerializeObject(this, Formatting.None);
|
|
|
|
// Write JSON to file
|
|
File.WriteAllText(dataFilePath, jsonString);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
$"Failed to write config file {dataFilePath}\n{e}".WriteLine();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public static void DeleteDailyData(String directory)
|
|
{
|
|
|
|
var jsonFiles = Directory.GetFiles(directory, "*.json");
|
|
foreach (var jsonFile in jsonFiles)
|
|
{
|
|
File.Delete(jsonFile);
|
|
Console.WriteLine($"Deleted daily data file: {jsonFile}");
|
|
}
|
|
}
|
|
|
|
public async Task<Boolean> PushToS3()
|
|
{
|
|
var jsonString = JsonConvert.SerializeObject(this, Formatting.None);
|
|
if (_S3Config is null)
|
|
return false;
|
|
|
|
var s3Path = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".json";
|
|
var request = _S3Config.CreatePutRequest(s3Path);
|
|
|
|
// Compress CSV data to a byte array
|
|
byte[] compressedBytes;
|
|
using (var memoryStream = new MemoryStream())
|
|
{
|
|
//Create a zip directory and put the compressed file inside
|
|
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
|
|
{
|
|
var entry = archive.CreateEntry("data.json", CompressionLevel.SmallestSize); // Add CSV data to the ZIP archive
|
|
using (var entryStream = entry.Open())
|
|
using (var writer = new StreamWriter(entryStream))
|
|
{
|
|
writer.Write(jsonString);
|
|
}
|
|
}
|
|
|
|
compressedBytes = memoryStream.ToArray();
|
|
}
|
|
|
|
// Encode the compressed byte array as a Base64 string
|
|
string base64String = Convert.ToBase64String(compressedBytes);
|
|
|
|
// Create StringContent from Base64 string
|
|
var stringContent = new StringContent(base64String, Encoding.UTF8, "application/base64");
|
|
|
|
// Upload the compressed data (ZIP archive) to S3
|
|
var response = await request.PutAsync(stringContent);
|
|
|
|
|
|
if (response.StatusCode != 200)
|
|
{
|
|
Console.WriteLine("ERROR: PUT");
|
|
var error = await response.GetStringAsync();
|
|
Console.WriteLine(error);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
} |