public IObservable <double> WhenReadingTaken() { this.observable = this.observable ?? Observable.Create <double>(ob => { var handler = new EventHandler <HumiditySensorDataUpdatedEventArgs>((sender, args) => ob.OnNext(args.Humidity) ); var sensor = new HumiditySensor { Interval = 250 }; sensor.DataUpdated += handler; sensor.Start(); return(() => { sensor.Stop(); sensor.DataUpdated -= handler; sensor.Dispose(); }); }) .Publish() .RefCount(); return(this.observable); }
/// <summary> /// Starts sensor and registers listener to a sensor /// </summary> /// <param name="listener">Event handler to listen sensor events</param> public void Start(EventHandler <SensorEventArgs> listener) { handler = (sender, e) => { listener?.Invoke(this, new SensorEventArgs(new List <float>() { e.Humidity })); }; sensor.DataUpdated += handler; sensor.Start(); }