FitTest.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using UnityEngine;
  2. using Dynastream.Fit;
  3. using System.IO;
  4. public class FitTest : MonoBehaviour
  5. {
  6. private void Start()
  7. {
  8. EncodeFitFile();
  9. }
  10. //FIT USAGE EXAMPLE
  11. void EncodeFitFile()
  12. {
  13. System.DateTime systemStartTime = System.DateTime.Now;
  14. System.DateTime systemTimeNow = systemStartTime;
  15. FileStream fitDest = new FileStream("ExampleMonitoringFile.fit", FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
  16. // Create file encode object
  17. Encode encodeDemo = new Encode();
  18. // Write our header
  19. encodeDemo.Open(fitDest);
  20. // Generate some FIT messages
  21. FileIdMesg fileIdMesg = new FileIdMesg(); // Every FIT file MUST contain a 'File ID' message as the first message
  22. fileIdMesg.SetSerialNumber(54321);
  23. fileIdMesg.SetTimeCreated(new Dynastream.Fit.DateTime(systemTimeNow));
  24. fileIdMesg.SetManufacturer(Manufacturer.Dynastream);
  25. fileIdMesg.SetProduct(1001);
  26. fileIdMesg.SetNumber(0);
  27. fileIdMesg.SetType(Dynastream.Fit.File.Activity); // See the 'FIT FIle Types Description' document for more information about this file type.
  28. encodeDemo.Write(fileIdMesg); // Write the 'File ID Message'
  29. DeviceInfoMesg deviceInfoMesg = new DeviceInfoMesg();
  30. deviceInfoMesg.SetTimestamp(new Dynastream.Fit.DateTime(systemTimeNow));
  31. deviceInfoMesg.SetSerialNumber(54321);
  32. deviceInfoMesg.SetManufacturer(Manufacturer.Dynastream);
  33. deviceInfoMesg.SetBatteryStatus(Dynastream.Fit.BatteryStatus.Good);
  34. encodeDemo.Write(deviceInfoMesg);
  35. MonitoringMesg monitoringMesg = new MonitoringMesg();
  36. // By default, each time a new message is written the Local Message Type 0 will be redefined to match the new message.
  37. // In this case,to avoid having a definition message each time there is a DeviceInfoMesg, we can manually set the Local Message Type of the MonitoringMessage to '1'.
  38. // By doing this we avoid an additional 7 definition messages in our FIT file.
  39. monitoringMesg.LocalNum = 1;
  40. // Simulate some data
  41. System.Random numberOfCycles = new System.Random(); // Fake a number of cycles
  42. for (int i = 0; i < 4; i++) // Each of these loops represent a quarter of a day
  43. {
  44. for (int j = 0; j < 6; j++) // Each of these loops represent 1 hour
  45. {
  46. monitoringMesg.SetTimestamp(new Dynastream.Fit.DateTime(systemTimeNow));
  47. monitoringMesg.SetActivityType(Dynastream.Fit.ActivityType.Walking); // Setting this to walking will cause Cycles to be interpretted as steps.
  48. monitoringMesg.SetCycles(monitoringMesg.GetCycles() + numberOfCycles.Next(0, 1000)); // Cycles are accumulated (i.e. must be increasing)
  49. encodeDemo.Write(monitoringMesg);
  50. systemTimeNow = systemTimeNow.AddHours(1); // Add an hour to our contrieved timestamp
  51. }
  52. deviceInfoMesg.SetTimestamp(new Dynastream.Fit.DateTime(systemTimeNow));
  53. deviceInfoMesg.SetBatteryStatus(Dynastream.Fit.BatteryStatus.Good); // Report the battery status every quarter day
  54. encodeDemo.Write(deviceInfoMesg);
  55. }
  56. // Update header datasize and file CRC
  57. encodeDemo.Close();
  58. fitDest.Close();
  59. }
  60. }