private void PublishEvents(ButtonStateValue state) { if (state == ButtonStateValue.Pressed) { _pressedDurationStopwatch.Restart(); if (!_messageBroker.HasSubscribers <ButtonPressedLongEvent>(Id)) { _messageBroker.Publish(Id, new ButtonPressedShortEvent()); } else { _pressedLongTimeout.Start(Settings.PressedLongDuration); } } else if (state == ButtonStateValue.Released) { _pressedDurationStopwatch.Stop(); if (_pressedLongTimeout.IsEnabled && !_pressedLongTimeout.IsElapsed) { _pressedLongTimeout.Stop(); _messageBroker.Publish(Id, new ButtonPressedShortEvent()); } _log.Verbose($"Button '{Id}' pressed for {_pressedDurationStopwatch.ElapsedMilliseconds} ms."); } }
public static bool HasSubscribers <TPayload>(this IMessageBrokerService messageBrokerService, string topic) where TPayload : class { if (messageBrokerService == null) { throw new ArgumentNullException(nameof(messageBrokerService)); } if (topic == null) { throw new ArgumentNullException(nameof(topic)); } return(messageBrokerService.HasSubscribers(topic, typeof(TPayload).Name)); }