private async Task Write(ChannelWriter <SensorReading> writer, SensorReading val, CancellationToken cancellationToken) { try { await writer.WriteAsync(val, cancellationToken); } catch (Exception ex) { writer.TryComplete(ex); } }
// TODO: investigate SignalR 3 pub-sub // TODO: dotnet core 3 async enumerables public ChannelReader <SensorReading> StartListening(string sensorIdentifier, CancellationToken cancellationToken) { var channel = Channel.CreateUnbounded <SensorReading>(); _sensorManager.GetSensorObservable(sensorIdentifier) .Subscribe(val => { var sensorReading = new SensorReading { Value = val, Timestamp = DateTime.Now }; _ = Write(channel.Writer, sensorReading, cancellationToken); }, cancellationToken); return(channel.Reader); }