using System.Text.Json; using InnovEnergy.App.SaliMax.Devices; 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 () { RelaysIp = new() { Host = "localhost", Port = 5006, DeviceState = DeviceState.Measured}, GridMeterIp = new() { Host = "localhost", Port = 5003, DeviceState = DeviceState.Measured}, PvOnAcGrid = new() { Host = "false" , Port = 0 , DeviceState = DeviceState.Measured}, LoadOnAcGrid = new() { Host = "false" , Port = 0 , DeviceState = DeviceState.Measured}, PvOnAcIsland = new() { Host = "true" , Port = 0 , DeviceState = DeviceState.Measured}, IslandBusLoadMeterIp = new() { Host = "localhost", Port = 5004, DeviceState = DeviceState.Measured}, TruConvertAcIp = new() { Host = "localhost", Port = 5001, DeviceState = DeviceState.Measured}, PvOnDc = new() { Host = "localhost", Port = 5005, DeviceState = DeviceState.Measured}, LoadOnDc = new() { Host = "false" , Port = 0 , DeviceState = DeviceState.Measured}, TruConvertDcIp = new() { Host = "localhost", Port = 5002, DeviceState = DeviceState.Measured}, BatteryIp = new() { Host = "localhost", Port = 5007, DeviceState = DeviceState.Measured}, BatteryNodes = new []{ 2, 3, 4, 5, 6 } }, S3 = new() { Bucket = "1-3e5b3069-214a-43ee-8d85-57d72000c19d", Region = "sos-ch-dk-2", Provider = "exo.io", ContentType = "text/plain; charset=utf-8", Key = "EXO4ec5faf1a7650b79b5722fb5", Secret = "LUxu1PGEA-POEIckoEyq6bYyz0RnenW6tmqccMKgkHQ" }, }; #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 = "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" }, Devices = new () { RelaysIp = new() { Host = "10.0.1.1", Port = 502, DeviceState = DeviceState.Measured}, GridMeterIp = new() { Host = "10.0.4.1", Port = 502, DeviceState = DeviceState.Measured}, PvOnAcGrid = new() { Host = "false" , Port = 0 , DeviceState = DeviceState.Measured}, LoadOnAcGrid = new() { Host = "true" , Port = 0 , DeviceState = DeviceState.Measured}, PvOnAcIsland = new() { Host = "false" , Port = 0 , DeviceState = DeviceState.Measured}, IslandBusLoadMeterIp = new() { Host = "10.0.4.2", Port = 502, DeviceState = DeviceState.Measured}, TruConvertAcIp = new() { Host = "10.0.2.1", Port = 502, DeviceState = DeviceState.Measured}, PvOnDc = new() { Host = "10.0.5.1", Port = 502, DeviceState = DeviceState.Measured}, LoadOnDc = new() { Host = "false" , Port = 0 , DeviceState = DeviceState.Measured}, TruConvertDcIp = new() { Host = "10.0.3.1", Port = 502, DeviceState = DeviceState.Measured}, BatteryIp = new() { Host = "localhost", Port = 6855, DeviceState = DeviceState.Measured }, 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; } } }