2023-03-15 13:38:06 +00:00
|
|
|
using InnovEnergy.App.Backend.DataTypes;
|
|
|
|
using InnovEnergy.App.Backend.Relations;
|
|
|
|
|
|
|
|
|
|
|
|
namespace InnovEnergy.App.Backend.Database;
|
|
|
|
|
|
|
|
|
2024-07-02 14:53:51 +00:00
|
|
|
public static partial class Db
|
2023-03-15 13:38:06 +00:00
|
|
|
{
|
2024-12-16 14:03:27 +00:00
|
|
|
|
2023-07-13 11:23:05 +00:00
|
|
|
private static Boolean Insert(Object obj)
|
|
|
|
{
|
|
|
|
var success = Connection.Insert(obj) > 0;
|
2024-12-16 14:03:27 +00:00
|
|
|
if (success) Backup();
|
2023-07-13 11:23:05 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
public static Boolean Create(Installation installation)
|
|
|
|
{
|
2024-12-16 14:03:27 +00:00
|
|
|
// The bucket Id it calculated as follows: It is 1 + the maximum bucket id of all the existing installations of the same product
|
2023-03-15 13:38:06 +00:00
|
|
|
// SQLite wrapper is smart and *modifies* t's Id to the one generated (autoincrement) by the insertion
|
2024-12-16 14:03:27 +00:00
|
|
|
installation.S3BucketId = Installations.Where(inst => inst.Product == installation.Product).Max(inst => (int?)inst.S3BucketId)+1 ?? 0;
|
2023-07-13 11:23:05 +00:00
|
|
|
return Insert(installation);
|
2023-03-15 13:38:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 16:22:42 +00:00
|
|
|
public static Boolean Create(Error error)
|
|
|
|
{
|
|
|
|
return Insert(error);
|
|
|
|
}
|
|
|
|
|
2023-11-20 16:29:45 +00:00
|
|
|
public static Boolean Create(Warning warning)
|
|
|
|
{
|
|
|
|
return Insert(warning);
|
|
|
|
}
|
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
public static Boolean Create(Folder folder)
|
|
|
|
{
|
2023-07-13 11:23:05 +00:00
|
|
|
return Insert(folder);
|
2023-03-15 13:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Boolean Create(User user)
|
|
|
|
{
|
2023-07-13 11:23:05 +00:00
|
|
|
return Insert(user);
|
2023-03-15 13:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Boolean Create(Session session)
|
|
|
|
{
|
2023-07-13 11:23:05 +00:00
|
|
|
return Insert(session);
|
2023-03-15 13:38:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 08:25:36 +00:00
|
|
|
public static Boolean Create(InstallationAccess installationAccess)
|
2023-03-16 08:15:59 +00:00
|
|
|
{
|
2023-07-13 11:23:05 +00:00
|
|
|
return Insert(installationAccess);
|
2023-03-16 08:15:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 08:25:36 +00:00
|
|
|
public static Boolean Create(FolderAccess folderAccess)
|
2023-03-16 08:15:59 +00:00
|
|
|
{
|
2023-07-13 11:23:05 +00:00
|
|
|
return Insert(folderAccess);
|
2023-03-16 08:15:59 +00:00
|
|
|
}
|
2023-03-23 13:58:59 +00:00
|
|
|
|
|
|
|
public static Boolean Create(OrderNumber2Installation o2i)
|
|
|
|
{
|
2023-07-13 11:23:05 +00:00
|
|
|
return Insert(o2i);
|
2023-03-23 13:58:59 +00:00
|
|
|
}
|
2023-11-15 16:22:42 +00:00
|
|
|
|
2024-06-11 12:31:08 +00:00
|
|
|
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
|
|
|
|
{
|
2024-06-18 14:19:40 +00:00
|
|
|
Console.WriteLine("---------------Added the new Action to the database-----------------");
|
2024-06-11 12:31:08 +00:00
|
|
|
Create(newAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-16 14:03:27 +00:00
|
|
|
//This function is called from the RabbitMQ manager when a new error arrives to the database.
|
|
|
|
//We keep only the last 100 errors for each installation. If we already have stored 100 errors, we delete the older one and we insert the new one.
|
2024-07-18 07:37:40 +00:00
|
|
|
|
2023-11-15 16:22:42 +00:00
|
|
|
public static void HandleError(Error newError,int installationId)
|
|
|
|
{
|
|
|
|
//Find the total number of errors for this installation
|
|
|
|
var totalErrors = Errors.Count(error => error.InstallationId == installationId);
|
|
|
|
|
|
|
|
//If there are 100 errors, remove the one with the oldest timestamp
|
|
|
|
if (totalErrors == 100)
|
|
|
|
{
|
|
|
|
var oldestError =
|
|
|
|
Errors.Where(error => error.InstallationId == installationId)
|
2023-11-22 08:35:29 +00:00
|
|
|
.OrderBy(error => error.Date)
|
2023-11-15 16:22:42 +00:00
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
//Remove the old error
|
|
|
|
Delete(oldestError);
|
|
|
|
|
|
|
|
//Add the new error
|
|
|
|
Create(newError);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-12-16 14:03:27 +00:00
|
|
|
Console.WriteLine("---------------Added the new Alarm to the database-----------------");
|
2023-11-15 16:22:42 +00:00
|
|
|
Create(newError);
|
|
|
|
}
|
|
|
|
}
|
2023-11-20 16:29:45 +00:00
|
|
|
|
|
|
|
public static void HandleWarning(Warning newWarning,int installationId)
|
|
|
|
{
|
|
|
|
//Find the total number of warnings for this installation
|
|
|
|
var totalWarnings = Warnings.Count(warning => warning.InstallationId == installationId);
|
|
|
|
|
|
|
|
//If there are 100 warnings, remove the one with the oldest timestamp
|
|
|
|
if (totalWarnings == 100)
|
|
|
|
{
|
|
|
|
var oldestWarning =
|
|
|
|
Warnings.Where(warning => warning.InstallationId == installationId)
|
2023-11-22 08:35:29 +00:00
|
|
|
.OrderBy(warning => warning.Date)
|
2023-11-20 16:29:45 +00:00
|
|
|
.FirstOrDefault();
|
|
|
|
|
2024-12-16 14:03:27 +00:00
|
|
|
//Remove the old warning
|
2023-11-20 16:29:45 +00:00
|
|
|
Delete(oldestWarning);
|
|
|
|
|
2024-12-16 14:03:27 +00:00
|
|
|
//Add the new warning
|
2023-11-20 16:29:45 +00:00
|
|
|
Create(newWarning);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-21 09:42:24 +00:00
|
|
|
Console.WriteLine("---------------Added the new Warning to the database-----------------");
|
2023-11-20 16:29:45 +00:00
|
|
|
Create(newWarning);
|
|
|
|
}
|
|
|
|
}
|
2024-06-26 15:05:27 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
}
|