public async Task Handle(DoorClosedNotification notification, CancellationToken cancellationToken)
            {
                _logger.LogInformation("Door closed");

                if (_state.LockWhenShut)
                {
                    await _mediator.Send(new LockCommand());

                    await _ledService.ToggleOff();
                }

                if (_state.ArmWhenShut)
                {
                    await _mediator.Send(new ArmCommand());

                    await _ledService.ToggleOff();
                }

                _state.Timer?.Dispose();
            }
        public static async Task ToggleTimedColor(this ILEDService ledService, Color color, TimeSpan timeSpan, CancellationToken cancellationToken = default)
        {
            try
            {
                await ledService.SetColorAsync(color);

                await Task.Delay(timeSpan, cancellationToken);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                await ledService.ToggleOff();
            }
        }
Exemplo n.º 3
0
            public async Task <AlarmStateDto> Handle(ArmCommand request, CancellationToken cancellationToken)
            {
                if (!_state.Armed)
                {
                    var lockState = await _mediator.Send(new LockCommand());

                    _state.Armed = true;

                    await _ledService.SetColorAsync(Color.Purple);

                    _logger.LogInformation("Armed");

                    await _serviceEventClient.PublishEvent(new AlarmEvent(AlarmState.Armed));

                    await Task.Delay(2000);

                    await _ledService.ToggleOff();
                }

                return(await _mediator.Send(new GetAlarmStateQuery()));
            }
Exemplo n.º 4
0
            public async Task Handle(DoorOpenedNotification notification, CancellationToken cancellationToken)
            {
                _logger.LogInformation("Door opened");

                if (_state.Armed || _state.Locked)
                {
                    await _serviceEventClient.PublishEvent(new UnauthorizedAccessEvent());

                    await _ledService.ToggleRedLedOn();

                    // In a real-world scenario, this would be going on until manually stopped.

                    await _buzzerService
                    .BuzzAsync(_state.BuzzTime)
                    .ConfigureAwait(false);

                    await _ledService.ToggleOff();

                    return;
                }

                await _serviceEventClient.PublishEvent(new AccessControl.Contracts.Events.AccessEvent());
            }
        public static Task Blink(this ILEDService ledService, int times, Color color, TimeSpan delay, Action callback = null, CancellationToken cancellationToken = default)
        {
            return(Task.Run(async() =>
            {
                for (int i = 0; i < times; i++)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }

                    await ledService.SetColorAsync(color);

                    callback?.Invoke();

                    await Task.Delay(delay);

                    await ledService.ToggleOff();

                    await Task.Delay(delay);
                }
            }));
        }
Exemplo n.º 6
0
            public async Task Handle(MotionNotDetectedNotification notification, CancellationToken cancellationToken)
            {
                _logger.LogInformation("Motion not detected");

                await _ledService.ToggleOff();
            }