From 8d04d89eabc776819b85d0b0958d7bae6f59fe59 Mon Sep 17 00:00:00 2001 From: atef Date: Tue, 11 Jun 2024 11:39:45 +0200 Subject: [PATCH 1/2] Changed to Output string instead of input --- csharp/Lib/Devices/AMPT/AmptDevices.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/csharp/Lib/Devices/AMPT/AmptDevices.cs b/csharp/Lib/Devices/AMPT/AmptDevices.cs index 1674baffe..003e09b20 100644 --- a/csharp/Lib/Devices/AMPT/AmptDevices.cs +++ b/csharp/Lib/Devices/AMPT/AmptDevices.cs @@ -77,8 +77,8 @@ public class AmptDevices Current = busCurrent }; - // flatten the 2 strings of each SO into one array - var strings = soStati.SelectMany(GetStrings).ToArray(nStrings); + // flatten the output strings of each SO into one array + var strings = soStati.SelectMany(GetDc).ToArray(nStrings); return new AmptStatus { @@ -107,6 +107,16 @@ public class AmptDevices }; } + private static IEnumerable GetDc(StringOptimizerRegisters r) + { + // hardcoded: every SO has 2 strings (produced like this by AMPT) + + yield return new() + { + Voltage = r.Voltage, + Current = r.Current, + }; + } private static IEnumerable> StringOptimizers(ModbusClient modbusClient) { From 9e15be4aae106acd93705b339a86f8a7a6ebbb58 Mon Sep 17 00:00:00 2001 From: Yinyin Liu Date: Tue, 11 Jun 2024 14:31:08 +0200 Subject: [PATCH 2/2] Add Record User Action Backend --- csharp/App/Backend/Backend.sln | 25 +++++++++++++ csharp/App/Backend/Controller.cs | 35 ++++++++++++++++--- csharp/App/Backend/DataTypes/Configuration.cs | 4 +++ .../App/Backend/DataTypes/Methods/Session.cs | 22 ++++++++++++ csharp/App/Backend/DataTypes/UserAction.cs | 18 ++++++++++ csharp/App/Backend/Database/Create.cs | 31 ++++++++++++++++ csharp/App/Backend/Database/Db.cs | 5 +++ csharp/App/Backend/Database/Delete.cs | 15 ++++++++ 8 files changed, 150 insertions(+), 5 deletions(-) create mode 100644 csharp/App/Backend/Backend.sln create mode 100644 csharp/App/Backend/DataTypes/UserAction.cs diff --git a/csharp/App/Backend/Backend.sln b/csharp/App/Backend/Backend.sln new file mode 100644 index 000000000..c72530762 --- /dev/null +++ b/csharp/App/Backend/Backend.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Backend", "Backend.csproj", "{161624D7-33B9-48B8-BA05-303DCFAEB03E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {161624D7-33B9-48B8-BA05-303DCFAEB03E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {161624D7-33B9-48B8-BA05-303DCFAEB03E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {161624D7-33B9-48B8-BA05-303DCFAEB03E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {161624D7-33B9-48B8-BA05-303DCFAEB03E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A377D3C5-E56D-4A16-AC4B-F3B0BB4CFCCE} + EndGlobalSection +EndGlobal diff --git a/csharp/App/Backend/Controller.cs b/csharp/App/Backend/Controller.cs index 8afccf35e..b4dabe8cc 100644 --- a/csharp/App/Backend/Controller.cs +++ b/csharp/App/Backend/Controller.cs @@ -100,6 +100,24 @@ public class Controller : ControllerBase .ToList(); } + [HttpGet(nameof(GetHistoryForInstallation))] + public ActionResult> GetHistoryForInstallation(Int64 id, Token authToken) + { + var user = Db.GetSession(authToken)?.User; + if (user == null) + return Unauthorized(); + + var installation = Db.GetInstallationById(id); + + if (installation is null || !user.HasAccessTo(installation)) + return Unauthorized(); + + return Db.UserActions + .Where(action =>action.InstallationId == id) + .OrderByDescending(action => action.Timestamp) + .ToList(); + } + [HttpGet(nameof(GetAllWarningsForInstallation))] public ActionResult> GetAllWarningsForInstallation(Int64 id, Token authToken) { @@ -552,12 +570,19 @@ public class Controller : ControllerBase { var session = Db.GetSession(authToken); //Console.WriteLine(config.GridSetPoint); - - //var installationToUpdate = Db.GetInstallationById(installationId); - return await session.SendInstallationConfig(installationId, config) - ? Ok() - : Unauthorized(); + // Send configuration changes + var success = await session.SendInstallationConfig(installationId, config); + + // Record configuration change + if (success) + { + var actionSuccess = await session.RecordUserAction(installationId, config); + return actionSuccess?Ok():Unauthorized(); + } + + return Unauthorized(); + } [HttpPut(nameof(MoveFolder))] diff --git a/csharp/App/Backend/DataTypes/Configuration.cs b/csharp/App/Backend/DataTypes/Configuration.cs index 7bd6a830c..76ac6edbe 100644 --- a/csharp/App/Backend/DataTypes/Configuration.cs +++ b/csharp/App/Backend/DataTypes/Configuration.cs @@ -6,6 +6,10 @@ public class Configuration public Double GridSetPoint { get; set; } public CalibrationChargeType CalibrationChargeState { get; set; } public DateTime CalibrationChargeDate { get; set; } + public String GetConfigurationString() + { + return $"MinimumSoC: {MinimumSoC}, GridSetPoint: {GridSetPoint}, CalibrationChargeState: {CalibrationChargeState}, CalibrationChargeDate: {CalibrationChargeDate}"; + } } public enum CalibrationChargeType diff --git a/csharp/App/Backend/DataTypes/Methods/Session.cs b/csharp/App/Backend/DataTypes/Methods/Session.cs index f8ff5d571..bc829bdc8 100644 --- a/csharp/App/Backend/DataTypes/Methods/Session.cs +++ b/csharp/App/Backend/DataTypes/Methods/Session.cs @@ -101,6 +101,28 @@ public static class SessionMethods && user.HasAccessTo(installation) && await installation.SendConfig(configuration); } + + public static async Task RecordUserAction(this Session? session, Int64 installationId, Configuration newConfiguration) + { + var user = session?.User; + var timestamp = DateTime.Now; + + if (user is null || user.UserType == 0) + return false; + + // Create a new UserAction object + var action = new UserAction + { + UserName = user.Name, + InstallationId = installationId, + Timestamp = timestamp, + Description = newConfiguration.GetConfigurationString() + }; + + // Save the configuration change to the database + Db.HandleAction(action); + return true; + } public static Boolean Delete(this Session? session, Folder? folder) { diff --git a/csharp/App/Backend/DataTypes/UserAction.cs b/csharp/App/Backend/DataTypes/UserAction.cs new file mode 100644 index 000000000..9a20cff26 --- /dev/null +++ b/csharp/App/Backend/DataTypes/UserAction.cs @@ -0,0 +1,18 @@ +using SQLite; + +namespace InnovEnergy.App.Backend.DataTypes; + +public class UserAction +{ + [PrimaryKey, AutoIncrement] + public Int64 Id { get; set; } // Primary key for the table, auto-incremented + + [Indexed] + public String UserName { get; set; } = null!;// User Name who made the configuration change + + public Int64 InstallationId { get; set; } // Installation ID where the configuration change is made + + public DateTime Timestamp { get; set; } // Timestamp of the configuration change + + public String Description { get; set; } = null!;// Serialized string representing the new configuration +} \ No newline at end of file diff --git a/csharp/App/Backend/Database/Create.cs b/csharp/App/Backend/Database/Create.cs index d1b50e107..b25cc089f 100644 --- a/csharp/App/Backend/Database/Create.cs +++ b/csharp/App/Backend/Database/Create.cs @@ -62,6 +62,37 @@ public static partial class Db return Insert(o2i); } + public static Boolean Create(UserAction action) + { + return Insert(action); + } + + public static void HandleAction(UserAction newAction) + { + //Find the total number of actions for this installation + var totalActions = UserActions.Count(action => action.InstallationId == newAction.InstallationId); + + //If there are 100 actions, remove the one with the oldest timestamp + if (totalActions == 100) + { + var oldestAction = + UserActions.Where(action => action.InstallationId == newAction.InstallationId) + .OrderBy(action => action.Timestamp) + .FirstOrDefault(); + + //Remove the old action + Delete(oldestAction); + + //Add the new action + Create(newAction); + } + else + { + Console.WriteLine("---------------Added the new Error to the database-----------------"); + Create(newAction); + } + } + public static void HandleError(Error newError,int installationId) { //Find the total number of errors for this installation diff --git a/csharp/App/Backend/Database/Db.cs b/csharp/App/Backend/Database/Db.cs index 130c10649..9ec5ecc6d 100644 --- a/csharp/App/Backend/Database/Db.cs +++ b/csharp/App/Backend/Database/Db.cs @@ -36,6 +36,7 @@ public static partial class Db fileConnection.CreateTable(); fileConnection.CreateTable(); fileConnection.CreateTable(); + fileConnection.CreateTable(); return fileConnection; //return CopyDbToMemory(fileConnection); @@ -55,6 +56,7 @@ public static partial class Db memoryConnection.CreateTable(); memoryConnection.CreateTable(); memoryConnection.CreateTable(); + fileConnection.CreateTable(); //Copy all the existing tables from the disk to main memory fileConnection.Table().ForEach(memoryConnection.Insert); @@ -66,6 +68,7 @@ public static partial class Db fileConnection.Table().ForEach(memoryConnection.Insert); fileConnection.Table().ForEach(memoryConnection.Insert); fileConnection.Table().ForEach(memoryConnection.Insert); + fileConnection.Table().ForEach(memoryConnection.Insert); return memoryConnection; } @@ -85,6 +88,7 @@ public static partial class Db public static TableQuery OrderNumber2Installation => Connection.Table(); public static TableQuery Errors => Connection.Table(); public static TableQuery Warnings => Connection.Table(); + public static TableQuery UserActions => Connection.Table(); public static void Init() { @@ -106,6 +110,7 @@ public static partial class Db Connection.CreateTable(); Connection.CreateTable(); Connection.CreateTable(); + Connection.CreateTable(); }); //UpdateKeys(); diff --git a/csharp/App/Backend/Database/Delete.cs b/csharp/App/Backend/Database/Delete.cs index f50fa3761..d9ee41aab 100644 --- a/csharp/App/Backend/Database/Delete.cs +++ b/csharp/App/Backend/Database/Delete.cs @@ -1,6 +1,7 @@ using InnovEnergy.App.Backend.DataTypes; using InnovEnergy.App.Backend.DataTypes.Methods; using InnovEnergy.App.Backend.Relations; +using Microsoft.AspNetCore.Authentication.OAuth.Claims; namespace InnovEnergy.App.Backend.Database; @@ -49,6 +50,20 @@ public static partial class Db } } + public static Boolean Delete(UserAction actionToDelete) + { + var deleteSuccess = RunTransaction(DeleteAction); + if (deleteSuccess) + BackupDatabase(); + return deleteSuccess; + + + Boolean DeleteAction() + { + return UserActions.Delete(action => action.Id == actionToDelete.Id) >0; + } + } + public static Boolean Delete(Warning warningToDelete) { var deleteSuccess = RunTransaction(DeleteWarning);