Update backend to receive calls for updating the firmware of a battery

This commit is contained in:
Noe 2024-03-19 16:41:36 +01:00
parent a4e3683082
commit 3a3f2fe1b8
2 changed files with 39 additions and 3 deletions

View File

@ -1,7 +1,4 @@
using System.Net;
using System.Net.WebSockets;
using System.Text;
using System.Text.Json;
using InnovEnergy.App.Backend.Database;
using InnovEnergy.App.Backend.DataTypes;
using InnovEnergy.App.Backend.DataTypes.Methods;
@ -516,6 +513,23 @@ public class Controller : ControllerBase
: Unauthorized();
}
[HttpPost(nameof(UpdateFirmware))]
public ActionResult UpdateFirmware(Int64 batteryNode, Int64 installationId,Token authToken)
{
var session = Db.GetSession(authToken);
var installationToUpdate = Db.GetInstallationById(installationId);
Console.WriteLine("Inside firmware function controller,batteryNode="+batteryNode+ "and installation is "+installationId);
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
if (installationToUpdate != null)
{
session.RunScriptInBackground(installationToUpdate.VpnIp, batteryNode);
}
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
return Ok();
}
[HttpPost(nameof(EditInstallationConfig))]
public async Task<ActionResult<IEnumerable<Object>>> EditInstallationConfig([FromBody] Configuration config, Int64 installationId, Token authToken)

View File

@ -1,3 +1,4 @@
using System.Diagnostics;
using InnovEnergy.App.Backend.Database;
using InnovEnergy.App.Backend.Relations;
using InnovEnergy.Lib.Utils;
@ -65,6 +66,27 @@ public static class SessionMethods
.Do(() => installation.ParentId = parentId)
.Apply(Db.Update);
}
public static async Task<Boolean> RunScriptInBackground(this Session? session, String vpnIp, Int64 batteryNode)
{
string scriptPath = "/home/ubuntu/backend/uploadBatteryFw/update_firmware.sh";
await Task.Run(() =>
{
Process process = new Process();
process.StartInfo.FileName = "/bin/bash";
process.StartInfo.Arguments = $"{scriptPath} {vpnIp} {batteryNode}"; // Pass batteryNode as argument to the script
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.WaitForExit(); // This can be removed if you don't need to wait for the process to finish
});
return true;
}
public static async Task<Boolean> SendInstallationConfig(this Session? session, Int64 installationId, Configuration configuration)
{