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); } }
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); }
public LockManager(ILoggerFactory loggerFactory, IGpioPinFactory gpioPinFactory, IOptions <LockOptions> lockOptions, LockDbContext lockDbContext) { IdToLock = new Dictionary <string, Lock>(); Logger = loggerFactory.CreateLogger <LockManager>(); LockDbContext = lockDbContext; Timer = new Timer(TimerExpired); TimerSemaphore = new SemaphoreSlim(1); Initialize(lockOptions.Value, gpioPinFactory, lockDbContext); }
private async Task UpdateLockDto(string id, LockState state, DateTime?timeout) { LockDto lockDto = LockDbContext.Locks.Find(id); if (lockDto == null) { lockDto = new LockDto() { Id = id, State = state, Timeout = timeout }; LockDbContext.Add(lockDto); } else { lockDto.State = state; lockDto.Timeout = timeout; LockDbContext.Update(lockDto); } await LockDbContext.SaveChangesAsync(); }
public LocksController(LockDbContext context, UserManager <ApplicationUser> userManager) { _context = context; _userManager = userManager; }
public RequestResponseLoggingMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, LockDbContext lockDbContext) { Next = next; Logger = loggerFactory.CreateLogger <RequestResponseLoggingMiddleware>(); LockDbContext = lockDbContext; }
private void Initialize(LockOptions lockOptions, IGpioPinFactory gpioPinFactory, LockDbContext lockDbContext) { DateTime now = DateTime.Now; async Task InitializeLock(LockConfiguration lockConfiguration) { LockDto lockDto = await lockDbContext.Locks.FindAsync(lockConfiguration.Id); LockState mode = lockDto?.State ?? LockState.Locked; bool timedOut = lockDto?.Timeout <= now; if (timedOut) { mode = mode.Toggle(); } Lock @lock = new Lock(lockConfiguration.Id, gpioPinFactory.CreatePin((BcmPin)lockConfiguration.Pin), (LockState)(-1)); if (!timedOut) { @lock.Timeout = lockDto?.Timeout; } await SetAsync(@lock, mode, "INITIALIZE"); IdToLock.Add(@lock.Id, @lock); } Task.WaitAll(lockOptions.LockConfigurations.Select(InitializeLock).ToArray()); UpdateTimerAsync().Wait(); }
/// <summary> /// Constructs the <see cref="SqlServerLockManager"/> /// </summary> /// <param name="dbContext"></param> public SqlServerLockManager(LockDbContext dbContext) { _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext)); }
/// <summary> /// Constructs the <see cref="RelationalLockManager"/>. /// </summary> /// <param name="dbContext">Contains the required tables to implement a locking mechanism using a relational database.</param> public RelationalLockManager(LockDbContext dbContext) { _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext)); _queryDescriptor = new LockManagerQueryDescriptor(dbContext); }