added renaming to installations

This commit is contained in:
Kim 2023-05-11 15:41:41 +02:00
parent 69c105296f
commit be099118c1
1 changed files with 48 additions and 8 deletions

View File

@ -2,6 +2,8 @@ using System.Diagnostics.CodeAnalysis;
using System.Reactive.Concurrency; using System.Reactive.Concurrency;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Reactive.Threading.Tasks; using System.Reactive.Threading.Tasks;
using CliWrap;
using CliWrap.Buffered;
using InnovEnergy.App.RemoteSupportConsole; using InnovEnergy.App.RemoteSupportConsole;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using InnovEnergy.Lib.Victron.VictronVRM; using InnovEnergy.Lib.Victron.VictronVRM;
@ -47,27 +49,65 @@ public static partial class Db
InstallationsAndDetails = new Dictionary<Lib.Victron.VictronVRM.Installation, InstallationDetails>(); InstallationsAndDetails = new Dictionary<Lib.Victron.VictronVRM.Installation, InstallationDetails>();
Observable.Interval(TimeSpan.FromMinutes(5)) Observable.Interval(TimeSpan.FromMinutes(5)) //Repeat(0)
.ObserveOn(TaskPoolScheduler.Default) .ObserveOn(TaskPoolScheduler.Default)
.SubscribeOn(TaskPoolScheduler.Default) .SubscribeOn(TaskPoolScheduler.Default)
.StartWith(0) // Do it right away (on startup) .StartWith(0) // Do it right away (on startup)
.Select(UpdateInstallationsAndDetailsFromVrm) .Select(UpdateInstallationsAndDetailsFromVrm)
.Select(t => t.ToObservable()) // .Select(t => t.ToObservable())
.Concat() // .Concat()
.Subscribe(d => InstallationsAndDetails = d, exception => exception.WriteLine()); .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 = "<Pending>")] [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
private static async Task<Dictionary<Lib.Victron.VictronVRM.Installation, InstallationDetails>> UpdateInstallationsAndDetailsFromVrm(Int64 _) private static Dictionary<Lib.Victron.VictronVRM.Installation, InstallationDetails> UpdateInstallationsAndDetailsFromVrm(Int64 _)
{ {
var fileContent = await File.ReadAllTextAsync("./token.json"); var fileContent = File.ReadAllTextAsync("./token.json").Result;
var acc = Deserialize<AccToken>(fileContent); var acc = Deserialize<AccToken>(fileContent);
var user = VrmAccount.Token(acc.idUser, acc.token); var user = VrmAccount.Token(acc.idUser, acc.token);
var installations = await user.GetInstallations(); var installations = user.GetInstallations().Result;
return installations return installations.Take(30)
.Do(i=>i.Name.WriteLine()) .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<Detail> 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<Detail>());
}
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");
}
} }
} }