Пример #1
0
        private void UpdateState(object sender, MotionDetectorAdapterStateChangedEventArgs e)
        {
            var state = e.State == AdapterMotionDetectionState.MotionDetected
                ? MotionDetectionStateValue.MotionDetected
                : MotionDetectionStateValue.Idle;

            lock (_syncRoot)
            {
                if (state == _motionDetectionState)
                {
                    return;
                }

                if (state == MotionDetectionStateValue.MotionDetected && !Settings.IsEnabled)
                {
                    return;
                }

                var oldState = GetState();
                _motionDetectionState = state;
                OnStateChanged(oldState);

                if (state == MotionDetectionStateValue.MotionDetected)
                {
                    _messageBroker.Publish(Id, new MotionDetectedEvent());
                }
                else if (state == MotionDetectionStateValue.Idle)
                {
                    _messageBroker.Publish(Id, new MotionDetectionCompletedEvent());
                }
            }
        }
Пример #2
0
 private void PressInternal(ButtonPressedDuration duration)
 {
     if (duration == ButtonPressedDuration.Short)
     {
         _messageBroker.Publish(Id, new ButtonPressedShortEvent());
     }
     else
     {
         _messageBroker.Publish(Id, new ButtonPressedLongEvent());
     }
 }
Пример #3
0
        public Button(string id, IButtonAdapter adapter, ITimerService timerService, ISettingsService settingsService, IMessageBrokerService messageBroker, ILogService logService)
            : base(id)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }
            if (settingsService == null)
            {
                throw new ArgumentNullException(nameof(settingsService));
            }
            _messageBroker = messageBroker ?? throw new ArgumentNullException(nameof(messageBroker));

            _log = logService?.CreatePublisher(id) ?? throw new ArgumentNullException(nameof(logService));
            settingsService.CreateSettingsMonitor <ButtonSettings>(this, s => Settings = s.NewSettings);

            _pressedLongTimeout          = new Timeout(timerService);
            _pressedLongTimeout.Elapsed += (s, e) =>
            {
                _messageBroker.Publish(Id, new ButtonPressedLongEvent());
            };

            adapter.StateChanged += UpdateState;

            _commandExecutor.Register <ResetCommand>();
            _commandExecutor.Register <PressCommand>(c => PressInternal(c.Duration));
        }
Пример #4
0
        private void Update(WindowStateChangedEventArgs eventArgs)
        {
            WindowStateValue newState;

            if (eventArgs.OpenReedSwitchState == AdapterSwitchState.Open)
            {
                newState = WindowStateValue.Open;
            }
            else if (eventArgs.TildReedSwitchState.HasValue && eventArgs.TildReedSwitchState.Value == AdapterSwitchState.Open)
            {
                newState = WindowStateValue.TildOpen;
            }
            else
            {
                newState = WindowStateValue.Closed;
            }

            if (newState.Equals(_state))
            {
                return;
            }

            var oldState = GetState();

            _state = newState;

            if (!_settingsService.GetSettings <ComponentSettings>(this).IsEnabled)
            {
                return;
            }

            OnStateChanged(oldState);

            if (_state == WindowStateValue.Closed)
            {
                _messageBroker.Publish(Id, new WindowClosedEvent());
            }
            else
            {
                _messageBroker.Publish(Id, new WindowOpenedEvent());
            }
        }
Пример #5
0
        public static Task Publish(this IMessageBrokerService messageBrokerService, string topic, object payload)
        {
            if (messageBrokerService == null)
            {
                throw new ArgumentNullException(nameof(messageBrokerService));
            }
            if (topic == null)
            {
                throw new ArgumentNullException(nameof(topic));
            }
            if (payload == null)
            {
                throw new ArgumentNullException(nameof(payload));
            }

            var messagePayload = new MessagePayload <JObject>(payload.GetType().Name, JObject.FromObject(payload));
            var message        = new Message <JObject>(topic, messagePayload);

            return(messageBrokerService.Publish(message));
        }
Пример #6
0
 public void Publish(string topic, string type, string payload)
 {
     _messageBrokerService.Publish(new Message <JObject>(topic, new MessagePayload <JObject>(type, JObject.Parse(payload))));
 }