public FitDecoder(Stream stream, Dynastream.Fit.File fileType) { inputStream = stream; this.fileType = fileType; Messages = new FitMessages(); }
static void CreateActivityFile(List <Mesg> messages, String filename, Dynastream.Fit.DateTime startTime) { // The combination of file type, manufacturer id, product id, and serial number should be unique. // When available, a non-random serial number should be used. Dynastream.Fit.File fileType = Dynastream.Fit.File.Activity; ushort manufacturerId = Manufacturer.Development; ushort productId = 0; float softwareVersion = 1.0f; Random random = new Random(); uint serialNumber = (uint)random.Next(); // Every FIT file MUST contain a File ID message var fileIdMesg = new FileIdMesg(); fileIdMesg.SetType(fileType); fileIdMesg.SetManufacturer(manufacturerId); fileIdMesg.SetProduct(productId); fileIdMesg.SetTimeCreated(startTime); fileIdMesg.SetSerialNumber(serialNumber); // A Device Info message is a BEST PRACTICE for FIT ACTIVITY files var deviceInfoMesg = new DeviceInfoMesg(); deviceInfoMesg.SetDeviceIndex(DeviceIndex.Creator); deviceInfoMesg.SetManufacturer(Manufacturer.Development); deviceInfoMesg.SetProduct(productId); deviceInfoMesg.SetProductName("FIT Cookbook"); // Max 20 Chars deviceInfoMesg.SetSerialNumber(serialNumber); deviceInfoMesg.SetSoftwareVersion(softwareVersion); deviceInfoMesg.SetTimestamp(startTime); // Create the output stream, this can be any type of stream, including a file or memory stream. Must have read/write access FileStream fitDest = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.Read); // Create a FIT Encode object Encode encoder = new Encode(ProtocolVersion.V20); // Write the FIT header to the output stream encoder.Open(fitDest); // Write the messages to the file, in the proper sequence encoder.Write(fileIdMesg); encoder.Write(deviceInfoMesg); foreach (Mesg message in messages) { encoder.Write(message); } // Update the data size in the header and calculate the CRC encoder.Close(); // Close the output stream fitDest.Close(); Console.WriteLine($"Encoded FIT file {fitDest.Name}"); }
static void CreateWorkout(WorkoutMesg workoutMesg, List <WorkoutStepMesg> workoutSteps) { // The combination of file type, manufacturer id, product id, and serial number should be unique. // When available, a non-random serial number should be used. Dynastream.Fit.File fileType = Dynastream.Fit.File.Workout; ushort manufacturerId = Manufacturer.Development; ushort productId = 0; Random random = new Random(); uint serialNumber = (uint)random.Next(); // Every FIT file MUST contain a File ID message var fileIdMesg = new FileIdMesg(); fileIdMesg.SetType(fileType); fileIdMesg.SetManufacturer(manufacturerId); fileIdMesg.SetProduct(productId); fileIdMesg.SetTimeCreated(new Dynastream.Fit.DateTime(System.DateTime.UtcNow)); fileIdMesg.SetSerialNumber(serialNumber); // Create the output stream, this can be any type of stream, including a file or memory stream. Must have read/write access FileStream fitDest = new FileStream($"{workoutMesg.GetWktNameAsString().Replace(' ', '_')}.fit", FileMode.Create, FileAccess.ReadWrite, FileShare.Read); // Create a FIT Encode object Encode encoder = new Encode(ProtocolVersion.V10); // Write the FIT header to the output stream encoder.Open(fitDest); // Write the messages to the file, in the proper sequence encoder.Write(fileIdMesg); encoder.Write(workoutMesg); foreach (WorkoutStepMesg workoutStep in workoutSteps) { encoder.Write(workoutStep); } // Update the data size in the header and calculate the CRC encoder.Close(); // Close the output stream fitDest.Close(); Console.WriteLine($"Encoded FIT file {fitDest.Name}"); }