public bool Duplicate(Stream dest, Stream src) { var decode = new Decode(); if (!decode.CheckIntegrity(src)) { Console.WriteLine("FIT file integrity failed."); return false; } fitDest = dest; // Copy header. var header = new Header(src); header.Write(fitDest); // Copy body. decode.MesgDefinitionEvent += this.OnMesgDefinition; decode.MesgEvent += this.OnMesg; var result = decode.Read(src); // Update header. (data size) fitDest.Position = 4; fitDest.Write(BitConverter.GetBytes((uint)(fitDest.Length - header.Size)), 0, 4); // Update CRC. var data = new byte[fitDest.Length]; fitDest.Position = 0; fitDest.Read(data, 0, data.Length); fitDest.Write(BitConverter.GetBytes(CRC.Calc16(data, data.Length)), 0, 2); fitDest = null; Array.Clear(mesgDefinitions, 0, mesgDefinitions.Length); return result; }
static void Main(string[] args) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Console.WriteLine("FIT Decode Example Application"); if (args.Length != 1) { Console.WriteLine("Usage: decode.exe <filename>"); return; } // Attempt to open .FIT file fitSource = new FileStream(args[0], FileMode.Open); Console.WriteLine("Opening {0}", args[0]); Decode decodeDemo = new Decode(); MesgBroadcaster mesgBroadcaster = new MesgBroadcaster(); // Connect the Broadcaster to our event (message) source (in this case the Decoder) decodeDemo.MesgEvent += mesgBroadcaster.OnMesg; decodeDemo.MesgDefinitionEvent += mesgBroadcaster.OnMesgDefinition; // Subscribe to message events of interest by connecting to the Broadcaster mesgBroadcaster.MesgEvent += new MesgEventHandler(OnMesg); mesgBroadcaster.MesgDefinitionEvent += new MesgDefinitionEventHandler(OnMesgDefn); mesgBroadcaster.FileIdMesgEvent += new MesgEventHandler(OnFileIDMesg); mesgBroadcaster.UserProfileMesgEvent += new MesgEventHandler(OnUserProfileMesg); bool status = decodeDemo.IsFIT(fitSource); status &= decodeDemo.CheckIntegrity(fitSource); // Process the file if (status == true) { Console.WriteLine("Decoding..."); decodeDemo.Read(fitSource); Console.WriteLine("Decoded FIT file {0}", args[0]); } else { try { Console.WriteLine("Integrity Check Failed {0}", args[0]); Console.WriteLine("Attempting to decode..."); decodeDemo.Read(fitSource); } catch (FitException ex) { Console.WriteLine("DecodeDemo caught FitException: " + ex.Message); } } fitSource.Close(); Console.WriteLine(""); Console.WriteLine("Summary:"); int totalMesgs = 0; foreach (KeyValuePair<ushort, int> pair in mesgCounts) { Console.WriteLine("MesgID {0,3} Count {1}", pair.Key, pair.Value); totalMesgs += pair.Value; } Console.WriteLine("{0} Message Types {1} Total Messages", mesgCounts.Count, totalMesgs); stopwatch.Stop(); Console.WriteLine(""); Console.WriteLine("Time elapsed: {0:0.#}s", stopwatch.Elapsed.TotalSeconds); Console.ReadKey(); return; }