Update 'access_level_use_limit_c#/AlsResetScript.cs'
This commit is contained in:
parent
869834833c
commit
dd528e0d45
|
@ -27,10 +27,10 @@ private readonly Interval[] _accessIntervals =
|
|||
new() {Start = new TimeSpan(16, 25, 0), End = new TimeSpan(16, 26, 0), Name = "Dinner"}
|
||||
};
|
||||
|
||||
// Set a list of doors that will be monitored
|
||||
// Set a list of door names that will be monitored
|
||||
private readonly string[] _canteenDoors =
|
||||
{
|
||||
"Mr-1&3", "CANTEEN-DOOR-R-TUR-2 Ent"
|
||||
"Carteen_Door_1", "Carteen_Door_2"
|
||||
};
|
||||
|
||||
private string _path;
|
||||
|
@ -44,6 +44,14 @@ private Timer _timer;
|
|||
|
||||
private static readonly ConcurrentDictionary<int, IList<string>> _failedAddOrUpdate = new();
|
||||
|
||||
/// <summary>
|
||||
/// True -> remove last access granted user al with door open event
|
||||
/// False -> remove user al with access granted event
|
||||
/// </summary>
|
||||
private const bool RemoveUserAlThenDoorOpened = false;
|
||||
|
||||
private int? _lastAccessGrantedUserId;
|
||||
|
||||
private async Task ResetUsersAccessLevelsAsync()
|
||||
{
|
||||
try
|
||||
|
@ -198,7 +206,29 @@ private void AddFromFailed(Dictionary<int, IList<string>> users)
|
|||
|
||||
private async void EventReceived(Event received)
|
||||
{
|
||||
if (received.TypeUid == "891adc81-6991-44c2-8aa4-f7a0792f77f5" && _canteenDoors.Contains(received.DoorName))
|
||||
if (_canteenDoors.Contains(received.DoorName) is false) return;
|
||||
|
||||
if (received.TypeUid == "891adc81-6991-44c2-8aa4-f7a0792f77f5")
|
||||
{
|
||||
if (received.UserId.HasValue)
|
||||
{
|
||||
_lastAccessGrantedUserId = received.UserId.Value;
|
||||
|
||||
if (RemoveUserAlThenDoorOpened) return;
|
||||
|
||||
await RemoveUserAccessLevelAsync(received);
|
||||
}
|
||||
}
|
||||
// Door opened
|
||||
else if (RemoveUserAlThenDoorOpened && received.TypeUid == "293F433D-F23E-4D33-B93C-BFD7C72763E6")
|
||||
{
|
||||
await RemoveUserAccessLevelAsync(received);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RemoveUserAccessLevelAsync(Event received)
|
||||
{
|
||||
if (_lastAccessGrantedUserId.HasValue)
|
||||
{
|
||||
var accessTime = received.Time.ToDateTime().ToLocalTime().TimeOfDay;
|
||||
|
||||
|
@ -211,24 +241,26 @@ private async void EventReceived(Event received)
|
|||
return;
|
||||
}
|
||||
|
||||
var userAccessLevels = await Context.GetUserAccessLevelsAsync((int)received.UserId);
|
||||
var userAccessLevels = await Context.GetUserAccessLevelsAsync(_lastAccessGrantedUserId.Value);
|
||||
|
||||
var userAccessLevel = userAccessLevels.FirstOrDefault(a => a.Name == interval.Name);
|
||||
|
||||
if (userAccessLevel is null)
|
||||
{
|
||||
Context.LogWarning($"Al for user with Id: {(int)received.UserId} not found");
|
||||
Context.LogWarning($"Al for user with Id: {_lastAccessGrantedUserId.Value} not found");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await Context.DeleteUserAccessLevelAsync(userAccessLevel.Id);
|
||||
|
||||
Context.LogDebug($"Al: {userAccessLevel.Name} removed for user with Id: {(int)received.UserId}");
|
||||
Context.LogDebug($"Al: {userAccessLevel.Name} removed for user with Id: {_lastAccessGrantedUserId.Value}");
|
||||
|
||||
AddOrUpdateUser((int)received.UserId, userAccessLevel.Name);
|
||||
AddOrUpdateUser(_lastAccessGrantedUserId.Value, userAccessLevel.Name);
|
||||
|
||||
_lastAccessGrantedUserId = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OnResetEvent()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue