From 5c5d2b72e5cfcc1ce6fae9c404ccf295344d035f Mon Sep 17 00:00:00 2001 From: ig Date: Mon, 19 Jun 2023 16:15:59 +0200 Subject: [PATCH] remove node detection from BmsTunnel, did more harm than good. --- .../{BatteryConnection.cs => BatteryTty.cs} | 51 +++++--------- csharp/App/BmsTunnel/Program.cs | 67 ++----------------- 2 files changed, 23 insertions(+), 95 deletions(-) rename csharp/App/BmsTunnel/{BatteryConnection.cs => BatteryTty.cs} (58%) diff --git a/csharp/App/BmsTunnel/BatteryConnection.cs b/csharp/App/BmsTunnel/BatteryTty.cs similarity index 58% rename from csharp/App/BmsTunnel/BatteryConnection.cs rename to csharp/App/BmsTunnel/BatteryTty.cs index a3cfc5cc2..b1ef964fd 100644 --- a/csharp/App/BmsTunnel/BatteryConnection.cs +++ b/csharp/App/BmsTunnel/BatteryTty.cs @@ -7,39 +7,26 @@ namespace InnovEnergy.App.BmsTunnel; using Nodes = IReadOnlyList; -public readonly struct BatteryConnection +public static class BatteryTty { - public String Tty { get; } - public Nodes Nodes { get; } - - private BatteryConnection(String tty, Nodes nodes) - { - Tty = tty; - Nodes = nodes; - } + const String DevDir = "/dev"; - private BatteryConnection(String tty, params Byte[] nodes) : this(tty, (Nodes) nodes) - { - } - - - public static async Task Connect(SshHost? host = null) + public static async Task GetTty() { Console.WriteLine("searching battery connection..."); - return await GetConnectionFromDBus(host) - ?? await GetConnectionFromDevTty(host); + return await GetTtyFromDBus() + ?? await GetTtyFromDev(); } - private static async Task GetConnectionFromDevTty(SshHost? host) + private static async Task GetTtyFromDev() { - var fs = FileSystem.OnHost(host); + var ttys = await FileSystem + .Local + .GetFiles(DevDir, FileType.CharacterDevice); - var ttys = await fs.GetFiles("/dev", FileType.CharacterDevice); - var candidateTtys = ttys - .Where(f => f.Contains("ttyUSB")) - .Select(t => t.Split("/").LastOrDefault()) + .Where(f => f.StartsWith(DevDir + "/ttyUSB")) .NotNull() .ToList(); @@ -49,17 +36,15 @@ public readonly struct BatteryConnection return null; } - var userSelectedTty = "Select TTY:".ChooseFrom(candidateTtys); + if (candidateTtys.Count == 1) + return candidateTtys[0]; - return userSelectedTty != null - ? new BatteryConnection(userSelectedTty) - : null; + return "Select TTY:".ChooseFrom(candidateTtys); } - private static async Task GetConnectionFromDBus(SshHost? host) + private static async Task GetTtyFromDBus() { var tty = await LsDBus - .OnHost(host) .ExecuteBufferedAsync() .Select(ParseBatteryTty); @@ -67,17 +52,15 @@ public readonly struct BatteryConnection return null; Console.WriteLine("found battery on DBus"); - - var availableNodes = await GetNodes(tty, host); - return new BatteryConnection(tty, availableNodes); + + return $"{DevDir}/{tty}"; } - private static CommandTask GetNodes(String tty, SshHost? host = null) + private static CommandTask GetNodes(String tty) { const String defaultArgs = "--system --print-reply --type=method_call / com.victronenergy.BusItem.GetValue"; return DBusSend - .OnHost(host) .AppendArgument($"--dest=com.victronenergy.battery.{tty}") .AppendArgument(defaultArgs) .ExecuteBufferedAsync() diff --git a/csharp/App/BmsTunnel/Program.cs b/csharp/App/BmsTunnel/Program.cs index 538ff348b..eb23e57d8 100644 --- a/csharp/App/BmsTunnel/Program.cs +++ b/csharp/App/BmsTunnel/Program.cs @@ -13,41 +13,19 @@ public static class Program public static async Task Main(String[] args) { - var hostName = args.FirstOrDefault(); - - BatteryConnection? connection = hostName is not null ? await ConnectToBms(hostName, new SshHost(hostName)): new BatteryConnection(); + var tty = await BatteryTty.GetTty(); - if (connection is null) + if (tty is null) return 2; - - // connection.Tty; + Console.WriteLine("\nstarting BMS tunnel\n"); - var path = $"/dev/{connection.Value.Tty}"; - // if (hostName != null) - // path = $"root@{hostName}:" + path; - // - // var node = connection.Nodes.Any() - // ? connection.Nodes.First() - // : DefaultNode; - // - - // TODO: Fixme - // var path = ""; - Byte node = 2; - var nodes = new Byte[] { 1, 2, 3 }; - - // TODO: Fixme - - using var tunnel = new BmsTunnel(path, node, hostName is not null ? new SshHost(hostName): null); + using var tunnel = new BmsTunnel(tty, 2); ExplainNode(); - ExplainNodes(); ExplainExit(); Console.WriteLine(""); - - ListNodes(); while (true) { @@ -73,14 +51,11 @@ public static class Program Boolean ProcessLocalCommand(String cmd) { - cmd = cmd.TrimStart('/').Trim(); + cmd = cmd.TrimStart('/').Trim().ToUpper(); if (cmd == "EXIT") return true; - - if (cmd == "NODES") - ListNodes(); - else if (cmd.StartsWith("NODE ")) + if (cmd.StartsWith("NODE ")) ChangeNode(cmd); else Console.WriteLine("unrecognized command"); @@ -90,14 +65,6 @@ public static class Program return 0; - void ListNodes() - { - if(nodes.Length >= 250) - return; - - nodes.Aggregate("available nodes:", (a, b) => $"{a} {b}") - .Apply(Console.WriteLine); - } void ChangeNode(String cmd) { @@ -109,32 +76,10 @@ public static class Program return; } - if (!nodes.Contains(newNode)) - { - Console.WriteLine(newNode + " is not available"); - ListNodes(); - return; - } - tunnel.Node = newNode; } } - private static async Task ConnectToBms(String? hostName, SshHost host) - { - - if (await host.Ping()) - return await BatteryConnection.Connect(host); - - $"Cannot connect to {hostName}".WriteLine(ConsoleColor.Red); - - return null; - } - - private static void ExplainExit() => Console.WriteLine("/exit exit bms cli"); - private static void ExplainNodes() => Console.WriteLine("/nodes list available nodes"); private static void ExplainNode() => Console.WriteLine("/node change to node number "); - - } \ No newline at end of file