From be099118c1134b5dd29c7e5ce87e5899f130f333 Mon Sep 17 00:00:00 2001 From: Kim Date: Thu, 11 May 2023 15:41:41 +0200 Subject: [PATCH] added renaming to installations --- csharp/App/VrmGrabber/Database/Db.cs | 56 ++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/csharp/App/VrmGrabber/Database/Db.cs b/csharp/App/VrmGrabber/Database/Db.cs index ac2efe7a5..4c9304e71 100644 --- a/csharp/App/VrmGrabber/Database/Db.cs +++ b/csharp/App/VrmGrabber/Database/Db.cs @@ -2,6 +2,8 @@ using System.Diagnostics.CodeAnalysis; using System.Reactive.Concurrency; using System.Reactive.Linq; using System.Reactive.Threading.Tasks; +using CliWrap; +using CliWrap.Buffered; using InnovEnergy.App.RemoteSupportConsole; using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Victron.VictronVRM; @@ -47,27 +49,65 @@ public static partial class Db InstallationsAndDetails = new Dictionary(); - Observable.Interval(TimeSpan.FromMinutes(5)) + Observable.Interval(TimeSpan.FromMinutes(5)) //Repeat(0) .ObserveOn(TaskPoolScheduler.Default) .SubscribeOn(TaskPoolScheduler.Default) .StartWith(0) // Do it right away (on startup) .Select(UpdateInstallationsAndDetailsFromVrm) - .Select(t => t.ToObservable()) - .Concat() + // .Select(t => t.ToObservable()) + // .Concat() .Subscribe(d => InstallationsAndDetails = d, exception => exception.WriteLine()); } [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "")] - private static async Task> UpdateInstallationsAndDetailsFromVrm(Int64 _) + private static Dictionary UpdateInstallationsAndDetailsFromVrm(Int64 _) { - var fileContent = await File.ReadAllTextAsync("./token.json"); + var fileContent = File.ReadAllTextAsync("./token.json").Result; var acc = Deserialize(fileContent); var user = VrmAccount.Token(acc.idUser, acc.token); - var installations = await user.GetInstallations(); - return installations + var installations = user.GetInstallations().Result; + return installations.Take(30) .Do(i=>i.Name.WriteLine()) - .ToDictionary(i => i, i => new InstallationDetails(VpnInfo.LookUpIp(i.Identifier, i.GetDetails().Result.MachineSerial()).Result ?? "Unknown",i.GetDetails().Result)); + .ToDictionary(i => i, GetInstallationDetails); + } + + private static InstallationDetails GetInstallationDetails(Lib.Victron.VictronVRM.Installation i) + { + // Thread.Sleep(3000); + IReadOnlyList details; + try + { + details = i.GetDetails().Result; + + var ip = VpnInfo.LookUpIp(i.Identifier, details.MachineSerial()).Result ?? "Unknown"; + + if(ip != "Unknown") + UpdateInstallationName(i, ip); + + return new InstallationDetails(ip,details); + } + catch (Exception e) + { + Console.WriteLine(e); + } + + return new InstallationDetails("Unkown", Array.Empty()); + } + + private static async Task UpdateInstallationName(Lib.Victron.VictronVRM.Installation installation, String ip) + { + var command = Cli.Wrap("ssh") + .WithArguments($@"root@{ip}") + .AppendArgument("-o StrictHostKeyChecking=accept-new") + .AppendArgument($"echo '{installation.Name}' > /data/innovenergy/openvpn/installation-name"); + Console.WriteLine(command.ToString()); + var x = await command + .WithValidation(CommandResultValidation.None).ExecuteBufferedAsync(); + if (x.ExitCode != 0) + { Console.WriteLine(x.StandardError); + Console.WriteLine("Renaming did not work"); + } } }