Add Observable for the run task

Add few constant control
This commit is contained in:
atef 2023-08-02 11:08:40 +02:00
parent 6990bda8ff
commit f6699787dd
1 changed files with 58 additions and 30 deletions

View File

@ -1,3 +1,5 @@
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Flurl.Http; using Flurl.Http;
using InnovEnergy.App.SaliMax.Ess; using InnovEnergy.App.SaliMax.Ess;
@ -13,6 +15,7 @@ using InnovEnergy.Lib.Devices.Trumpf.SystemControl.DataTypes;
using InnovEnergy.Lib.Devices.Trumpf.TruConvertAc; using InnovEnergy.Lib.Devices.Trumpf.TruConvertAc;
using InnovEnergy.Lib.Devices.Trumpf.TruConvertAc.DataTypes; using InnovEnergy.Lib.Devices.Trumpf.TruConvertAc.DataTypes;
using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc; using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc;
using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc.Control;
using InnovEnergy.Lib.Protocols.Modbus.Channels; using InnovEnergy.Lib.Protocols.Modbus.Channels;
using InnovEnergy.Lib.Time.Unix; using InnovEnergy.Lib.Time.Unix;
using InnovEnergy.Lib.Units; using InnovEnergy.Lib.Units;
@ -31,7 +34,9 @@ internal static class Program
[DllImport("libsystemd.so.0")] [DllImport("libsystemd.so.0")]
private static extern Int32 sd_notify(Int32 unsetEnvironment, String state); private static extern Int32 sd_notify(Int32 unsetEnvironment, String state);
private const UInt32 UpdateIntervalSeconds = 2; const String batteryTty = "/dev/ttyUSB0";
private static readonly UnixTimeSpan UpdateInterval = UnixTimeSpan.FromSeconds(2);
private static readonly IReadOnlyList<Byte> BatteryNodes; private static readonly IReadOnlyList<Byte> BatteryNodes;
@ -177,22 +182,33 @@ internal static class Program
dcDcDevices.Write(r.DcDc); dcDcDevices.Write(r.DcDc);
} }
const Int32 delayTime = 10; Console.WriteLine("press ctrl-c to stop");
Console.WriteLine("press ctrl-C to stop");
while (true) while (true)
{
await Observable
.Interval(UpdateInterval.ToTimeSpan())
.Select(_ => RunIteration())
.SelectMany(r => UploadCsv(r, UnixTime.Now.RoundTo(UpdateInterval)))
.SelectError()
.ToTask();
}
// var iterations = from _ in Observable.Interval(UpdateInterval.ToTimeSpan())
// let t = UnixTime.Now.RoundTo(UpdateInterval)
// let record = RunIteration()
// from uploaded in UploadCsv(record, t)
// select uploaded;
//
// using var running = iterations.Subscribe();
StatusRecord RunIteration()
{ {
sd_notify(0, "WATCHDOG=1"); sd_notify(0, "WATCHDOG=1");
var t = UnixTime.Now; var t = UnixTime.Now;
while (t.Ticks % UpdateIntervalSeconds != 0)
{
await Task.Delay(delayTime);
t = UnixTime.Now;
}
var record = ReadStatus(); var record = ReadStatus();
if (record.Relays is not null) if (record.Relays is not null)
@ -205,6 +221,7 @@ internal static class Program
(t + "\n").LogInfo(); (t + "\n").LogInfo();
$"{record.StateMachine.State}: {record.StateMachine.Message}".LogInfo(); $"{record.StateMachine.State}: {record.StateMachine.Message}".LogInfo();
$"Batteries SOC: {record.Battery.Soc}".LogInfo(); $"Batteries SOC: {record.Battery.Soc}".LogInfo();
var essControl = record.ControlEss().LogInfo(); var essControl = record.ControlEss().LogInfo();
record.EssControl = essControl; record.EssControl = essControl;
@ -218,12 +235,15 @@ internal static class Program
PrintTopology(record); PrintTopology(record);
await UploadCsv(record, t); //await UploadCsv(record, t);
record.Config.Save(); record.Config.Save();
"===========================================".LogInfo(); "===========================================".LogInfo();
return record;
} }
// ReSharper disable once FunctionNeverReturns // ReSharper disable once FunctionNeverReturns
} }
@ -407,13 +427,19 @@ internal static class Program
var inverters = r.AcDc.Devices; var inverters = r.AcDc.Devices;
var dcDevices = r.DcDc.Devices; var dcDevices = r.DcDc.Devices;
inverters.ForEach(d => d.Control.Dc.MaxVoltage = r.Config.MaxDcBusVoltage); inverters.ForEach(d => d.Control.Dc.MaxVoltage = r.Config.MaxDcLinkVoltageFromAcDc);
inverters.ForEach(d => d.Control.Dc.MinVoltage = r.Config.MinDcBusVoltage); inverters.ForEach(d => d.Control.Dc.MinVoltage = r.Config.MinDcLinkVoltageFromAcDc);
inverters.ForEach(d => d.Control.Dc.ReferenceVoltage = r.Config.ReferenceDcBusVoltage); inverters.ForEach(d => d.Control.Dc.ReferenceVoltage = r.Config.ReferenceDcLinkVoltageFromAcDc);
inverters.ForEach(d => d.Control.Dc.PrechargeConfig = DcPrechargeConfig.PrechargeDcWithInternal);
// dcDevices.ForEach(d => d.Control. Dc.MaxVoltage = r.Config.MaxDcBusVoltage); dcDevices.ForEach(d => d.Control.DroopControl.UpperVoltage = r.Config.UpperDcLinkVoltageFromDc);
// dcDevices.ForEach(d => d.Control. Dc.MinVoltage = r.Config.MinDcBusVoltage); dcDevices.ForEach(d => d.Control.DroopControl.LowerVoltage = r.Config.LowerDcLinkVoltageFromDc);
// dcDevices.ForEach(d => d.Control. Dc.ReferenceVoltage = r.Config.ReferenceDcBusVoltage); dcDevices.ForEach(d => d.Control.DroopControl.ReferenceVoltage = r.Config.ReferenceDcLinkVoltageFromDc);
dcDevices.ForEach(d => d.Control.CurrentControl.MaxBatteryChargingCurrent = r.Config.MaxBatteryChargingCurrent);
dcDevices.ForEach(d => d.Control.CurrentControl.MaxBatteryDischargingCurrent = r.Config.MaxBatteryDischargingCurrent);
dcDevices.ForEach(d => d.Control.VoltageLimits.MaxBatteryVoltage = r.Config.MaxChargeBatteryVoltage);
dcDevices.ForEach(d => d.Control.VoltageLimits.MinBatteryVoltage = r.Config.MinDischargeBatteryVoltage);
dcDevices.ForEach(d => d.Control.ControlMode = DcControlMode.VoltageDroop);
r.DcDc.ResetAlarms(); r.DcDc.ResetAlarms();
r.AcDc.ResetAlarms(); r.AcDc.ResetAlarms();
@ -483,11 +509,11 @@ internal static class Program
sc.ResetAlarmsAndWarnings = true; sc.ResetAlarmsAndWarnings = true;
} }
private static async Task UploadCsv(StatusRecord status, UnixTime timeStamp) private static async Task<Boolean> UploadCsv(StatusRecord status, UnixTime timeStamp)
{ {
var s3Config = status.Config.S3; var s3Config = status.Config.S3;
if (s3Config is null) if (s3Config is null)
return; return false;
var csv = status.ToCsv(); var csv = status.ToCsv();
var s3Path = timeStamp + ".csv"; var s3Path = timeStamp + ".csv";
@ -500,6 +526,8 @@ internal static class Program
var error = response.GetStringAsync(); var error = response.GetStringAsync();
Console.WriteLine(error); Console.WriteLine(error);
} }
return true;
} }
} }