2023-06-13 10:53:17 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2023-02-16 12:57:06 +00:00
|
|
|
using Flurl.Http;
|
2023-06-13 10:53:17 +00:00
|
|
|
using InnovEnergy.App.SaliMax.Ess;
|
2023-02-25 14:53:58 +00:00
|
|
|
using InnovEnergy.App.SaliMax.SaliMaxRelays;
|
2023-06-13 10:53:17 +00:00
|
|
|
using InnovEnergy.App.SaliMax.System;
|
2023-02-25 14:53:58 +00:00
|
|
|
using InnovEnergy.App.SaliMax.SystemConfig;
|
2023-06-20 08:21:06 +00:00
|
|
|
using InnovEnergy.App.SaliMax.VirtualDevices;
|
2023-02-25 14:53:58 +00:00
|
|
|
using InnovEnergy.Lib.Devices.AMPT;
|
|
|
|
using InnovEnergy.Lib.Devices.Battery48TL;
|
2023-02-16 12:57:06 +00:00
|
|
|
using InnovEnergy.Lib.Devices.EmuMeter;
|
2023-06-13 10:53:17 +00:00
|
|
|
using InnovEnergy.Lib.Devices.Trumpf.SystemControl;
|
|
|
|
using InnovEnergy.Lib.Devices.Trumpf.SystemControl.DataTypes;
|
2023-02-16 12:57:06 +00:00
|
|
|
using InnovEnergy.Lib.Devices.Trumpf.TruConvertAc;
|
2023-06-13 10:53:17 +00:00
|
|
|
using InnovEnergy.Lib.Devices.Trumpf.TruConvertAc.DataTypes;
|
2023-02-16 12:57:06 +00:00
|
|
|
using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc;
|
2023-06-13 10:53:17 +00:00
|
|
|
using InnovEnergy.Lib.Protocols.Modbus.Channels;
|
2023-02-25 14:53:58 +00:00
|
|
|
using InnovEnergy.Lib.Time.Unix;
|
2023-06-13 10:53:17 +00:00
|
|
|
using InnovEnergy.Lib.Units;
|
2023-06-22 08:00:01 +00:00
|
|
|
using InnovEnergy.Lib.Units.Power;
|
2023-04-04 14:37:37 +00:00
|
|
|
using InnovEnergy.Lib.Utils;
|
2023-06-13 10:53:17 +00:00
|
|
|
using static InnovEnergy.Lib.Devices.Trumpf.SystemControl.DataTypes.SystemConfig;
|
2023-06-20 08:21:06 +00:00
|
|
|
using AcPower = InnovEnergy.Lib.Units.Composite.AcPower;
|
2023-06-13 10:53:17 +00:00
|
|
|
using Exception = System.Exception;
|
2023-02-23 12:45:09 +00:00
|
|
|
|
2023-02-16 12:57:06 +00:00
|
|
|
#pragma warning disable IL2026
|
|
|
|
|
2023-02-25 14:53:58 +00:00
|
|
|
namespace InnovEnergy.App.SaliMax;
|
2023-02-16 12:57:06 +00:00
|
|
|
|
|
|
|
internal static class Program
|
|
|
|
{
|
2023-06-13 10:53:17 +00:00
|
|
|
[DllImport("libsystemd.so.0")]
|
|
|
|
private static extern Int32 sd_notify(Int32 unsetEnvironment, String state);
|
|
|
|
|
2023-02-16 12:57:06 +00:00
|
|
|
private const UInt32 UpdateIntervalSeconds = 2;
|
2023-06-13 10:53:17 +00:00
|
|
|
|
2023-06-20 08:21:06 +00:00
|
|
|
private static readonly Byte[] BatteryNodes = { 2, 3, 4, 5, 6 };
|
2023-06-13 10:53:17 +00:00
|
|
|
private const String BatteryTty = "/dev/ttyUSB0";
|
|
|
|
|
2023-06-23 08:42:02 +00:00
|
|
|
private static readonly TcpChannel RelaysChannel = new TcpChannel("10.0.1.1", 502); // "192.168.1.242";
|
|
|
|
private static readonly TcpChannel TruConvertAcChannel = new TcpChannel("10.0.2.1", 502); // "192.168.1.2";
|
|
|
|
private static readonly TcpChannel TruConvertDcChannel = new TcpChannel("10.0.3.1", 502); // "192.168.1.3";
|
|
|
|
private static readonly TcpChannel GridMeterChannel = new TcpChannel("10.0.4.1", 502); // "192.168.1.241";
|
|
|
|
private static readonly TcpChannel AcOutLoadChannel = new TcpChannel("10.0.4.2", 502); // "192.168.1.241";
|
|
|
|
private static readonly TcpChannel AmptChannel = new TcpChannel("10.0.5.1", 502); // "192.168.1.249";
|
2023-06-13 10:53:17 +00:00
|
|
|
|
2023-06-23 08:42:02 +00:00
|
|
|
//private static readonly TcpChannel TruConvertAcChannel = new TcpChannel("localhost", 5001);
|
|
|
|
//private static readonly TcpChannel TruConvertDcChannel = new TcpChannel("localhost", 5002);
|
|
|
|
//private static readonly TcpChannel GridMeterChannel = new TcpChannel("localhost", 5003);
|
|
|
|
//private static readonly TcpChannel AcOutLoadChannel = new TcpChannel("localhost", 5004);
|
|
|
|
//private static readonly TcpChannel AmptChannel = new TcpChannel("localhost", 5005);
|
|
|
|
//private static readonly TcpChannel RelaysChannel = new TcpChannel("localhost", 5006);
|
|
|
|
//private static readonly TcpChannel BatteriesChannel = new TcpChannel("localhost", 5007);
|
2023-02-23 12:45:09 +00:00
|
|
|
|
2023-06-13 10:53:17 +00:00
|
|
|
|
|
|
|
private static readonly S3Config S3Config = new S3Config
|
|
|
|
{
|
|
|
|
Bucket = "saliomameiringen",
|
|
|
|
Region = "sos-ch-dk-2",
|
|
|
|
Provider = "exo.io",
|
|
|
|
ContentType = "text/plain; charset=utf-8",
|
|
|
|
Key = "EXO2bf0cbd97fbfa75aa36ed46f",
|
|
|
|
Secret = "Bn1CDPqOG-XpDSbYjfIJxojcHTm391vZTc8z8l_fEPs"
|
|
|
|
};
|
|
|
|
|
2023-02-16 12:57:06 +00:00
|
|
|
public static async Task Main(String[] args)
|
|
|
|
{
|
2023-06-13 10:53:17 +00:00
|
|
|
while (true)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-06-13 10:53:17 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
await Run();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine(e);
|
|
|
|
}
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async Task Run()
|
|
|
|
{
|
|
|
|
Console.WriteLine("Starting SaliMax");
|
2023-04-04 14:37:37 +00:00
|
|
|
|
2023-06-13 10:53:17 +00:00
|
|
|
// Send the initial "service started" message to systemd
|
|
|
|
var sdNotifyReturn = sd_notify(0, "READY=1");
|
2023-04-04 14:37:37 +00:00
|
|
|
|
2023-06-13 10:53:17 +00:00
|
|
|
var battery48TlDevices = BatteryNodes
|
2023-06-23 08:42:02 +00:00
|
|
|
.Select(n => new Battery48TlDevice(BatteryTty, n))
|
2023-06-13 10:53:17 +00:00
|
|
|
.ToList();
|
|
|
|
|
2023-06-20 08:21:06 +00:00
|
|
|
var batteryDevices = new Battery48TlDevices(battery48TlDevices);
|
|
|
|
var acDcDevices = new TruConvertAcDcDevices(TruConvertAcChannel);
|
|
|
|
var dcDcDevices = new TruConvertDcDcDevices(TruConvertDcChannel);
|
|
|
|
var gridMeterDevice = new EmuMeterDevice(GridMeterChannel);
|
|
|
|
var acIslandLoadMeter = new EmuMeterDevice(AcOutLoadChannel);
|
|
|
|
var amptDevice = new AmptDevices(AmptChannel);
|
|
|
|
var saliMaxRelaysDevice = new RelaysDevice(RelaysChannel);
|
|
|
|
|
|
|
|
StatusRecord ReadStatus()
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-06-20 08:21:06 +00:00
|
|
|
var acDc = acDcDevices.Read();
|
|
|
|
var dcDc = dcDcDevices.Read();
|
|
|
|
var battery = batteryDevices.Read();
|
|
|
|
var relays = saliMaxRelaysDevice.Read();
|
|
|
|
var loadOnAcIsland = acIslandLoadMeter.Read();
|
|
|
|
var gridMeter = gridMeterDevice.Read();
|
|
|
|
var pvOnDc = amptDevice.Read();
|
|
|
|
|
2023-06-22 08:17:06 +00:00
|
|
|
var pvOnAcGrid = AcPowerDevice.Null;
|
|
|
|
var pvOnAcIsland = AcPowerDevice.Null;
|
|
|
|
var gridPower = gridMeter is null ? AcPower.Null : gridMeter.Ac.Power;
|
|
|
|
var islandLoadPower = loadOnAcIsland is null ? AcPower.Null : loadOnAcIsland.Ac.Power;
|
|
|
|
var inverterAcPower = acDc.Ac.Power;
|
|
|
|
|
|
|
|
var loadOnAcGrid = gridPower
|
|
|
|
+ pvOnAcGrid.Power
|
|
|
|
+ pvOnAcIsland.Power
|
|
|
|
- islandLoadPower
|
|
|
|
- inverterAcPower;
|
2023-06-20 08:21:06 +00:00
|
|
|
|
2023-06-22 08:17:06 +00:00
|
|
|
var gridBusToIslandBusPower = gridPower
|
|
|
|
+ pvOnAcGrid.Power
|
|
|
|
- loadOnAcGrid;
|
2023-06-20 08:21:06 +00:00
|
|
|
|
2023-06-22 08:17:06 +00:00
|
|
|
// var dcPower = acDc.Dc.Power.Value
|
|
|
|
// + pvOnDc.Dc?.Power.Value ?? 0
|
|
|
|
// - dcDc.Dc.Link.Power.Value;
|
2023-06-20 08:21:06 +00:00
|
|
|
|
2023-06-22 08:17:06 +00:00
|
|
|
var dcPower = 0;
|
|
|
|
|
|
|
|
var loadOnDc = new DcPowerDevice { Power = dcPower} ;
|
2023-06-20 08:21:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
return new StatusRecord
|
|
|
|
{
|
2023-06-22 08:17:06 +00:00
|
|
|
AcDc = acDc ?? AcDcDevicesRecord.Null,
|
|
|
|
DcDc = dcDc ?? DcDcDevicesRecord.Null,
|
2023-06-20 08:21:06 +00:00
|
|
|
Battery = battery ?? Battery48TlRecords.Null,
|
|
|
|
Relays = relays,
|
|
|
|
GridMeter = gridMeter,
|
|
|
|
|
|
|
|
PvOnAcGrid = pvOnAcGrid,
|
|
|
|
PvOnAcIsland = pvOnAcIsland,
|
|
|
|
PvOnDc = pvOnDc ?? AmptStatus.Null,
|
|
|
|
|
2023-06-22 08:17:06 +00:00
|
|
|
AcGridToAcIsland = new AcPowerDevice { Power = gridBusToIslandBusPower },
|
|
|
|
LoadOnAcGrid = new AcPowerDevice { Power = loadOnAcGrid },
|
|
|
|
LoadOnAcIsland = loadOnAcIsland,
|
|
|
|
LoadOnDc = loadOnDc,
|
2023-06-20 08:21:06 +00:00
|
|
|
|
2023-06-27 15:01:40 +00:00
|
|
|
StateMachine = StateMachine.Default,
|
|
|
|
EssControl = EssControl.Default,
|
|
|
|
Config = Config.Load() // load from disk every iteration, so config can be changed while running
|
2023-06-20 08:21:06 +00:00
|
|
|
};
|
|
|
|
}
|
2023-04-04 14:37:37 +00:00
|
|
|
|
2023-06-13 10:53:17 +00:00
|
|
|
void WriteControl(StatusRecord r)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-06-13 10:53:17 +00:00
|
|
|
if (r.Relays is not null)
|
|
|
|
saliMaxRelaysDevice.Write(r.Relays);
|
2023-04-04 14:37:37 +00:00
|
|
|
|
2023-06-13 10:53:17 +00:00
|
|
|
acDcDevices.Write(r.AcDc);
|
|
|
|
dcDcDevices.Write(r.DcDc);
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 12:45:09 +00:00
|
|
|
|
2023-02-16 12:57:06 +00:00
|
|
|
Console.WriteLine("press ctrl-C to stop");
|
2023-06-13 10:53:17 +00:00
|
|
|
|
2023-02-16 12:57:06 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2023-06-13 10:53:17 +00:00
|
|
|
sd_notify(0, "WATCHDOG=1");
|
|
|
|
|
|
|
|
var t = UnixTime.FromTicks(UnixTime.Now.Ticks / 2 * 2);
|
|
|
|
|
2023-06-20 08:21:06 +00:00
|
|
|
//t.ToUtcDateTime().WriteLine();
|
2023-06-13 10:53:17 +00:00
|
|
|
|
|
|
|
var record = ReadStatus();
|
|
|
|
|
2023-06-22 08:00:01 +00:00
|
|
|
PrintTopology(record);
|
2023-06-20 08:21:06 +00:00
|
|
|
|
2023-06-22 08:00:01 +00:00
|
|
|
if (record.Relays is not null)
|
|
|
|
record.Relays.ToCsv().WriteLine();
|
|
|
|
|
|
|
|
|
2023-06-22 08:17:06 +00:00
|
|
|
var emuMeterRegisters = record.GridMeter;
|
|
|
|
if (emuMeterRegisters is not null)
|
|
|
|
{
|
|
|
|
emuMeterRegisters.Ac.Power.Active.WriteLine("Grid Active");
|
|
|
|
//emuMeterRegisters.Ac.Power.Reactive.WriteLine("Grid Reactive");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-20 08:21:06 +00:00
|
|
|
record.ControlConstants();
|
2023-06-13 10:53:17 +00:00
|
|
|
|
|
|
|
record.ControlSystemState();
|
|
|
|
|
2023-06-20 08:21:06 +00:00
|
|
|
Console.WriteLine($"{record.StateMachine.State}: {record.StateMachine.Message}");
|
|
|
|
|
2023-06-22 08:00:01 +00:00
|
|
|
var essControl = record.ControlEss().WriteLine();
|
2023-06-13 10:53:17 +00:00
|
|
|
|
2023-06-20 08:21:06 +00:00
|
|
|
record.EssControl = essControl;
|
2023-06-13 10:53:17 +00:00
|
|
|
|
2023-06-22 08:17:06 +00:00
|
|
|
record.AcDc.SystemControl.ApplyAcDcDefaultSettings();
|
|
|
|
record.DcDc.SystemControl.ApplyDcDcDefaultSettings();
|
2023-06-13 10:53:17 +00:00
|
|
|
|
|
|
|
DistributePower(record, essControl);
|
2023-02-23 12:45:09 +00:00
|
|
|
|
2023-06-13 10:53:17 +00:00
|
|
|
WriteControl(record);
|
2023-06-20 08:21:06 +00:00
|
|
|
|
2023-06-13 10:53:17 +00:00
|
|
|
await UploadCsv(record, t);
|
2023-02-23 12:45:09 +00:00
|
|
|
|
2023-06-20 08:21:06 +00:00
|
|
|
record.Config.Save();
|
|
|
|
|
|
|
|
"===========================================".WriteLine();
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
|
|
|
// ReSharper disable once FunctionNeverReturns
|
|
|
|
}
|
|
|
|
|
2023-06-22 08:00:01 +00:00
|
|
|
private static void PrintTopology(StatusRecord s)
|
|
|
|
{
|
|
|
|
// Power Measurement Values
|
|
|
|
var gridPower = s.GridMeter!.Ac.Power.Active;
|
|
|
|
var inverterPower = s.AcDc.Ac.Power.Active;
|
|
|
|
var islandLoadPower = s.LoadOnAcIsland is null ? 0 : s.LoadOnAcIsland.Ac.Power.Active;
|
|
|
|
var dcBatteryPower = s.DcDc.Dc.Battery.Power;
|
|
|
|
var dcdcPower = s.DcDc.Dc.Link.Power;
|
|
|
|
var pvOnDcPower = s.PvOnDc.Dc!.Power.Value;
|
|
|
|
|
|
|
|
// Power Calculated Values
|
|
|
|
var islandToGridBusPower = inverterPower + islandLoadPower;
|
2023-06-23 08:41:20 +00:00
|
|
|
var gridLoadPower = s.LoadOnAcGrid is null ? 0: s.LoadOnAcGrid.Power.Active;
|
|
|
|
|
2023-06-27 15:01:40 +00:00
|
|
|
var gridPowerByPhase = TextBlock.AlignLeft(s.GridMeter.Ac.L1.Power.Active.ToDisplayString(),
|
|
|
|
s.GridMeter.Ac.L2.Power.Active.ToDisplayString(),
|
|
|
|
s.GridMeter.Ac.L3.Power.Active.ToDisplayString());
|
2023-06-23 08:41:20 +00:00
|
|
|
|
2023-06-27 15:01:40 +00:00
|
|
|
var gridVoltageByPhase = TextBlock.AlignLeft(s.GridMeter.Ac.L1.Voltage.ToDisplayString(),
|
|
|
|
s.GridMeter.Ac.L2.Voltage.ToDisplayString(),
|
|
|
|
s.GridMeter.Ac.L3.Voltage.ToDisplayString());
|
2023-06-23 08:41:20 +00:00
|
|
|
|
2023-06-27 15:01:40 +00:00
|
|
|
var inverterPowerByPhase = TextBlock.AlignLeft(s.AcDc.Ac.L1.Power.Active.ToDisplayString(),
|
|
|
|
s.AcDc.Ac.L2.Power.Active.ToDisplayString(),
|
|
|
|
s.AcDc.Ac.L3.Power.Active.ToDisplayString());
|
2023-06-23 08:41:20 +00:00
|
|
|
|
|
|
|
// ReSharper disable once CoVariantArrayConversion
|
|
|
|
var inverterPowerByAcDc = TextBlock.AlignLeft(s.AcDc.Devices
|
|
|
|
.Select(s1 => s1.Status.Ac.Power)
|
|
|
|
.ToArray());
|
|
|
|
|
|
|
|
var dcLinkVoltage = TextBlock.CenterHorizontal("",
|
2023-06-27 15:01:40 +00:00
|
|
|
s.DcDc.Dc.Link.Voltage.ToDisplayString(),
|
2023-06-23 08:41:20 +00:00
|
|
|
"");
|
|
|
|
|
2023-06-22 08:00:01 +00:00
|
|
|
//var inverterPowerByPhase = new ActivePower[(Int32)s.AcDc.Ac.L1.Power.Active, (Int32)s.AcDc.Ac.L2.Power.Active, (Int32)s.AcDc.Ac.L3.Power.Active];
|
|
|
|
|
|
|
|
// Voltage Measurement Values
|
|
|
|
//var inverterVoltage = new Voltage [(Int32)s.AcDc.Ac.L1.Voltage, (Int32)s.AcDc.Ac.L2.Voltage, (Int32)s.AcDc.Ac.L3.Voltage];
|
|
|
|
//var dcLinkVoltage = s.DcDc.Dc.Link.Voltage;
|
|
|
|
var dc48Voltage = s.DcDc.Dc.Battery.Voltage;
|
|
|
|
var batteryVoltage = s.Battery.Dc.Voltage;
|
|
|
|
var batterySoc = s.Battery.Soc;
|
|
|
|
var batteryCurrent = s.Battery.Dc.Current;
|
|
|
|
var batteryTemp = s.Battery.Temperature;
|
2023-06-23 08:41:20 +00:00
|
|
|
|
|
|
|
var gridBusColumn = ColumnBox("Pv", "Grid Bus", "Load" , gridVoltageByPhase , gridLoadPower);
|
|
|
|
var islandBusColumn = ColumnBox("Pv", "Island Bus", "Load" , inverterPowerByPhase, islandLoadPower);
|
|
|
|
var dcBusColumn = ColumnBox("Pv", "Dc Bus", "Load" , dcLinkVoltage, 0, pvOnDcPower);
|
2023-06-22 08:00:01 +00:00
|
|
|
var gridBusFlow = Flow.Horizontal(gridPower);
|
|
|
|
var flowGridBusToIslandBus = Flow.Horizontal((ActivePower)islandToGridBusPower);
|
|
|
|
var flowIslandBusToInverter = Flow.Horizontal(inverterPower);
|
|
|
|
var flowInverterToDcBus = Flow.Horizontal(inverterPower);
|
2023-06-23 08:41:20 +00:00
|
|
|
var flowDcBusToDcDc = Flow.Horizontal(dcdcPower);
|
|
|
|
var flowDcDcToBattery = Flow.Horizontal(dcBatteryPower);
|
2023-06-22 08:00:01 +00:00
|
|
|
|
2023-06-23 08:41:20 +00:00
|
|
|
var gridBox = TextBlock.AlignLeft(gridPowerByPhase).TitleBox("Grid");
|
|
|
|
var inverterBox = TextBlock.AlignLeft(inverterPowerByAcDc).TitleBox("Inverter");
|
2023-06-22 08:00:01 +00:00
|
|
|
var dcDcBox = TextBlock.AlignLeft(dc48Voltage).TitleBox("DC/DC");
|
2023-06-27 15:01:40 +00:00
|
|
|
var batteryBox = TextBlock.AlignLeft(batteryVoltage.ToDisplayString(), batterySoc.ToDisplayString(), batteryCurrent.ToDisplayString(), batteryTemp.ToDisplayString()).TitleBox("Battery");
|
2023-06-22 08:00:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var totalBoxes = TextBlock.CenterVertical(gridBox,
|
|
|
|
gridBusFlow,
|
|
|
|
gridBusColumn,
|
|
|
|
flowGridBusToIslandBus,
|
|
|
|
islandBusColumn,
|
|
|
|
flowIslandBusToInverter,
|
|
|
|
inverterBox,
|
|
|
|
flowInverterToDcBus,
|
|
|
|
dcBusColumn,
|
|
|
|
flowDcBusToDcDc,
|
|
|
|
dcDcBox,
|
|
|
|
flowDcDcToBattery,
|
|
|
|
batteryBox);
|
|
|
|
|
|
|
|
totalBoxes.WriteLine();
|
|
|
|
}
|
|
|
|
|
2023-06-23 08:41:20 +00:00
|
|
|
private static TextBlock ColumnBox(String pvTitle, String busTitle, String loadTitle, TextBlock dataBox)
|
2023-06-22 08:00:01 +00:00
|
|
|
{
|
|
|
|
return ColumnBox(pvTitle, busTitle, loadTitle, dataBox, 0);
|
|
|
|
}
|
|
|
|
|
2023-06-23 08:41:20 +00:00
|
|
|
private static TextBlock ColumnBox(String pvTitle, String busTitle, String loadTitle, TextBlock dataBox, ActivePower loadPower)
|
2023-06-22 08:00:01 +00:00
|
|
|
{
|
|
|
|
return ColumnBox(pvTitle, busTitle, loadTitle, dataBox, loadPower, 0);
|
|
|
|
}
|
|
|
|
|
2023-06-23 08:41:20 +00:00
|
|
|
private static TextBlock ColumnBox(String pvTitle, String busTitle, String loadTitle, TextBlock dataBox, ActivePower loadPower, ActivePower pvPower)
|
2023-06-22 08:00:01 +00:00
|
|
|
{
|
|
|
|
var pvBox = TextBlock.AlignLeft("").TitleBox(pvTitle);
|
|
|
|
var pvToBus = Flow.Vertical(pvPower);
|
|
|
|
var busBox = TextBlock.AlignLeft(dataBox).TitleBox(busTitle);
|
|
|
|
var busToLoad = Flow.Vertical(loadPower);
|
|
|
|
var loadBox = TextBlock.AlignLeft("").TitleBox(loadTitle);
|
|
|
|
|
|
|
|
return TextBlock.CenterHorizontal(pvBox, pvToBus, busBox, busToLoad, loadBox);
|
|
|
|
}
|
|
|
|
|
2023-06-20 08:21:06 +00:00
|
|
|
private static async Task<T?> ResultOrNull<T>(this Task<T> task)
|
|
|
|
{
|
|
|
|
if (task.Status == TaskStatus.RanToCompletion)
|
|
|
|
return await task;
|
|
|
|
|
|
|
|
return default;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void ControlConstants(this StatusRecord r)
|
|
|
|
{
|
|
|
|
var inverters = r.AcDc.Devices;
|
|
|
|
|
2023-06-22 08:00:01 +00:00
|
|
|
inverters.ForEach(d => d.Control.Dc.MaxVoltage = r.Config.MaxDcBusVoltage);
|
|
|
|
inverters.ForEach(d => d.Control.Dc.MinVoltage = r.Config.MinDcBusVoltage);
|
2023-06-20 08:21:06 +00:00
|
|
|
inverters.ForEach(d => d.Control.Dc.ReferenceVoltage = r.Config.ReferenceDcBusVoltage);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-22 08:00:01 +00:00
|
|
|
// why this is not in Controller?
|
2023-06-13 10:53:17 +00:00
|
|
|
private static void DistributePower(StatusRecord record, EssControl essControl)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-06-13 10:53:17 +00:00
|
|
|
var nInverters = record.AcDc.Devices.Count;
|
|
|
|
|
|
|
|
var powerPerInverterPhase = nInverters > 0
|
|
|
|
? AcPower.FromActiveReactive(essControl.PowerSetpoint / nInverters / 3, 0)
|
|
|
|
: AcPower.Null;
|
|
|
|
|
|
|
|
//var powerPerInverterPhase = AcPower.Null;
|
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
2023-02-16 12:57:06 +00:00
|
|
|
|
2023-06-22 08:17:06 +00:00
|
|
|
private static void ApplyAcDcDefaultSettings(this SystemControlRegisters? sc)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-06-13 10:53:17 +00:00
|
|
|
if (sc is null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sc.ReferenceFrame = ReferenceFrame.Consumer;
|
|
|
|
sc.SystemConfig = AcDcAndDcDc;
|
|
|
|
|
|
|
|
#if DEBUG
|
2023-06-20 08:21:06 +00:00
|
|
|
sc.CommunicationTimeout = TimeSpan.FromMinutes(2);
|
2023-06-13 10:53:17 +00:00
|
|
|
#else
|
2023-06-22 08:00:44 +00:00
|
|
|
sc.CommunicationTimeout = TimeSpan.FromSeconds(20);
|
2023-06-13 10:53:17 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
sc.PowerSetPointActivation = PowerSetPointActivation.Immediate;
|
|
|
|
sc.UseSlaveIdForAddressing = true;
|
|
|
|
sc.SlaveErrorHandling = SlaveErrorHandling.Relaxed;
|
|
|
|
sc.SubSlaveErrorHandling = SubSlaveErrorHandling.Off;
|
|
|
|
sc.ResetAlarmsAndWarnings = true;
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
2023-06-22 08:00:44 +00:00
|
|
|
|
2023-06-22 08:17:06 +00:00
|
|
|
private static void ApplyDcDcDefaultSettings(this SystemControlRegisters? sc)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-06-22 08:00:44 +00:00
|
|
|
if (sc is null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sc.SystemConfig = DcDcOnly;
|
2023-06-22 08:17:06 +00:00
|
|
|
#if DEBUG
|
2023-06-22 08:00:44 +00:00
|
|
|
sc.CommunicationTimeout = TimeSpan.FromMinutes(2);
|
2023-06-22 08:17:06 +00:00
|
|
|
#else
|
2023-06-22 08:00:44 +00:00
|
|
|
sc.CommunicationTimeout = TimeSpan.FromSeconds(20);
|
2023-06-22 08:17:06 +00:00
|
|
|
#endif
|
2023-06-22 08:00:44 +00:00
|
|
|
|
|
|
|
sc.PowerSetPointActivation = PowerSetPointActivation.Immediate;
|
|
|
|
sc.UseSlaveIdForAddressing = true;
|
|
|
|
sc.SlaveErrorHandling = SlaveErrorHandling.Relaxed;
|
|
|
|
sc.SubSlaveErrorHandling = SubSlaveErrorHandling.Off;
|
|
|
|
sc.ResetAlarmsAndWarnings = true;
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
2023-02-23 12:45:09 +00:00
|
|
|
|
2023-06-13 10:53:17 +00:00
|
|
|
private static async Task UploadCsv(StatusRecord status, UnixTime timeStamp)
|
2023-02-23 12:45:09 +00:00
|
|
|
{
|
2023-06-20 08:21:06 +00:00
|
|
|
timeStamp.WriteLine();
|
|
|
|
|
2023-06-22 08:17:06 +00:00
|
|
|
var csv = status.ToCsv();
|
2023-06-13 10:53:17 +00:00
|
|
|
var s3Path = timeStamp + ".csv";
|
|
|
|
var request = S3Config.CreatePutRequest(s3Path);
|
|
|
|
var response = await request.PutAsync(new StringContent(csv));
|
2023-02-23 12:45:09 +00:00
|
|
|
|
2023-06-20 08:21:06 +00:00
|
|
|
//csv.WriteLine();
|
|
|
|
//timeStamp.Ticks.WriteLine();
|
2023-06-13 10:53:17 +00:00
|
|
|
|
2023-02-23 12:45:09 +00:00
|
|
|
if (response.StatusCode != 200)
|
|
|
|
{
|
|
|
|
Console.WriteLine("ERROR: PUT");
|
|
|
|
var error = response.GetStringAsync();
|
|
|
|
Console.WriteLine(error);
|
|
|
|
}
|
|
|
|
}
|
2023-06-13 10:53:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|