From 9c927bfc3a07f888ab8b3107f82a02ca8279a957 Mon Sep 17 00:00:00 2001 From: Kim Date: Thu, 11 May 2023 13:26:13 +0200 Subject: [PATCH] updated VrmGrabber to include online-status --- csharp/App/VrmGrabber/Controller.cs | 34 ++++++++++++++----- .../App/VrmGrabber/DataTypes/Installation.cs | 2 +- csharp/App/VrmGrabber/Database/Create.cs | 13 ------- csharp/App/VrmGrabber/Database/Db.cs | 23 +++++++++---- csharp/App/VrmGrabber/Database/Delete.cs | 13 ------- csharp/App/VrmGrabber/Database/Read.cs | 14 -------- csharp/InnovEnergy.sln | 4 +-- csharp/Lib/Utils/JsonNodeAccessors.cs | 4 +-- csharp/Lib/Victron/VictronVRM/Installation.cs | 1 + 9 files changed, 48 insertions(+), 60 deletions(-) delete mode 100644 csharp/App/VrmGrabber/Database/Create.cs delete mode 100644 csharp/App/VrmGrabber/Database/Delete.cs delete mode 100644 csharp/App/VrmGrabber/Database/Read.cs diff --git a/csharp/App/VrmGrabber/Controller.cs b/csharp/App/VrmGrabber/Controller.cs index 653aede9b..08ee31f46 100644 --- a/csharp/App/VrmGrabber/Controller.cs +++ b/csharp/App/VrmGrabber/Controller.cs @@ -14,7 +14,8 @@ public record Install( UInt64 Vrm, String Identifier, String Serial, - String EscapedName + String EscapedName, + String Online ); [Controller] @@ -40,6 +41,7 @@ tbody { table { border-collapse: collapse; + width: 100%; border: 2px solid rgb(200, 200, 200); letter-spacing: 1px; font-family: sans-serif; @@ -67,17 +69,19 @@ td { String partialSource = @"{{Name}} - {{Ip}} + {{online}} {{Ip}} VRM + Grafana {{Identifier}} {{Serial}} - Grafana"; + "; Handlebars.RegisterTemplate("installations", partialSource); var template = Handlebars.Compile(source); var insts = instList.Select(i => { - return new Install(i.Name, Ip(i), i.IdSite, i.Identifier, Serial(i), HttpUtility.UrlEncode(i.Name)); + var ip = Ip(i); + return new Install(i.Name, ip[0], i.IdSite, i.Identifier, Serial(i), HttpUtility.UrlEncode(i.Name), ip[1]); }); @@ -95,15 +99,27 @@ td { }; } - private String Ip(Installation installation) + private String[] Ip(Installation installation) { - - return VpnInfo.LookUpIp(installation.Identifier, Serial(installation)).Result ?? "Unknown"; + var online = "❌"; + var lookup = Db.InstallationsAndDetails[installation].Ip; + if (lookup == "Unknown") + { + var serial = Serial(installation); + if (serial != "Unknown" && FILE.Exists($@"/etc/openvpn/server/Salino/ccd/{serial}")) + lookup = FILE.ReadAllText($@"/etc/openvpn/server/Salino/ccd/{serial}").Split(' ')[1]; + } + else + { + online = "✅"; + } + + return new [] {lookup,online}; } - private String Serial(Installation installation) + public static String Serial(Installation installation) { - return Db.InstallationsAndDetails[installation].MachineSerial() ?? "Unknown"; + return Db.InstallationsAndDetails[installation].Details.MachineSerial() ?? "Unknown"; } // [HttpGet(nameof(GetInstallation))] diff --git a/csharp/App/VrmGrabber/DataTypes/Installation.cs b/csharp/App/VrmGrabber/DataTypes/Installation.cs index 2d118add8..6199bbb08 100644 --- a/csharp/App/VrmGrabber/DataTypes/Installation.cs +++ b/csharp/App/VrmGrabber/DataTypes/Installation.cs @@ -10,7 +10,7 @@ public class Installation : TreeNode public Installation() { } - + public String Ip { get; set; } public String Name { get; set; } // Settings public IReadOnlyList Notes { get; set; } diff --git a/csharp/App/VrmGrabber/Database/Create.cs b/csharp/App/VrmGrabber/Database/Create.cs deleted file mode 100644 index b8d7dc25f..000000000 --- a/csharp/App/VrmGrabber/Database/Create.cs +++ /dev/null @@ -1,13 +0,0 @@ -using InnovEnergy.App.VrmGrabber.DataTypes; - -namespace InnovEnergy.App.VrmGrabber.Database; - - -public static partial class Db -{ - public static Boolean Create(Installation installation) - { - // SQLite wrapper is smart and *modifies* t's Id to the one generated (autoincrement) by the insertion - return Connection.Insert(installation) > 0; - } -} \ No newline at end of file diff --git a/csharp/App/VrmGrabber/Database/Db.cs b/csharp/App/VrmGrabber/Database/Db.cs index 41f91a55c..ac2efe7a5 100644 --- a/csharp/App/VrmGrabber/Database/Db.cs +++ b/csharp/App/VrmGrabber/Database/Db.cs @@ -2,6 +2,7 @@ using System.Diagnostics.CodeAnalysis; using System.Reactive.Concurrency; using System.Reactive.Linq; using System.Reactive.Threading.Tasks; +using InnovEnergy.App.RemoteSupportConsole; using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Victron.VictronVRM; using SQLite; @@ -11,13 +12,23 @@ using Installation = InnovEnergy.App.VrmGrabber.DataTypes.Installation; namespace InnovEnergy.App.VrmGrabber.Database; +public class InstallationDetails +{ + public InstallationDetails(String ip, IReadOnlyList details) + { + Details = details; + Ip = ip; + } + + public IReadOnlyList? Details { get; set; } + public String Ip { get; set; } +} public static partial class Db { - public static Dictionary> InstallationsAndDetails; internal const String DbPath = "./db.sqlite"; + public static Dictionary InstallationsAndDetails; - private static SQLiteConnection Connection { get; } = new SQLiteConnection(DbPath); public static TableQuery Installations => Connection.Table(); public static void Init() @@ -32,9 +43,9 @@ public static partial class Db Connection.RunInTransaction(() => { // Connection.CreateTable(); - }); + });// on startup create/migrate tables - InstallationsAndDetails = new Dictionary>(); + InstallationsAndDetails = new Dictionary(); Observable.Interval(TimeSpan.FromMinutes(5)) .ObserveOn(TaskPoolScheduler.Default) @@ -47,7 +58,7 @@ public static partial class Db } [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 async Task> UpdateInstallationsAndDetailsFromVrm(Int64 _) { var fileContent = await File.ReadAllTextAsync("./token.json"); @@ -56,7 +67,7 @@ public static partial class Db var installations = await user.GetInstallations(); return installations .Do(i=>i.Name.WriteLine()) - .ToDictionary(i => i, i => i.GetDetails().Result); + .ToDictionary(i => i, i => new InstallationDetails(VpnInfo.LookUpIp(i.Identifier, i.GetDetails().Result.MachineSerial()).Result ?? "Unknown",i.GetDetails().Result)); } } diff --git a/csharp/App/VrmGrabber/Database/Delete.cs b/csharp/App/VrmGrabber/Database/Delete.cs deleted file mode 100644 index e15fa4b31..000000000 --- a/csharp/App/VrmGrabber/Database/Delete.cs +++ /dev/null @@ -1,13 +0,0 @@ -using InnovEnergy.App.VrmGrabber.DataTypes; - -namespace InnovEnergy.App.VrmGrabber.Database; - - -public static partial class Db -{ - - public static Boolean Delete(Installation installation) - { - return Installations.Delete(i => i.Id == installation.Id) > 0; - } -} \ No newline at end of file diff --git a/csharp/App/VrmGrabber/Database/Read.cs b/csharp/App/VrmGrabber/Database/Read.cs deleted file mode 100644 index 4ef6c174c..000000000 --- a/csharp/App/VrmGrabber/Database/Read.cs +++ /dev/null @@ -1,14 +0,0 @@ -using InnovEnergy.App.VrmGrabber.DataTypes; - -namespace InnovEnergy.App.VrmGrabber.Database; - - -public static partial class Db -{ - - public static Installation? GetInstallationById(Int64? id) - { - return Installations - .FirstOrDefault(i => i.Id == id); - } -} \ No newline at end of file diff --git a/csharp/InnovEnergy.sln b/csharp/InnovEnergy.sln index 3a0401858..640f8ab8f 100644 --- a/csharp/InnovEnergy.sln +++ b/csharp/InnovEnergy.sln @@ -188,8 +188,8 @@ Global {B816BB44-E97E-4E02-B80A-BEDB5B923A96}.Release|Any CPU.Build.0 = Release|Any CPU {4F9BB20B-8030-48AB-A37B-23796459D516}.Release|Any CPU.ActiveCfg = Release|Any CPU {4F9BB20B-8030-48AB-A37B-23796459D516}.Release|Any CPU.Build.0 = Release|Any CPU - {4F9BB20B-8030-48AB-A37B-23796459D516}.Debug|Any CPU.ActiveCfg = Release-Server|linux-arm - {4F9BB20B-8030-48AB-A37B-23796459D516}.Debug|Any CPU.Build.0 = Release-Server|linux-arm + {4F9BB20B-8030-48AB-A37B-23796459D516}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4F9BB20B-8030-48AB-A37B-23796459D516}.Debug|Any CPU.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {CF4834CB-91B7-4172-AC13-ECDA8613CD17} = {145597B4-3E30-45E6-9F72-4DD43194539A} diff --git a/csharp/Lib/Utils/JsonNodeAccessors.cs b/csharp/Lib/Utils/JsonNodeAccessors.cs index 723673111..182e30877 100644 --- a/csharp/Lib/Utils/JsonNodeAccessors.cs +++ b/csharp/Lib/Utils/JsonNodeAccessors.cs @@ -39,11 +39,11 @@ public static class JsonNodeAccessors } } - public static String? TryGetString(this JsonNode n, String propName) + public static String TryGetString(this JsonNode n, String propName) { try { - return n.TryGet(propName); + return n.TryGet(propName) ?? ""; } catch (Exception e) { diff --git a/csharp/Lib/Victron/VictronVRM/Installation.cs b/csharp/Lib/Victron/VictronVRM/Installation.cs index 82fcd85ce..fc9781da5 100644 --- a/csharp/Lib/Victron/VictronVRM/Installation.cs +++ b/csharp/Lib/Victron/VictronVRM/Installation.cs @@ -13,6 +13,7 @@ public readonly partial record struct Installation(VrmAccount VrmAccount, JsonNo public UnixTime Created => Json.GetUInt32("syscreated").Apply(UnixTime.FromTicks); // Settings + public String Name => Json.GetString("name"); public String Notes => Json.GetString("notes"); public String PhoneNumber => Json.GetString("phonenumber");