Пример #1
0
 private async Task Write(ChannelWriter <SensorReading> writer, SensorReading val, CancellationToken cancellationToken)
 {
     try
     {
         await writer.WriteAsync(val, cancellationToken);
     }
     catch (Exception ex)
     {
         writer.TryComplete(ex);
     }
 }
Пример #2
0
        // 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);
        }