private void ProcessCalibrationFactor(Mesg mesg, string[] fieldsXYZ, float[] calibratedXYZ, ushort globalMesgNum)
        {
            if ((fieldsXYZ.Length != NUM_AXIS) || (calibratedXYZ.Length != NUM_AXIS))
            {
                //Invalid number of arguments
                return;
            }

            //Add the newly calculated calibrated values to the calibrated data fields
            if (mesg.GetField(fieldsXYZ[X_AXIS_OFFSET]) == null)
            {
                mesg.SetField(new Field(Profile.GetField(globalMesgNum, fieldsXYZ[X_AXIS_OFFSET])));
            }

            if (mesg.GetField(fieldsXYZ[Y_AXIS_OFFSET]) == null)
            {
                mesg.SetField(new Field(Profile.GetField(globalMesgNum, fieldsXYZ[Y_AXIS_OFFSET])));
            }

            if (mesg.GetField(fieldsXYZ[Z_AXIS_OFFSET]) == null)
            {
                mesg.SetField(new Field(Profile.GetField(globalMesgNum, fieldsXYZ[Z_AXIS_OFFSET])));
            }

            mesg.GetField(fieldsXYZ[X_AXIS_OFFSET]).AddValue(calibratedXYZ[X_AXIS_OFFSET]);
            mesg.GetField(fieldsXYZ[Y_AXIS_OFFSET]).AddValue(calibratedXYZ[Y_AXIS_OFFSET]);
            mesg.GetField(fieldsXYZ[Z_AXIS_OFFSET]).AddValue(calibratedXYZ[Z_AXIS_OFFSET]);
        }
Exemplo n.º 2
0
            /// <summary>
            /// Checks if the reference fields in a given message indicate the subfield (alternate)
            /// definition should be used
            /// </summary>
            /// <param name="mesg">message of interest</param>
            /// <returns>true if the subfield is active</returns>
            internal bool CanMesgSupport(Mesg mesg)
            {
                Field field = mesg.GetField(refFieldNum);

                if (field != null)
                {
                    object value = field.GetValue(0, Fit.SubfieldIndexMainField);
                    if (value == refFieldValue)
                    {
                        return(true);
                    }
                }
                return(false);
            }
Exemplo n.º 3
0
            /// <summary>
            /// Checks if the reference fields in a given message indicate the subfield (alternate)
            /// definition should be used
            /// </summary>
            /// <param name="mesg">message of interest</param>
            /// <returns>true if the subfield is active</returns>
            internal bool CanMesgSupport(Mesg mesg)
            {
                Field field = mesg.GetField(refFieldNum);

                if (field != null)
                {
                    object value = field.GetValue(0, Fit.SubfieldIndexMainField);
                    // Float refvalues are not supported
                    if (Convert.ToInt64(value) == Convert.ToInt64(refFieldValue))
                    {
                        return(true);
                    }
                }
                return(false);
            }
Exemplo n.º 4
0
        public void DecodeNextMessage(Stream fitStream)
        {
            BinaryReader br       = new BinaryReader(fitStream);
            byte         nextByte = br.ReadByte();

            // Is it a compressed timestamp mesg?
            if ((nextByte & Fit.CompressedHeaderMask) == Fit.CompressedHeaderMask)
            {
                MemoryStream mesgBuffer = new MemoryStream();

                int timeOffset = nextByte & Fit.CompressedTimeMask;
                timestamp     += (uint)((timeOffset - lastTimeOffset) & Fit.CompressedTimeMask);
                lastTimeOffset = timeOffset;
                Field timestampField = new Field(Profile.GetMesg(MesgNum.Record).GetField("Timestamp"));
                timestampField.SetValue(timestamp);

                byte localMesgNum = (byte)((nextByte & Fit.CompressedLocalMesgNumMask) >> 5);
                mesgBuffer.WriteByte(localMesgNum);
                if (localMesgDefs[localMesgNum] == null)
                {
                    throw new FitException("Decode:DecodeNextMessage - FIT decode error: Missing message definition for local message number " + localMesgNum + " at stream position " + fitStream.Position);
                }
                int fieldsSize = localMesgDefs[localMesgNum].GetMesgSize() - 1;
                try
                {
                    byte[] read = br.ReadBytes(fieldsSize);
                    if (read.Length < fieldsSize)
                    {
                        throw new Exception("Field size mismatch, expected: " + fieldsSize + "received: " + read.Length);
                    }
                    mesgBuffer.Write(read, 0, fieldsSize);
                }
                catch (Exception e)
                {
                    throw new FitException("Decode:DecodeNextMessage - Compressed Data Message unexpected end of file.  Wanted " + fieldsSize + " bytes at stream position " + fitStream.Position, e);
                }

                Mesg newMesg = new Mesg(mesgBuffer, localMesgDefs[localMesgNum]);
                newMesg.InsertField(0, timestampField);
                RaiseMesgEvent(newMesg);
            }
            // Is it a mesg def?
            else if ((nextByte & Fit.MesgDefinitionMask) == Fit.MesgDefinitionMask)
            {
                MemoryStream mesgDefBuffer = new MemoryStream();

                // Figure out number of fields (length) of our defn and build buffer
                mesgDefBuffer.WriteByte(nextByte);
                mesgDefBuffer.Write(br.ReadBytes(4), 0, 4);
                byte numFields = br.ReadByte();
                mesgDefBuffer.WriteByte(numFields);
                int numBytes = numFields * 3; //3 Bytes per field
                try
                {
                    byte[] read = br.ReadBytes(numBytes);
                    if (read.Length < numBytes)
                    {
                        throw new Exception("Message Definition size mismatch, expected: " + numBytes + "received: " + read.Length);
                    }
                    mesgDefBuffer.Write(read, 0, numBytes);

                    if ((nextByte & Fit.DevDataMask) == Fit.DevDataMask)
                    {
                        // Definition Contains Dev Data
                        byte numDevFields = br.ReadByte();
                        mesgDefBuffer.WriteByte(numDevFields);

                        numBytes = numDevFields * 3;
                        read     = br.ReadBytes(numBytes);
                        if (read.Length < numBytes)
                        {
                            throw new Exception("Message Definition size mismatch, expected: " + numBytes + "received: " + read.Length);
                        }

                        // Read Dev Data
                        mesgDefBuffer.Write(read, 0, numBytes);
                    }
                }
                catch (Exception e)
                {
                    throw new FitException("Decode:DecodeNextMessage - Defn Message unexpected end of file.  Wanted " + numBytes + " bytes at stream position " + fitStream.Position, e);
                }

                MesgDefinition newMesgDef = new MesgDefinition(mesgDefBuffer, m_lookup);
                localMesgDefs[newMesgDef.LocalMesgNum] = newMesgDef;
                if (MesgDefinitionEvent != null)
                {
                    MesgDefinitionEvent(this, new MesgDefinitionEventArgs(newMesgDef));
                }
            }
            // Is it a data mesg?
            else if ((nextByte & Fit.MesgDefinitionMask) == Fit.MesgHeaderMask)
            {
                MemoryStream mesgBuffer = new MemoryStream();

                byte localMesgNum = (byte)(nextByte & Fit.LocalMesgNumMask);
                mesgBuffer.WriteByte(localMesgNum);
                if (localMesgDefs[localMesgNum] == null)
                {
                    throw new FitException("Decode:DecodeNextMessage - FIT decode error: Missing message definition for local message number " + localMesgNum + " at stream position " + fitStream.Position);
                }
                int fieldsSize = localMesgDefs[localMesgNum].GetMesgSize() - 1;
                try
                {
                    byte[] read = br.ReadBytes(fieldsSize);
                    if (read.Length < fieldsSize)
                    {
                        throw new Exception("Field size mismatch, expected: " + fieldsSize + "received: " + read.Length);
                    }
                    mesgBuffer.Write(read, 0, fieldsSize);
                }
                catch (Exception e)
                {
                    throw new FitException("Decode:DecodeNextMessage - Data Message unexpected end of file.  Wanted " + fieldsSize + " bytes at stream position " + fitStream.Position, e);
                }

                Mesg newMesg = new Mesg(mesgBuffer, localMesgDefs[localMesgNum]);

                // If the new message contains a timestamp field, record the value to use as
                // a reference for compressed timestamp headers
                Field timestampField = newMesg.GetField("Timestamp");
                if (timestampField != null)
                {
                    object tsValue = timestampField.GetValue();
                    if (tsValue != null)
                    {
                        timestamp      = (uint)tsValue;
                        lastTimeOffset = (int)timestamp & Fit.CompressedTimeMask;
                    }
                }

                foreach (Field field in newMesg.FieldsList)
                {
                    if (field.IsAccumulated)
                    {
                        int i;
                        for (i = 0; i < field.GetNumValues(); i++)
                        {
                            long value = Convert.ToInt64(field.GetRawValue(i));

                            foreach (Field fieldIn in newMesg.FieldsList)
                            {
                                foreach (FieldComponent fc in fieldIn.components)
                                {
                                    if ((fc.fieldNum == field.Num) && (fc.accumulate))
                                    {
                                        value = (long)((((value / field.Scale) - field.Offset) + fc.offset) * fc.scale);
                                    }
                                }
                            }
                            accumulator.Set(newMesg.Num, field.Num, value);
                        }
                    }
                }

                // Now that the entire message is decoded we can evaluate subfields and expand any components
                newMesg.ExpandComponents(accumulator);

                RaiseMesgEvent(newMesg);
            }
            else
            {
                throw new FitException("Decode:Read - FIT decode error: Unexpected Record Header Byte 0x" + nextByte.ToString("X") + " at stream position: " + fitStream.Position);
            }
        }
Exemplo n.º 5
0
        public void DecodeNextMessage(Stream fitStream)
        {
            BinaryReader br = new BinaryReader(fitStream);
             byte nextByte = br.ReadByte();

             // Is it a compressed timestamp mesg?
             if ((nextByte & Fit.CompressedHeaderMask) == Fit.CompressedHeaderMask)
             {
            MemoryStream mesgBuffer = new MemoryStream();

            int timeOffset = nextByte & Fit.CompressedTimeMask;
            timestamp += (uint)((timeOffset - lastTimeOffset) & Fit.CompressedTimeMask);
            lastTimeOffset = timeOffset;
            Field timestampField = new Field(Profile.mesgs[Profile.RecordIndex].GetField("Timestamp"));
            timestampField.SetValue(timestamp);

            byte localMesgNum = (byte)((nextByte & Fit.CompressedLocalMesgNumMask) >> 5);
            mesgBuffer.WriteByte(localMesgNum);
            if (localMesgDefs[localMesgNum] == null)
            {
               throw new FitException("Decode:DecodeNextMessage - FIT decode error: Missing message definition for local message number " + localMesgNum + ".");
            }
            int fieldsSize = localMesgDefs[localMesgNum].GetMesgSize() - 1;
            try
            {
               mesgBuffer.Write(br.ReadBytes(fieldsSize), 0, fieldsSize);
            }
            catch (IOException e)
            {
               throw new FitException("Decode:DecodeNextMessage - Compressed Data Message unexpected end of file.  Wanted " + fieldsSize + " bytes at stream position " + fitStream.Position, e);
            }

            Mesg newMesg = new Mesg(mesgBuffer, localMesgDefs[localMesgNum]);
            newMesg.InsertField(0, timestampField);
            if (MesgEvent != null)
            {
               MesgEvent(this, new MesgEventArgs(newMesg));
            }
             }
             // Is it a mesg def?
             else if ((nextByte & Fit.HeaderTypeMask) == Fit.MesgDefinitionMask)
             {
            MemoryStream mesgDefBuffer = new MemoryStream();

            // Figure out number of fields (length) of our defn and build buffer
            mesgDefBuffer.WriteByte(nextByte);
            mesgDefBuffer.Write(br.ReadBytes(4), 0, 4);
            byte numfields = br.ReadByte();
            mesgDefBuffer.WriteByte(numfields);
            try
            {
               mesgDefBuffer.Write(br.ReadBytes(numfields * 3), 0, numfields * 3);
            }
            catch (IOException e)
            {
               throw new FitException("Decode:DecodeNextMessage - Defn Message unexpected end of file.  Wanted " + (numfields * 3) + " bytes at stream position " + fitStream.Position, e);
            }

            MesgDefinition newMesgDef = new MesgDefinition(mesgDefBuffer);
            localMesgDefs[newMesgDef.LocalMesgNum] = newMesgDef;
            if (MesgDefinitionEvent != null)
            {
               MesgDefinitionEvent(this, new MesgDefinitionEventArgs(newMesgDef));
            }
             }
             // Is it a data mesg?
             else if ((nextByte & Fit.HeaderTypeMask) == Fit.MesgHeaderMask)
             {
            MemoryStream mesgBuffer = new MemoryStream();

            byte localMesgNum = (byte)(nextByte & Fit.LocalMesgNumMask);
            mesgBuffer.WriteByte(localMesgNum);
            if (localMesgDefs[localMesgNum] == null)
            {
               throw new FitException("Decode:DecodeNextMessage - FIT decode error: Missing message definition for local message number " + localMesgNum + ".");
            }
            int fieldsSize = localMesgDefs[localMesgNum].GetMesgSize() - 1;
            try
            {
               mesgBuffer.Write(br.ReadBytes(fieldsSize), 0, fieldsSize);
            }
            catch (IOException e)
            {
               throw new FitException("Decode:DecodeNextMessage - Data Message unexpected end of file.  Wanted " + fieldsSize + " bytes at stream position " + fitStream.Position, e);
            }

            Mesg newMesg = new Mesg(mesgBuffer, localMesgDefs[localMesgNum]);
            // If the new message contains a timestamp field, record the value to use as
            // a reference for compressed timestamp headers
            Field timestampField = newMesg.GetField("Timestamp");
            if (timestampField != null)
            {
               timestamp = (uint)timestampField.GetValue();
               lastTimeOffset = (int)timestamp & Fit.CompressedTimeMask;
            }

            if (MesgEvent != null)
            {
               MesgEvent(this, new MesgEventArgs(newMesg));
            }
             }
             else
             {
            throw new FitException("Decode:Read - FIT decode error: Unexpected Record Header Byte 0x" + nextByte.ToString("X"));
             }
        }
Exemplo n.º 6
0
            /// <summary>
            /// Checks if the reference fields in a given message indicate the subfield (alternate)
            /// definition should be used
            /// </summary>
            /// <param name="mesg">message of interest</param>
            /// <returns>true if the subfield is active</returns>                  
            internal bool CanMesgSupport(Mesg mesg)
            {
                Field field = mesg.GetField(refFieldNum);

                if (field != null)
                {
                   object value = field.GetValue(0, Fit.SubfieldIndexMainField);
                   if (value == refFieldValue)
                   {
                  return true;
                   }
                }
                return false;
            }
Exemplo n.º 7
0
        public void DecodeNextMessage(Stream fitStream)
        {
            BinaryReader br       = new BinaryReader(fitStream);
            byte         nextByte = br.ReadByte();

            // Is it a compressed timestamp mesg?
            if ((nextByte & Fit.CompressedHeaderMask) == Fit.CompressedHeaderMask)
            {
                MemoryStream mesgBuffer = new MemoryStream();

                int timeOffset = nextByte & Fit.CompressedTimeMask;
                timestamp     += (uint)((timeOffset - lastTimeOffset) & Fit.CompressedTimeMask);
                lastTimeOffset = timeOffset;
                Field timestampField = new Field(Profile.mesgs[Profile.RecordIndex].GetField("Timestamp"));
                timestampField.SetValue(timestamp);

                byte localMesgNum = (byte)((nextByte & Fit.CompressedLocalMesgNumMask) >> 5);
                mesgBuffer.WriteByte(localMesgNum);
                if (localMesgDefs[localMesgNum] == null)
                {
                    throw new FitException("Decode:DecodeNextMessage - FIT decode error: Missing message definition for local message number " + localMesgNum + ".");
                }
                int fieldsSize = localMesgDefs[localMesgNum].GetMesgSize() - 1;
                try
                {
                    mesgBuffer.Write(br.ReadBytes(fieldsSize), 0, fieldsSize);
                }
                catch (IOException e)
                {
                    throw new FitException("Decode:DecodeNextMessage - Compressed Data Message unexpected end of file.  Wanted " + fieldsSize + " bytes at stream position " + fitStream.Position, e);
                }

                Mesg newMesg = new Mesg(mesgBuffer, localMesgDefs[localMesgNum]);
                newMesg.InsertField(0, timestampField);
                if (MesgEvent != null)
                {
                    MesgEvent(this, new MesgEventArgs(newMesg));
                }
            }
            // Is it a mesg def?
            else if ((nextByte & Fit.HeaderTypeMask) == Fit.MesgDefinitionMask)
            {
                MemoryStream mesgDefBuffer = new MemoryStream();

                // Figure out number of fields (length) of our defn and build buffer
                mesgDefBuffer.WriteByte(nextByte);
                mesgDefBuffer.Write(br.ReadBytes(4), 0, 4);
                byte numfields = br.ReadByte();
                mesgDefBuffer.WriteByte(numfields);
                try
                {
                    mesgDefBuffer.Write(br.ReadBytes(numfields * 3), 0, numfields * 3);
                }
                catch (IOException e)
                {
                    throw new FitException("Decode:DecodeNextMessage - Defn Message unexpected end of file.  Wanted " + (numfields * 3) + " bytes at stream position " + fitStream.Position, e);
                }

                MesgDefinition newMesgDef = new MesgDefinition(mesgDefBuffer);
                localMesgDefs[newMesgDef.LocalMesgNum] = newMesgDef;
                if (MesgDefinitionEvent != null)
                {
                    MesgDefinitionEvent(this, new MesgDefinitionEventArgs(newMesgDef));
                }
            }
            // Is it a data mesg?
            else if ((nextByte & Fit.HeaderTypeMask) == Fit.MesgHeaderMask)
            {
                MemoryStream mesgBuffer = new MemoryStream();

                byte localMesgNum = (byte)(nextByte & Fit.LocalMesgNumMask);
                mesgBuffer.WriteByte(localMesgNum);
                if (localMesgDefs[localMesgNum] == null)
                {
                    throw new FitException("Decode:DecodeNextMessage - FIT decode error: Missing message definition for local message number " + localMesgNum + ".");
                }
                int fieldsSize = localMesgDefs[localMesgNum].GetMesgSize() - 1;
                try
                {
                    mesgBuffer.Write(br.ReadBytes(fieldsSize), 0, fieldsSize);
                }
                catch (Exception e)
                {
                    throw new FitException("Decode:DecodeNextMessage - Data Message unexpected end of file.  Wanted " + fieldsSize + " bytes at stream position " + fitStream.Position, e);
                }

                Mesg newMesg = new Mesg(mesgBuffer, localMesgDefs[localMesgNum]);

                // If the new message contains a timestamp field, record the value to use as
                // a reference for compressed timestamp headers
                Field timestampField = newMesg.GetField("Timestamp");
                if (timestampField != null)
                {
                    timestamp      = (uint)timestampField.GetValue();
                    lastTimeOffset = (int)timestamp & Fit.CompressedTimeMask;
                }
                // Now that the entire message is decoded we can evaluate subfields and expand any components
                newMesg.ExpandComponents();

                if (MesgEvent != null)
                {
                    MesgEvent(this, new MesgEventArgs(newMesg));
                }
            }
            else
            {
                throw new FitException("Decode:Read - FIT decode error: Unexpected Record Header Byte 0x" + nextByte.ToString("X"));
            }
        }
Exemplo n.º 8
0
            /// <summary>
            /// Checks if the reference fields in a given message indicate the subfield (alternate)
            /// definition should be used
            /// </summary>
            /// <param name="mesg">message of interest</param>
            /// <returns>true if the subfield is active</returns>                  
            internal bool CanMesgSupport(Mesg mesg)
            {
                Field field = mesg.GetField(refFieldNum);

                if (field != null)
                {
                   object value = field.GetValue(0, Fit.SubfieldIndexMainField);
                   // Float refvalues are not supported
                   if (Convert.ToInt64(value) == Convert.ToInt64(refFieldValue))
                   {
                  return true;
                   }
                }
                return false;
            }