Exemplo n.º 1
0
        private void writeDefintion(string[] str)
        {
            byte        header          = byte.Parse(str[1]);
            int         localMesgIndex  = header & 0x0f;
            byte        reserved        = byte.Parse(str[2]);
            localMsgDef msgDef          = new localMsgDef();
            byte        arch            = msgDef.Architecture = byte.Parse(str[3]);
            UInt16      globalMesgIndex = msgDef.globalMesgIndex = UInt16.Parse(str[4]);
            byte        fields          = msgDef.Fields = byte.Parse(str[5]);

            msgDef.localFields            = new localField[fields];
            msgDef.mesg                   = FIT.getMessageStruct(msgDef.globalMesgIndex);
            localMesgDefs[localMesgIndex] = msgDef;
            fitstream.writeValue(header);
            fitstream.writeValue(reserved);
            fitstream.writeValue(arch);
            fitstream.writeValue(globalMesgIndex, 0);
            fitstream.writeValue(fields);

            for (int i = 0; i < fields; i++)
            {
                localField fielddef = new localField();
                byte       FieldDefinitionNumber = fielddef.FieldDefinitionNumber = byte.Parse(str[6 + (i) * 3]);
                byte       size     = fielddef.size = byte.Parse(str[7 + (i) * 3]);
                byte       baseType = fielddef.baseType = byte.Parse(str[8 + (i) * 3]);
                fielddef.GlobalField = FIT.getFieldStruct(msgDef.mesg, (int)FieldDefinitionNumber);
                localMesgDefs[localMesgIndex].localFields[i] = fielddef;
                fitstream.writeValue(FieldDefinitionNumber);
                fitstream.writeValue(size);
                fitstream.writeValue(baseType);
            }
        }
Exemplo n.º 2
0
        private void writeDefintion(string[] str)
        {
            byte header = byte.Parse(str[1]);
            int localMesgIndex = header & 0x0f;
            byte reserved = byte.Parse(str[2]);
            localMsgDef msgDef = new localMsgDef();
            byte arch = msgDef.Architecture = byte.Parse(str[3]);
            UInt16 globalMesgIndex = msgDef.globalMesgIndex = UInt16.Parse(str[4]);
            byte fields = msgDef.Fields = byte.Parse(str[5]);
            msgDef.localFields = new localField[fields];
            msgDef.mesg = FIT.getMessageStruct(msgDef.globalMesgIndex);
            localMesgDefs[localMesgIndex] = msgDef;
            fitstream.writeValue(header);
            fitstream.writeValue(reserved);
            fitstream.writeValue(arch);
            fitstream.writeValue(globalMesgIndex, 0);
            fitstream.writeValue(fields);

            for (int i = 0; i < fields; i++)
            {
                localField fielddef = new localField();
                byte FieldDefinitionNumber = fielddef.FieldDefinitionNumber = byte.Parse(str[6 + (i) * 3]);
                byte size = fielddef.size = byte.Parse(str[7 + (i) * 3]);
                byte baseType = fielddef.baseType = byte.Parse(str[8 + (i) * 3]);
                fielddef.GlobalField = FIT.getFieldStruct(msgDef.mesg, (int)FieldDefinitionNumber);
                localMesgDefs[localMesgIndex].localFields[i] = fielddef;
                fitstream.writeValue(FieldDefinitionNumber);
                fitstream.writeValue(size);
                fitstream.writeValue(baseType);
            }
        }
