remove node detection from BmsTunnel, did more harm than good.

This commit is contained in:
ig 2023-06-19 16:15:59 +02:00
parent 0a9d95188a
commit 5c5d2b72e5
2 changed files with 23 additions and 95 deletions

View File

@ -7,39 +7,26 @@ namespace InnovEnergy.App.BmsTunnel;
using Nodes = IReadOnlyList<Byte>; using Nodes = IReadOnlyList<Byte>;
public readonly struct BatteryConnection public static class BatteryTty
{ {
public String Tty { get; } const String DevDir = "/dev";
public Nodes Nodes { get; }
private BatteryConnection(String tty, Nodes nodes) public static async Task<String?> GetTty()
{
Tty = tty;
Nodes = nodes;
}
private BatteryConnection(String tty, params Byte[] nodes) : this(tty, (Nodes) nodes)
{
}
public static async Task<BatteryConnection?> Connect(SshHost? host = null)
{ {
Console.WriteLine("searching battery connection..."); Console.WriteLine("searching battery connection...");
return await GetConnectionFromDBus(host) return await GetTtyFromDBus()
?? await GetConnectionFromDevTty(host); ?? await GetTtyFromDev();
} }
private static async Task<BatteryConnection?> GetConnectionFromDevTty(SshHost? host) private static async Task<String?> GetTtyFromDev()
{ {
var fs = FileSystem.OnHost(host); var ttys = await FileSystem
.Local
var ttys = await fs.GetFiles("/dev", FileType.CharacterDevice); .GetFiles(DevDir, FileType.CharacterDevice);
var candidateTtys = ttys var candidateTtys = ttys
.Where(f => f.Contains("ttyUSB")) .Where(f => f.StartsWith(DevDir + "/ttyUSB"))
.Select(t => t.Split("/").LastOrDefault())
.NotNull() .NotNull()
.ToList(); .ToList();
@ -49,17 +36,15 @@ public readonly struct BatteryConnection
return null; return null;
} }
var userSelectedTty = "Select TTY:".ChooseFrom(candidateTtys); if (candidateTtys.Count == 1)
return candidateTtys[0];
return userSelectedTty != null return "Select TTY:".ChooseFrom(candidateTtys);
? new BatteryConnection(userSelectedTty)
: null;
} }
private static async Task<BatteryConnection?> GetConnectionFromDBus(SshHost? host) private static async Task<String?> GetTtyFromDBus()
{ {
var tty = await LsDBus var tty = await LsDBus
.OnHost(host)
.ExecuteBufferedAsync() .ExecuteBufferedAsync()
.Select(ParseBatteryTty); .Select(ParseBatteryTty);
@ -68,16 +53,14 @@ public readonly struct BatteryConnection
Console.WriteLine("found battery on DBus"); Console.WriteLine("found battery on DBus");
var availableNodes = await GetNodes(tty, host); return $"{DevDir}/{tty}";
return new BatteryConnection(tty, availableNodes);
} }
private static CommandTask<Nodes> GetNodes(String tty, SshHost? host = null) private static CommandTask<Nodes> GetNodes(String tty)
{ {
const String defaultArgs = "--system --print-reply --type=method_call / com.victronenergy.BusItem.GetValue"; const String defaultArgs = "--system --print-reply --type=method_call / com.victronenergy.BusItem.GetValue";
return DBusSend return DBusSend
.OnHost(host)
.AppendArgument($"--dest=com.victronenergy.battery.{tty}") .AppendArgument($"--dest=com.victronenergy.battery.{tty}")
.AppendArgument(defaultArgs) .AppendArgument(defaultArgs)
.ExecuteBufferedAsync() .ExecuteBufferedAsync()

View File

@ -13,42 +13,20 @@ public static class Program
public static async Task<Int32> Main(String[] args) public static async Task<Int32> Main(String[] args)
{ {
var hostName = args.FirstOrDefault(); var tty = await BatteryTty.GetTty();
BatteryConnection? connection = hostName is not null ? await ConnectToBms(hostName, new SshHost(hostName)): new BatteryConnection(); if (tty is null)
if (connection is null)
return 2; return 2;
// connection.Tty;
Console.WriteLine("\nstarting BMS tunnel\n"); Console.WriteLine("\nstarting BMS tunnel\n");
var path = $"/dev/{connection.Value.Tty}"; using var tunnel = new BmsTunnel(tty, 2);
// 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);
ExplainNode(); ExplainNode();
ExplainNodes();
ExplainExit(); ExplainExit();
Console.WriteLine(""); Console.WriteLine("");
ListNodes();
while (true) while (true)
{ {
Console.WriteLine(""); Console.WriteLine("");
@ -73,14 +51,11 @@ public static class Program
Boolean ProcessLocalCommand(String cmd) Boolean ProcessLocalCommand(String cmd)
{ {
cmd = cmd.TrimStart('/').Trim(); cmd = cmd.TrimStart('/').Trim().ToUpper();
if (cmd == "EXIT") if (cmd == "EXIT")
return true; return true;
if (cmd.StartsWith("NODE "))
if (cmd == "NODES")
ListNodes();
else if (cmd.StartsWith("NODE "))
ChangeNode(cmd); ChangeNode(cmd);
else else
Console.WriteLine("unrecognized command"); Console.WriteLine("unrecognized command");
@ -90,14 +65,6 @@ public static class Program
return 0; return 0;
void ListNodes()
{
if(nodes.Length >= 250)
return;
nodes.Aggregate("available nodes:", (a, b) => $"{a} {b}")
.Apply(Console.WriteLine);
}
void ChangeNode(String cmd) void ChangeNode(String cmd)
{ {
@ -109,32 +76,10 @@ public static class Program
return; return;
} }
if (!nodes.Contains(newNode))
{
Console.WriteLine(newNode + " is not available");
ListNodes();
return;
}
tunnel.Node = newNode; tunnel.Node = newNode;
} }
} }
private static async Task<BatteryConnection?> 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 ExplainExit() => Console.WriteLine("/exit exit bms cli");
private static void ExplainNodes() => Console.WriteLine("/nodes list available nodes");
private static void ExplainNode() => Console.WriteLine("/node <nb> change to node number <nb>"); private static void ExplainNode() => Console.WriteLine("/node <nb> change to node number <nb>");
} }