#define Amax #undef GridLimit using System.IO.Compression; using System.Reactive.Linq; using System.Reactive.Threading.Tasks; using System.Text; using Flurl.Http; using InnovEnergy.App.SaliMax.Devices; using InnovEnergy.App.SaliMax.Ess; using InnovEnergy.App.SaliMax.MiddlewareClasses; using InnovEnergy.App.SaliMax.SaliMaxRelays; using InnovEnergy.App.SaliMax.System; using InnovEnergy.App.SaliMax.SystemConfig; using InnovEnergy.Lib.Devices.AMPT; using InnovEnergy.Lib.Devices.Battery48TL; using InnovEnergy.Lib.Devices.EmuMeter; using InnovEnergy.Lib.Devices.Trumpf.SystemControl; using InnovEnergy.Lib.Devices.Trumpf.SystemControl.DataTypes; using InnovEnergy.Lib.Devices.Trumpf.TruConvertAc; using InnovEnergy.Lib.Devices.Trumpf.TruConvertAc.DataTypes; using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc; using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc.Control; using InnovEnergy.Lib.Protocols.Modbus.Channels; using InnovEnergy.Lib.Units; using InnovEnergy.Lib.Utils; using InnovEnergy.App.SaliMax.DataTypes; using Newtonsoft.Json; using static System.Int32; using static InnovEnergy.App.SaliMax.AggregationService.Aggregator; using static InnovEnergy.App.SaliMax.MiddlewareClasses.MiddlewareAgent; using static InnovEnergy.Lib.Devices.Trumpf.SystemControl.DataTypes.SystemConfig; using DeviceState = InnovEnergy.App.SaliMax.Devices.DeviceState; #pragma warning disable IL2026 namespace InnovEnergy.App.SaliMax; internal static class Program { private static readonly TimeSpan UpdateInterval = TimeSpan.FromSeconds(2); private static readonly IReadOnlyList BatteryNodes; private static readonly Channel TruConvertAcChannel; private static readonly Channel TruConvertDcChannel; private static readonly Channel GridMeterChannel; private static readonly Channel IslandBusLoadChannel; private static readonly Channel PvOnDc; private static readonly Channel SecondPvOnDc; private static readonly Channel PvOnAcGrid; private static readonly Channel PvOnAcIsland; private static readonly Channel RelaysChannel; private static readonly Channel RelaysTsChannel; private static readonly Channel BatteriesChannel; private static Boolean _curtailFlag = false; private const String VpnServerIp = "10.2.0.11"; private static Boolean _subscribedToQueue = false; private static Boolean _subscribeToQueueForTheFirstTime = false; private static SalimaxAlarmState _prevSalimaxState = SalimaxAlarmState.Green; private const UInt16 NbrOfFileToConcatenate = 30; private static UInt16 _counterOfFile = 0; private static SalimaxAlarmState _salimaxAlarmState = SalimaxAlarmState.Green; static Program() { var config = Config.Load(); var d = config.Devices; Channel CreateChannel(SalimaxDevice device) => device.DeviceState == DeviceState.Disabled ? new NullChannel() : new TcpChannel(device); TruConvertAcChannel = CreateChannel(d.TruConvertAcIp); TruConvertDcChannel = CreateChannel(d.TruConvertDcIp); GridMeterChannel = CreateChannel(d.GridMeterIp); IslandBusLoadChannel = CreateChannel(d.IslandBusLoadMeterIp); PvOnDc = CreateChannel(d.PvOnDc); SecondPvOnDc = CreateChannel(d.SecondPvOnDc); PvOnAcGrid = CreateChannel(d.PvOnAcGrid); PvOnAcIsland = CreateChannel(d.PvOnAcIsland); RelaysChannel = CreateChannel(d.RelaysIp); RelaysTsChannel = CreateChannel(d.TsRelaysIp); BatteriesChannel = CreateChannel(d.BatteryIp); BatteryNodes = config .Devices .BatteryNodes .Select(n => n.ConvertTo()) .ToArray(config.Devices.BatteryNodes.Length); } public static async Task Main(String[] args) { //Do not await HourlyDataAggregationManager() .ContinueWith(t=>t.Exception.WriteLine(), TaskContinuationOptions.OnlyOnFaulted) .SupressAwaitWarning(); DailyDataAggregationManager() .ContinueWith(t=>t.Exception.WriteLine(), TaskContinuationOptions.OnlyOnFaulted) .SupressAwaitWarning(); InitializeCommunicationToMiddleware(); while (true) { try { await Run(); } catch (Exception e) { e.LogError(); } } } private static async Task Run() { "Starting Salimax".WriteLine(); Watchdog.NotifyReady(); var battery48TlDevices = BatteryNodes .Select(n => new Battery48TlDevice(BatteriesChannel, n)) .ToList(); var batteryDevices = new Battery48TlDevices(battery48TlDevices); var acDcDevices = new TruConvertAcDcDevices(TruConvertAcChannel); var dcDcDevices = new TruConvertDcDcDevices(TruConvertDcChannel); var gridMeterDevice = new EmuMeterDevice(GridMeterChannel); var acIslandLoadMeter = new EmuMeterDevice(IslandBusLoadChannel); var pvOnDcDevice = new AmptDevices(PvOnDc); var secondPvOnDcDevice = new AmptDevices(SecondPvOnDc); var pvOnAcGridDevice = new AmptDevices(PvOnAcGrid); var pvOnAcIslandDevice = new AmptDevices(PvOnAcIsland); var saliMaxTsRelaysDevice = new RelaysDeviceAdam6060(RelaysTsChannel); #if Amax var saliMaxRelaysDevice = new RelaysDeviceAmax(RelaysChannel); #else var saliMaxRelaysDevice = new RelaysDeviceAdam6360(RelaysChannel); #endif StatusRecord ReadStatus() { var config = Config.Load(); var devices = config.Devices; var acDc = acDcDevices.Read(); var dcDc = dcDcDevices.Read(); var relays = saliMaxRelaysDevice.Read(); var tsRelays = saliMaxTsRelaysDevice.Read(); var loadOnAcIsland = acIslandLoadMeter.Read(); var gridMeter = gridMeterDevice.Read(); var firstPvOnDc = pvOnDcDevice.Read(); var secondPvOnDc = secondPvOnDcDevice.Read(); var battery = batteryDevices.Read(); var pvOnAcGrid = pvOnAcGridDevice.Read(); var pvOnAcIsland = pvOnAcIslandDevice.Read(); IReadOnlyList pvOnDc = new List { firstPvOnDc, secondPvOnDc }; var gridBusToIslandBus = Topology.CalculateGridBusToIslandBusPower(pvOnAcIsland, loadOnAcIsland, acDc); var gridBusLoad = devices.LoadOnAcGrid.DeviceState == DeviceState.Disabled ? new AcPowerDevice { Power = 0 } : Topology.CalculateGridBusLoad(gridMeter, pvOnAcGrid, gridBusToIslandBus); var dcLoad = devices.LoadOnDc.DeviceState == DeviceState.Disabled ? new DcPowerDevice { Power = 0 } : Topology.CalculateDcLoad(acDc, pvOnDc, dcDc); var acDcToDcLink = devices.LoadOnDc.DeviceState == DeviceState.Disabled ? Topology.CalculateAcDcToDcLink(pvOnDc, dcDc, acDc) : new DcPowerDevice{ Power = acDc.Dc.Power}; #if Amax var combinedRelays = relays; #else var combinedRelays = new CombinedAdamRelaysRecord(tsRelays, relays); #endif return new StatusRecord { AcDc = acDc, DcDc = dcDc, Battery = battery, Relays = combinedRelays, GridMeter = gridMeter, PvOnAcGrid = pvOnAcGrid, PvOnAcIsland = pvOnAcIsland, PvOnDc = pvOnDc, AcGridToAcIsland = gridBusToIslandBus, AcDcToDcLink = acDcToDcLink, LoadOnAcGrid = gridBusLoad, LoadOnAcIsland = loadOnAcIsland, LoadOnDc = dcLoad, StateMachine = StateMachine.Default, EssControl = EssControl.Default, Log = new SystemLog { SalimaxAlarmState = SalimaxAlarmState.Green, Message = null, SalimaxAlarms = null, SalimaxWarnings = null}, //TODO: Put real stuff Config = config // load from disk every iteration, so config can be changed while running }; } void WriteControl(StatusRecord r) { if (r.Relays is not null) { #if Amax saliMaxRelaysDevice.Write((RelaysRecordAmax)r.Relays); #else if (r.Relays is CombinedAdamRelaysRecord adamRelays) { saliMaxRelaysDevice.Write(adamRelays.GetAdam6360DRecord() ?? throw new InvalidOperationException()); saliMaxTsRelaysDevice.Write(adamRelays.GetAdam6060Record() ?? throw new InvalidOperationException()); } #endif } acDcDevices.Write(r.AcDc); dcDcDevices.Write(r.DcDc); } Console.WriteLine("press ctrl-c to stop"); while (true) { await Observable .Interval(UpdateInterval) .Select(_ => RunIteration()) .SelectMany(r => UploadCsv(r, DateTime.Now.Round(UpdateInterval))) .SelectError() .ToTask(); } StatusRecord RunIteration() { Watchdog.NotifyAlive(); var record = ReadStatus(); /* if (record.Relays != null) { record.Relays.Do0StartPulse = true; record.Relays.PulseOut0HighTime = 20000; record.Relays.PulseOut0LowTime = 20000; record.Relays.DigitalOutput0Mode = 2; record.Relays.LedGreen = false; record.Relays.Do0StartPulse.WriteLine(" = start pulse 0"); record.Relays.PulseOut0HighTime.WriteLine(" = PulseOut0HighTime"); record.Relays.PulseOut0LowTime.WriteLine(" = PulseOut0LowTime"); record.Relays.DigitalOutput0Mode.WriteLine(" = DigitalOutput0Mode"); record.Relays.LedGreen.WriteLine(" = LedGreen"); record.Relays.LedRed.WriteLine(" = LedRed"); } else { " Relays are null".WriteLine(); }*/ SendSalimaxStateAlarm(GetSalimaxStateAlarm(record), record); // to improve record.ControlConstants(); record.ControlSystemState(); record.ControlPvPower(record.Config.CurtailP, record.Config.PvInstalledPower); var essControl = record.ControlEss().WriteLine(); record.EssControl = essControl; record.AcDc.SystemControl.ApplyAcDcDefaultSettings(); record.DcDc.SystemControl.ApplyDcDcDefaultSettings(); DistributePower(record, essControl); record.PerformLed(); WriteControl(record); $"{record.StateMachine.State}: {record.StateMachine.Message}".WriteLine(); $"{DateTime.Now.Round(UpdateInterval).ToUnixTime()} : {DateTime.Now.Round(UpdateInterval):dd/MM/yyyy HH:mm:ss}".WriteLine(); record.CreateTopologyTextBlock().WriteLine(); (record.Relays is null ? "No relay Data available" : record.Relays.FiWarning ? "Alert: Fi Warning Detected" : "No Fi Warning Detected").WriteLine(); (record.Relays is null ? "No relay Data available" : record.Relays.FiError ? "Alert: Fi Error Detected" : "No Fi Error Detected") .WriteLine(); record.Config.Save(); "===========================================".WriteLine(); return record; } // ReSharper disable once FunctionNeverReturns } private static void SendSalimaxStateAlarm(StatusMessage currentSalimaxState, StatusRecord record) { var s3Bucket = Config.Load().S3?.Bucket; var subscribedNow = false; //Every 15 iterations(30 seconds), the installation sends a heartbit message to the queue //_heartBitInterval++; //When the controller boots, it tries to subscribe to the queue if (_subscribeToQueueForTheFirstTime == false) { subscribedNow = true; _subscribeToQueueForTheFirstTime = true; _prevSalimaxState = currentSalimaxState.Status; _subscribedToQueue = RabbitMqManager.SubscribeToQueue(currentSalimaxState, s3Bucket, VpnServerIp); } //If already subscribed to the queue and the status has been changed, update the queue if (!subscribedNow && _subscribedToQueue && currentSalimaxState.Status != _prevSalimaxState) { _prevSalimaxState = currentSalimaxState.Status; if (s3Bucket != null) RabbitMqManager.InformMiddleware(currentSalimaxState); } //If there is an available message from the RabbitMQ Broker, apply the configuration file Configuration? config = SetConfigurationFile(); if (config != null) { record.ApplyConfigFile(config); } } // This preparing a message to send to salimax monitor private static StatusMessage GetSalimaxStateAlarm(StatusRecord record) { var alarmCondition = record.DetectAlarmStates(); // this need to be emailed to support or customer var s3Bucket = Config.Load().S3?.Bucket; var alarmList = new List(); var warningList = new List(); var bAlarmList = new List(); var bWarningList = new List(); if (record.Battery != null) { var i = 0; foreach (var battery in record.Battery.Devices) { var devicesBatteryNode = record.Config.Devices.BatteryNodes[i]; if (battery.LimpBitMap == 0) { // "All String are Active".WriteLine(); } else if (IsPowerOfTwo(battery.LimpBitMap)) { "1 String is disabled".WriteLine(); Console.WriteLine(" ****************** "); warningList.Add(new AlarmOrWarning { Date = DateTime.Now.ToString("yyyy-MM-dd"), Time = DateTime.Now.ToString("HH:mm:ss"), CreatedBy = "Battery node" + devicesBatteryNode, Description = "1 String is disabled" }); bWarningList.Add("/"+i+1 + "/1 String is disabled"); // battery id instead ( i +1 ) of node id: requested from the frontend } else { "2 or more string are disabled".WriteLine(); Console.WriteLine(" ****************** "); alarmList.Add(new AlarmOrWarning { Date = DateTime.Now.ToString("yyyy-MM-dd"), Time = DateTime.Now.ToString("HH:mm:ss"), CreatedBy = "Battery node" + devicesBatteryNode, Description = "2 or more string are disabled" }); bAlarmList.Add(i +";2 or more string are disabled"); } foreach (var warning in record.Battery.Warnings) { warningList.Add(new AlarmOrWarning { Date = DateTime.Now.ToString("yyyy-MM-dd"), Time = DateTime.Now.ToString("HH:mm:ss"), CreatedBy = "Battery node" + devicesBatteryNode, Description = warning }); bWarningList.Add(i +";" + warning); } foreach (var alarm in battery.Alarms) { alarmList.Add(new AlarmOrWarning { Date = DateTime.Now.ToString("yyyy-MM-dd"), Time = DateTime.Now.ToString("HH:mm:ss"), CreatedBy = "Battery node" + devicesBatteryNode, Description = alarm }); bWarningList.Add(i +";" + alarm); } i++; } } if (alarmCondition is not null) { alarmCondition.WriteLine(); alarmList.Add(new AlarmOrWarning { Date = DateTime.Now.ToString("yyyy-MM-dd"), Time = DateTime.Now.ToString("HH:mm:ss"), CreatedBy = "Salimax", Description = alarmCondition }); } foreach (var alarm in record.AcDc.Alarms) { alarmList.Add(new AlarmOrWarning { Date = DateTime.Now.ToString("yyyy-MM-dd"), Time = DateTime.Now.ToString("HH:mm:ss"), CreatedBy = "AcDc", Description = alarm.ToString() }); } foreach (var alarm in record.DcDc.Alarms) { alarmList.Add(new AlarmOrWarning { Date = DateTime.Now.ToString("yyyy-MM-dd"), Time = DateTime.Now.ToString("HH:mm:ss"), CreatedBy = "DcDc", Description = alarm.ToString() }); } foreach (var warning in record.AcDc.Warnings) { warningList.Add(new AlarmOrWarning { Date = DateTime.Now.ToString("yyyy-MM-dd"), Time = DateTime.Now.ToString("HH:mm:ss"), CreatedBy = "AcDc", Description = warning.ToString() }); } foreach (var warning in record.DcDc.Warnings) { warningList.Add(new AlarmOrWarning { Date = DateTime.Now.ToString("yyyy-MM-dd"), Time = DateTime.Now.ToString("HH:mm:ss"), CreatedBy = "DcDc", Description = warning.ToString() }); } _salimaxAlarmState = warningList.Any() ? SalimaxAlarmState.Orange : SalimaxAlarmState.Green; // this will be replaced by LedState _salimaxAlarmState = alarmList.Any() ? SalimaxAlarmState.Red : _salimaxAlarmState; // this will be replaced by LedState TryParse(s3Bucket?.Split("-")[0], out var installationId); var returnedStatus = new StatusMessage { InstallationId = installationId, Product = 0, Status = _salimaxAlarmState, Type = MessageType.AlarmOrWarning, Alarms = alarmList, Warnings = warningList }; return returnedStatus; } private static String? DetectAlarmStates(this StatusRecord r) => r.Relays switch { { K2ConnectIslandBusToGridBus: false, K2IslandBusIsConnectedToGridBus: true } => " Contradiction: R0 is opening the K2 but the K2 is still close ", { K1GridBusIsConnectedToGrid : false, K2IslandBusIsConnectedToGridBus: true } => " Contradiction: K1 is open but the K2 is still close ", { FiError: true, K2IslandBusIsConnectedToGridBus: true } => " Contradiction: Fi error occured but the K2 is still close ", _ => null }; private static void ControlConstants(this StatusRecord r) { var inverters = r.AcDc.Devices; var dcDevices = r.DcDc.Devices; var configFile = r.Config; var maxBatteryDischargingCurrentLive = 0.0; var devicesConfig = r.AcDc.Devices.All(d => d.Control.Ac.GridType == GridType.GridTied400V50Hz) ? configFile.GridTie : configFile.IslandMode; // TODO if any of the grid tie mode // This adapting the max discharging current to the current Active Strings if (r.Battery != null) { const Int32 stringsByBattery = 5; var numberOfBatteriesConfigured = r.Config.Devices.BatteryNodes.Length; var numberOfTotalStrings = stringsByBattery * numberOfBatteriesConfigured; var dischargingCurrentByString = devicesConfig.DcDc.MaxBatteryDischargingCurrent / numberOfTotalStrings; var boolList = new List(); foreach (var stringActive in r.Battery.Devices.Select(b => b.BatteryStrings).ToList()) { boolList.Add(stringActive.String1Active); boolList.Add(stringActive.String2Active); boolList.Add(stringActive.String3Active); boolList.Add(stringActive.String4Active); boolList.Add(stringActive.String5Active); } var numberOfBatteriesStringActive = boolList.Count(b => b); if (numberOfTotalStrings != 0) { maxBatteryDischargingCurrentLive = dischargingCurrentByString * numberOfBatteriesStringActive; } } // TODO The discharging current is well calculated but not communicated to live. But Written in S3 inverters.ForEach(d => d.Control.Dc.MaxVoltage = devicesConfig.AcDc.MaxDcLinkVoltage); inverters.ForEach(d => d.Control.Dc.MinVoltage = devicesConfig.AcDc.MinDcLinkVoltage); inverters.ForEach(d => d.Control.Dc.ReferenceVoltage = devicesConfig.AcDc.ReferenceDcLinkVoltage); inverters.ForEach(d => d.Control.Dc.PrechargeConfig = DcPrechargeConfig.PrechargeDcWithInternal); dcDevices.ForEach(d => d.Control.DroopControl.UpperVoltage = devicesConfig.DcDc.UpperDcLinkVoltage); dcDevices.ForEach(d => d.Control.DroopControl.LowerVoltage = devicesConfig.DcDc.LowerDcLinkVoltage); dcDevices.ForEach(d => d.Control.DroopControl.ReferenceVoltage = devicesConfig.DcDc.ReferenceDcLinkVoltage); dcDevices.ForEach(d => d.Control.CurrentControl.MaxBatteryChargingCurrent = devicesConfig.DcDc.MaxBatteryChargingCurrent); dcDevices.ForEach(d => d.Control.CurrentControl.MaxBatteryDischargingCurrent = maxBatteryDischargingCurrentLive); dcDevices.ForEach(d => d.Control.MaxDcPower = devicesConfig.DcDc.MaxDcPower); dcDevices.ForEach(d => d.Control.VoltageLimits.MaxBatteryVoltage = devicesConfig.DcDc.MaxChargeBatteryVoltage); dcDevices.ForEach(d => d.Control.VoltageLimits.MinBatteryVoltage = devicesConfig.DcDc.MinDischargeBatteryVoltage); dcDevices.ForEach(d => d.Control.ControlMode = DcControlMode.VoltageDroop); r.DcDc.ResetAlarms(); r.AcDc.ResetAlarms(); } // This will be used for provider throttling, this example is only for either 100% or 0 % private static void ControlPvPower(this StatusRecord r, UInt16 exportLimit = 0, UInt16 pvInstalledPower = 20) { // Maybe add a condition to do this only if we are in optimised Self consumption, this is not true if (r.GridMeter?.Ac.Power.Active == null) { Console.WriteLine(" No reading from Grid meter"); return; } if (pvInstalledPower == 0) { Console.WriteLine(" No curtailing, because Pv installed is equal to 0"); return; } const Int32 constantDeadBand = 5000; // magic number const Double voltageRange = 100; // 100 Voltage configured rang for PV slope, if the configured slope change this must change also var configFile = r.Config; var inverters = r.AcDc.Devices; var systemExportLimit = - exportLimit * 1000 ; // Conversion from Kw in W // the config file value is positive and limit should be negative from 0 to ... var stepSize = ClampStepSize((UInt16)Math.Floor(voltageRange/ pvInstalledPower)); // in Voltage per 1 Kw var deadBand = constantDeadBand/stepSize; // LINQ query to select distinct ActiveUpperVoltage var result = r.AcDc.Devices .Select(device => device?.Status?.DcVoltages?.Active?.ActiveUpperVoltage) .Select(voltage => voltage.Value) // Extract the value since we've confirmed it's non-null .Distinct() .ToList(); Double upperVoltage; if (result.Count == 1) { upperVoltage = result[0]; } else { Console.WriteLine(" Different ActiveUpperVoltage between inverters "); // this should be reported to salimax Alarm return; } /************* For debugging purposes ********************/ systemExportLimit.WriteLine(" Export Limit in W"); upperVoltage.WriteLine(" Upper Voltage"); r.GridMeter.Ac.Power.Active.WriteLine(" Active Export"); Console.WriteLine(" ****************** "); /*********************************************************/ if (r.GridMeter.Ac.Power.Active < systemExportLimit) { _curtailFlag = true; upperVoltage = IncreaseInverterUpperLimit(upperVoltage, stepSize); upperVoltage.WriteLine("Upper Voltage Increased: New Upper limit"); } else { if (_curtailFlag) { if (r.GridMeter.Ac.Power.Active > (systemExportLimit + deadBand)) { upperVoltage = DecreaseInverterUpperLimit(upperVoltage, stepSize); if (upperVoltage <= configFile.GridTie.AcDc.MaxDcLinkVoltage) { _curtailFlag = false; upperVoltage = configFile.GridTie.AcDc.MaxDcLinkVoltage; upperVoltage.WriteLine(" New Upper limit"); Console.WriteLine("Upper Voltage decreased: Smaller than the default value, value clamped"); } else { Console.WriteLine("Upper Voltage decreased: New Upper limit"); upperVoltage.WriteLine(" New Upper limit"); } } else { deadBand.WriteLine("W :We are in Dead band area"); upperVoltage.WriteLine(" same Upper limit from last cycle"); } } else { Console.WriteLine("Curtail Flag is false , no need to curtail"); upperVoltage.WriteLine(" same Upper limit from last cycle"); } } inverters.ForEach(d => d.Control.Dc.MaxVoltage = upperVoltage); Console.WriteLine(" ****************** "); } // why this is not in Controller? private static void DistributePower(StatusRecord record, EssControl essControl) { var nInverters = record.AcDc.Devices.Count; var powerPerInverterPhase = nInverters > 0 ? essControl.PowerSetpoint / nInverters / 3 : 0; record.AcDc.Devices.ForEach(d => { d.Control.Ac.PhaseControl = PhaseControl.Asymmetric; d.Control.Ac.Power.L1 = powerPerInverterPhase; d.Control.Ac.Power.L2 = powerPerInverterPhase; d.Control.Ac.Power.L3 = powerPerInverterPhase; }); } // To test, most probably the curtailing flag will not work private static void PerformLed(this StatusRecord record) { if (record.StateMachine.State == 23) { switch (record.EssControl.Mode) { case EssMode.CalibrationCharge: //record.Relays?.PerformSlowFlashingGreenLed(); break; case EssMode.OptimizeSelfConsumption when !_curtailFlag: //record.Relays?.PerformSolidGreenLed(); break; case EssMode.Off: break; case EssMode.OffGrid: break; case EssMode.HeatBatteries: break; case EssMode.ReachMinSoc: break; case EssMode.NoGridMeter: break; default: { if (_curtailFlag) { //record.Relays?.PerformFastFlashingGreenLed(); } break; } } } else if (record.Battery?.Soc != null && record.StateMachine.State != 23 && record.Battery.Soc > 50) { //record.Relays?.PerformSolidOrangeLed(); } else if (record.Battery?.Soc != null && record.StateMachine.State != 23 && record.Battery.Soc < 50 && record.Battery.Soc > 20) { //record.Relays?.PerformSlowFlashingOrangeLed(); } else if (record.Battery?.Soc != null && record.StateMachine.State != 23 && record.Battery.Soc < 20) { //record.Relays?.PerformFastFlashingOrangeLed(); } var criticalAlarm = record.DetectAlarmStates(); if (criticalAlarm is not null) { //record.Relays?.PerformFastFlashingRedLed(); } } private static Double IncreaseInverterUpperLimit(Double upperLimit, Double stepSize) { return upperLimit + stepSize; } private static Double DecreaseInverterUpperLimit(Double upperLimit, Double stepSize) { return upperLimit - stepSize; } private static UInt16 ClampStepSize(UInt16 stepSize) { return stepSize switch { > 5 => 5, <= 1 => 1, _ => stepSize }; } private static void ApplyAcDcDefaultSettings(this SystemControlRegisters? sc) { if (sc is null) return; sc.ReferenceFrame = ReferenceFrame.Consumer; sc.SystemConfig = AcDcAndDcDc; #if DEBUG sc.CommunicationTimeout = TimeSpan.FromMinutes(2); #else sc.CommunicationTimeout = TimeSpan.FromSeconds(20); #endif sc.PowerSetPointActivation = PowerSetPointActivation.Immediate; sc.UseSlaveIdForAddressing = true; sc.SlaveErrorHandling = SlaveErrorHandling.Relaxed; sc.SubSlaveErrorHandling = SubSlaveErrorHandling.Off; sc.ResetAlarmsAndWarnings = true; } private static void ApplyDcDcDefaultSettings(this SystemControlRegisters? sc) { if (sc is null) return; sc.SystemConfig = DcDcOnly; #if DEBUG sc.CommunicationTimeout = TimeSpan.FromMinutes(2); #else sc.CommunicationTimeout = TimeSpan.FromSeconds(20); #endif sc.PowerSetPointActivation = PowerSetPointActivation.Immediate; sc.UseSlaveIdForAddressing = true; sc.SlaveErrorHandling = SlaveErrorHandling.Relaxed; sc.SubSlaveErrorHandling = SubSlaveErrorHandling.Off; sc.ResetAlarmsAndWarnings = true; } private static void InsertIntoJson(Dictionary jsonDict, String[] keys, string value) { Dictionary currentDict = jsonDict; for (int i = 1; i < keys.Length; i++) // Start at 1 to skip empty root { string key = keys[i]; if (!currentDict.ContainsKey(key)) { currentDict[key] = new Dictionary(); } if (i == keys.Length - 1) // Last key, store the value { if (!value.Contains(",") && double.TryParse(value, out double doubleValue)) // Try to parse value as a number { currentDict[key] = Math.Round(doubleValue, 2); // Round to 2 decimal places } else { currentDict[key] = value; // Store as string if not a number } } else { currentDict = (Dictionary)currentDict[key]; } } } private static async Task UploadCsv(StatusRecord status, DateTime timeStamp) { //status.ToJson(); var csv = status.ToCsv(); Dictionary jsonData = new Dictionary(); foreach (var line in csv.Split('\n')) { if (string.IsNullOrWhiteSpace(line)) continue; var parts = line.Split(';'); var keyPath = parts[0]; var value = parts[1]; var unit = parts.Length > 2 ? parts[2].Trim() : ""; //Console.WriteLine(line); // Console.WriteLine($"Key: {keyPath}, Value: {value}, Unit: {unit}"); InsertIntoJson(jsonData, keyPath.Split('/'), value); } var jsonOutput = JsonConvert.SerializeObject(jsonData, Formatting.None); jsonOutput.LogInfo(); await RestApiSavingFile(csv); var s3Config = status.Config.S3; if (s3Config is null) return false; //Concatenating 15 files in one file return await ConcatinatingAndCompressingFiles(timeStamp, s3Config); } private static async Task ConcatinatingAndCompressingFiles(DateTime timeStamp, S3Config s3Config) { if (_counterOfFile >= NbrOfFileToConcatenate) { _counterOfFile = 0; var logFileConcatenator = new LogFileConcatenator(); var s3Path = timeStamp.ToUnixTime() + ".json"; s3Path.WriteLine(""); var csvToSend = logFileConcatenator.ConcatenateFiles(NbrOfFileToConcatenate); var request = s3Config.CreatePutRequest(s3Path); //Use this for no compression //var response = await request.PutAsync(new StringContent(csv)); var compressedBytes = CompresseBytes(csvToSend); // Encode the compressed byte array as a Base64 string var base64String = Convert.ToBase64String(compressedBytes); // Create StringContent from Base64 string var stringContent = new StringContent(base64String, Encoding.UTF8, "application/base64"); // Upload the compressed data (ZIP archive) to S3 var response = await request.PutAsync(stringContent); if (response.StatusCode != 200) { Console.WriteLine("ERROR: PUT"); var error = await response.GetStringAsync(); Console.WriteLine(error); Heartbit(); return false; } Console.WriteLine("----------------------------------------Sending Heartbit----------------------------------------"); Heartbit(); } _counterOfFile++; return true; } private static void Heartbit() { var s3Bucket = Config.Load().S3?.Bucket; var tryParse = TryParse(s3Bucket?.Split("-")[0], out var installationId); if (tryParse) { var returnedStatus = new StatusMessage { InstallationId = installationId, Product = 0, // Salimax is always 0 Status = _salimaxAlarmState, Type = MessageType.Heartbit, }; if (s3Bucket != null) RabbitMqManager.InformMiddleware(returnedStatus); } } private static Byte[] CompresseBytes(String csvToSend) { //Compress CSV data to a byte array Byte[] compressedBytes; using (var memoryStream = new MemoryStream()) { //Create a zip directory and put the compressed file inside using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { var entry = archive.CreateEntry("data.json", CompressionLevel.SmallestSize); // Add CSV data to the ZIP archive using (var entryStream = entry.Open()) using (var writer = new StreamWriter(entryStream)) { writer.Write(csvToSend); } } compressedBytes = memoryStream.ToArray(); } return compressedBytes; } private static async Task RestApiSavingFile(String csv) { // This is for the Rest API // Check if the directory exists, and create it if it doesn't const String directoryPath = "/var/www/html"; if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } string filePath = Path.Combine(directoryPath, "status.csv"); await File.WriteAllTextAsync(filePath, csv.SplitLines().Where(l => !l.Contains("Secret")).JoinLines()); } private static Boolean IsPowerOfTwo(Int32 n) { return n > 0 && (n & (n - 1)) == 0; } private static void ApplyConfigFile(this StatusRecord status, Configuration? config) { if (config == null) return; status.Config.MinSoc = config.MinimumSoC; status.Config.GridSetPoint = config.GridSetPoint * 1000; // converted from kW to W status.Config.ForceCalibrationChargeState = config.CalibrationChargeState; if (config.CalibrationChargeState == CalibrationChargeType.RepetitivelyEvery) { status.Config.DayAndTimeForRepetitiveCalibration = config.CalibrationChargeDate; } else if (config.CalibrationChargeState == CalibrationChargeType.AdditionallyOnce) { status.Config.DayAndTimeForAdditionalCalibration = config.CalibrationChargeDate; } } }