Exemplo n.º 3
0
            private string readFieldDef()
            {
                localMsgDef msgDef = new localMsgDef();

                msgDef.reserved = file.readbyte();
                if (msgDef.reserved != 0)
                {
                    return("");
                }

                msgDef.Architecture = file.readbyte();
                if (msgDef.Architecture > 1)
                {
                    return("");
                }

                msgDef.globalMesgIndex = file.readUInt16(msgDef.Architecture);
                if (msgDef.globalMesgIndex > 255 && msgDef.globalMesgIndex < 0xFE00)
                {
                    return("");
                }
                msgDef.mesg = FIT.getMessageStruct(msgDef.globalMesgIndex);

                msgDef.Fields = file.readbyte();
                if (msgDef.Fields > msgDef.mesg.fields.Length + 10)
                {
                    return("");
                }

                recordString          += (string.Format(";{0};{1};{2};{3}", msgDef.reserved, msgDef.Architecture, msgDef.globalMesgIndex, msgDef.Fields));
                recordDefinitionHeader = ";;reserved;Architecture;globalMesgIndex;Fields";
                msgDef.localFields     = new localField[msgDef.Fields];
                for (int i = 0; i < msgDef.Fields; i++)
                {
                    localField fieldDef = new localField();
                    byte       d        = fieldDef.FieldDefinitionNumber = file.readbyte();
                    byte       s        = fieldDef.size = file.readbyte();
                    byte       b        = fieldDef.baseType = file.readbyte();
                    fieldDef.arch        = (((b & 0x80) == 0x80) && msgDef.Architecture == 1) ? 1 : 0;;
                    fieldDef.GlobalField = FIT.getFieldStruct(msgDef.mesg, d);
                    {
                        if (d == 253)
                        {
                            msgDef.isCompressedMsg = false;
                        }
                        //if (fieldDef.field.valid && (fieldDef.field.type != b || fieldDef.field.num != d)) return "";
                        if (!(d <= 80 || d >= 253))
                        {
                            return("");
                        }
                        if (!(b == 0x00 && s == 1 || b == 0x01 && s == 1 || b == 0x02 && s == 1 || b == 0x83 && s == 2 || b == 0x84 && s == 2 || b == 0x85 && s == 4 || b == 0x86 && s == 4 || b == 0x07 && s > 0 || b == 0x88 && s == 4 || b == 0x89 && s == 8 || b == 0x0A && s == 1 || b == 0x8B && s == 2 || b == 0x8C && s == 4 || b == 0x0D && s > 0))
                        {
                            return("");
                        }
                        recordString += (string.Format(";{0};{1};{2}", d, s, b));
                    }
                    msgDef.localFields[i] = fieldDef;
                }
                msgDef.isValid = true;
                localMsgDef    = localMsgDefs[localMesgIndex] = msgDef;
                return(string.Format("{0}\n{1}\n{2}", recordDefinitionHeader, recordString, getHeader(localMesgIndex)));
            }
Exemplo n.º 4
0
            private string readFieldData()
            {
                string str = "";

                localMsgDef = localMsgDefs[localMesgIndex];
                foreach (localField localField in localMsgDef.localFields)
                {
                    str = file.readValue(localField);
                    globalField globalField = localField.GlobalField;
                    if (globalField.foutMarge >= 0)
                    {
                        Int64 value = file.getLastValue();
                        if (file.isLastValueValid)
                        {
                            if (localField.lastGoodValue == Int64.MinValue)
                            {
                                localField.lastGoodValue = value;
                            }
                            else
                            if (Math.Abs(localField.lastGoodValue - value) > globalField.foutMarge)
                            {
                                return("");
                            }
                            else
                            {
                                localField.lastGoodValue = value;
                            }
                        }
                        else
                        {
                            if (globalField.name == "software_version" || globalField.name == "device_index") //verplicht veld
                            {
                                return("");
                            }
                        }
                    }

                    if (globalField.name == "timestamp")
                    {
                        str = FIT.timestampToLocalTime(UInt32.Parse(str));
                    }
                    recordString += ";" + str;
                    switch (globalField.name)
                    {
                    case "timestamp": timestamp = file._Uint32; break;

                    case "position_lat": position_lat = file._int32; break;

                    case "position_long": position_long = file._int32; break;

                    case "altitude": altitude = file._Uint16; break;

                    case "distance": distance = file._Uint32; break;

                    case "speed": speed = file._Uint16; break;

                    case "power": power = file._Uint16; break;

                    case "cadence": cadence = file._byte; break;

                    case "event": _event = file._enum; break;

                    case "event_type": _event_type = file._enum; break;
                    }
                }
                return(recordString);
            }
