Added updating battery firmware

This commit is contained in:
Kim 2023-05-25 14:49:40 +02:00
parent ec4d76f8fd
commit 92721376a6
5 changed files with 27 additions and 22 deletions

View File

@ -19,7 +19,9 @@ public record InstallationToHtmlInterface(
String Online,
String LastSeen,
Int64 NumBatteries,
String BatteryVersion
String BatteryVersion,
String ServerIp = "10.2.0.1", //TODO MAKE ME DYNAMIC
String FirmwareVersion = "AF09" //Todo automatically grab newest version?
);
[Controller]
@ -27,7 +29,7 @@ public class Controller : ControllerBase
{
//Todo automatically grab newest version?
private const String FirmwareVersion = "VERSION";
private const String FirmwareVersion = "AF09";
[HttpGet]
@ -99,7 +101,7 @@ public class Controller : ControllerBase
<td>{{Serial}}</td>
<td>{{NumBatteries}}</td>
<td>{{BatteryVersion}}</td>
<td><a target='_blank' href=http://{{serverIp}}/UpdateBatteryFirmware/{{Ip}}>⬆️{{firmwareVersion}}</a></td>
<td><a target='_blank' href=http://{{ServerIp}}/UpdateBatteryFirmware/{{Ip}}/{{NumBatteries}}>⬆️{{FirmwareVersion}}</a></td>
</tr>";
var installationsInDb = Db.Installations.ToList();
@ -126,8 +128,6 @@ public class Controller : ControllerBase
var data = new
{
inst = installsForHtml,
serverIp = "10.2.0.1", //TODO MAKE ME DYNAMIC
firmwareVersion = FirmwareVersion
};
var result = template(data);
@ -140,22 +140,25 @@ public class Controller : ControllerBase
}
[HttpGet(nameof(UpdateBatteryFirmware))]
public async Task<ActionResult> UpdateBatteryFirmware(String installationIp, Int64 numOfBatteries)
[HttpGet("updatebatteryfirmware/{installationIp}/{numberOfBatteries}")]
public async Task<String> UpdateBatteryFirmware(String installationIp, String numberOfBatteries)
{
//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();
if (pathToBattery.Split('"')[1] == "Failed") return "Update failed";
SendNewBatteryFirmware(installationIp);
for (var batteryId = 0; batteryId < numOfBatteries; batteryId++)
for (var batteryId = 2; batteryId <= Int64.Parse(numberOfBatteries) + 1; 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();
var batteryTtyName = pathToBattery.Split('"')[1].Split(".").Last();
var localCommand = $"/opt/innovenergy/scripts/upload-bms-firmware {batteryTtyName} {batteryId} /opt/innovenergy/bms-firmware/{FirmwareVersion}.bin";
var remoteUpdateCommandResult = Db.ExecuteBufferedAsyncCommandOnIp(installationIp, localCommand);
// Console.WriteLine(remoteUpdateCommandResult); //Todo replace me with checking if it worked...
}
return new AcceptedResult();
return "Battery update is successfully initiated! You can close this page now.";
}
private static void SendNewBatteryFirmware(String installationIp)

View File

@ -8,6 +8,7 @@ using CliWrap.Buffered;
using InnovEnergy.App.RemoteSupportConsole;
using InnovEnergy.Lib.Utils;
using InnovEnergy.Lib.Victron.VictronVRM;
using Microsoft.AspNetCore.Mvc.TagHelpers.Cache;
using SQLite;
using static System.Text.Json.JsonSerializer;
using Installation = InnovEnergy.App.VrmGrabber.DataTypes.Installation;
@ -59,7 +60,7 @@ public static partial class Db
do {
await UpdateInstallationsAndDetailsFromVrm(0);
}
while (true) ;
while (false) ; //Todo change to true before
}
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
@ -72,7 +73,7 @@ public static partial class Db
var installations = await user.GetInstallations();
// var returnDictionary = new Dictionary<VrmInstallation, InstallationDetails>();
foreach (var installation in installations) //TODO REMOVE TAKE
foreach (var installation in installations.Skip(404).Take(3)) //TODO REMOVE TAKE
{
Console.WriteLine(installation.Name);
var details = await GetInstallationDetails(installation);
@ -109,11 +110,12 @@ public static partial class Db
if (ip is null or "Unknown" || online == "❌") return "Unknown";
var pathToBattery = await ExecuteBufferedAsyncCommandOnIp(ip, "dbus-send --system --dest=com.victronenergy.system --type=method_call --print-reply /ServiceMapping/com_victronenergy_battery_1 com.victronenergy.BusItem.GetText");
if (pathToBattery.StartsWith("Error")) return "Unknown";
if (pathToBattery.Split('"')[1].StartsWith("Error")) return "Unknown";
var command = $"dbus-send --system --dest={pathToBattery} --type=method_call --print-reply /FirmwareVersion com.victronenergy.BusItem.GetText";
return await ExecuteBufferedAsyncCommandOnIp(ip, command);
var command = $"dbus-send --system --dest={pathToBattery.Split('"')[1]} --type=method_call --print-reply /FirmwareVersion com.victronenergy.BusItem.GetText";
var returnString = await ExecuteBufferedAsyncCommandOnIp(ip, command);
return returnString.Split('"')[1];
}
private static async Task<Int64> NumberOfBatteries(String? ip, String? online)
@ -121,11 +123,11 @@ public static partial class Db
if (ip is null or "Unknown" || online == "❌") return 0;
var pathToBattery = await ExecuteBufferedAsyncCommandOnIp(ip, "dbus-send --system --dest=com.victronenergy.system --type=method_call --print-reply /ServiceMapping/com_victronenergy_battery_1 com.victronenergy.BusItem.GetText");
if (pathToBattery.StartsWith("Error")) return 0;
if (pathToBattery.Split('"')[1].StartsWith("Error")) return 0;
var cmd = await ExecuteBufferedAsyncCommandOnIp(ip,$"dbus-send --system --dest={pathToBattery} --type=method_call --print-reply /NbOfBatteries com.victronenergy.BusItem.GetText" );
var cmd = await ExecuteBufferedAsyncCommandOnIp(ip,$"dbus-send --system --dest={pathToBattery.Split('"')[1]} --type=method_call --print-reply /NbOfBatteries com.victronenergy.BusItem.GetText" );
return cmd == "Failed" ? 0 : Int64.Parse(cmd); //No Batteries can be found
return cmd.Split('"')[1] == "Failed" ? 0 : Int64.Parse(cmd.Split('"')[1]); //No Batteries can be found
}
@ -138,7 +140,7 @@ public static partial class Db
.AppendArgument("-o StrictHostKeyChecking=accept-new")
.AppendArgument(command)
.WithValidation(CommandResultValidation.None).ExecuteBufferedAsync();
return cmd.ExitCode == 0 ? cmd.StandardOutput.Split('"')[1] : cmd.StandardError;
return cmd.ExitCode == 0 ? cmd.StandardOutput : cmd.StandardError;
}
private static String?[] Ip(InstallationDetails details)

View File

@ -13,7 +13,6 @@ public static class Program
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", OpenApiInfo);
c.UseAllOfToExtendReferenceSchemas();
c.SupportNonNullableReferenceTypes();
});

Binary file not shown.

View File

@ -84,6 +84,7 @@ def init_modbus(tty):
def failed(response):
# type: (ModbusResponse) -> bool
# Todo 'ModbusIOException' object has no attribute 'function_code'
return response.function_code > 0x80