Beispiel #1
0
        private void SaveToDatabase()
        {
            try
            {
                if (_hrQueue.Count > 0)
                {
                    _isConnectedToBluetoothDevice = true;

                    var measurements = new List <HeartRateMeasurement>();

                    HeartRateMeasurement measurement = null;
                    while (!_hrQueue.IsEmpty)
                    {
                        _hrQueue.TryDequeue(out measurement);
                        if (measurement != null)
                        {
                            if (!double.IsNaN(_previousRR))
                            {
                                measurement.RRDifference = Math.Abs(measurement.RRInterval - _previousRR);
                            }
                            _previousRR = measurement.RRInterval;
                            measurements.Add(measurement);
                        }
                    }

                    if (Settings.IsDetailedCollectionEnabled)
                    {
                        DatabaseConnector.AddHeartMeasurementsToDatabase(measurements, false);
                    }

                    var ts = (measurements.Count >= 0) ? measurements[0].Timestamp : string.Empty;
                    var heartRateValues = measurements.Where(x => !double.IsNaN(x.HeartRateValue)).Select(x => x.HeartRateValue);
                    var rrdifferences   = measurements.Where(x => !double.IsNaN(x.RRDifference)).Select(x => x.RRDifference);
                    var rrintervals     = measurements.Where(x => !double.IsNaN(x.RRInterval)).Select(x => x.RRInterval);

                    var averages = new HeartRateMeasurement()
                    {
                        HeartRateValue = (heartRateValues.Count() == 0) ? double.NaN : heartRateValues.Average(),
                        RRDifference   = (rrdifferences.Count() == 0) ? double.NaN : rrdifferences.Average(),
                        RRInterval     = (rrintervals.Count() == 0) ? double.NaN : rrintervals.Average(),
                        Timestamp      = ts
                    };
                    DatabaseConnector.AddHeartMeasurementsToDatabase(new List <HeartRateMeasurement>()
                    {
                        averages
                    }, true);
                }
                else
                {
                    _isConnectedToBluetoothDevice = false;
                    Logger.WriteToConsole("Nothing to save...");
                }
            }
            catch (Exception e)
            {
                Logger.WriteToLogFile(e);
            }
        }