Exemplo n.º 5
0
        public fitFileReader(FileStream file, StreamWriter outFile)
        {
            string ErrorBytes = "";
            int    RecordsOk  = 5;

            localMsgDef[] localMesgDefs = new localMsgDef[16];
            FitRecord     record        = new FitRecord(file);

            long   pos = file.Position = (byte)file.ReadByte() == 14 ? 14 : 12;
            UInt32 TraveltotalSeconds   = 0;
            UInt32 TravelStartTimestamp = 0;
            UInt32 TravelEndTimestamp   = 0;
            UInt32 TraveltotalMeters    = 0;
            UInt32 TravelStartMeters    = 0;
            UInt32 TravelEndMeters      = 0;
            UInt32 maxSpeed             = 0;
            UInt32 totPower             = 0;
            UInt32 PowertotalSeconds    = 0;
            UInt32 PowerStartTimestamp  = 0;
            UInt32 PowerEndTimestamp    = 0;


            while (true)
            {
                try
                {
                    if (RecordsOk > 5)
                    {
                        pos = file.Position;
                    }
                    string str = record.read(localMesgDefs);
                    if (str == "")
                    {
                        if (pos + 2 >= file.Length)
                        {
                            break;
                        }
                        file.Position = pos++;
                        ErrorBytes   += ((RecordsOk > 0) ? string.Format("Error at pos {0} byte:", (file.Position).ToString("X4")) : "") + file.ReadByte().ToString("X2");
                        RecordsOk     = 0;
                    }
                    else
                    {
                        RecordsOk++;
                        if (ErrorBytes != "")
                        {
                            outFile.WriteLine(ErrorBytes);
                        }
                        ErrorBytes = "";
                        outFile.WriteLine(str);
                        if (record.localMsgDef.globalMesgIndex == FIT.FIT_MESG_NUM_RECORD && record.speed > 2000)
                        {
                            TravelEndTimestamp = record.timestamp;
                            TravelEndMeters    = record.distance;
                            if (TravelStartMeters == 0)
                            {
                                TravelStartTimestamp = TravelEndTimestamp;
                                TravelStartMeters    = TravelEndMeters;
                            }
                        }
                        else
                        {
                            TraveltotalSeconds  += TravelEndTimestamp - TravelStartTimestamp;
                            TraveltotalMeters   += TravelEndMeters - TravelStartMeters;
                            TravelStartTimestamp = TravelEndTimestamp = 0;
                            TravelStartMeters    = TravelEndMeters = 0;
                        }
                        if (record.localMsgDef.globalMesgIndex == FIT.FIT_MESG_NUM_RECORD && record.power > 0 && record.power != 0xFFFF)
                        {
                            PowerEndTimestamp = record.timestamp;
                            totPower         += record.power;
                            if (PowerStartTimestamp == 0)
                            {
                                PowerStartTimestamp = PowerEndTimestamp;
                            }
                        }
                        else
                        {
                            PowertotalSeconds  += PowerEndTimestamp - PowerStartTimestamp;
                            PowerStartTimestamp = PowerEndTimestamp = 0;
                        }
                    }
                }
                catch (IOException e) { break; }
                catch (Exception ex) { Console.WriteLine(ex.Message);; }
            }
            closeSession(outFile, localMesgDefs, TravelEndTimestamp);
            if (ErrorBytes != "")
            {
                outFile.WriteLine(ErrorBytes);
            }
            outFile.WriteLine(String.Format("Tijd: {0}, Afstand: {1}, Snelheid: {2}. Gem Vermogen: {3}, Calorien: {4}", new TimeSpan(0, 0, (int)TraveltotalSeconds), (TraveltotalMeters / 100000.0).ToString("0.00"), ((TraveltotalMeters / 100000.0) / (TraveltotalSeconds / 3600.0)).ToString("0.0"), PowertotalSeconds == 0 ? 0 : totPower / PowertotalSeconds, totPower / 1000.0 * 1.1));
        }
Exemplo n.º 6
0
            private string readFieldDef()
            {
                localMsgDef msgDef = new localMsgDef();

                msgDef.reserved = file.readbyte();
                if (msgDef.reserved != 0) return "";

                msgDef.Architecture = file.readbyte();
                if (msgDef.Architecture > 1) return "";

                msgDef.globalMesgIndex = file.readUInt16(msgDef.Architecture);
                if (msgDef.globalMesgIndex > 255 && msgDef.globalMesgIndex < 0xFE00) return "";
                msgDef.mesg = FIT.getMessageStruct(msgDef.globalMesgIndex);

                msgDef.Fields = file.readbyte();
                if (msgDef.Fields > msgDef.mesg.fields.Length + 10) return "";

                recordString += (string.Format(";{0};{1};{2};{3}", msgDef.reserved, msgDef.Architecture, msgDef.globalMesgIndex, msgDef.Fields));
                recordDefinitionHeader = ";;reserved;Architecture;globalMesgIndex;Fields";
                msgDef.localFields = new localField[msgDef.Fields];
                for (int i = 0; i < msgDef.Fields; i++)
                {
                    localField fieldDef = new localField();
                    byte d = fieldDef.FieldDefinitionNumber = file.readbyte();
                    byte s = fieldDef.size = file.readbyte();
                    byte b = fieldDef.baseType = file.readbyte();
                    fieldDef.arch = (((b & 0x80) == 0x80) && msgDef.Architecture == 1) ? 1 : 0; ;
                    fieldDef.GlobalField = FIT.getFieldStruct(msgDef.mesg, d);
                    {
                        if (d == 253) msgDef.isCompressedMsg = false;
                        //if (fieldDef.field.valid && (fieldDef.field.type != b || fieldDef.field.num != d)) return "";
                        if (!(d <= 80 || d >= 253)) return "";
                        if (!(b == 0x00 && s == 1 || b == 0x01 && s == 1 || b == 0x02 && s == 1 || b == 0x83 && s == 2 || b == 0x84 && s == 2 || b == 0x85 && s == 4 || b == 0x86 && s == 4 || b == 0x07 && s > 0 || b == 0x88 && s == 4 || b == 0x89 && s == 8 || b == 0x0A && s == 1 || b == 0x8B && s == 2 || b == 0x8C && s == 4 || b == 0x0D && s > 0)) return "";
                        recordString += (string.Format(";{0};{1};{2}", d, s, b));
                    }
                    msgDef.localFields[i] = fieldDef;
                }
                msgDef.isValid = true;
                localMsgDef = localMsgDefs[localMesgIndex] = msgDef;
                return string.Format("{0}\n{1}\n{2}", recordDefinitionHeader, recordString, getHeader(localMesgIndex));
            }
