91 lines
2.9 KiB
C#
91 lines
2.9 KiB
C#
using System.Text.Json;
|
|
using InnovEnergy.Lib.Time.Unix;
|
|
using InnovEnergy.Lib.Utils;
|
|
using static System.Text.Json.JsonSerializer;
|
|
|
|
namespace InnovEnergy.App.SaliMax.SystemConfig;
|
|
|
|
// shut up trim warnings
|
|
#pragma warning disable IL2026
|
|
|
|
public class Config //TODO: let IE choose from config files (Json) and connect to GUI
|
|
{
|
|
private static String DefaultConfigFilePath => Path.Combine(Environment.CurrentDirectory, "config.json");
|
|
|
|
private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true };
|
|
|
|
public Double MinSoc { get; set; }
|
|
public UnixTime LastEoc { get; set; }
|
|
public Double PConstant { get; set; }
|
|
public Double ForceChargePower { get; set; }
|
|
public Double ForceDischargePower { get; set; }
|
|
public Double MaxInverterPower { get; set; }
|
|
public Double GridSetPoint { get; set; }
|
|
public Double SelfDischargePower { get; set; }
|
|
public Double HoldSocZone { get; set; }
|
|
public Double ControllerPConstant { get; set; }
|
|
|
|
public static Config Default => new()
|
|
{
|
|
MinSoc = 20,
|
|
LastEoc = UnixTime.Epoch,
|
|
PConstant = .5,
|
|
ForceChargePower = 1_000_000,
|
|
ForceDischargePower = -1_000_000,
|
|
MaxInverterPower = 32_000,
|
|
GridSetPoint = 0.0,
|
|
SelfDischargePower = 200, // TODO: multiple batteries
|
|
HoldSocZone = 1, // TODO: find better name,
|
|
ControllerPConstant = 0.5
|
|
};
|
|
|
|
|
|
public void Save(String? path = null)
|
|
{
|
|
var configFilePath = path ?? DefaultConfigFilePath;
|
|
|
|
try
|
|
{
|
|
var jsonString = Serialize(this, JsonOptions);
|
|
File.WriteAllText(configFilePath, jsonString);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
$"Failed to write config file {configFilePath}\n{e}".Log();
|
|
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}".Log();
|
|
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;
|
|
}
|
|
}
|
|
} |