From 3a3f2fe1b81e83cfc2c623175524f7aa43adec12 Mon Sep 17 00:00:00 2001 From: Noe Date: Tue, 19 Mar 2024 16:41:36 +0100 Subject: [PATCH] Update backend to receive calls for updating the firmware of a battery --- csharp/App/Backend/Controller.cs | 20 ++++++++++++++--- .../App/Backend/DataTypes/Methods/Session.cs | 22 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/csharp/App/Backend/Controller.cs b/csharp/App/Backend/Controller.cs index cd8091d79..2550edd40 100644 --- a/csharp/App/Backend/Controller.cs +++ b/csharp/App/Backend/Controller.cs @@ -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>> EditInstallationConfig([FromBody] Configuration config, Int64 installationId, Token authToken) diff --git a/csharp/App/Backend/DataTypes/Methods/Session.cs b/csharp/App/Backend/DataTypes/Methods/Session.cs index 4b01788ba..4a9900af3 100644 --- a/csharp/App/Backend/DataTypes/Methods/Session.cs +++ b/csharp/App/Backend/DataTypes/Methods/Session.cs @@ -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 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 SendInstallationConfig(this Session? session, Int64 installationId, Configuration configuration) {