コード例 #1
0
ファイル: LockManager.cs プロジェクト: trexug/trexlock
        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);
            }
        }
コード例 #2
0
ファイル: LockManager.cs プロジェクト: trexug/trexlock
        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);
        }