updated vrmgrabber

This commit is contained in:
Kim 2023-05-04 17:22:58 +02:00
parent d1fb0a8ac2
commit 70a4a1bfb8
12 changed files with 124 additions and 74 deletions

View File

@ -1,75 +1,133 @@
using System.ComponentModel.Design; using HandlebarsDotNet;
using System.Diagnostics.CodeAnalysis; using InnovEnergy.App.VrmGrabber.Database;
using System.Text.Json;
using System.Text.Json.Nodes;
using Flurl.Util;
using InnovEnergy.App.Backend.Database;
using InnovEnergy.Lib.Victron.VictronVRM; using InnovEnergy.Lib.Victron.VictronVRM;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using FILE=System.IO.File;
namespace InnovEnergy.App.Backend.Controllers; namespace InnovEnergy.App.VrmGrabber;
using Token = String; public record Install(
String Name,
String Ip,
UInt64 Vrm,
String Identifier,
String Serial
);
[ApiController] [Controller]
[Route("api/")]
public class Controller : ControllerBase public class Controller : ControllerBase
{ {
[HttpGet(nameof(GetInstallation))] [HttpGet]
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")] [Route("/")]
public Object GetInstallation(UInt64 serialNumber) [Produces("text/html")]
{ public ActionResult Index()
var instList = Db.InstallationsAndDetails.Values.ToList();
foreach (var detailList in instList.Select((value, index) => new { Value = value, Index = index}))
{
if (detailList.Value.All(detail => detail.Json["idSite"]?.GetValue<UInt64>() != serialNumber)) continue;
var retour = Db.InstallationsAndDetails.Keys.ToList()[detailList.Index].Json;
retour["details"] = JsonSerializer.Deserialize<JsonArray>(JsonSerializer.Serialize(detailList.Value.Select(d => d.Json).ToArray()));
return retour;
}
return new NotFoundResult();
}
[HttpGet(nameof(GetInstallationList))]
public Object GetInstallationList()
{ {
var instList = Db.InstallationsAndDetails.Keys.ToList(); var instList = Db.InstallationsAndDetails.Keys.ToList();
if (instList.Count == 0) return 0; if (instList.Count == 0) return new ContentResult
var returnJson = new Dictionary<String, Dictionary<String, String>>(); {
foreach (var installation in instList) ContentType = "text/html",
Content = "<p>Please wait page is still loading</p>"
};
String source = @"<head>
<style>
tbody {
background-color: #e4f0f5;
}
table {
border-collapse: collapse;
border: 2px solid rgb(200, 200, 200);
letter-spacing: 1px;
font-family: sans-serif;
font-size: 0.8rem;
position: absolute; top: 0; bottom: 0; left: 0; right: 0;
}
td,
th {
border: 1px solid rgb(190, 190, 190);
padding: 5px 10px;
}
td {
text-align: left;
}</style></head>
<table>
<tbody>
{{#inst}}
{{> installations}}
{{/inst}}
</tbody>
</table>";
String partialSource =
@"<tr><td>{{Name}}</td>
<td><a target='_blank' href=https://{{Ip}}>{{Ip}}</a></td>
<td><a target='_blank' href=https://vrm.victronenergy.com/installation/{{Vrm}}/dashboard>VRM</a></td>
<td>{{Identifier}}</td>
<td>{{Serial}}</td></tr>";
Handlebars.RegisterTemplate("installations", partialSource);
var template = Handlebars.Compile(source);
var insts = instList.Select(i =>
{ {
returnJson[installation.Name] = new Dictionary<String, String>();
returnJson[installation.Name].Add("ip", $"<a target='_blank' href=https://{Ip(installation)}> On-Device-Gui </a>");
try try
{ {
returnJson[installation.Name].Add("idSite", return new Install(i.Name, Ip(i), i.IdSite, i.Identifier, Serial(i));
$"<a target='_blank' href=https://vrm.victronenergy.com/installation/{installation.IdSite}/dashboard> Dashboard </a>");
returnJson[installation.Name]
.Add("identifier", $"{installation.Json["identifier"]?.GetValue<String>()}");
returnJson[installation.Name].Add("machine serial number", $"{Serial(installation)}");
} }
catch (Exception) catch (Exception)
{ {
continue; return new Install(i.Name, Ip(i), i.IdSite, "0", Serial(i));
} }
} });
return returnJson;
var data = new
{
inst = insts
};
var result = template(data);
return new ContentResult
{
ContentType = "text/html",
Content = result
};
} }
private static String? Ip(Installation installation) private String Ip(Installation installation)
{ {
return Db.InstallationsAndDetails[installation].RemoteSupportIp(); return Db.InstallationsAndDetails[installation].RemoteSupportIp() ?? "Unknown";
} }
private static String? Serial(Installation installation) private String Serial(Installation installation)
{ {
return Db.InstallationsAndDetails[installation].MachineSerial(); return Db.InstallationsAndDetails[installation].MachineSerial() ?? "Unknown";
} }
// [HttpGet(nameof(GetInstallation))]
// [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
// public Object GetInstallation(UInt64 serialNumber)
// {
// var instList = Db.InstallationsAndDetails.Values.ToList();
// foreach (var detailList in instList.Select((value, index) => new { Value = value, Index = index}))
// {
// if (detailList.Value.All(detail => detail.Json["idSite"]?.GetValue<UInt64>() != serialNumber)) continue;
// var retour = Db.InstallationsAndDetails.Keys.ToList()[detailList.Index].Json;
// retour["details"] = JsonSerializer.Deserialize<JsonArray>(JsonSerializer.Serialize(detailList.Value.Select(d => d.Json).ToArray()));
// return retour;
// }
//
// return new NotFoundResult();
// }
} }
// installation Name, ip (link uf gui), idSite (vrm link), identifier , machineserial (HQ...) // installation Name, ip (link uf gui), idSite (vrm link), identifier , machineserial (HQ...)

View File

@ -1,7 +1,6 @@
using InnovEnergy.Lib.Victron.VictronVRM; using InnovEnergy.Lib.Victron.VictronVRM;
using SQLite;
namespace InnovEnergy.App.Backend.DataTypes; namespace InnovEnergy.App.VrmGrabber.DataTypes;
public class Installation : TreeNode public class Installation : TreeNode

View File

@ -1,4 +1,4 @@
namespace InnovEnergy.App.Backend.DataTypes.Methods; namespace InnovEnergy.App.VrmGrabber.DataTypes.Methods;
public static class InstallationMethods public static class InstallationMethods

View File

@ -1,6 +1,6 @@
using SQLite; using SQLite;
namespace InnovEnergy.App.Backend.DataTypes; namespace InnovEnergy.App.VrmGrabber.DataTypes;
public abstract partial class TreeNode public abstract partial class TreeNode
{ {

View File

@ -1,7 +1,6 @@
using InnovEnergy.App.Backend.DataTypes; using InnovEnergy.App.VrmGrabber.DataTypes;
namespace InnovEnergy.App.VrmGrabber.Database;
namespace InnovEnergy.App.Backend.Database;
public static partial class Db public static partial class Db

View File

@ -1,17 +1,15 @@
using System.Diagnostics.CodeAnalysis; 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 static System.Text.Json.JsonSerializer;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using InnovEnergy.Lib.Victron.VictronVRM; using InnovEnergy.Lib.Victron.VictronVRM;
using SQLite; using SQLite;
using Installation = InnovEnergy.App.Backend.DataTypes.Installation; using static System.Text.Json.JsonSerializer;
using Installation = InnovEnergy.App.VrmGrabber.DataTypes.Installation;
namespace InnovEnergy.App.Backend.Database; namespace InnovEnergy.App.VrmGrabber.Database;
public static partial class Db public static partial class Db

View File

@ -1,8 +1,6 @@
using InnovEnergy.App.Backend.DataTypes; using InnovEnergy.App.VrmGrabber.DataTypes;
namespace InnovEnergy.App.VrmGrabber.Database;
namespace InnovEnergy.App.Backend.Database;
public static partial class Db public static partial class Db

View File

@ -1,8 +1,6 @@
using InnovEnergy.App.Backend.DataTypes; using InnovEnergy.App.VrmGrabber.DataTypes;
namespace InnovEnergy.App.VrmGrabber.Database;
namespace InnovEnergy.App.Backend.Database;
public static partial class Db public static partial class Db

View File

@ -1,17 +1,12 @@
using InnovEnergy.App.Backend.Database; using InnovEnergy.App.VrmGrabber.Database;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
namespace InnovEnergy.App.Backend; namespace InnovEnergy.App.VrmGrabber;
public static class Program public static class Program
{ {
public static void Main(String[] args) public static void Main(String[] args)
{ {
//Db.CreateFakeRelations();
Db.Init();
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -26,9 +21,9 @@ public static class Program
var app = builder.Build(); var app = builder.Build();
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI();
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.MapControllers(); app.MapControllers();
// app.MapGet("/", () => Controller.Index());
app.Run(); app.Run();
} }

View File

@ -3,6 +3,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Flurl.Http" Version="3.2.4" /> <PackageReference Include="Flurl.Http" Version="3.2.4" />
<PackageReference Include="Handlebars.Net" Version="2.1.4" />
<PackageReference Include="Hellang.Middleware.ProblemDetails" Version="6.5.1" /> <PackageReference Include="Hellang.Middleware.ProblemDetails" Version="6.5.1" />
<PackageReference Include="MailKit" Version="3.6.0" /> <PackageReference Include="MailKit" Version="3.6.0" />
<PackageReference Include="Microsoft.AspNet.Identity.Core" Version="2.2.3" /> <PackageReference Include="Microsoft.AspNet.Identity.Core" Version="2.2.3" />
@ -41,4 +42,8 @@
</Content> </Content>
</ItemGroup> </ItemGroup>
<ItemGroup>
<_ContentIncludedByDefault Remove="wwwroot\index.html" />
</ItemGroup>
</Project> </Project>

Binary file not shown.

Binary file not shown.