2023-05-25 07:18:41 +00:00
|
|
|
using CliWrap;
|
2023-05-04 15:22:58 +00:00
|
|
|
using HandlebarsDotNet;
|
|
|
|
using InnovEnergy.App.VrmGrabber.Database;
|
2023-05-25 07:18:41 +00:00
|
|
|
using InnovEnergy.Lib.Utils;
|
2023-04-27 14:59:45 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-05-18 08:45:44 +00:00
|
|
|
using VrmInstallation = InnovEnergy.Lib.Victron.VictronVRM.Installation;
|
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-25 07:18:41 +00:00
|
|
|
public record InstallationToHtmlInterface(
|
2023-05-18 10:38:34 +00:00
|
|
|
String Name,
|
|
|
|
String Ip,
|
|
|
|
Int64 Vrm,
|
|
|
|
String Identifier,
|
|
|
|
String Serial,
|
|
|
|
String EscapedName,
|
|
|
|
String Online,
|
|
|
|
String LastSeen,
|
2023-06-01 09:21:13 +00:00
|
|
|
String NumBatteries,
|
2023-05-25 12:49:40 +00:00
|
|
|
String BatteryVersion,
|
2023-08-23 09:31:30 +00:00
|
|
|
String BatteryUpdateStatus,
|
2023-05-25 12:49:40 +00:00
|
|
|
String ServerIp = "10.2.0.1", //TODO MAKE ME DYNAMIC
|
|
|
|
String FirmwareVersion = "AF09" //Todo automatically grab newest version?
|
2023-08-23 09:31:30 +00:00
|
|
|
);
|
2023-05-18 10:38:34 +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-25 07:18:41 +00:00
|
|
|
|
|
|
|
//Todo automatically grab newest version?
|
2023-05-25 12:49:40 +00:00
|
|
|
private const String FirmwareVersion = "AF09";
|
2023-05-25 07:18:41 +00:00
|
|
|
|
|
|
|
|
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-25 07:18:41 +00:00
|
|
|
const String source = @"<head>
|
2023-05-18 08:45:44 +00:00
|
|
|
<style>
|
|
|
|
tbody {
|
|
|
|
background-color: #e4f0f5;
|
|
|
|
}
|
2023-06-01 09:21:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
tbody tr:nth-child(odd) {
|
|
|
|
background-color: #ECE9E9;
|
|
|
|
}
|
|
|
|
|
|
|
|
th, td { /* cell */
|
|
|
|
padding: 0.75rem;
|
|
|
|
font-size: 0.9375rem;
|
|
|
|
}
|
|
|
|
|
|
|
|
th { /* header cell */
|
|
|
|
font-weight: 700;
|
|
|
|
text-align: left;
|
|
|
|
color: #272838;
|
|
|
|
border-bottom: 2px solid #EB9486;
|
|
|
|
|
|
|
|
position: sticky;
|
|
|
|
top: 0;
|
|
|
|
background-color: #F9F8F8;
|
|
|
|
}
|
2023-05-18 08:45:44 +00:00
|
|
|
table {
|
|
|
|
border-collapse: collapse;
|
|
|
|
width: 100%;
|
|
|
|
border: 2px solid rgb(200, 200, 200);
|
|
|
|
letter-spacing: 1px;
|
|
|
|
font-family: sans-serif;
|
|
|
|
font-size: 0.8rem;
|
2023-06-01 09:21:13 +00:00
|
|
|
position: absolute; top: 0; bottom: 0; left: 0; right: 0;
|
2023-05-18 08:45:44 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 09:21:13 +00:00
|
|
|
thead th {
|
2023-05-18 08:45:44 +00:00
|
|
|
border: 1px solid rgb(190, 190, 190);
|
|
|
|
padding: 5px 10px;
|
2023-05-18 10:38:34 +00:00
|
|
|
position: sticky;
|
2023-06-01 09:21:13 +00:00
|
|
|
position: -webkit-sticky;
|
2023-05-18 10:38:34 +00:00
|
|
|
top: 0px;
|
|
|
|
background: white;
|
2023-06-01 09:21:13 +00:00
|
|
|
z-index: 999;
|
2023-05-18 08:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
td {
|
|
|
|
text-align: left;
|
2023-05-18 10:38:34 +00:00
|
|
|
}
|
|
|
|
#managerTable {
|
|
|
|
overflow: hidden;
|
2023-05-18 08:45:44 +00:00
|
|
|
}</style></head>
|
2023-05-18 10:38:34 +00:00
|
|
|
|
|
|
|
<div id='managerTable'>
|
2023-05-18 08:45:44 +00:00
|
|
|
<table>
|
|
|
|
<tbody>
|
2023-05-18 10:38:34 +00:00
|
|
|
<tr>
|
2023-08-23 09:31:30 +00:00
|
|
|
<th>Name This site is updated once per day!</th>
|
2023-05-18 10:38:34 +00:00
|
|
|
<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>
|
2023-06-01 09:21:13 +00:00
|
|
|
<th>Update</th>
|
2023-08-23 09:31:30 +00:00
|
|
|
<th>Last Update Status</th>
|
2023-05-18 10:38:34 +00:00
|
|
|
</tr>
|
2023-05-18 08:45:44 +00:00
|
|
|
{{#inst}}
|
|
|
|
{{> installations}}
|
|
|
|
{{/inst}}
|
|
|
|
</tbody>
|
2023-05-18 10:38:34 +00:00
|
|
|
</table>
|
|
|
|
<div id='managerTable'>";
|
2023-05-04 15:22:58 +00:00
|
|
|
|
2023-05-25 07:18:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
const String partialSource = @"<tr><td>{{Name}}</td>
|
2023-05-11 11:26:13 +00:00
|
|
|
<td><a target='_blank' href=http://{{Ip}}>{{online}} {{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>
|
2023-05-11 11:26:13 +00:00
|
|
|
<td><a target='_blank' href='https://salidomo.innovenergy.ch/d/ENkNRQXmk/installation?refresh=5s&orgId=1&var-Installation={{EscapedName}}&kiosk=tv'>Grafana</a></td>
|
2023-05-04 15:22:58 +00:00
|
|
|
<td>{{Identifier}}</td>
|
2023-05-18 08:45:44 +00:00
|
|
|
<td>{{LastSeen}}</td>
|
2023-05-11 08:20:47 +00:00
|
|
|
<td>{{Serial}}</td>
|
2023-05-18 10:38:34 +00:00
|
|
|
<td>{{NumBatteries}}</td>
|
|
|
|
<td>{{BatteryVersion}}</td>
|
2023-10-02 13:40:03 +00:00
|
|
|
<td><a target='_blank' href=http://{{ServerIp}}/UpdateBatteryFirmware/{{Ip}}>⬆️{{FirmwareVersion}}</a></td>
|
2023-08-23 09:31:30 +00:00
|
|
|
<td>{{BatteryUpdateStatus}}</td>
|
2023-05-11 11:26:13 +00:00
|
|
|
</tr>";
|
2023-05-04 15:22:58 +00:00
|
|
|
|
2023-06-01 14:10:57 +00:00
|
|
|
var installationsInDb = Db.Installations.OrderBy(i => i.Name, StringComparer.OrdinalIgnoreCase).ToList();
|
2023-05-25 07:18:41 +00:00
|
|
|
if (installationsInDb.Count == 0) return new ContentResult
|
2023-05-18 08:45:44 +00:00
|
|
|
{
|
|
|
|
ContentType = "text/html",
|
|
|
|
Content = "<p>Please wait page is still loading</p>"
|
|
|
|
};
|
|
|
|
|
2023-05-04 15:22:58 +00:00
|
|
|
Handlebars.RegisterTemplate("installations", partialSource);
|
|
|
|
var template = Handlebars.Compile(source);
|
2023-05-25 07:18:41 +00:00
|
|
|
var installsForHtml = installationsInDb.Select(i => new InstallationToHtmlInterface(
|
2023-05-18 10:38:34 +00:00
|
|
|
i.Name,
|
|
|
|
i.Ip,
|
|
|
|
i.Vrm,
|
|
|
|
i.Identifier,
|
|
|
|
i.Serial,
|
|
|
|
i.EscapedName,
|
|
|
|
i.Online,
|
|
|
|
DateTimeOffset.FromUnixTimeSeconds(Convert.ToInt64(i.LastSeen)).ToString(),
|
|
|
|
i.NumberOfBatteries,
|
2023-08-23 09:31:30 +00:00
|
|
|
i.BatteryFirmwareVersion,
|
|
|
|
i.BatteryUpdateStatus));
|
2023-05-18 10:38:34 +00:00
|
|
|
|
2023-05-04 15:22:58 +00:00
|
|
|
var data = new
|
|
|
|
{
|
2023-05-25 07:18:41 +00:00
|
|
|
inst = installsForHtml,
|
2023-05-04 15:22:58 +00:00
|
|
|
};
|
2023-05-04 11:59:47 +00:00
|
|
|
|
2023-05-04 15:22:58 +00:00
|
|
|
var result = template(data);
|
|
|
|
|
|
|
|
return new ContentResult
|
|
|
|
{
|
|
|
|
ContentType = "text/html",
|
|
|
|
Content = result
|
|
|
|
};
|
2023-05-04 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 07:18:41 +00:00
|
|
|
|
2023-10-02 13:40:03 +00:00
|
|
|
[HttpGet("UpdateBatteryFirmware/{installationIp}")]
|
|
|
|
public async Task<String> UpdateBatteryFirmware(String installationIp)
|
2023-05-25 07:18:41 +00:00
|
|
|
{
|
|
|
|
//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");
|
2023-06-01 13:29:39 +00:00
|
|
|
|
|
|
|
var split = pathToBattery.Split('"');
|
|
|
|
var split2 = pathToBattery.Split(' ');
|
2023-05-25 07:18:41 +00:00
|
|
|
|
2023-06-01 13:29:39 +00:00
|
|
|
if (split.Length < 2 || split2.Length < 1)
|
|
|
|
{
|
|
|
|
Console.WriteLine(pathToBattery + " Split failed ");
|
|
|
|
return "Update failed";
|
|
|
|
}
|
|
|
|
if (split[1] == "Failed" || split2[0] == "Error") return "Update failed";
|
|
|
|
|
2023-08-23 09:31:30 +00:00
|
|
|
|
2023-05-25 07:18:41 +00:00
|
|
|
|
2023-08-23 09:31:30 +00:00
|
|
|
await SendNewBatteryFirmware(installationIp);
|
|
|
|
var batteryTtyName = split[1].Split(".").Last();
|
2023-10-02 13:40:03 +00:00
|
|
|
var localCommand = "echo start";
|
2023-08-23 09:31:30 +00:00
|
|
|
var installation = Db.Installations.First(installation => installation.Ip == installationIp);
|
|
|
|
installation.BatteryUpdateStatus = "Running";
|
|
|
|
Db.Update(installation: installation);
|
2023-10-02 13:40:03 +00:00
|
|
|
var batteryIdsResult = await Db.ExecuteBufferedAsyncCommandOnIp(installationIp, $"dbus-send --system --dest=com.victronenergy.battery.{batteryTtyName} --type=method_call --print-reply / com.victronenergy.BusItem.GetText | grep -E -o '_Battery/[0-9]+/' | grep -E -o '[0-9]+'| sort -u");
|
|
|
|
|
|
|
|
var batteryIds = batteryIdsResult.Split("\n").ToList();
|
|
|
|
batteryIds.Pop();
|
|
|
|
foreach (var batteryId in batteryIds)
|
2023-05-25 07:18:41 +00:00
|
|
|
{
|
2023-08-23 09:31:30 +00:00
|
|
|
localCommand = localCommand.Append(
|
2023-10-02 13:40:03 +00:00
|
|
|
$" && /opt/innovenergy/scripts/upload-bms-firmware {batteryTtyName} {batteryId} /opt/innovenergy/bms-firmware/{FirmwareVersion}.bin");
|
2023-05-25 07:18:41 +00:00
|
|
|
}
|
2023-08-23 09:31:30 +00:00
|
|
|
#pragma warning disable CS4014
|
2023-10-02 13:40:03 +00:00
|
|
|
Console.WriteLine(localCommand);
|
2023-08-23 09:31:30 +00:00
|
|
|
Db.ExecuteBufferedAsyncCommandOnIp(installationIp, localCommand)
|
|
|
|
.ContinueWith(t =>
|
|
|
|
{
|
2023-10-02 13:40:03 +00:00
|
|
|
Console.WriteLine(t.Result);
|
2023-08-23 09:31:30 +00:00
|
|
|
installation.BatteryUpdateStatus = "Complete";
|
|
|
|
Db.Update(installation: installation);
|
|
|
|
UpdateVrmTagsToNewFirmware(installationIp);
|
|
|
|
});
|
|
|
|
#pragma warning restore CS4014
|
2023-07-27 11:24:14 +00:00
|
|
|
return "Battery update is successfully initiated, it will take around 15 minutes to complete! You can close this page now.";
|
2023-05-25 07:18:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 13:29:39 +00:00
|
|
|
private static async Task UpdateVrmTagsToNewFirmware(String installationIp)
|
|
|
|
{
|
|
|
|
var vrmInstallation = await FindVrmInstallationByIp(installationIp);
|
|
|
|
var tags = await vrmInstallation.GetTags();
|
|
|
|
|
|
|
|
async void RemoveTag(String t) => await vrmInstallation.RemoveTags(t);
|
|
|
|
|
|
|
|
tags.Where(tag => tag.StartsWith("FM-"))
|
|
|
|
.Do(RemoveTag);
|
|
|
|
|
|
|
|
await vrmInstallation.AddTags("FM-" + FirmwareVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async Task<VrmInstallation> FindVrmInstallationByIp(String installationIp)
|
|
|
|
{
|
|
|
|
var installationId = Db.Installations.Where(i => i.Ip == installationIp).Select(i => i.Vrm).First();
|
|
|
|
var vrmAccount = await Db.GetVrmAccount();
|
|
|
|
return await vrmAccount.GetInstallation(installationId!);
|
|
|
|
}
|
|
|
|
|
2023-08-23 09:31:30 +00:00
|
|
|
private static async Task SendNewBatteryFirmware(String installationIp)
|
2023-05-25 07:18:41 +00:00
|
|
|
{
|
2023-09-08 09:56:49 +00:00
|
|
|
await Cli.Wrap("rsync")
|
|
|
|
.WithArguments($@"-r {FirmwareVersion}.bin")
|
|
|
|
.AppendArgument($@"root@{installationIp}:/opt/innovenergy/bms-firmware/{FirmwareVersion}.bin")
|
2023-08-23 09:31:30 +00:00
|
|
|
.ExecuteAsync();
|
2023-05-25 07:18:41 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|