Innovenergy_trunk/csharp/App/VrmGrabber/Controller.cs

132 lines
3.8 KiB
C#
Raw Normal View History

2023-05-11 08:20:47 +00:00
using System.Web;
2023-05-04 15:22:58 +00:00
using HandlebarsDotNet;
2023-05-11 08:20:47 +00:00
using InnovEnergy.App.RemoteSupportConsole;
2023-05-04 15:22:58 +00:00
using InnovEnergy.App.VrmGrabber.Database;
2023-04-27 14:59:45 +00:00
using InnovEnergy.Lib.Victron.VictronVRM;
using Microsoft.AspNetCore.Mvc;
2023-05-04 15:22:58 +00:00
using FILE=System.IO.File;
2023-04-27 14:59:45 +00:00
2023-05-04 15:22:58 +00:00
namespace InnovEnergy.App.VrmGrabber;
2023-04-27 14:59:45 +00:00
2023-05-04 15:22:58 +00:00
public record Install(
String Name,
String Ip,
UInt64 Vrm,
String Identifier,
2023-05-11 08:20:47 +00:00
String Serial,
String EscapedName
2023-05-04 15:22:58 +00:00
);
2023-04-27 14:59:45 +00:00
2023-05-04 15:22:58 +00:00
[Controller]
2023-04-27 14:59:45 +00:00
public class Controller : ControllerBase
{
2023-05-04 15:22:58 +00:00
[HttpGet]
[Route("/")]
[Produces("text/html")]
public ActionResult Index()
2023-04-27 14:59:45 +00:00
{
2023-05-04 15:22:58 +00:00
var instList = Db.InstallationsAndDetails.Keys.ToList();
if (instList.Count == 0) return new ContentResult
2023-04-27 14:59:45 +00:00
{
2023-05-04 15:22:58 +00:00
ContentType = "text/html",
Content = "<p>Please wait page is still loading</p>"
};
String source = @"<head>
<style>
tbody {
background-color: #e4f0f5;
}
2023-04-27 14:59:45 +00:00
2023-05-04 15:22:58 +00:00
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>
2023-05-11 08:20:47 +00:00
<td><a target='_blank' href=http://{{Ip}}>{{Ip}}</a></td>
2023-05-04 15:22:58 +00:00
<td><a target='_blank' href=https://vrm.victronenergy.com/installation/{{Vrm}}/dashboard>VRM</a></td>
<td>{{Identifier}}</td>
2023-05-11 08:20:47 +00:00
<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>";
2023-05-04 15:22:58 +00:00
Handlebars.RegisterTemplate("installations", partialSource);
var template = Handlebars.Compile(source);
var insts = instList.Select(i =>
{
2023-05-11 08:20:47 +00:00
return new Install(i.Name, Ip(i), i.IdSite, i.Identifier, Serial(i), HttpUtility.UrlEncode(i.Name));
2023-05-04 15:22:58 +00:00
});
var data = new
{
inst = insts
};
2023-05-04 15:22:58 +00:00
var result = template(data);
return new ContentResult
{
ContentType = "text/html",
Content = result
};
}
2023-05-04 15:22:58 +00:00
private String Ip(Installation installation)
{
2023-05-11 08:20:47 +00:00
return VpnInfo.LookUpIp(installation.Identifier, Serial(installation)).Result ?? "Unknown";
}
2023-05-04 15:22:58 +00:00
private String Serial(Installation installation)
{
2023-05-04 15:22:58 +00:00
return Db.InstallationsAndDetails[installation].MachineSerial() ?? "Unknown";
}
2023-05-04 15:22:58 +00:00
// [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();
// }
2023-04-27 14:59:45 +00:00
}
2023-05-04 15:22:58 +00:00
// installation Name, ip (link uf gui), idSite (vrm link), identifier , machineserial (HQ...)
2023-04-27 14:59:45 +00:00