Ejemplo n.º 1
0
        public MainViewModel(IDataService dataService)
        {
            _dataService = dataService;
            _dataService.GetSamples().GroupBy(sample => new { sample.SensorId, sample.UnitOfMeasure }).ObserveOnDispatcher().Subscribe(grp =>
                                                                                       {
                                                                                           var sampleGroup = new SampleGroup
                                                                                                                 {
                                                                                                                     SensorId = grp.Key.SensorId,
                                                                                                                     UnitOfMeasure = grp.Key.UnitOfMeasure,
                                                                                                                     Samples = new ObservableCollection<double>()
                                                                                                                 };
                                                                                           SampleGroups.Add(sampleGroup);

                                                                                           grp.Buffer(5).Subscribe(window => sampleGroup.AverageMeasurement = window.Average(avg => avg.SensorValue));

                                                                                           /*grp.Scan(new { sum = .0, count = 0 }, (agg, sample) => new { sum = agg.sum + sample.SensorValue, count = agg.count + 1 })
                                                                                                    .Select(agg => agg.sum / agg.count).ObserveOnDispatcher().Subscribe(average => sampleGroup.AverageMeasurement = average);*/

                                                                                           grp.Sample(TimeSpan.FromSeconds(1)).ObserveOnDispatcher().Select(sample => sample.SensorValue).Subscribe(sampleGroup.Samples.Add);
                                                                                       });
            _dataService
                .GetSamples()
                .Where(sample => sample.SampleType == SensorSample.SampleTypes.Temperature && sample.SensorValue < -25)
                .ObserveOnDispatcher().Subscribe(sample => Warnings.Add(string.Format("Warning: Sensor {0} registerede temperature of {1:0.00}", sample.SensorId, sample.SensorValue)));
        }