TDMSFile.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using MessagePack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Threading;
  10. using TDMS.Common;
  11. using TDMS.Externals;
  12. namespace TDMS.Default
  13. {
  14. internal class TDMSFile : BaseTDMSLevel, ITDMSFile
  15. {
  16. #region Implementation of ITDMSFile
  17. /// <inheritdoc />
  18. [AllowNull]
  19. public TDMSFileInfo FileInfo { get; private set; }
  20. /// <inheritdoc />
  21. public bool Save()
  22. {
  23. var success = DDC.SaveFile(_SelfPtr);
  24. return success == 0;
  25. }
  26. public bool IsOpen => _SelfPtr != IntPtr.Zero;
  27. /// <inheritdoc />
  28. public bool Open(string filePath)
  29. {
  30. var tdmsFileInfo = new TDMSFileInfo(filePath);
  31. return Open(tdmsFileInfo);
  32. }
  33. /// <inheritdoc />
  34. public bool Open(TDMSFileInfo fileInfo)
  35. {
  36. if(!fileInfo.Exists)
  37. {
  38. return Create(fileInfo);
  39. }
  40. FileInfo = fileInfo;
  41. var success = DDC.OpenFile(fileInfo.FilePath, fileInfo.FileType, out var filePtr);
  42. if (success != Error.NoError) return false;
  43. _SelfPtr = filePtr;
  44. PropertyOperator = new FilePropertyOperator(_SelfPtr);
  45. if (TryGetProperty<DateTime>(Constants.DDC_FILE_DATETIME, out var dateTime))
  46. fileInfo.DateTime = dateTime;
  47. if(TryGetProperty(Constants.DDC_FILE_NAME, out string name))
  48. fileInfo.Name = name;
  49. if(TryGetProperty(Constants.DDC_FILE_DESCRIPTION, out string desc))
  50. fileInfo.Description = desc;
  51. if(TryGetProperty(Constants.DDC_FILE_TITLE, out string title))
  52. fileInfo.Title = title;
  53. if(TryGetProperty(Constants.DDC_FILE_AUTHOR, out string author))
  54. fileInfo.Author = author;
  55. SetNameAndDescription();
  56. return success == 0;
  57. }
  58. /// <inheritdoc />
  59. public bool Create(string filePath,
  60. string fileType,
  61. string name,
  62. string description,
  63. string title,
  64. string author)
  65. {
  66. var tdmsFileInfo = new TDMSFileInfo(filePath)
  67. {
  68. Name = name,
  69. Description = description,
  70. Title = title,
  71. Author = author
  72. };
  73. return Create(tdmsFileInfo);
  74. }
  75. /// <inheritdoc />
  76. public bool Create(TDMSFileInfo fileInfo)
  77. {
  78. FileInfo = fileInfo;
  79. var success = DDC.CreateFile(FileInfo.FilePath,
  80. FileInfo.FileType,
  81. FileInfo.Name,
  82. FileInfo.Description,
  83. FileInfo.Title,
  84. FileInfo.Author,
  85. out var filePtr);
  86. if (success != Error.NoError) return false;
  87. _SelfPtr = filePtr;
  88. PropertyOperator = new FilePropertyOperator(_SelfPtr);
  89. SetNameAndDescription();
  90. CreateOrUpdateProperty(Constants.DDC_FILE_DATETIME, FileInfo.DateTime); //即将存储,添加文件创建时间
  91. var isSave = Save();
  92. Thread.Sleep(6); //持久化到硬盘需要一些时间,略做等待
  93. return success == 0 && isSave;
  94. }
  95. /// <inheritdoc />
  96. public ITDMSChannelGroup? this[int index]
  97. {
  98. get
  99. {
  100. if (index < 0)
  101. throw new IndexOutOfRangeException($"Index[{index}] must be greater than or equal to 0");
  102. var count = (int)ChildCount;
  103. if (index >= count)
  104. throw new IndexOutOfRangeException($"Index[{index}] must be less than the number of channel groups");
  105. var channelGroupsBuffer = new IntPtr[count];
  106. var success = DDC.GetChannelGroups(_SelfPtr, channelGroupsBuffer, (uint)count);
  107. if(success!= Error.NoError)return null;
  108. var groupPtr = channelGroupsBuffer[index];
  109. return new TDMSChannelGroup(groupPtr);
  110. }
  111. }
  112. /// <inheritdoc />
  113. public ITDMSChannelGroup this[string groupName]
  114. {
  115. get
  116. {
  117. if(string.IsNullOrEmpty(groupName))
  118. throw new ArgumentNullException(nameof(groupName), "Group name cannot be null or empty");
  119. var count = ChildCount;
  120. if(count <= 0)
  121. return null;
  122. var channelGroupsBuffer = new IntPtr[count];
  123. var success = DDC.GetChannelGroups(_SelfPtr, channelGroupsBuffer, (uint)count);
  124. if (success != Error.NoError) return null;
  125. foreach (var intPtr in channelGroupsBuffer)
  126. {
  127. var group = new TDMSChannelGroup(intPtr);
  128. if(group.Name == groupName)
  129. return group;
  130. group.Dispose();
  131. }
  132. return null;
  133. }
  134. }
  135. /// <inheritdoc />
  136. public IDictionary<string, string> GetDefaultProperties()
  137. {
  138. var dict = new Dictionary<string, string>(5);
  139. GetDefaultPropertyToDictionary(Constants.DDC_FILE_NAME, dict);
  140. GetDefaultPropertyToDictionary(Constants.DDC_FILE_DESCRIPTION, dict);
  141. GetDefaultPropertyToDictionary(Constants.DDC_FILE_TITLE, dict);
  142. GetDefaultPropertyToDictionary(Constants.DDC_FILE_AUTHOR, dict);
  143. GetDefaultPropertyToDictionary(Constants.DDC_FILE_DATETIME, dict);
  144. return dict;
  145. }
  146. private void GetDefaultPropertyToDictionary(string key, Dictionary<string, string> dict)
  147. {
  148. if(key == Constants.DDC_FILE_DATETIME)
  149. {
  150. if(TryGetProperty(key, out DateTime dateTime))
  151. {
  152. dict.Add(key, dateTime.ToString("O"));
  153. }
  154. }
  155. else
  156. {
  157. if(TryGetProperty(key, out string propertyValue))
  158. {
  159. dict.Add(key, propertyValue);
  160. }
  161. }
  162. }
  163. #endregion
  164. #region Implementation of BaseTDMSLevel
  165. /// <inheritdoc />
  166. public override bool Close()
  167. {
  168. if(!_IsClosed)
  169. {
  170. var success = DDC.CloseFile(_SelfPtr);
  171. }
  172. return _IsClosed = true;
  173. }
  174. /// <inheritdoc />
  175. public override ulong ChildCount
  176. {
  177. get
  178. {
  179. var success = DDC.CountChannelGroups(_SelfPtr, out var count);
  180. return count;
  181. }
  182. }
  183. /// <inheritdoc />
  184. public ITDMSChannelGroup? AddGroup(string groupName, string description = "")
  185. {
  186. if(string.IsNullOrEmpty(groupName))
  187. throw new TDMSErrorException("Channel group name cannot be null or empty",
  188. new ArgumentNullException(nameof(groupName)));
  189. if(Contains(groupName))
  190. return null;
  191. var success = DDC.AddChannelGroup(_SelfPtr, Tail(groupName), Tail(description), out var groupPtr);
  192. if (success != Error.NoError) return null;
  193. return new TDMSChannelGroup(groupPtr);
  194. }
  195. /// <inheritdoc />
  196. public override bool Clear()
  197. {
  198. var groupsBuffer = new IntPtr[ChildCount];
  199. var success = DDC.GetChannelGroups(_SelfPtr, groupsBuffer, (uint)ChildCount);
  200. foreach (var ptr in groupsBuffer)
  201. {
  202. success = DDC.RemoveChannelGroup(ptr);
  203. }
  204. return success == 0;
  205. }
  206. /// <inheritdoc />
  207. public override bool Contains(string groupName)
  208. {
  209. if(string.IsNullOrEmpty(groupName))
  210. return false;
  211. var count = ChildCount;
  212. if (count <= 0)
  213. return false;
  214. var channelGroupsBuffer = new IntPtr[count];
  215. var success = DDC.GetChannelGroups(_SelfPtr, channelGroupsBuffer, (uint)count);
  216. if (success != Error.NoError) return false;
  217. foreach (var intPtr in channelGroupsBuffer)
  218. {
  219. using var group = new TDMSChannelGroup(intPtr);
  220. if(group.Name == groupName)
  221. return true;
  222. }
  223. return false;
  224. }
  225. /// <inheritdoc />
  226. public override bool TryGetItem(string groupName, out ITDMSLevel level)
  227. {
  228. var has = Contains(groupName);
  229. if(has)
  230. {
  231. level = this[groupName];
  232. return true;
  233. }
  234. level = null;
  235. return false;
  236. }
  237. /// <inheritdoc />
  238. public override bool Remove(string groupName)
  239. {
  240. if (TryGetItem(groupName, out var group) && group is TDMSChannelGroup @in)
  241. {
  242. var success = DDC.RemoveChannelGroup(@in.GetPtr());
  243. }
  244. return false;
  245. }
  246. /// <inheritdoc />
  247. public override bool RemoveAt(int index)
  248. {
  249. var group = this[index];
  250. if(group is TDMSChannelGroup groupIn)
  251. {
  252. var success = DDC.RemoveChannelGroup(groupIn.GetPtr());
  253. }
  254. return false;
  255. }
  256. #endregion
  257. }
  258. }