Encode.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #region Copyright
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // The following FIT Protocol software provided may be used with FIT protocol
  4. // devices only and remains the copyrighted property of Dynastream Innovations Inc.
  5. // The software is being provided on an "as-is" basis and as an accommodation,
  6. // and therefore all warranties, representations, or guarantees of any kind
  7. // (whether express, implied or statutory) including, without limitation,
  8. // warranties of merchantability, non-infringement, or fitness for a particular
  9. // purpose, are specifically disclaimed.
  10. //
  11. // Copyright 2016 Dynastream Innovations Inc.
  12. ////////////////////////////////////////////////////////////////////////////////
  13. // ****WARNING**** This file is auto-generated! Do NOT edit this file.
  14. // Profile Version = 16.60Release
  15. // Tag = production-akw-16.60.00-0-g5d3d436
  16. ////////////////////////////////////////////////////////////////////////////////
  17. #endregion
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Diagnostics;
  21. using System.Text;
  22. using System.IO;
  23. namespace Dynastream.Fit
  24. {
  25. /// <summary>
  26. /// Supports generating binary .FIT files. Header, Message Definition and Message
  27. /// data may be written.
  28. /// </summary>
  29. public class Encode
  30. {
  31. #region Fields
  32. private MesgDefinition[] lastMesgDef = new MesgDefinition[Fit.MaxLocalMesgs];
  33. private bool open = false;
  34. private Stream fitDest;
  35. /// <summary>
  36. /// If default ctor is used Header object may be manipulated if desired before Open is called.
  37. /// </summary>
  38. public Header header = new Header();
  39. #endregion // Fields
  40. #region Properties
  41. #endregion // Properties
  42. #region Constructors
  43. public Encode()
  44. {
  45. }
  46. public Encode(Stream fitDest)
  47. {
  48. Open(fitDest);
  49. }
  50. #endregion // Constructors
  51. #region Methods
  52. public void Open(Stream fitDest)
  53. {
  54. this.fitDest = fitDest;
  55. open = true;
  56. // Write header so we are ready to append messages
  57. header.Write(this.fitDest);
  58. }
  59. public void OnMesgDefinition(MesgDefinition newMesgDefinition)
  60. {
  61. Write(newMesgDefinition);
  62. }
  63. public void OnMesg(Mesg newMesg)
  64. {
  65. Write(newMesg);
  66. }
  67. public void Write(MesgDefinition mesgDefinition)
  68. {
  69. if (open == false)
  70. {
  71. throw new FitException("Encode:Write - Encode not opened yet, must call Encode:Open()");
  72. }
  73. mesgDefinition.Write(fitDest);
  74. lastMesgDef[mesgDefinition.LocalMesgNum] = mesgDefinition;
  75. }
  76. public void Write(Mesg mesg)
  77. {
  78. if (open == false)
  79. {
  80. throw new FitException("Encode:Write - Encode not opened yet, must call Encode:Open()");
  81. }
  82. // Fit file must always contain a defn message before data messages
  83. if ((lastMesgDef[mesg.LocalNum] == null) || !lastMesgDef[mesg.LocalNum].Supports(mesg))
  84. {
  85. Write(new MesgDefinition(mesg));
  86. }
  87. mesg.Write(fitDest, lastMesgDef[mesg.LocalNum]);
  88. }
  89. public void Write(List<Mesg> mesgs)
  90. {
  91. foreach (Mesg mesg in mesgs)
  92. {
  93. Write(mesg);
  94. }
  95. }
  96. /// <summary>
  97. /// Updates the data size and CRC in the file header
  98. /// Updates file CRC
  99. /// </summary>
  100. public void Close()
  101. {
  102. if (open == false)
  103. {
  104. throw new FitException("Encode:Close - Encode not opened yet, must call Encode:Open()");
  105. }
  106. // Rewrites the header now that the datasize is known
  107. header.DataSize = (uint)(fitDest.Length - header.Size);
  108. header.UpdateCRC();
  109. header.Write(fitDest);
  110. // Compute and write the file CRC to the end of the file
  111. byte[] data = new byte[fitDest.Length];
  112. fitDest.Position = 0;
  113. fitDest.Read(data, 0, data.Length);
  114. ushort fileCrc = CRC.Calc16(data, data.Length);
  115. byte[] buffer = BitConverter.GetBytes(fileCrc);
  116. fitDest.Write(buffer, 0, 2);
  117. }
  118. #endregion // Methods
  119. } // Class
  120. } // namespace