93 lines
3.0 KiB
C#
93 lines
3.0 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using System.Text.Json;
|
|
using InnovEnergy.Lib.Utils;
|
|
using static System.Text.Json.JsonSerializer;
|
|
|
|
|
|
namespace InnovEnergy.App.SinexcelCommunication.SystemConfig;
|
|
|
|
[SuppressMessage("Trimming", "IL2026:Members annotated with \'RequiresUnreferencedCodeAttribute\' require dynamic access otherwise can break functionality when trimming application code")]
|
|
public class Config
|
|
{
|
|
|
|
private static String DefaultConfigFilePath => Path.Combine(Environment.CurrentDirectory, "config.json");
|
|
private static DateTime DefaultDatetime => new(2025, 01, 01, 09, 00, 00);
|
|
|
|
private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true };
|
|
|
|
public required Double MinSoc { get; set; }
|
|
public required Double GridSetPoint { get; set; }
|
|
|
|
public required S3Config? S3 { get; set; }
|
|
|
|
|
|
private static String? LastSavedData { get; set; }
|
|
|
|
public static Config Default => new()
|
|
{
|
|
MinSoc = 20,
|
|
GridSetPoint = 0,
|
|
S3 = new()
|
|
{
|
|
Bucket = "1-3e5b3069-214a-43ee-8d85-57d72000c19d",
|
|
Region = "sos-ch-dk-2",
|
|
Provider = "exo.io",
|
|
Key = "EXObb5a49acb1061781761895e7",
|
|
Secret = "sKhln0w8ii3ezZ1SJFF33yeDo8NWR1V4w2H0D4-350I",
|
|
ContentType = "text/plain; charset=utf-8"
|
|
}
|
|
};
|
|
|
|
public void Save(String? path = null)
|
|
{
|
|
var configFilePath = path ?? DefaultConfigFilePath;
|
|
|
|
try
|
|
{
|
|
var jsonString = Serialize(this, JsonOptions);
|
|
|
|
if (LastSavedData == jsonString)
|
|
return;
|
|
|
|
LastSavedData = jsonString;
|
|
|
|
File.WriteAllText(configFilePath, jsonString);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
$"Failed to write config file {configFilePath}\n{e}".WriteLine();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public static Config Load(String? path = null)
|
|
{
|
|
var configFilePath = path ?? DefaultConfigFilePath;
|
|
try
|
|
{
|
|
var jsonString = File.ReadAllText(configFilePath);
|
|
return Deserialize<Config>(jsonString)!;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
$"Failed to read config file {configFilePath}, using default config\n{e}".WriteLine();
|
|
return Default;
|
|
}
|
|
}
|
|
|
|
public static async Task<Config> LoadAsync(String? path = null)
|
|
{
|
|
var configFilePath = path ?? DefaultConfigFilePath;
|
|
try
|
|
{
|
|
var jsonString = await File.ReadAllTextAsync(configFilePath);
|
|
return Deserialize<Config>(jsonString)!;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine($"Couldn't read config file {configFilePath}, using default config");
|
|
e.Message.WriteLine();
|
|
return Default;
|
|
}
|
|
}
|
|
} |