Update 'access_level_use_limit_c#/AlsResetScript.cs'
This commit is contained in:
parent
898d8e74fe
commit
e0ff13893a
|
@ -6,6 +6,7 @@ using System.Linq;
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MDP.SID.Scripting.Grpc;
|
||||
using MDP.SID.Shared.Public.Events;
|
||||
using Newtonsoft.Json;
|
||||
using Event = MDP.SID.Scripting.Grpc.Event;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
@ -23,7 +24,7 @@ private readonly Interval[] _accessIntervals =
|
|||
// TimeSpan(hours, minutes, seconds)
|
||||
// Name = "Access_level"
|
||||
new() {Start = new TimeSpan(16, 23, 0), End = new TimeSpan(16, 24, 0), Name = "Breakfast"},
|
||||
new() {Start = new TimeSpan(16, 24, 0), End = new TimeSpan(16, 25, 0), Name = "Lunch"},
|
||||
new() {Start = new TimeSpan(10, 24, 0), End = new TimeSpan(16, 25, 0), Name = "Everywhere"},
|
||||
new() {Start = new TimeSpan(16, 25, 0), End = new TimeSpan(16, 26, 0), Name = "Dinner"}
|
||||
};
|
||||
|
||||
|
@ -37,7 +38,7 @@ private readonly string[] _canteenDoors =
|
|||
/// 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 const bool RemoveUserAlThenDoorOpened = true;
|
||||
|
||||
// Set time when access levels will reset
|
||||
public readonly TimeSpan AccessLevelsResetTime = new(16, 27, 0);
|
||||
|
@ -50,7 +51,9 @@ private Timer _timer;
|
|||
|
||||
private static readonly ConcurrentDictionary<int, IList<string>> _failedAddOrUpdate = new();
|
||||
|
||||
private int? _lastAccessGrantedUserId;
|
||||
private int? _lastAccessGrantedUserId;
|
||||
|
||||
private int? _lastAccessGrantedId;
|
||||
|
||||
private async Task ResetUsersAccessLevelsAsync()
|
||||
{
|
||||
|
@ -206,29 +209,41 @@ private void AddFromFailed(Dictionary<int, IList<string>> users)
|
|||
|
||||
private async void EventReceived(Event received)
|
||||
{
|
||||
if (_canteenDoors.Contains(received.DoorName) is false) return;
|
||||
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 (Guid.TryParse(received.TypeUid, out var typeUid) is false) return;
|
||||
|
||||
if (RemoveUserAlThenDoorOpened) return;
|
||||
if (typeUid == EventTypes.AccessGranted)
|
||||
{
|
||||
if (received.UserId.HasValue is false) return;
|
||||
|
||||
await RemoveUserAccessLevelAsync(received);
|
||||
}
|
||||
}
|
||||
// Door opened
|
||||
else if (RemoveUserAlThenDoorOpened && received.TypeUid == "293f433d-f23e-4d33-b93c-bfd7c72763e6")
|
||||
{
|
||||
await RemoveUserAccessLevelAsync(received);
|
||||
}
|
||||
_lastAccessGrantedUserId = received.UserId.Value;
|
||||
|
||||
if (RemoveUserAlThenDoorOpened)
|
||||
{
|
||||
if (_lastAccessGrantedId.HasValue)
|
||||
{
|
||||
await Context.SetEventsDescriptionAsync(new List<int> { (int)_lastAccessGrantedId }, "Ignore");
|
||||
}
|
||||
|
||||
_lastAccessGrantedId = received.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
await RemoveUserAccessLevelAsync(received);
|
||||
}
|
||||
}
|
||||
else if (RemoveUserAlThenDoorOpened && typeUid == EventTypes.DoorOpened)
|
||||
{
|
||||
await RemoveUserAccessLevelAsync(received);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RemoveUserAccessLevelAsync(Event received)
|
||||
private async Task RemoveUserAccessLevelAsync(Event received)
|
||||
{
|
||||
if (_lastAccessGrantedUserId.HasValue)
|
||||
if (_lastAccessGrantedUserId.HasValue is false) return;
|
||||
|
||||
try
|
||||
{
|
||||
var accessTime = received.Time.ToDateTime().ToLocalTime().TimeOfDay;
|
||||
|
||||
|
@ -257,10 +272,14 @@ private async Task RemoveUserAccessLevelAsync(Event received)
|
|||
Context.LogDebug($"Al: {userAccessLevel.Name} removed for user with Id: {_lastAccessGrantedUserId.Value}");
|
||||
|
||||
AddOrUpdateUser(_lastAccessGrantedUserId.Value, userAccessLevel.Name);
|
||||
|
||||
_lastAccessGrantedUserId = null;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_lastAccessGrantedUserId = null;
|
||||
|
||||
_lastAccessGrantedId = null;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OnResetEvent()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue