/// <summary> /// Creates an observable for smart card events. /// </summary> /// <param name="factory">Factory to use for <see cref="ISCardMonitor"/> creation.</param> /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param> /// <param name="readerName">Name of the smart card reader that shall be monitored.</param> /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param> /// <returns></returns> public static IObservable <MonitorEvent> CreateObservable(this IMonitorFactory factory, SCardScope scope, string readerName, IScheduler scheduler = null) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } if (readerName == null) { throw new ArgumentNullException(nameof(readerName)); } return(factory.CreateObservable(scope, new[] { readerName }, scheduler)); }
private static string GetEventAsPrintableText(DeviceMonitorEvent ev) { string message = $"Event type {ev.GetType().Name}, (readers: {string.Join(", ", ev.Readers)})"; Console.WriteLine("Event select"); switch (ev) { case ReadersDetached detached: message += "detached"; subscription?.Dispose(); break; case ReadersAttached attached: message += "attached"; subscription = monitorFactory .CreateObservable(SCardScope.System, SmartCardReaderName) .Select(GetEventText) .Do(Console.WriteLine) .Subscribe( onNext: _ => { }, onError: OnError); break; case DeviceMonitorInitialized initialized: message += "initialized"; SmartCardReaderName = ev.Readers.Where(n => !n.Contains("SAM")).First(); message += $" {SmartCardReaderName}"; subscription = monitorFactory .CreateObservable(SCardScope.System, SmartCardReaderName) .Select(GetEventText) .Do(Console.WriteLine) .Subscribe( onNext: _ => { }, onError: OnError); break; } return(message); }
private void SubscribeToReaderEvents(IMonitorFactory monitorFactory, IReadOnlyCollection <string> readerNames) { _subscription?.Dispose(); if (readerNames.Count <= 0) { return; } _subscription = monitorFactory .CreateObservable(SCardScope.System, readerNames) .Do(ev => EventHistory.AddOnScheduler(ev)) // Always add elements using the UI scheduler! .Subscribe( onNext: _ => { }, onError: OnError); }
private void UpdateReaderList(object parameter) { Readers.Clear(); EventHistory.Clear(); using (var ctx = _contextFactory.Establish(SCardScope.System)) { foreach (var reader in ctx.GetReaders()) { Readers.Add(reader); } } _subscription?.Dispose(); if (Readers.Count > 0) { _subscription = _monitorFactory .CreateObservable(SCardScope.System, Readers) .ObserveOn(_uiScheduler) // Important! Monitor does not run on UI thread. .Subscribe(OnNext, OnError); } }