Innovenergy_trunk/csharp/App/SaliMax/src/SystemConfig/Config.cs

149 lines
5.7 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 required Double MinSoc { get; set; }
public required UnixTime LastEoc { get; set; }
public required Double PConstant { get; set; }
public required Double GridSetPoint { get; set; }
public required Double BatterySelfDischargePower { get; set; }
public required Double HoldSocZone { get; set; }
public required Double MaxDcBusVoltage { get; set; }
public required Double MinDcBusVoltage { get; set; }
public required Double ReferenceDcBusVoltage { get; set; }
public required DeviceConfig Devices { get; set; }
public required S3Config? S3 { get; set; }
#if DEBUG
public static Config Default => new()
{
MinSoc = 20,
LastEoc = UnixTime.Epoch, // TODO: remove, use new LastEoc feature from BMS
PConstant = .5,
GridSetPoint = 0,
BatterySelfDischargePower = 200,
HoldSocZone = 1, // TODO: find better name,
MinDcBusVoltage = 690,
ReferenceDcBusVoltage = 750,
MaxDcBusVoltage = 810,
Devices = new ()
{
TruConvertAcIp = new() { Host = "localhost", Port = 5001},
TruConvertDcIp = new() { Host = "localhost", Port = 5002},
GridMeterIp = new() { Host = "localhost", Port = 5003},
IslandBusLoadMeterIp = new() { Host = "localhost", Port = 5004},
AmptIp = new() { Host = "localhost", Port = 5005},
RelaysIp = new() { Host = "localhost", Port = 5006},
BatteryIp = new() { Host = "localhost", Port = 5007},
BatteryNodes = new []{ 2, 3, 4, 5, 6 },
},
S3 = new()
{
Bucket = "saliomameiringen",
Region = "sos-ch-dk-2",
Provider = "exo.io",
ContentType = "text/plain; charset=utf-8",
Key = "EXO2bf0cbd97fbfa75aa36ed46f",
Secret = "Bn1CDPqOG-XpDSbYjfIJxojcHTm391vZTc8z8l_fEPs"
}
};
#else
public static Config Default => new()
{
MinSoc = 20,
LastEoc = UnixTime.Epoch, // TODO: remove, use new LastEoc feature from BMS
PConstant = .5,
GridSetPoint = 0,
BatterySelfDischargePower = 200,
HoldSocZone = 1, // TODO: find better name,
MinDcBusVoltage = 690,
ReferenceDcBusVoltage = 750,
MaxDcBusVoltage = 810,
S3 = new()
{
Bucket = "saliomameiringen",
Region = "sos-ch-dk-2",
Provider = "exo.io",
ContentType = "text/plain; charset=utf-8",
Key = "EXO2bf0cbd97fbfa75aa36ed46f",
Secret = "Bn1CDPqOG-XpDSbYjfIJxojcHTm391vZTc8z8l_fEPs"
},
Devices = new ()
{
RelaysIp = new() { Host = "10.0.1.1", Port = 502},
TruConvertAcIp = new() { Host = "10.0.2.1", Port = 502},
TruConvertDcIp = new() { Host = "10.0.3.1", Port = 502},
GridMeterIp = new() { Host = "10.0.4.1", Port = 502},
IslandBusLoadMeterIp = new() { Host = "10.0.4.2", Port = 502},
AmptIp = new() { Host = "10.0.5.1", Port = 502},
BatteryIp = new() { Host = "localhost", Port = 6855},
BatteryNodes = new []{ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
},
};
#endif
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}".LogInfo();
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;
}
}
}