public async Task StartAsync(CancellationToken cancellationToken)
        {
            _state.AccessTime   = TimeSpan.Parse(await GetParam("accessTime") ?? "00:00:10");
            _state.LockWhenShut = bool.Parse(await GetParam("lockOnClose") ?? bool.TrueString);
            _state.ArmWhenShut  = bool.Parse(await GetParam("armOnClose") ?? bool.TrueString);

            await _relayControlService.SetRelayStateAsync(1, false);

            whenButtonPressedSubscription = WhenButtonPressedOpened.Subscribe(async _ =>
                                                                              await _mediator.Publish(new ButtonPressedNotification()));

            whenButtonReleasedSubscription = WhenButtonReleasedClosed.Subscribe(async _ =>
                                                                                await _mediator.Publish(new ButtonReleasedNotification()));

            whenSwitchClosedSubscription = WhenSwitchClosed.Subscribe(async _ =>
                                                                      await _mediator.Publish(new DoorClosedNotification()));

            whenSwitchOpenedSubscription = WhenSwitchOpened.Subscribe(async _ =>
                                                                      await _mediator.Publish(new DoorOpenedNotification()));

            whenMotionDetectedSubscription = WhenMotionDetected.Subscribe(async _ =>
                                                                          await _mediator.Publish(new MotionDetectedNotification()));

            whenMotionNotDetectedSubscription = WhenMotionNotDetected.Subscribe(async _ =>
                                                                                await _mediator.Publish(new MotionNotDetectedNotification()));

            whenCardDataReceivedSubscription = _rfidReader
                                               .WhenCardDetected
                                               .Throttle(TimeSpan.FromMilliseconds(800))
                                               .Subscribe(async cardData =>
                                                          await _mediator.Publish(new OnTagReadNotification(cardData)));

            await _rfidReader.StartAsync();
        }
Esempio n. 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()));
            }
Esempio n. 3
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;
                }
            }