remove node detection from BmsTunnel, did more harm than good.
This commit is contained in:
parent
0a9d95188a
commit
5c5d2b72e5
|
@ -7,39 +7,26 @@ namespace InnovEnergy.App.BmsTunnel;
|
|||
|
||||
using Nodes = IReadOnlyList<Byte>;
|
||||
|
||||
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<BatteryConnection?> Connect(SshHost? host = null)
|
||||
public static async Task<String?> GetTty()
|
||||
{
|
||||
Console.WriteLine("searching battery connection...");
|
||||
|
||||
return await GetConnectionFromDBus(host)
|
||||
?? await GetConnectionFromDevTty(host);
|
||||
return await GetTtyFromDBus()
|
||||
?? 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
|
||||
.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<BatteryConnection?> GetConnectionFromDBus(SshHost? host)
|
||||
private static async Task<String?> 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<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";
|
||||
|
||||
return DBusSend
|
||||
.OnHost(host)
|
||||
.AppendArgument($"--dest=com.victronenergy.battery.{tty}")
|
||||
.AppendArgument(defaultArgs)
|
||||
.ExecuteBufferedAsync()
|
|
@ -13,41 +13,19 @@ public static class Program
|
|||
|
||||
public static async Task<Int32> 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<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 ExplainNodes() => Console.WriteLine("/nodes list available nodes");
|
||||
private static void ExplainNode() => Console.WriteLine("/node <nb> change to node number <nb>");
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue