Exemplo n.º 1
0
 public DataMessage(BinaryReader dataReader, DefinitionMessage definitionMessage)
 {
     this.definitionMessage = definitionMessage;
     data = dataReader.ReadBytes(definitionMessage.DataLength);
 }
Exemplo n.º 2
0
 public DataMessage(BinaryReader dataReader, DefinitionMessage definitionMessage)
 {
     this.definitionMessage = definitionMessage;
     data = dataReader.ReadBytes(definitionMessage.DataLength);
 }
Exemplo n.º 3
0
            public Data(BinaryReader reader, UInt32 dataSize)
            {
                Waypoints = new List <FITWaypoint>();
                Laps      = new List <FITLap>();
                var bytes = reader.ReadBytes((int)dataSize);

                using (var stream = new MemoryStream(bytes))
                {
                    using (var dataReader = new BinaryReader(stream))
                    {
                        var recordHeader = new RecordHeader(dataReader);

                        if (!recordHeader.IsDefinitionMessage)
                        {
                            throw new Exception("First record is not a definition message.");
                        }
                        var def = new DefinitionMessage(dataReader);
                        if (def.GlobalMessageNumber != (ushort)MesgNum.file_id)
                        {
                            throw new Exception("First record's global message number is not file_id.");
                        }
                        recordHeader = new RecordHeader(dataReader);
                        if (recordHeader.IsDefinitionMessage)
                        {
                            throw new Exception("Encountered a definition message, but expected a data message.");
                        }
                        var d        = new DataMessage(dataReader, def);
                        var fileType = d.GetByte(0);
                        if (fileType != 4)
                        {
                            throw new Exception("Not a FIT activity file.");
                        }

                        var    messageTypeTranslator = new Dictionary <byte, DefinitionMessage>();
                        UInt32 lastTimestamp         = 0;

                        while (dataReader.BaseStream.Position < dataReader.BaseStream.Length)
                        {
                            recordHeader = new RecordHeader(dataReader);
                            if (recordHeader.IsDefinitionMessage)
                            {
                                def = new DefinitionMessage(dataReader);
                                FITUtil.AddOrReplace(messageTypeTranslator, recordHeader.LocalMessageType, def);
                            }
                            else
                            {
                                var currentDef = messageTypeTranslator[recordHeader.LocalMessageType];
                                d = new DataMessage(dataReader, currentDef);

                                var timestamp = d.GetUInt32(253);
                                if (timestamp == null)
                                {
                                    timestamp = FITUtil.AddCompressedTimestamp(lastTimestamp, recordHeader.TimeOffset);
                                }
                                var time = FITUtil.ToDateTime(timestamp.Value);

                                var gmn = currentDef.GlobalMessageNumber;
                                if (gmn == (byte)MesgNum.record)
                                {
                                    var lat = d.GetInt32(0);
                                    var lng = d.GetInt32(1);
                                    var alt = d.GetUInt16(2);
                                    var hr  = d.GetByte(3);
                                    if (lng != null && lng != invalidInt32 && lat != null && lat != invalidInt32)
                                    {
                                        Waypoints.Add(new FITWaypoint()
                                        {
                                            Time      = time,
                                            Latitude  = positionFactor * lat.Value,
                                            Longitude = positionFactor * lng.Value,
                                            Altitude  = alt == null || alt == invalidUInt16 ? (double?)null : (double)alt.Value / 5 - 500,
                                            HeartRate = hr == null || hr == invalidByte ? null : hr
                                        });
                                    }
                                }
                                else if (gmn == (byte)MesgNum.lap)
                                {
                                    Laps.Add(new FITLap()
                                    {
                                        Time = time
                                    });
                                }
                                else if (gmn == 22)
                                {
                                }
                                lastTimestamp = timestamp.Value;
                            }
                        }
                    }
                }
            }
Exemplo n.º 4
0
            public Data(BinaryReader reader, UInt32 dataSize)
            {
                Waypoints = new List<FITWaypoint>();
                Laps = new List<FITLap>();
                var bytes = reader.ReadBytes((int)dataSize);
                using (var stream = new MemoryStream(bytes))
                {
                  using (var dataReader = new BinaryReader(stream))
                  {
                var recordHeader = new RecordHeader(dataReader);

                if (!recordHeader.IsDefinitionMessage)
                {
                  throw new Exception("First record is not a definition message.");
                }
                var def = new DefinitionMessage(dataReader);
                if (def.GlobalMessageNumber != (ushort) MesgNum.file_id)
                  throw new Exception("First record's global message number is not file_id.");
                recordHeader = new RecordHeader(dataReader);
                if (recordHeader.IsDefinitionMessage)
                  throw new Exception("Encountered a definition message, but expected a data message.");
                var d = new DataMessage(dataReader, def);
                var fileType = d.GetByte(0);
                if (fileType != 4) throw new Exception("Not a FIT activity file.");

                var messageTypeTranslator = new Dictionary<byte, DefinitionMessage>();
                UInt32 lastTimestamp = 0;

                while (dataReader.BaseStream.Position < dataReader.BaseStream.Length)
                {
                  recordHeader = new RecordHeader(dataReader);
                  if (recordHeader.IsDefinitionMessage)
                  {
                def = new DefinitionMessage(dataReader);
                FITUtil.AddOrReplace(messageTypeTranslator, recordHeader.LocalMessageType, def);
                  }
                  else
                  {
                var currentDef = messageTypeTranslator[recordHeader.LocalMessageType];
                d = new DataMessage(dataReader, currentDef);

                var timestamp = d.GetUInt32(253);
                if (timestamp == null) timestamp = FITUtil.AddCompressedTimestamp(lastTimestamp, recordHeader.TimeOffset);
                var time = FITUtil.ToDateTime(timestamp.Value);

                var gmn = currentDef.GlobalMessageNumber;
                if (gmn == (byte) MesgNum.record)
                {
                  var lat = d.GetInt32(0);
                  var lng = d.GetInt32(1);
                  var alt = d.GetUInt16(2);
                  var hr = d.GetByte(3);
                  var cadence = d.GetByte(4);
                  var power = d.GetUInt16(7);
                  if (lng != null && lng != invalidInt32 && lat != null && lat != invalidInt32)
                  {
                    Waypoints.Add(new FITWaypoint()
                                    {
                                      Time = time,
                                      Latitude = positionFactor * lat.Value,
                                      Longitude = positionFactor * lng.Value,
                                      Altitude = alt == null || alt == invalidUInt16 ? (double?)null : (double)alt.Value / 5 - 500,
                                      HeartRate = hr == null || hr == invalidByte ? null : hr,
                                      Cadence = cadence == null || cadence == invalidByte ? null : cadence,
                                      Power = power == null || power == invalidUInt16 ? null : power
                                    });
                  }
                }
                else if (gmn == (byte)MesgNum.lap)
                {
                  Laps.Add(new FITLap() { Time = time });
                }
                else if (gmn == 22)
                {

                }
                lastTimestamp = timestamp.Value;
                  }
                }
                  }
                }
            }