185 lines
8.7 KiB
C#
185 lines
8.7 KiB
C#
using CliWrap;
|
|
using CliWrap.Buffered;
|
|
using HandlebarsDotNet;
|
|
using InnovEnergy.App.VrmGrabber.Database;
|
|
using InnovEnergy.Lib.Utils;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using FILE=System.IO.File;
|
|
using VrmInstallation = InnovEnergy.Lib.Victron.VictronVRM.Installation;
|
|
|
|
namespace InnovEnergy.App.VrmGrabber;
|
|
|
|
public record InstallationToHtmlInterface(
|
|
String Name,
|
|
String Ip,
|
|
Int64 Vrm,
|
|
String Identifier,
|
|
String Serial,
|
|
String EscapedName,
|
|
String Online,
|
|
String LastSeen,
|
|
Int64 NumBatteries,
|
|
String BatteryVersion
|
|
);
|
|
|
|
[Controller]
|
|
public class Controller : ControllerBase
|
|
{
|
|
|
|
//Todo automatically grab newest version?
|
|
private const String FirmwareVersion = "VERSION";
|
|
|
|
|
|
[HttpGet]
|
|
[Route("/")]
|
|
[Produces("text/html")]
|
|
public ActionResult Index()
|
|
{
|
|
const String source = @"<head>
|
|
<style>
|
|
tbody {
|
|
background-color: #e4f0f5;
|
|
}
|
|
|
|
table {
|
|
border-collapse: collapse;
|
|
width: 100%;
|
|
border: 2px solid rgb(200, 200, 200);
|
|
letter-spacing: 1px;
|
|
font-family: sans-serif;
|
|
font-size: 0.8rem;
|
|
position: relative; top: 0; bottom: 0; left: 0; right: 0;
|
|
}
|
|
|
|
td,
|
|
th {
|
|
border: 1px solid rgb(190, 190, 190);
|
|
padding: 5px 10px;
|
|
position: sticky;
|
|
top: 0px;
|
|
background: white;
|
|
}
|
|
|
|
td {
|
|
text-align: left;
|
|
}
|
|
#managerTable {
|
|
overflow: hidden;
|
|
}</style></head>
|
|
|
|
<div id='managerTable'>
|
|
<table>
|
|
<tbody>
|
|
<tr>
|
|
<th>Name</th>
|
|
<th>Gui</th>
|
|
<th>VRM</th>
|
|
<th>Grafana</th>
|
|
<th>Identifier</th>
|
|
<th>Last Seen</th>
|
|
<th>Serial</th>
|
|
<th>#Batteries</th>
|
|
<th>Firmware-Version</th>
|
|
</tr>
|
|
{{#inst}}
|
|
{{> installations}}
|
|
{{/inst}}
|
|
</tbody>
|
|
</table>
|
|
<div id='managerTable'>";
|
|
|
|
|
|
|
|
const String partialSource = @"<tr><td>{{Name}}</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>{{LastSeen}}</td>
|
|
<td>{{Serial}}</td>
|
|
<td>{{NumBatteries}}</td>
|
|
<td>{{BatteryVersion}}</td>
|
|
<td><a target='_blank' href=http://{{serverIp}}/UpdateBatteryFirmware/{{Ip}}>⬆️{{firmwareVersion}}</a></td>
|
|
</tr>";
|
|
|
|
var installationsInDb = Db.Installations.ToList();
|
|
if (installationsInDb.Count == 0) return new ContentResult
|
|
{
|
|
ContentType = "text/html",
|
|
Content = "<p>Please wait page is still loading</p>"
|
|
};
|
|
|
|
Handlebars.RegisterTemplate("installations", partialSource);
|
|
var template = Handlebars.Compile(source);
|
|
var installsForHtml = installationsInDb.Select(i => new InstallationToHtmlInterface(
|
|
i.Name,
|
|
i.Ip,
|
|
i.Vrm,
|
|
i.Identifier,
|
|
i.Serial,
|
|
i.EscapedName,
|
|
i.Online,
|
|
DateTimeOffset.FromUnixTimeSeconds(Convert.ToInt64(i.LastSeen)).ToString(),
|
|
i.NumberOfBatteries,
|
|
i.BatteryFirmwareVersion));
|
|
|
|
var data = new
|
|
{
|
|
inst = installsForHtml,
|
|
serverIp = "10.2.0.1", //TODO MAKE ME DYNAMIC
|
|
firmwareVersion = FirmwareVersion
|
|
};
|
|
|
|
var result = template(data);
|
|
|
|
return new ContentResult
|
|
{
|
|
ContentType = "text/html",
|
|
Content = result
|
|
};
|
|
}
|
|
|
|
|
|
[HttpGet(nameof(UpdateBatteryFirmware))]
|
|
public async Task<ActionResult> UpdateBatteryFirmware(String installationIp, Int64 numOfBatteries)
|
|
{
|
|
//We need the DeviceName of the battery (ttyUSB?)
|
|
var pathToBattery = await Db.ExecuteBufferedAsyncCommandOnIp(installationIp, "dbus-send --system --dest=com.victronenergy.system --type=method_call --print-reply /ServiceMapping/com_victronenergy_battery_1 com.victronenergy.BusItem.GetText");
|
|
|
|
if (pathToBattery == "Failed") return new BadRequestResult();
|
|
|
|
SendNewBatteryFirmware(installationIp);
|
|
|
|
for (var batteryId = 0; batteryId < numOfBatteries; batteryId++)
|
|
{
|
|
var updateCommand = await Db.ExecuteBufferedAsyncCommandOnIp(installationIp, $"/opt/innovenergy/scripts/upload-bms-firmware {pathToBattery} {batteryId} /opt/innovenergy/bms-firmware/{FirmwareVersion}.bin");
|
|
if (updateCommand == "Failed") return new BadRequestResult();
|
|
}
|
|
return new AcceptedResult();
|
|
}
|
|
|
|
private static void SendNewBatteryFirmware(String installationIp)
|
|
{
|
|
Cli.Wrap("scp")
|
|
.WithArguments($@"{FirmwareVersion}.bin")
|
|
.AppendArgument($@"root@{installationIp}:/opt/innovenergy/bms-firmware/{FirmwareVersion}.bin")
|
|
.WithValidation(CommandResultValidation.None).ExecuteBufferedAsync();
|
|
}
|
|
// [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();
|
|
// }
|
|
}
|
|
|