Exemplo n.º 7
0
            private string readFieldData()
            {
                string str = "";
                localMsgDef = localMsgDefs[localMesgIndex];
                foreach (localField localField in localMsgDef.localFields)
                {
                    str = file.readValue(localField);
                    globalField globalField = localField.GlobalField;
                    if (globalField.foutMarge >= 0)
                    {
                        Int64 value = file.getLastValue();
                        if (file.isLastValueValid)
                            if (localField.lastGoodValue == Int64.MinValue)
                                localField.lastGoodValue = value;
                            else
                                if (Math.Abs(localField.lastGoodValue - value) > globalField.foutMarge)
                                    return "";
                                else
                                    localField.lastGoodValue = value;
                        else
                        {
                            if (globalField.name == "software_version" || globalField.name == "device_index") //verplicht veld
                                return "";
                        }
                    }

                    if (globalField.name == "timestamp") str = FIT.timestampToLocalTime(UInt32.Parse(str));
                    recordString += ";" + str;
                    switch (globalField.name)
                    {
                        case "timestamp": timestamp = file._Uint32; break;
                        case "position_lat": position_lat = file._int32; break;
                        case "position_long": position_long = file._int32; break;
                        case "altitude": altitude = file._Uint16; break;
                        case "distance": distance = file._Uint32; break;
                        case "speed": speed = file._Uint16; break;
                        case "power": power = file._Uint16; break;
                        case "cadence": cadence = file._byte; break;
                        case "event": _event = file._enum; break;
                        case "event_type": _event_type = file._enum; break;
                    }
                }
                return recordString;
            }
Exemplo n.º 8
0
 public string read(localMsgDef[] lMesgDefs)
 {
     this.localMsgDefs = lMesgDefs;
     switch (readRecordHeader())
     {
         case recordtype.definition:
             return readFieldDef();
         case recordtype.data:
             return readFieldData();
         case recordtype.invalid:
             error = true;
             return "";
         default:
             throw new Exception("recordtype onbekend");
     }
 }
