Merge remote-tracking branch 'origin/main'

This commit is contained in:
Kim 2023-07-13 13:23:09 +02:00
commit add66565c4
1 changed files with 87 additions and 34 deletions

View File

@ -73,14 +73,14 @@ internal static class Program
}
catch (Exception e)
{
Console.WriteLine(e);
e.LogError();
}
}
}
private static async Task Run()
{
Console.WriteLine("Starting SaliMax");
"Starting SaliMax".LogInfo();
// Send the initial "service started" message to systemd
var sdNotifyReturn = sd_notify(0, "READY=1");
@ -99,25 +99,25 @@ internal static class Program
StatusRecord ReadStatus()
{
Console.WriteLine(" Reading AcDC");
"Reading AcDC".LogInfo();
var acDc = acDcDevices.Read();
Console.WriteLine(" Reading dcDc");
"Reading dcDc".LogInfo();
var dcDc = dcDcDevices.Read();
Console.WriteLine(" Reading battery");
"Reading battery".LogInfo();
var battery = batteryDevices.Read();
Console.WriteLine(" Reading relays");
"Reading relays".LogInfo();
var relays = saliMaxRelaysDevice.Read();
Console.WriteLine(" loadOnAcIsland");
"Reading loadOnAcIsland".LogInfo();
var loadOnAcIsland = acIslandLoadMeter.Read();
Console.WriteLine(" Reading gridMeter");
"Reading gridMeter".LogInfo();
var gridMeter = gridMeterDevice.Read();
Console.WriteLine(" Reading pvOnDc");
"Reading pvOnDc".LogInfo();
var pvOnDc = amptDevice.Read();
var pvOnAcGrid = AcPowerDevice.Null;
@ -177,30 +177,35 @@ internal static class Program
dcDcDevices.Write(r.DcDc);
}
const Int32 delayTime = 10;
Console.WriteLine("press ctrl-C to stop");
while (true)
{
sd_notify(0, "WATCHDOG=1");
var t = UnixTime.FromTicks(UnixTime.Now.Ticks / 2 * 2);
//t.ToUtcDateTime().WriteLine();
var t = UnixTime.Now;
while (t.Ticks % UpdateIntervalSeconds != 0)
{
await Task.Delay(delayTime);
t = UnixTime.Now;
}
var record = ReadStatus();
PrintTopology(record);
if (record.Relays is not null)
record.Relays.ToCsv().WriteLine();
record.Relays.ToCsv().LogInfo();
record.ControlConstants();
record.ControlSystemState();
Console.WriteLine($"{record.StateMachine.State}: {record.StateMachine.Message}");
var essControl = record.ControlEss().WriteLine();
(t + "\n").LogInfo();
$"{record.StateMachine.State}: {record.StateMachine.Message}".LogInfo();
$"Batteries SOC: {record.Battery.Soc}".LogInfo();
var essControl = record.ControlEss().LogInfo();
record.EssControl = essControl;
@ -211,11 +216,13 @@ internal static class Program
WriteControl(record);
PrintTopology(record);
await UploadCsv(record, t);
record.Config.Save();
"===========================================".WriteLine();
"===========================================".LogInfo();
}
// ReSharper disable once FunctionNeverReturns
}
@ -234,13 +241,32 @@ internal static class Program
var islandToGridBusPower = inverterPower + islandLoadPower;
var gridLoadPower = s.LoadOnAcGrid is null ? 0: s.LoadOnAcGrid.Power.Active;
var gridPowerByPhase = TextBlock.AlignLeft(s.GridMeter.Ac.L1.Power.Active.ToDisplayString(),
TextBlock gridBusColumn;
TextBlock gridBox;
TextBlock totalBoxes;
if (s.GridMeter is not null)
{
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());
var gridVoltageByPhase = TextBlock.AlignLeft(s.GridMeter.Ac.L1.Voltage.ToDisplayString(),
s.GridMeter.Ac.L2.Voltage.ToDisplayString(),
s.GridMeter.Ac.L3.Voltage.ToDisplayString());
gridBusColumn = ColumnBox("Pv", "Grid Bus", "Load" , gridVoltageByPhase , gridLoadPower);
gridBox = TextBlock.AlignLeft(gridPowerByPhase).TitleBox("Grid");
}
else
{
gridBusColumn = TextBlock.Spacer(0);
gridBox = TextBlock.Spacer(0);
}
var gridVoltageByPhase = TextBlock.AlignLeft(s.GridMeter.Ac.L1.Voltage.ToDisplayString(),
s.GridMeter.Ac.L2.Voltage.ToDisplayString(),
s.GridMeter.Ac.L3.Voltage.ToDisplayString());
var inverterPowerByPhase = TextBlock.AlignLeft(s.AcDc.Ac.L1.Power.Active.ToDisplayString(),
s.AcDc.Ac.L2.Power.Active.ToDisplayString(),
@ -264,7 +290,6 @@ internal static class Program
var anyBatteryAlarm = s.Battery.Alarms.Any();
var anyBatteryWarning = s.Battery.Warnings.Any();
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);
var gridBusFlow = Flow.Horizontal(gridPower);
@ -274,8 +299,7 @@ internal static class Program
var flowDcBusToDcDc = Flow.Horizontal(dcdcPower);
var flowDcDcToBattery = Flow.Horizontal(dcBatteryPower);
var gridBox = TextBlock.AlignLeft(gridPowerByPhase).TitleBox("Grid");
var inverterBox = TextBlock.AlignLeft(inverterPowerByAcDc).TitleBox("Inverter");
var inverterBox = TextBlock.AlignLeft(inverterPowerByAcDc).TitleBox("AC/DC");
var dcDcBox = TextBlock.AlignLeft(dc48Voltage).TitleBox("DC/DC");
var batteryAvgBox = TextBlock.AlignLeft(batteryVoltage,
batterySoc,
@ -297,11 +321,27 @@ internal static class Program
var individualBatteries = batteryBoxes.Any()
? TextBlock.AlignLeft(batteryBoxes)
: TextBlock.Spacer(1);
var totalBoxes = TextBlock.AlignCenterVertical(gridBox,
gridBusFlow,
gridBusColumn,
flowGridBusToIslandBus,
if (s.GridMeter is not null)
{
totalBoxes = TextBlock.AlignCenterVertical(gridBox,
gridBusFlow,
gridBusColumn,
flowGridBusToIslandBus,
islandBusColumn,
flowIslandBusToInverter,
inverterBox,
flowInverterToDcBus,
dcBusColumn,
flowDcBusToDcDc,
dcDcBox,
flowDcDcToBattery,
batteryAvgBox,
individualBatteries);
}
else
{
totalBoxes = TextBlock.AlignCenterVertical(
islandBusColumn,
flowIslandBusToInverter,
inverterBox,
@ -312,6 +352,8 @@ internal static class Program
flowDcDcToBattery,
batteryAvgBox,
individualBatteries);
}
totalBoxes.WriteLine();
}
@ -363,10 +405,18 @@ internal static class Program
private static void ControlConstants(this StatusRecord r)
{
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);
// 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();
}
@ -408,11 +458,13 @@ internal static class Program
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;
@ -427,7 +479,8 @@ internal static class Program
sc.UseSlaveIdForAddressing = true;
sc.SlaveErrorHandling = SlaveErrorHandling.Relaxed;
sc.SubSlaveErrorHandling = SubSlaveErrorHandling.Off;
sc.ResetAlarmsAndWarnings = true;
sc.ResetAlarmsAndWarnings = true;
}
private static async Task UploadCsv(StatusRecord status, UnixTime timeStamp)