Files
Aperio_Control_Centre/Aperio_Control_Centre.ACSDatabase/Models/Extensions/LoggingExtension.cs
Martijn Scheepers 5961276657 Restructure aadp
2025-08-13 15:13:22 +02:00

275 lines
11 KiB
C#

using Aperio_Control_Centre.Aadp.Enumerations;
using Aperio_Control_Centre.ACSDatabase.Types;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
using System.Security.Principal;
namespace Aperio_Control_Centre.ACSDatabase.Models.Extensions
{
public static class LoggingExtensions
{
public static async Task AddLogAsync<T>(this DbSet<Logging> loggings, Device device, T state, CancellationToken token) where T : Enum
{
ArgumentNullException.ThrowIfNull(device);
switch (state)
{
case ConnectionStates:
if (device.ConnectionState == ConnectionStates.Online)
{
await loggings.AddLogAsync(device, "Apparaat online", token);
}
if (device.ConnectionState == ConnectionStates.Offline)
{
await loggings.AddLogAsync(device, "Apparaat offline", token);
}
break;
case BatteryStates:
if (device.BatteryState == BatteryStates.Ok)
{
await loggings.AddLogAsync(device, "Batterij ok", token);
}
if (device.BatteryState == BatteryStates.Low)
{
await loggings.AddLogAsync(device, "Batterij laag", token);
}
if (device.BatteryState == BatteryStates.Flat)
{
await loggings.AddLogAsync(device, "Batterij leeg", token);
}
break;
case LockState:
switch (state)
{
case LockState.UNKNOWN:
case LockState.UNLOCKED:
case LockState.LOCKED:
case LockState.SECURED:
case LockState.INDETERMINATE:
await loggings.AddLogAsync(device, $"Slot ok ({state})", token);
break;
case LockState.JAMMED:
await loggings.AddLogAsync(device, "Slot geblokkeerd", token);
break;
default:
break;
}
break;
case DoorState:
await loggings.AddLogAsync(device, $"Doorstate ({state})", token);
break;
case KeyCylinderState:
await loggings.AddLogAsync(device, $"KeyCylinderState ({state})", token);
break;
case HandleState:
await loggings.AddLogAsync(device, $"HandleState ({state})", token);
break;
case TamperState:
switch (state)
{
case TamperState.UNKNOWN:
await loggings.AddLogAsync(device, "Tamper onbekend", token);
break;
case TamperState.TAMPER:
await loggings.AddLogAsync(device, "Tamper", token);
break;
case TamperState.NO_TAMPER:
await loggings.AddLogAsync(device, "No Tamper", token);
break;
default:
break;
}
break;
case DoorMode:
await loggings.AddLogAsync(device, $"DoorMode ({state})", token);
break;
case ProductClass:
await loggings.AddLogAsync(device, $"ProductClass ({state})", token);
break;
case Status:
await loggings.AddLogAsync(device, $"device Status ({state})", token);
break;
default:
Debug.WriteLine($"AddLogAsync state {state}");
break;
}
}
public static async Task AddLogAsync(this DbSet<Logging> loggings, Device device, ActivatorTypes type, ActivatorState state, CancellationToken token)
{
string activator = string.Empty;
switch (type)
{
case ActivatorTypes.EmergencyInside:
activator = "binnenzijde";
break;
case ActivatorTypes.EmergencyOutside:
activator = "buitenzijde";
break;
}
if (state == ActivatorState.NOT_USED)
{
await loggings.AddLogAsync(device, $"Breekruit {activator} herstelt", token);
}
else
{
await loggings.AddLogAsync(device, $"Breekruit {activator} geactiveerd", token);
}
}
public static void AddLog(this DbSet<Logging> loggings, string msg, LogTypes type)
{
ArgumentNullException.ThrowIfNull(loggings);
Logging log = new()
{
Time = DateTime.UtcNow,
LogType = type,
Message = msg
};
loggings.Add(log);
}
public static async Task AddLogAsync(this DbSet<Logging> loggings, string msg, LogTypes type, CancellationToken token)
{
ArgumentNullException.ThrowIfNull(loggings);
Logging log = new()
{
Time = DateTime.UtcNow,
LogType = type,
Message = msg
};
await loggings.AddAsync(log, token);
}
public static async Task AddLogAsync(this DbSet<Logging> loggings, Device? device, string msg, CancellationToken token)
{
ArgumentNullException.ThrowIfNull(loggings);
Logging log = new()
{
Time = DateTime.UtcNow,
LogType = LogTypes.Device,
Device = device,
Message = msg
};
await loggings.AddAsync(log, token);
}
public static async Task AddLogAsync(this DbSet<Logging> loggings, Device? dev, string msg, LogTypes type, CancellationToken token)
{
ArgumentNullException.ThrowIfNull(loggings);
Logging log = new()
{
Time = DateTime.UtcNow,
LogType = type,
Device = dev,
Message = msg
};
await loggings.AddAsync(log, token);
}
public static async Task AddLogAsync(this DbSet<Logging> loggings, IIdentity? identity, string msg, LogTypes logType, CancellationToken token)
{
ArgumentNullException.ThrowIfNull(loggings);
ArgumentNullException.ThrowIfNull(identity);
Logging log = new()
{
Time = DateTime.UtcNow,
UserName = identity.Name,
Message = msg,
LogType = logType
};
await loggings.AddAsync(log, token);
}
public static async Task AddUserLogAsync(this DbSet<Logging> loggings, Device? device, User? user, AccessTypes access, string msg, CancellationToken token)
{
ArgumentNullException.ThrowIfNull(loggings);
Logging log = new()
{
Time = DateTime.UtcNow,
LogType = LogTypes.User,
Device = device,
User = user,
Access = access,
//CredentialString = user?.CredentialString,
CredentialType = user?.CredentialType,
Message = msg
};
await loggings.AddAsync(log, token);
}
public static async Task AddUnknownUserLogAsync(this DbSet<Logging> loggings, Device? device, string cred, CredentialTypes credtype, string msg, CancellationToken token)
{
ArgumentNullException.ThrowIfNull(loggings);
Logging log = new()
{
Time = DateTime.UtcNow,
LogType = LogTypes.UnknownUser,
Device = device,
CredentialString = cred,
CredentialType = credtype,
Message = msg
};
await loggings.AddAsync(log, token);
}
//public static async Task AddLogAsync(this ACSDatabaseContext db, IIdentity? identity, User user, string msg, LogTypes logType, CancellationToken token)
//{
// ArgumentNullException.ThrowIfNull(db);
// ArgumentNullException.ThrowIfNull(identity);
// ArgumentNullException.ThrowIfNull(user);
// Logging log = new()
// {
// Time = DateTime.UtcNow,
// UserName = identity.Name,
// Message = msg,
// LogType = logType,
// CredentialString = user.CredentialString,
// CredentialType = user.CredentialType,
// UserId = db.Entry(user).Property(i => i.Id).IsTemporary ? db.Entry(user).Property(i => i.Id).CurrentValue : user.Id
// };
// await db.Loggings.AddAsync(log, token);
//}
//public static async Task AddLogAsync(this ACSDatabaseContext db, IIdentity? identity, Device device, string msg, LogTypes logType, CancellationToken token)
//{
// ArgumentNullException.ThrowIfNull(db);
// ArgumentNullException.ThrowIfNull(identity);
// ArgumentNullException.ThrowIfNull(device);
// Logging log = new()
// {
// Time = DateTime.UtcNow,
// UserName = identity.Name,
// Message = msg,
// LogType = logType,
// DeviceId = db.Entry(device).Property(i => i.Id).IsTemporary ? db.Entry(device).Property(i => i.Id).CurrentValue : device.Id
// };
// await db.Loggings.AddAsync(log, token);
//}
public static Task<List<Logging>> LastUserLoggings(this DbSet<Logging> loggings, User user, int count, CancellationToken token)
{
return loggings
.Where(l => l.UserId == user.Id)
.OrderByDescending(t => t.Time)
.Take(count)
.Include(d => d.Device)
.Include(d => d.Device.Location)
.ToListAsync(token);
}
public static Task<List<Logging>> LastDeviceLoggings(this DbSet<Logging> loggings, Device device, int count, CancellationToken token)
{
return loggings
.Where(l => l.DeviceId == device.Id)
.OrderByDescending(t => t.Time)
.Take(count)
.Include(d => d.User)
.ToListAsync(token);
}
}
}