Exemplo n.º 9
0
        public fitFileReader(FileStream file, StreamWriter outFile)
        {
            string ErrorBytes = "";
            int RecordsOk = 5;
            localMsgDef[] localMesgDefs = new localMsgDef[16];
            FitRecord record = new FitRecord(file);

            long pos = file.Position = (byte)file.ReadByte() == 14 ? 14 : 12;
            UInt32 TraveltotalSeconds = 0;
            UInt32 TravelStartTimestamp = 0;
            UInt32 TravelEndTimestamp = 0;
            UInt32 TraveltotalMeters = 0;
            UInt32 TravelStartMeters = 0;
            UInt32 TravelEndMeters = 0;
            UInt32 maxSpeed = 0;
            UInt32 totPower = 0;
            UInt32 PowertotalSeconds = 0;
            UInt32 PowerStartTimestamp = 0;
            UInt32 PowerEndTimestamp = 0;

            while (true)
            {
                try
                {
                    if (RecordsOk > 5)
                        pos = file.Position;
                    string str = record.read(localMesgDefs);
                    if (str == "")
                    {
                        if (pos + 2 >= file.Length) break;
                        file.Position = pos++;
                        ErrorBytes += ((RecordsOk > 0) ? string.Format("Error at pos {0} byte:", (file.Position).ToString("X4")) : "") + file.ReadByte().ToString("X2");
                        RecordsOk = 0;
                    }
                    else
                    {
                        RecordsOk++;
                        if (ErrorBytes != "") outFile.WriteLine(ErrorBytes);
                        ErrorBytes = "";
                        outFile.WriteLine(str);
                        if (record.localMsgDef.globalMesgIndex == FIT.FIT_MESG_NUM_RECORD && record.speed > 2000)
                        {
                            TravelEndTimestamp = record.timestamp;
                            TravelEndMeters = record.distance;
                            if (TravelStartMeters == 0)
                            {
                                TravelStartTimestamp = TravelEndTimestamp;
                                TravelStartMeters = TravelEndMeters;
                            }
                        }
                        else
                        {
                            TraveltotalSeconds += TravelEndTimestamp - TravelStartTimestamp;
                            TraveltotalMeters += TravelEndMeters - TravelStartMeters;
                            TravelStartTimestamp = TravelEndTimestamp = 0;
                            TravelStartMeters = TravelEndMeters = 0;
                        }
                        if (record.localMsgDef.globalMesgIndex == FIT.FIT_MESG_NUM_RECORD && record.power > 0 && record.power != 0xFFFF)
                        {
                            PowerEndTimestamp = record.timestamp;
                            totPower += record.power;
                            if (PowerStartTimestamp == 0)
                            {
                                PowerStartTimestamp = PowerEndTimestamp;
                            }
                        }
                        else
                        {
                            PowertotalSeconds += PowerEndTimestamp - PowerStartTimestamp;
                            PowerStartTimestamp = PowerEndTimestamp = 0;
                        }

                    }
                }
                catch (IOException e) { break; }
                catch (Exception ex) { Console.WriteLine(ex.Message); ; }
            }
            closeSession(outFile, localMesgDefs, TravelEndTimestamp);
            if (ErrorBytes != "") outFile.WriteLine(ErrorBytes);
            outFile.WriteLine(String.Format("Tijd: {0}, Afstand: {1}, Snelheid: {2}. Gem Vermogen: {3}, Calorien: {4}", new TimeSpan(0, 0, (int)TraveltotalSeconds), (TraveltotalMeters / 100000.0).ToString("0.00"), ((TraveltotalMeters / 100000.0) / (TraveltotalSeconds / 3600.0)).ToString("0.0"), PowertotalSeconds == 0 ? 0 : totPower / PowertotalSeconds, totPower / 1000.0 * 1.1));
        }
Exemplo n.º 10
0
        //, DateTime dt, UInt32 start_position_lat, UInt32 start_position_long, UInt32 total_elapsed_time, UInt32 total_timer_time, UInt32 total_distance, UInt32 total_calories, UInt32 avg_speed, UInt32 max_speed, UInt32 avg_power, UInt32 max_power, UInt32 total_ascent, UInt32 total_descent, UInt32 avg_heart_rate, UInt32 max_heart_rate, UInt32 avg_cadence, UInt32 max_cadence)
        private void closeSession(StreamWriter outFile, localMsgDef[] localMsgDefs, UInt32 TravelEndTimestamp)
        {
            int _event = 0;
            int i = 0;
            for (i = 0; i < 16; i++)
            {

                if (localMsgDefs[i] == null) break;
                if (localMsgDefs[i].globalMesgIndex == FIT.FIT_MESG_NUM_EVENT) _event = i;
                if (localMsgDefs[i].globalMesgIndex == FIT.FIT_MESG_NUM_SESSION) return;
            }
            outFile.WriteLine(string.Format("def;{0};0;0;18;31;253;4;134;2;4;134;3;4;133;4;4;133;7;4;134;8;4;134;9;4;134;10;4;134;29;4;133;30;4;133;31;4;133;32;4;133;254;2;132;11;2;132;13;2;132;14;2;132;15;2;132;20;2;132;21;2;132;22;2;132;23;2;132;25;2;132;26;2;132;0;1;0;1;1;0;5;1;0;6;1;0;16;1;2;17;1;2;18;1;2;19;1;2", i + 64));
            outFile.WriteLine(string.Format("data;{0};{1};708788214;627355257;56710458;6176,85;5831,27;52979,47;;;;;;0;997;;9,085;13,627;231;712;137;133;0;53;SESSION;STOP;CYCLING;255;154;180;76;116", i, FIT.timestampToLocalTime(TravelEndTimestamp)));
            outFile.WriteLine(string.Format("data;{0};{1};1;SESSION;STOP_DISABLE_ALL;1", _event, FIT.timestampToLocalTime(TravelEndTimestamp)));
            outFile.WriteLine(string.Format("def;{0};0;0;34;6;253;4;134;0;4;134;1;2;132;2;1;0;3;1;0;4;1;0", i + 1 + 64));
            outFile.WriteLine(string.Format("data;{0};{1};5831,27;1;0;ACTIVITY;STOP", i + 1, FIT.timestampToLocalTime(TravelEndTimestamp)));
        }