Beispiel #1
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());
            }
Beispiel #2
0
            public async Task <LockStateDto> Handle(LockCommand request, CancellationToken cancellationToken)
            {
                if (!_state.Locked)
                {
                    await _relayControlService.SetRelayStateAsync(_state.LockRelay, true);

                    _state.Locked = true;

                    _logger.LogInformation("Locked");

                    await _serviceEventClient.PublishEvent(new LockEvent(LockState.Locked));
                }

                return(await _mediator.Send(new GetLockStateQuery()));
            }
            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()));
            }
            public async Task <AlarmStateDto> Handle(DisarmCommand request, CancellationToken cancellationToken)
            {
                try
                {
                    if (_state.Armed)
                    {
                        _state.Armed = false;

                        _logger.LogInformation("Disarmed");

                        await _serviceEventClient.PublishEvent(new AlarmEvent(AlarmState.Disarmed, request.Rex));

                        var lockState = await _mediator.Send(new UnlockCommand());

                        await _ledService.ToggleGreenLedOn();

                        if (_state.AccessTime != TimeSpan.Zero) // Infinite access time
                        {
                            _state.Timer = new Timer(async _ =>
                            {
                                _state.Timer?.Dispose();
                                _state.Timer = null;

                                await _mediator.Send(new ArmCommand());
                            }, null, (int)_state.AccessTime.TotalMilliseconds, 0);
                        }
                    }

                    return(await _mediator.Send(new GetAlarmStateQuery()));
                }
                catch (Exception e)
                {
                    _logger.LogError(e, string.Empty);

                    throw;
                }
            }
Beispiel #5
0
            public async Task <LockStateDto> Handle(UnlockCommand request, CancellationToken cancellationToken)
            {
                try
                {
                    if (_state.Locked)
                    {
                        await _relayControlService.SetRelayStateAsync(_state.LockRelay, false);

                        _state.Locked = false;

                        _logger.LogInformation("Unlocked");

                        await _serviceEventClient.PublishEvent(new LockEvent(LockState.Unlocked));
                    }

                    return(await _mediator.Send(new GetLockStateQuery()));
                }
                catch (Exception e)
                {
                    _logger.LogError(e, string.Empty);

                    throw;
                }
            }