updated VrmGrabber to include online-status

This commit is contained in:
Kim 2023-05-11 13:26:13 +02:00
parent fa9d06192d
commit 9c927bfc3a
9 changed files with 48 additions and 60 deletions

View File

@ -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 =
@"<tr><td>{{Name}}</td>
<td><a target='_blank' href=http://{{Ip}}>{{Ip}}</a></td>
<td><a target='_blank' href=http://{{Ip}}>{{online}} {{Ip}}</a></td>
<td><a target='_blank' href=https://vrm.victronenergy.com/installation/{{Vrm}}/dashboard>VRM</a></td>
<td><a target='_blank' href='https://salidomo.innovenergy.ch/d/ENkNRQXmk/installation?refresh=5s&orgId=1&var-Installation={{EscapedName}}&kiosk=tv'>Grafana</a></td>
<td>{{Identifier}}</td>
<td>{{Serial}}</td>
<td><a target='_blank' href='https://salidomo.innovenergy.ch/d/ENkNRQXmk/installation?refresh=5s&orgId=1&var-Installation={{EscapedName}}&kiosk=tv'>Grafana</a></td></tr>";
</tr>";
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)
{
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 VpnInfo.LookUpIp(installation.Identifier, Serial(installation)).Result ?? "Unknown";
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))]

View File

@ -10,7 +10,7 @@ public class Installation : TreeNode
public Installation()
{
}
public String Ip { get; set; }
public String Name { get; set; }
// Settings
public IReadOnlyList<Detail> Notes { get; set; }

View File

@ -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;
}
}

View File

@ -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,12 +12,22 @@ using Installation = InnovEnergy.App.VrmGrabber.DataTypes.Installation;
namespace InnovEnergy.App.VrmGrabber.Database;
public class InstallationDetails
{
public InstallationDetails(String ip, IReadOnlyList<Detail> details)
{
Details = details;
Ip = ip;
}
public IReadOnlyList<Detail>? Details { get; set; }
public String Ip { get; set; }
}
public static partial class Db
{
public static Dictionary<Lib.Victron.VictronVRM.Installation, IReadOnlyList<Detail>> InstallationsAndDetails;
internal const String DbPath = "./db.sqlite";
public static Dictionary<Lib.Victron.VictronVRM.Installation, InstallationDetails> InstallationsAndDetails;
private static SQLiteConnection Connection { get; } = new SQLiteConnection(DbPath);
public static TableQuery<Installation> Installations => Connection.Table<Installation>();
@ -32,9 +43,9 @@ public static partial class Db
Connection.RunInTransaction(() =>
{
// Connection.CreateTable<Installation>();
});
});// on startup create/migrate tables
InstallationsAndDetails = new Dictionary<Lib.Victron.VictronVRM.Installation, IReadOnlyList<Detail>>();
InstallationsAndDetails = new Dictionary<Lib.Victron.VictronVRM.Installation, InstallationDetails>();
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 = "<Pending>")]
private static async Task<Dictionary<Lib.Victron.VictronVRM.Installation, IReadOnlyList<Detail>>> UpdateInstallationsAndDetailsFromVrm(Int64 _)
private static async Task<Dictionary<Lib.Victron.VictronVRM.Installation, InstallationDetails>> 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));
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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}

View File

@ -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<String>(propName);
return n.TryGet<String>(propName) ?? "";
}
catch (Exception e)
{

View File

@ -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");