From 6648d318ca1fa86416960dbfe8ec81af0de204bd Mon Sep 17 00:00:00 2001 From: ig Date: Mon, 3 Jul 2023 15:47:34 +0200 Subject: [PATCH] Minor improvements for TextBlocks --- csharp/App/SaliMax/src/Flow.cs | 4 +- csharp/App/SaliMax/src/Program.cs | 87 +++++++++++------------ csharp/Lib/Utils/TextBlock.cs | 113 ++++++------------------------ 3 files changed, 63 insertions(+), 141 deletions(-) diff --git a/csharp/App/SaliMax/src/Flow.cs b/csharp/App/SaliMax/src/Flow.cs index 8b74b48c5..0d173bc02 100644 --- a/csharp/App/SaliMax/src/Flow.cs +++ b/csharp/App/SaliMax/src/Flow.cs @@ -19,7 +19,7 @@ public static class Flow var arrow = Enumerable.Repeat(arrowChar, width).Join(); // note : appending "fake label" below to make it vertically symmetric - return TextBlock.CenterHorizontal(label, arrow, ""); + return TextBlock.AlignCenterHorizontal(label, arrow, ""); } [SuppressMessage("ReSharper", "PossibleMultipleEnumeration")] @@ -35,6 +35,6 @@ public static class Flow .Concat(halfArrow) .ToArray(height / 2 * 2 + 1); - return TextBlock.CenterHorizontal(lines); + return TextBlock.AlignCenterHorizontal(lines); } } diff --git a/csharp/App/SaliMax/src/Program.cs b/csharp/App/SaliMax/src/Program.cs index 0de19fc93..9ce766dc4 100644 --- a/csharp/App/SaliMax/src/Program.cs +++ b/csharp/App/SaliMax/src/Program.cs @@ -34,13 +34,12 @@ internal static class Program private const UInt32 UpdateIntervalSeconds = 2; private static readonly Byte[] BatteryNodes = { 2, 3, 4, 5, 6 }; - private const String BatteryTty = "/dev/ttyUSB0"; #if DEBUG 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 IslandBusLoadChannel = 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); @@ -49,7 +48,7 @@ internal static class Program 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 IslandBusLoadChannel = 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"; private static readonly TcpChannel BatteriesChannel = new TcpChannel("localhost", 5007); #endif @@ -88,14 +87,14 @@ internal static class Program var sdNotifyReturn = sd_notify(0, "READY=1"); var battery48TlDevices = BatteryNodes - .Select(n => new Battery48TlDevice(BatteryTty, n)) + .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(AcOutLoadChannel); + var acIslandLoadMeter = new EmuMeterDevice(IslandBusLoadChannel); var amptDevice = new AmptDevices(AmptChannel); var saliMaxRelaysDevice = new RelaysDevice(RelaysChannel); @@ -240,24 +239,24 @@ internal static class Program .Select(s1 => s1.Status.Ac.Power) .ToArray()); - var dcLinkVoltage = TextBlock.CenterHorizontal("", - s.DcDc.Dc.Link.Voltage.ToDisplayString(), - ""); + var dcLinkVoltage = TextBlock.AlignCenterHorizontal("", + s.DcDc.Dc.Link.Voltage.ToDisplayString(), + ""); //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.ToDisplayString(); - var batteryVoltage = s.Battery.Dc.Voltage.ToDisplayString; - var batterySoc = s.Battery.Soc.ToDisplayString(); - var batteryCurrent = s.Battery.Dc.Current.ToDisplayString(); - var batteryTemp = s.Battery.Temperature.ToDisplayString(); + 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; - 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 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); var flowGridBusToIslandBus = Flow.Horizontal((ActivePower)islandToGridBusPower); var flowIslandBusToInverter = Flow.Horizontal(inverterPower); @@ -268,37 +267,38 @@ internal static class Program var gridBox = TextBlock.AlignLeft(gridPowerByPhase).TitleBox("Grid"); var inverterBox = TextBlock.AlignLeft(inverterPowerByAcDc).TitleBox("Inverter"); var dcDcBox = TextBlock.AlignLeft(dc48Voltage).TitleBox("DC/DC"); - var batteryAvgBox = TextBlock.AlignLeft(batteryVoltage, - batterySoc, - batteryCurrent, - batteryTemp) + var batteryAvgBox = TextBlock.AlignLeft(batteryVoltage.ToDisplayString(), + batterySoc.ToDisplayString(), + batteryCurrent.ToDisplayString(), + batteryTemp.ToDisplayString()) .TitleBox("Battery"); //////////////////// Batteries ///////////////////////// - var batteryBoxes = s.Battery + IReadOnlyList batteryBoxes = s.Battery .Devices .Select(CreateIndividualBattery) .ToArray(s.Battery.Devices.Count); - var individualBatteries = TextBlock.AlignLeft(batteryBoxes); + var individualBatteries = batteryBoxes.Any() + ? TextBlock.AlignLeft(batteryBoxes) + : TextBlock.Spacer(1); - - var totalBoxes = TextBlock.CenterVertical(gridBox, - gridBusFlow, - gridBusColumn, - flowGridBusToIslandBus, - islandBusColumn, - flowIslandBusToInverter, - inverterBox, - flowInverterToDcBus, - dcBusColumn, - flowDcBusToDcDc, - dcDcBox, - flowDcDcToBattery, - batteryAvgBox, - individualBatteries); + var totalBoxes = TextBlock.AlignCenterVertical(gridBox, + gridBusFlow, + gridBusColumn, + flowGridBusToIslandBus, + islandBusColumn, + flowIslandBusToInverter, + inverterBox, + flowInverterToDcBus, + dcBusColumn, + flowDcBusToDcDc, + dcDcBox, + flowDcDcToBattery, + batteryAvgBox, + individualBatteries); totalBoxes.WriteLine(); } @@ -316,12 +316,7 @@ internal static class Program var flow = Flow.Horizontal(battery.Dc.Power); - return TextBlock.CenterVertical(flow, box); - } - - private static TextBlock ColumnBox(String pvTitle, String busTitle, String loadTitle, TextBlock dataBox) - { - return ColumnBox(pvTitle, busTitle, loadTitle, dataBox, 0); + return TextBlock.AlignCenterVertical(flow, box); } private static TextBlock ColumnBox(String pvTitle, String busTitle, String loadTitle, TextBlock dataBox, ActivePower loadPower) @@ -331,13 +326,13 @@ internal static class Program private static TextBlock ColumnBox(String pvTitle, String busTitle, String loadTitle, TextBlock dataBox, ActivePower loadPower, ActivePower pvPower) { - var pvBox = TextBlock.AlignLeft("").TitleBox(pvTitle); + var pvBox = TextBlock.FromString(pvTitle).Box(); var pvToBus = Flow.Vertical(pvPower); var busBox = TextBlock.AlignLeft(dataBox).TitleBox(busTitle); var busToLoad = Flow.Vertical(loadPower); - var loadBox = TextBlock.AlignLeft("").TitleBox(loadTitle); + var loadBox = TextBlock.FromString(loadTitle).Box(); - return TextBlock.CenterHorizontal(pvBox, pvToBus, busBox, busToLoad, loadBox); + return TextBlock.AlignCenterHorizontal(pvBox, pvToBus, busBox, busToLoad, loadBox); } private static async Task ResultOrNull(this Task task) diff --git a/csharp/Lib/Utils/TextBlock.cs b/csharp/Lib/Utils/TextBlock.cs index f7720d224..2f03efae7 100644 --- a/csharp/Lib/Utils/TextBlock.cs +++ b/csharp/Lib/Utils/TextBlock.cs @@ -1,82 +1,5 @@ - namespace InnovEnergy.Lib.Utils; -// public class TextBlock -// { -// public static TextBlock Empty { get; } = new TextBlock(Array.Empty()); -// -// public IReadOnlyList Lines { get; } -// -// public Int32 Width => Lines.Count == 0 -// ? 0 -// : Lines.Max(l=>l.Length); -// -// public Int32 Height => Lines.Count; -// -// public TextBlock(IReadOnlyList lines) => Lines = lines; -// -// public TextBlock AlignLeft (Int32 width = -1) => AlignHorizontal((l, w) => l.PadRight(w), width); -// public TextBlock AlignRight (Int32 width = -1) => AlignHorizontal((l, w) => l.PadLeft(w), width); -// public TextBlock AlignHCenter(Int32 width = -1) => AlignHorizontal((l, w) => l.PadLeft((w + l.Length) / 2).PadRight(w), width); -// -// public TextBlock AlignTop (Int32 height) => Lines.Concat(EmptyLines(height)).Take(height).ToArray(height); -// public TextBlock AlignBottom (Int32 height) => EmptyLines(Lines.Count - height).Concat(Lines).Take(height).ToArray(height); -// //public TextBlock AlignVCenter(Int32 height) => EmptyLines(height/2).Concat(Lines).Take(height).ToArray(height); -// //public TextBlock AlignVCenter(Int32 height) => AlignHorizontal((l, w) => l.PadLeft((w + l.Length) / 2).PadRight(w), width); -// -// private static IEnumerable EmptyLines(Int32 height) -// { -// return height > 0 -// ? Enumerable.Repeat("", height) -// : Enumerable.Empty(); -// } -// -// -// public static TextBlock HSpace(Int32 width ) => new TextBlock(new []{"".PadRight(width)}); -// public static TextBlock VSpace(Int32 height) => new TextBlock(Enumerable.Repeat("", height).ToArray(height)); -// -// public static TextBlock Space(Int32 width, Int32 height) -// { -// var lines = Enumerable -// .Repeat("".PadRight(width), height) -// .ToArray(height); -// -// return new TextBlock(lines); -// } -// -// private TextBlock AlignHorizontal(Func alignLine, Int32 width = -1) -// { -// if (!Lines.Any()) -// return Empty; -// -// var strings = Lines -// .SelectMany(GetLines) -// .ToList(); -// -// width = width < 0 ? strings.Max(l => l.Length) : width; -// -// var aligned = strings -// .Select(l => l.Length > width ? l.Substring(0, width) : l) -// .Select(l => alignLine(l, width)) -// .ToArray(strings.Count); -// -// return new TextBlock(aligned); -// } -// -// -// private static IReadOnlyList GetLines(Object l) -// { -// return l is TextBlock tb -// ? tb.Lines -// : l.ToString()?.SplitLines() ?? new[] { "" }; -// } -// -// public override String ToString() => String.Join(Environment.NewLine, Lines); -// -// public static implicit operator TextBlock(String[] lines) => new TextBlock(lines); -// } - - public class TextBlock { private readonly IReadOnlyList _Lines; @@ -85,8 +8,7 @@ public class TextBlock public override String ToString() => _Lines.JoinLines(); - - public static TextBlock AlignLeft(params Object[] things) + public static TextBlock AlignLeft(IReadOnlyList things) { var lines = things .SelectMany(GetLines) @@ -94,12 +16,14 @@ public class TextBlock var width = lines.Max(l => l.Length); - var alignedLines = lines.Select(l => l.PadRight(width)).ToArray(lines.Count); + var alignedLines = lines + .Select(l => l.PadRight(width)) + .ToArray(lines.Count); return new TextBlock(alignedLines); } - public static TextBlock AlignRight(params Object[] things) + public static TextBlock AlignRight(IReadOnlyList things) { var lines = things .SelectMany(GetLines) @@ -107,12 +31,14 @@ public class TextBlock var width = lines.Max(l => l.Length); - var alignedLines = lines.Select(l => l.PadLeft(width)).ToArray(lines.Count); + var alignedLines = lines + .Select(l => l.PadLeft(width)) + .ToArray(lines.Count); return new TextBlock(alignedLines); } - public static TextBlock CenterHorizontal(IReadOnlyList things) + public static TextBlock AlignCenterHorizontal(IReadOnlyList things) { var lines = things .SelectMany(GetLines) @@ -127,12 +53,9 @@ public class TextBlock return new TextBlock(alignedLines); } - public static TextBlock CenterHorizontal(params Object[] things) - { - return CenterHorizontal((IReadOnlyList)things); - } + - public static TextBlock AlignTop(params Object[] things) + public static TextBlock AlignTop(IReadOnlyList things) { var columns = things .Select(GetLines) @@ -149,7 +72,7 @@ public class TextBlock } - public static TextBlock AlignBottom(params Object[] things) + public static TextBlock AlignBottom(IReadOnlyList things) { var columns = things .Select(GetLines) @@ -166,7 +89,7 @@ public class TextBlock } - public static TextBlock CenterVertical(params Object[] things) + public static TextBlock AlignCenterVertical(IReadOnlyList things) { var columns = things .Select(GetLines) @@ -182,7 +105,14 @@ public class TextBlock return new TextBlock(alignedLines); } + public static TextBlock AlignLeft (params Object[] things) => AlignLeft ((IReadOnlyList) things); + public static TextBlock AlignRight (params Object[] things) => AlignRight ((IReadOnlyList) things); + public static TextBlock AlignTop (params Object[] things) => AlignTop ((IReadOnlyList) things); + public static TextBlock AlignBottom (params Object[] things) => AlignBottom ((IReadOnlyList) things); + public static TextBlock AlignCenterVertical (params Object[] things) => AlignCenterVertical ((IReadOnlyList) things); + public static TextBlock AlignCenterHorizontal(params Object[] things) => AlignCenterHorizontal((IReadOnlyList) things); + public static TextBlock FromString(String thing) => AlignLeft(thing); public TextBlock Box() { @@ -229,9 +159,6 @@ public class TextBlock return new TextBlock(lines); } - - - public static TextBlock Spacer(Int32 n) { return new TextBlock(Enumerable.Repeat("".PadRight(n), n).ToArray(n));