Merge remote-tracking branch 'origin/main'

This commit is contained in:
Noe 2024-06-11 21:35:16 +02:00
commit 7bc1c6c435
9 changed files with 162 additions and 7 deletions

View File

@ -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

View File

@ -100,6 +100,24 @@ public class Controller : ControllerBase
.ToList(); .ToList();
} }
[HttpGet(nameof(GetHistoryForInstallation))]
public ActionResult<IEnumerable<UserAction>> 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))] [HttpGet(nameof(GetAllWarningsForInstallation))]
public ActionResult<IEnumerable<Warning>> GetAllWarningsForInstallation(Int64 id, Token authToken) public ActionResult<IEnumerable<Warning>> GetAllWarningsForInstallation(Int64 id, Token authToken)
{ {
@ -553,11 +571,18 @@ public class Controller : ControllerBase
var session = Db.GetSession(authToken); var session = Db.GetSession(authToken);
//Console.WriteLine(config.GridSetPoint); //Console.WriteLine(config.GridSetPoint);
//var installationToUpdate = Db.GetInstallationById(installationId); // 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();
return await session.SendInstallationConfig(installationId, config)
? Ok()
: Unauthorized();
} }
[HttpPut(nameof(MoveFolder))] [HttpPut(nameof(MoveFolder))]

View File

@ -6,6 +6,10 @@ public class Configuration
public Double GridSetPoint { get; set; } public Double GridSetPoint { get; set; }
public CalibrationChargeType CalibrationChargeState { get; set; } public CalibrationChargeType CalibrationChargeState { get; set; }
public DateTime CalibrationChargeDate { get; set; } public DateTime CalibrationChargeDate { get; set; }
public String GetConfigurationString()
{
return $"MinimumSoC: {MinimumSoC}, GridSetPoint: {GridSetPoint}, CalibrationChargeState: {CalibrationChargeState}, CalibrationChargeDate: {CalibrationChargeDate}";
}
} }
public enum CalibrationChargeType public enum CalibrationChargeType

View File

@ -102,6 +102,28 @@ public static class SessionMethods
&& await installation.SendConfig(configuration); && await installation.SendConfig(configuration);
} }
public static async Task<Boolean> 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) public static Boolean Delete(this Session? session, Folder? folder)
{ {
var user = session?.User; var user = session?.User;

View File

@ -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
}

View File

@ -62,6 +62,37 @@ public static partial class Db
return Insert(o2i); 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) public static void HandleError(Error newError,int installationId)
{ {
//Find the total number of errors for this installation //Find the total number of errors for this installation

View File

@ -36,6 +36,7 @@ public static partial class Db
fileConnection.CreateTable<OrderNumber2Installation>(); fileConnection.CreateTable<OrderNumber2Installation>();
fileConnection.CreateTable<Error>(); fileConnection.CreateTable<Error>();
fileConnection.CreateTable<Warning>(); fileConnection.CreateTable<Warning>();
fileConnection.CreateTable<UserAction>();
return fileConnection; return fileConnection;
//return CopyDbToMemory(fileConnection); //return CopyDbToMemory(fileConnection);
@ -55,6 +56,7 @@ public static partial class Db
memoryConnection.CreateTable<OrderNumber2Installation>(); memoryConnection.CreateTable<OrderNumber2Installation>();
memoryConnection.CreateTable<Error>(); memoryConnection.CreateTable<Error>();
memoryConnection.CreateTable<Warning>(); memoryConnection.CreateTable<Warning>();
fileConnection.CreateTable<UserAction>();
//Copy all the existing tables from the disk to main memory //Copy all the existing tables from the disk to main memory
fileConnection.Table<Session>().ForEach(memoryConnection.Insert); fileConnection.Table<Session>().ForEach(memoryConnection.Insert);
@ -66,6 +68,7 @@ public static partial class Db
fileConnection.Table<OrderNumber2Installation>().ForEach(memoryConnection.Insert); fileConnection.Table<OrderNumber2Installation>().ForEach(memoryConnection.Insert);
fileConnection.Table<Error>().ForEach(memoryConnection.Insert); fileConnection.Table<Error>().ForEach(memoryConnection.Insert);
fileConnection.Table<Warning>().ForEach(memoryConnection.Insert); fileConnection.Table<Warning>().ForEach(memoryConnection.Insert);
fileConnection.Table<UserAction>().ForEach(memoryConnection.Insert);
return memoryConnection; return memoryConnection;
} }
@ -85,6 +88,7 @@ public static partial class Db
public static TableQuery<OrderNumber2Installation> OrderNumber2Installation => Connection.Table<OrderNumber2Installation>(); public static TableQuery<OrderNumber2Installation> OrderNumber2Installation => Connection.Table<OrderNumber2Installation>();
public static TableQuery<Error> Errors => Connection.Table<Error>(); public static TableQuery<Error> Errors => Connection.Table<Error>();
public static TableQuery<Warning> Warnings => Connection.Table<Warning>(); public static TableQuery<Warning> Warnings => Connection.Table<Warning>();
public static TableQuery<UserAction> UserActions => Connection.Table<UserAction>();
public static void Init() public static void Init()
{ {
@ -106,6 +110,7 @@ public static partial class Db
Connection.CreateTable<OrderNumber2Installation>(); Connection.CreateTable<OrderNumber2Installation>();
Connection.CreateTable<Error>(); Connection.CreateTable<Error>();
Connection.CreateTable<Warning>(); Connection.CreateTable<Warning>();
Connection.CreateTable<UserAction>();
}); });
//UpdateKeys(); //UpdateKeys();

View File

@ -1,6 +1,7 @@
using InnovEnergy.App.Backend.DataTypes; using InnovEnergy.App.Backend.DataTypes;
using InnovEnergy.App.Backend.DataTypes.Methods; using InnovEnergy.App.Backend.DataTypes.Methods;
using InnovEnergy.App.Backend.Relations; using InnovEnergy.App.Backend.Relations;
using Microsoft.AspNetCore.Authentication.OAuth.Claims;
namespace InnovEnergy.App.Backend.Database; 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) public static Boolean Delete(Warning warningToDelete)
{ {
var deleteSuccess = RunTransaction(DeleteWarning); var deleteSuccess = RunTransaction(DeleteWarning);

View File

@ -77,8 +77,8 @@ public class AmptDevices
Current = busCurrent Current = busCurrent
}; };
// flatten the 2 strings of each SO into one array // flatten the output strings of each SO into one array
var strings = soStati.SelectMany(GetStrings).ToArray(nStrings); var strings = soStati.SelectMany(GetDc).ToArray(nStrings);
return new AmptStatus return new AmptStatus
{ {
@ -107,6 +107,16 @@ public class AmptDevices
}; };
} }
private static IEnumerable<DcBus> 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<ModbusDevice<StringOptimizerRegisters>> StringOptimizers(ModbusClient modbusClient) private static IEnumerable<ModbusDevice<StringOptimizerRegisters>> StringOptimizers(ModbusClient modbusClient)
{ {