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 Flurl.Http;
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.DataTypes;
using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc;
using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc.Control;
using InnovEnergy.Lib.Protocols.Modbus.Channels;
using InnovEnergy.Lib.Time.Unix;
using InnovEnergy.Lib.Units;
@ -31,7 +34,9 @@ internal static class Program
[DllImport("libsystemd.so.0")]
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;
@ -176,28 +181,39 @@ internal static class Program
acDcDevices.Write(r.AcDc);
dcDcDevices.Write(r.DcDc);
}
const Int32 delayTime = 10;
Console.WriteLine("press ctrl-C to stop");
Console.WriteLine("press ctrl-c to stop");
while (true)
{
sd_notify(0, "WATCHDOG=1");
var t = UnixTime.Now;
while (t.Ticks % UpdateIntervalSeconds != 0)
{
await Task.Delay(delayTime);
t = UnixTime.Now;
}
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");
var t = UnixTime.Now;
var record = ReadStatus();
if (record.Relays is not null)
record.Relays.ToCsv().LogInfo();
record.Relays.ToCsv().LogInfo();
record.ControlConstants();
record.ControlSystemState();
@ -205,25 +221,29 @@ internal static class Program
(t + "\n").LogInfo();
$"{record.StateMachine.State}: {record.StateMachine.Message}".LogInfo();
$"Batteries SOC: {record.Battery.Soc}".LogInfo();
var essControl = record.ControlEss().LogInfo();
record.EssControl = essControl;
record.AcDc.SystemControl.ApplyAcDcDefaultSettings();
record.DcDc.SystemControl.ApplyDcDcDefaultSettings();
DistributePower(record, essControl);
WriteControl(record);
PrintTopology(record);
await UploadCsv(record, t);
//await UploadCsv(record, t);
record.Config.Save();
"===========================================".LogInfo();
return record;
}
// ReSharper disable once FunctionNeverReturns
}
@ -407,14 +427,20 @@ internal static class Program
var inverters = r.AcDc.Devices;
var dcDevices = r.DcDc.Devices;
inverters.ForEach(d => d.Control.Dc.MaxVoltage = r.Config.MaxDcBusVoltage);
inverters.ForEach(d => d.Control.Dc.MinVoltage = r.Config.MinDcBusVoltage);
inverters.ForEach(d => d.Control.Dc.ReferenceVoltage = r.Config.ReferenceDcBusVoltage);
inverters.ForEach(d => d.Control.Dc.MaxVoltage = r.Config.MaxDcLinkVoltageFromAcDc);
inverters.ForEach(d => d.Control.Dc.MinVoltage = r.Config.MinDcLinkVoltageFromAcDc);
inverters.ForEach(d => d.Control.Dc.ReferenceVoltage = r.Config.ReferenceDcLinkVoltageFromAcDc);
inverters.ForEach(d => d.Control.Dc.PrechargeConfig = DcPrechargeConfig.PrechargeDcWithInternal);
dcDevices.ForEach(d => d.Control.DroopControl.UpperVoltage = r.Config.UpperDcLinkVoltageFromDc);
dcDevices.ForEach(d => d.Control.DroopControl.LowerVoltage = r.Config.LowerDcLinkVoltageFromDc);
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);
// dcDevices.ForEach(d => d.Control. Dc.MaxVoltage = r.Config.MaxDcBusVoltage);
// dcDevices.ForEach(d => d.Control. Dc.MinVoltage = r.Config.MinDcBusVoltage);
// dcDevices.ForEach(d => d.Control. Dc.ReferenceVoltage = r.Config.ReferenceDcBusVoltage);
r.DcDc.ResetAlarms();
r.AcDc.ResetAlarms();
}
@ -483,11 +509,11 @@ internal static class Program
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;
if (s3Config is null)
return;
return false;
var csv = status.ToCsv();
var s3Path = timeStamp + ".csv";
@ -500,6 +526,8 @@ internal static class Program
var error = response.GetStringAsync();
Console.WriteLine(error);
}
return true;
}
}