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 Boolean ForceCalibrationCharge { 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 DevicesConfig IslandMode { get; set; } public required DevicesConfig GridTie { get; set; } public required Double MaxBatteryChargingCurrent { get; set; } public required Double MaxBatteryDischargingCurrent { get; set; } public required Double MaxDcPower { get; set; } public required Double MaxChargeBatteryVoltage { get; set; } public required Double MinDischargeBatteryVoltage { get; set; } public required DeviceConfig Devices { get; set; } public required S3Config? S3 { get; set; } private static String? LastSavedData { get; set; } #if DEBUG public static Config Default => new() { MinSoc = 20, ForceCalibrationCharge = false, PConstant = .5, GridSetPoint = 0, BatterySelfDischargePower = 200, HoldSocZone = 1, // TODO: find better name, IslandMode = new() { AcDc = new () { MinDcLinkVoltage = 690, ReferenceDcLinkVoltage = 750, MaxDcLinkVoltage = 810, }, DcDc = new () { UpperDcLinkVoltage = 50, LowerDcLinkVoltage = 50, ReferenceDcLinkVoltage = 750, }, }, GridTie = new() { AcDc = new () { MinDcLinkVoltage = 720, ReferenceDcLinkVoltage = 750, MaxDcLinkVoltage = 810, }, DcDc = new () { UpperDcLinkVoltage = 50, LowerDcLinkVoltage = 50, ReferenceDcLinkVoltage = 750, }, }, MaxBatteryChargingCurrent = 210, MaxBatteryDischargingCurrent = 210, MaxDcPower = 10000, MaxChargeBatteryVoltage = 57, MinDischargeBatteryVoltage = 0, 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 = null }; #else public static Config Default => new() { MinSoc = 20, ForceCalibrationCharge = false, PConstant = .5, GridSetPoint = 0, BatterySelfDischargePower = 200, HoldSocZone = 1, // TODO: find better name, IslandMode = new() { AcDc = new () { MinDcLinkVoltage = 690, ReferenceDcLinkVoltage = 750, MaxDcLinkVoltage = 810, }, DcDc = new () { UpperDcLinkVoltage = 50, LowerDcLinkVoltage = 50, ReferenceDcLinkVoltage = 750, }, }, GridTie = new() { AcDc = new () { MinDcLinkVoltage = 720, ReferenceDcLinkVoltage = 750, MaxDcLinkVoltage = 780, }, DcDc = new () { UpperDcLinkVoltage = 20, LowerDcLinkVoltage = 20, ReferenceDcLinkVoltage = 750, }, }, MaxBatteryChargingCurrent = 210, MaxBatteryDischargingCurrent = 210, MaxDcPower = 10000, MaxChargeBatteryVoltage = 57, MinDischargeBatteryVoltage = 0, 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); if (LastSavedData == jsonString) return; LastSavedData = jsonString; 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(jsonString)!; } catch (Exception e) { $"Failed to read config file {configFilePath}, using default config\n{e}".WriteLine(); return Default; } } public static async Task LoadAsync(String? path = null) { var configFilePath = path ?? DefaultConfigFilePath; try { var jsonString = await File.ReadAllTextAsync(configFilePath); return Deserialize(jsonString)!; } catch (Exception e) { Console.WriteLine($"Couldn't read config file {configFilePath}, using default config"); e.Message.WriteLine(); return Default; } } }