add minor error handling in Texblock

This commit is contained in:
ig 2023-08-18 15:57:00 +02:00
parent ed39b8e847
commit d731c12f64
12 changed files with 62 additions and 31 deletions

View File

@ -18,5 +18,5 @@ echo -e "\n============================ Deploy ============================\n"
rsync -v \ rsync -v \
./bin/Release/$dotnet_version/linux-x64/publish/* \ ./bin/Release/$dotnet_version/linux-x64/publish/* \
$username@$salimax_ip:~/salimax $username@"$salimax_ip":~/salimax

View File

@ -96,7 +96,7 @@ internal static class Program
StatusRecord ReadStatus() StatusRecord ReadStatus()
{ {
var battery = batteryDevices.Read().WriteLine(); var battery = batteryDevices.Read();
var acDc = acDcDevices.Read(); var acDc = acDcDevices.Read();
var dcDc = dcDcDevices.Read(); var dcDc = dcDcDevices.Read();
var relays = saliMaxRelaysDevice.Read(); var relays = saliMaxRelaysDevice.Read();
@ -190,12 +190,9 @@ internal static class Program
var t = UnixTime.Now; var t = UnixTime.Now;
var record = ReadStatus(); var record = ReadStatus();
record.Relays?.ToCsv().LogInfo();
record.ControlConstants(); record.ControlConstants();
record.ControlSystemState(); record.ControlSystemState();
(t + "\n").LogInfo();
$"{record.StateMachine.State}: {record.StateMachine.Message}".LogInfo(); $"{record.StateMachine.State}: {record.StateMachine.Message}".LogInfo();
var essControl = record.ControlEss().LogInfo(); var essControl = record.ControlEss().LogInfo();
@ -211,6 +208,8 @@ internal static class Program
Topology.From(record).WriteLine(); Topology.From(record).WriteLine();
//record.ToCsv().WriteLine();
//await UploadCsv(record, t); //await UploadCsv(record, t);
record.Config.Save(); record.Config.Save();

View File

@ -2,7 +2,6 @@ using System.Diagnostics.CodeAnalysis;
using InnovEnergy.App.SaliMax.Ess; using InnovEnergy.App.SaliMax.Ess;
using InnovEnergy.Lib.Devices.Battery48TL; using InnovEnergy.Lib.Devices.Battery48TL;
using InnovEnergy.Lib.Units; using InnovEnergy.Lib.Units;
using InnovEnergy.Lib.Units.Composite;
using InnovEnergy.Lib.Units.Power; using InnovEnergy.Lib.Units.Power;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using Ac3Bus = InnovEnergy.Lib.Units.Composite.Ac3Bus; using Ac3Bus = InnovEnergy.Lib.Units.Composite.Ac3Bus;
@ -61,6 +60,13 @@ public static class Topology
? TextBlock.AlignLeft(batteryBoxes) ? TextBlock.AlignLeft(batteryBoxes)
: TextBlock.Empty; : TextBlock.Empty;
var batteries = TextBlock
.AlignCenterVertical
(
batteryAvgBox //,
//individualBatteries // TODO
);
var islandTopology = TextBlock var islandTopology = TextBlock
.AlignCenterVertical .AlignCenterVertical
( (
@ -68,8 +74,7 @@ public static class Topology
inverterBox , Flow.Horizontal(inverterPower), inverterBox , Flow.Horizontal(inverterPower),
dcBusColumn , Flow.Horizontal(dcdcPower), dcBusColumn , Flow.Horizontal(dcdcPower),
dcDcBox , Flow.Horizontal(dcBatteryPower), dcDcBox , Flow.Horizontal(dcBatteryPower),
batteryAvgBox, batteries
individualBatteries
); );
if (s.GridMeter is null) if (s.GridMeter is null)

View File

@ -3,6 +3,7 @@ using System.Runtime.InteropServices;
namespace InnovEnergy.App.SaliMax; namespace InnovEnergy.App.SaliMax;
// https://www.freedesktop.org/software/systemd/man/sd_notify.html // https://www.freedesktop.org/software/systemd/man/sd_notify.html
public static class Watchdog public static class Watchdog
{ {
// "it is generally recommended to ignore the return value of this call. " // "it is generally recommended to ignore the return value of this call. "

View File

@ -8,7 +8,7 @@ tunnel() {
rPort=$3 rPort=$3
lPort=$4 lPort=$4
echo -n "localhost:$lPort $name " echo -n "$name @ $ip mapped to localhost:$lPort "
ssh -nNTL "$lPort:$ip:$rPort" "$host" 2> /dev/null & ssh -nNTL "$lPort:$ip:$rPort" "$host" 2> /dev/null &
until nc -vz 127.0.0.1 $lPort 2> /dev/null until nc -vz 127.0.0.1 $lPort 2> /dev/null
@ -22,12 +22,11 @@ tunnel() {
echo "" echo ""
tunnel "Trumpf Inverter (http) " 10.0.2.1 80 7001 tunnel "Trumpf Inverter (http) " 10.0.2.1 80 8001
tunnel "Trumpf DCDC (http) " 10.0.3.1 80 7002 tunnel "Trumpf DCDC (http) " 10.0.3.1 80 8002
tunnel "Ext Emu Meter (http) " 10.0.4.1 80 7003 tunnel "Ext Emu Meter (http) " 10.0.4.1 80 8003
tunnel "Int Emu Meter (http) " 10.0.4.2 80 7004 tunnel "Int Emu Meter (http) " 10.0.4.2 80 8004
tunnel "AMPT (http) " 10.0.5.1 8080 7005 tunnel "AMPT (http) " 10.0.5.1 8080 8005
tunnel "Trumpf Inverter (modbus)" 10.0.2.1 502 5001 tunnel "Trumpf Inverter (modbus)" 10.0.2.1 502 5001
tunnel "Trumpf DCDC (modbus) " 10.0.3.1 502 5002 tunnel "Trumpf DCDC (modbus) " 10.0.3.1 502 5002
@ -35,11 +34,11 @@ tunnel "Ext Emu Meter (modbus) " 10.0.4.1 502 5003
tunnel "Int Emu Meter " 10.0.4.2 502 5004 tunnel "Int Emu Meter " 10.0.4.2 502 5004
tunnel "AMPT (modbus) " 10.0.5.1 502 5005 tunnel "AMPT (modbus) " 10.0.5.1 502 5005
tunnel "Adam " 10.0.1.1 502 5006 tunnel "Adam " 10.0.1.1 502 5006
tunnel "Batteries " 127.0.0.1 6855 5007 tunnel "Batteries " 127.0.0.1 6855 5007
echo echo
echo "press any key to close the tunnels ..." echo "press any key to close the tunnels ..."
read -r -n 1 -s read -r -n 1 -s
kill $(jobs -p) kill $(jobs -p)
echo "done" echo "done"

View File

@ -3,7 +3,7 @@ using InnovEnergy.Lib.Utils;
namespace InnovEnergy.Lib.Channels.Framed; namespace InnovEnergy.Lib.Channels.Framed;
public class Channel<Tx, Rx> : Connection public class Channel<Tx, Rx> : Connection
{ {
private readonly AsyncAction<Tx> _Transmit; private readonly AsyncAction<Tx> _Transmit;
private readonly Async<Rx> _Receive; private readonly Async<Rx> _Receive;

View File

@ -7,7 +7,7 @@ using static System.IO.Ports.Parity;
namespace InnovEnergy.Lib.Devices.Battery48TL; namespace InnovEnergy.Lib.Devices.Battery48TL;
public class Battery48TlDevice: ModbusDevice<Battery48TlRecord> public class Battery48TlDevice : ModbusDevice<Battery48TlRecord>
{ {
public const Parity Parity = Odd; public const Parity Parity = Odd;
public const Int32 StopBits = 1; public const Int32 StopBits = 1;

View File

@ -20,7 +20,7 @@ public class Battery48TlDevices
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine( "Failed to read Battery data \n"+ e.Message ); Console.WriteLine("Failed to read Battery data \n" + e.Message);
// TODO: log // TODO: log
return Battery48TlRecords.Null; return Battery48TlRecords.Null;

View File

@ -9,6 +9,11 @@ public abstract class Unit
public override String ToString() => $"{Value} {Symbol}"; public override String ToString() => $"{Value} {Symbol}";
private static readonly IReadOnlyList<String> Prefix = new[] { "y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Y" };
private static Int32 MaxPrefix { get; } = Prefix.Count - 1;
private static Int32 DefaultIndex { get; } = Prefix.TakeWhile(e => e != "").Count();
public String ToDisplayString() public String ToDisplayString()
{ {
if (Value == 0) if (Value == 0)
@ -17,7 +22,7 @@ public abstract class Unit
var a = Math.Abs(Value); var a = Math.Abs(Value);
var s = Math.Sign(Value); var s = Math.Sign(Value);
var i = 8; var i = DefaultIndex;
while (a >= 10000 && i < MaxPrefix) while (a >= 10000 && i < MaxPrefix)
{ {
@ -37,8 +42,6 @@ public abstract class Unit
return $"{r * s} {Prefix[i]}{Symbol}"; return $"{r * s} {Prefix[i]}{Symbol}";
} }
private static readonly IReadOnlyList<String> Prefix = new[] { "y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Y" };
private static Int32 MaxPrefix { get; } = Prefix.Count - 1;
} }

View File

@ -14,6 +14,7 @@ public class TextBlock
{ {
var lines = things var lines = things
.SelectMany(GetLines) .SelectMany(GetLines)
.Where(l => !String.IsNullOrEmpty(l))
.ToList(); .ToList();
if (!lines.Any()) if (!lines.Any())
@ -32,6 +33,7 @@ public class TextBlock
{ {
var lines = things var lines = things
.SelectMany(GetLines) .SelectMany(GetLines)
.Where(l => !String.IsNullOrEmpty(l))
.ToList(); .ToList();
if (!lines.Any()) if (!lines.Any())
@ -50,8 +52,12 @@ public class TextBlock
{ {
var lines = things var lines = things
.SelectMany(GetLines) .SelectMany(GetLines)
.Where(l => !String.IsNullOrEmpty(l))
.ToList(); .ToList();
if (!lines.Any())
return Empty;
var width = lines.Max(l => l.Length); var width = lines.Max(l => l.Length);
var alignedLines = lines var alignedLines = lines
@ -65,8 +71,12 @@ public class TextBlock
{ {
var columns = things var columns = things
.Select(GetLines) .Select(GetLines)
.Where(c => c.Count > 0)
.ToList(); .ToList();
if (!columns.Any())
return Empty;
var height = columns.Max(l => l.Count); var height = columns.Max(l => l.Count);
var alignedLines = Enumerable var alignedLines = Enumerable
@ -82,8 +92,12 @@ public class TextBlock
{ {
var columns = things var columns = things
.Select(GetLines) .Select(GetLines)
.Where(c => c.Count > 0)
.ToList(); .ToList();
if (!columns.Any())
return Empty;
var height = columns.Max(l => l.Count); var height = columns.Max(l => l.Count);
var alignedLines = Enumerable var alignedLines = Enumerable
@ -99,8 +113,12 @@ public class TextBlock
{ {
var columns = things var columns = things
.Select(GetLines) .Select(GetLines)
.Where(c => c.Count > 0)
.ToList(); .ToList();
if (!columns.Any())
return Empty;
var height = columns.Max(l => l.Count); var height = columns.Max(l => l.Count);
var alignedLines = Enumerable var alignedLines = Enumerable
@ -122,7 +140,7 @@ public class TextBlock
public TextBlock Box() public TextBlock Box()
{ {
var width = _Lines.Max(l => l.Length); var width = _Lines.Any() ? _Lines.Max(l => l.Length) : 0;
var hLine = "".PadRight(width + 2, '─'); var hLine = "".PadRight(width + 2, '─');
var top = "┌" + hLine + "┐"; var top = "┌" + hLine + "┐";
@ -140,7 +158,7 @@ public class TextBlock
public TextBlock TitleBox(String title) public TextBlock TitleBox(String title)
{ {
var linesWidth = _Lines.Max(l => l.Length); var linesWidth = _Lines.Any() ? _Lines.Max(l => l.Length) : 0;
var titleWidth = title.Length; var titleWidth = title.Length;
var width = Math.Max(linesWidth, titleWidth); var width = Math.Max(linesWidth, titleWidth);

View File

@ -60,10 +60,16 @@ public static class Utils
return t; return t;
} }
// Below does not work ;(
// [DebuggerStepThrough][MethodImpl(AggressiveInlining | AggressiveOptimization)]
// public static R Apply<T, S, R>(this T t, Func<S, R> f) where T : S
// {
// return f(t);
// }
[DebuggerStepThrough][MethodImpl(AggressiveInlining | AggressiveOptimization)] [DebuggerStepThrough][MethodImpl(AggressiveInlining | AggressiveOptimization)]
public static R Apply<T, R>(this T t, Func<T, R> f) => f(t); public static R Apply<T, R>(this T t, Func<T, R> f) => f(t);
[DebuggerStepThrough][MethodImpl(AggressiveInlining | AggressiveOptimization)] [DebuggerStepThrough][MethodImpl(AggressiveInlining | AggressiveOptimization)]
public static R Apply<T1, T2, R>(this (T1 p1, T2 p2) t, Func<T1, T2, R> f) => f(t.p1, t.p2); public static R Apply<T1, T2, R>(this (T1 p1, T2 p2) t, Func<T1, T2, R> f) => f(t.p1, t.p2);