示例#1
0
        public DataReader(Guid trackId)
        {
            _trackId = trackId;

            var filepath = FileNaming.GetDataTrackFilepath(trackId);

            _reader = new StreamReader(FileOperations.ReadFile(filepath));
        }
        /// <summary>
        /// Collects a new piece of data.
        /// </summary>
        public void Collect(DataPiece piece)
        {
            if (_previous != null)
            {
                if (_previous.TrackId == piece.TrackId)
                {
                    _ppeAccumulator += piece.Ppe;
                    _ppeCount       += 1;
                    _ppeBins[PpeMapper.GetBinIndex(piece.Ppe)] += 1;
                    if (piece.Ppe > _ppeMax)
                    {
                        _ppeMax = piece.Ppe;
                    }

                    if (piece.StartTimestamp < _tsStart)
                    {
                        _tsStart = piece.StartTimestamp;
                    }
                    if (piece.EndTimestamp > _tsEnd)
                    {
                        _tsEnd = piece.EndTimestamp;
                    }
                    _elapsed += (piece.EndTimestamp - piece.StartTimestamp);

                    var traveledDistance = GeoHelper.DistanceBetweenPoints(
                        _previous.Latitude, _previous.Longitude,
                        piece.Latitude, piece.Longitude
                        ) * DistanceFactor;
                    _distance += traveledDistance;

                    Log.Debug("Traveled {0:F3}km, count {1} {2:t}-{3:t}", traveledDistance, _ppeCount, _tsStart, _tsEnd);
                }
                else
                {
                    Log.Warning(new ArgumentException(nameof(piece.TrackId)), "Different track ID seen while collecting statistics");

                    CompleteSession();
                }
            }

            // Dump data
            // Generates approximately 42 bytes per measurement (~147 KB/hour)
            if (_dumpWriter == null)
            {
                var dumpStream = FileOperations.AppendFile(FileNaming.GetDataTrackFilepath(piece.TrackId));
                _dumpWriter = new StreamWriter(dumpStream);
            }
            _dumpWriter.WriteLine(
                string.Format(CultureInfo.InvariantCulture, "{0},{1:F5},{2:F5},{3:F2}", piece.StartTimestamp.Ticks, piece.Latitude, piece.Longitude, piece.Ppe)
                );

            _previous = piece;

            Log.Debug("Data piece collected");
        }
示例#3
0
        /// <summary>
        /// Collects a new piece of data.
        /// </summary>
        public void Collect(DataPiece piece)
        {
            if (_record != null && (_record.TrackId != piece.TrackId))
            {
                Log.Warning(new ArgumentException(nameof(piece.TrackId)), "Different track ID seen while collecting statistics");

                CompleteSession();
            }

            if (_record == null)
            {
                _record = GetRecordForPiece(piece);
            }

            // Record set, update
            _ppeAccumulator += piece.Ppe;
            _ppeCount       += 1;
            _record.Bins[PpeMapper.GetBinIndex(piece.Ppe)] += 1;
            if (piece.Ppe > _record.MaxPpe)
            {
                _record.MaxPpe = piece.Ppe;
            }
            _record.AvgPpe               = _ppeAccumulator / _ppeCount;
            _record.DataPieceCount       = (int)_ppeCount;
            _record.End                  = piece.EndTimestamp;
            _record.ElapsedTime         += (piece.EndTimestamp - piece.StartTimestamp);
            _record.LocationEndLatitude  = piece.Latitude;
            _record.LocationEndLongitude = piece.Longitude;

            if (_previous != null)
            {
                var traveledDistance = GeoHelper.DistanceBetweenPoints(
                    _previous.Latitude, _previous.Longitude,
                    piece.Latitude, piece.Longitude
                    ) * DistanceFactor;
                _record.DistanceTraveled += traveledDistance;

                Log.Debug("Traveled {0:F3}km, count {1} {2:t}-{3:t}",
                          traveledDistance, _ppeCount, _record.Start, _record.End);
            }

            _previous = piece;

            // Flush every few minutes
            if (_ppeCount > 0 && (_ppeCount % FlushIntervalSize == 0))
            {
                Flush();
            }

            // Dump data
            // Generates approximately 89 bytes per measurement (~313 KB/hour)
            if (_dumpWriter == null)
            {
                var outputFilepath = FileNaming.GetDataTrackFilepath(piece.TrackId);
                Log.Debug("Appending to file {0}", outputFilepath);

                var dumpStream = FileOperations.AppendFile(outputFilepath);
                _dumpWriter = new StreamWriter(dumpStream);
                _dumpWriter.WriteLine("# SRS Track {0:N} {1:u}", piece.TrackId, DateTime.Now);
                _dumpWriter.WriteLine("# Start,End,Lat,Lng,PPE,PPEx,PPEy,PPEz,Speed,Bearing,Accuracy");
            }
            _dumpWriter.WriteLine(
                string.Format(CultureInfo.InvariantCulture, "{0},{1},{2:F5},{3:F5},{4:F3},{5:F2},{6:F2},{7:F2},{8:F1},{9:D},{10:D}",
                              piece.StartTimestamp.Ticks,
                              piece.EndTimestamp.Ticks,
                              piece.Latitude,
                              piece.Longitude,
                              piece.Ppe,
                              piece.PpeX,
                              piece.PpeY,
                              piece.PpeZ,
                              piece.Speed,
                              (int)piece.Bearing,
                              piece.Accuracy
                              )
                );

            Log.Debug("Data piece collected");
        }