Beispiel #1
0
        public async Task ToggleLockAsync(string id, string reason)
        {
            if (!IdToLock.TryGetValue(id, out Lock @lock))
            {
                throw new ArgumentException("Unknown id", nameof(id));
            }

            await @lock.Semaphore.WaitAsync();

            try
            {
                @lock.Timeout = null;
                LockState newState = await ToggleAsync(@lock, reason);

                await DbSemaphore.WaitAsync();

                try
                {
                    await UpdateLockDto(id, newState, null);
                }
                finally
                {
                    DbSemaphore.Release();
                }
            }
            finally
            {
                @lock.Semaphore.Release();
            }
            await UpdateTimerAsync();
        }
Beispiel #2
0
        private async Task <LockState> ToggleAsync(Lock @lock, string reason)
        {
            DateTime  now         = DateTime.Now;
            LockState targetState = @lock.State.Toggle();

            @lock.State = targetState;
            PinLogDto pinLog = new PinLogDto()
            {
                Pin      = @lock.PinId,
                PinState = Lock.ToPinState(@lock.State),
                Reason   = reason,
                Time     = now
            };

            Logger.LogInformation("{0} toggled to {1} ({2})", @lock.Id, targetState, reason);
            await DbSemaphore.WaitAsync();

            try
            {
                LockDbContext.Add(pinLog);
                await LockDbContext.SaveChangesAsync();
            }
            finally
            {
                DbSemaphore.Release();
            }
            return(targetState);
        }
Beispiel #3
0
        private async Task SetAsync(Lock @lock, LockState mode, string reason)
        {
            if (@lock.State != mode)
            {
                DateTime now = DateTime.Now;
                @lock.State = mode;
                PinLogDto pinLog = new PinLogDto()
                {
                    Pin      = @lock.PinId,
                    PinState = Lock.ToPinState(@lock.State),
                    Reason   = reason,
                    Time     = now
                };
                Logger.LogInformation("{0} set to {1} ({2})", @lock.Id, mode, reason);
                await DbSemaphore.WaitAsync();

                try
                {
                    LockDbContext.Add(pinLog);
                    await LockDbContext.SaveChangesAsync();
                }
                finally
                {
                    DbSemaphore.Release();
                }
            }
            else
            {
                Logger.LogInformation("{0} is already in mode {1} ({2})", @lock.Id, mode, reason);
            }
        }
Beispiel #4
0
        public async Task SetLockAsync(string id, LockState state, string reason, DateTime?timeout = null)
        {
            if (!IdToLock.TryGetValue(id, out Lock @lock))
            {
                throw new ArgumentException("Unknown id", nameof(id));
            }

            await @lock.Semaphore.WaitAsync();

            try
            {
                @lock.Timeout = timeout;
                await SetAsync(@lock, state, reason);

                await DbSemaphore.WaitAsync();

                try
                {
                    await UpdateLockDto(id, state, timeout);
                }
                finally
                {
                    DbSemaphore.Release();
                }
            }
            finally
            {
                @lock.Semaphore.Release();
            }
            await UpdateTimerAsync();
        }