PropertyOperator.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using TDMS.Externals;
  5. namespace TDMS.Common
  6. {
  7. internal class ChannelPropertyOperator(IntPtr selfPtr) : PropertyOperator(selfPtr)
  8. {
  9. public override TDMSDataType GetDataType(string propertyName)
  10. {
  11. var success = DDC.GetChannelPropertyType(_SelfPtr, propertyName, out var type);
  12. return type;
  13. }
  14. public override bool Exists(string propertyName)
  15. {
  16. var success = DDC.ChannelPropertyExists(_SelfPtr, propertyName, out var exists);
  17. return success == Error.NoError ? exists : false;
  18. }
  19. public override uint GetStringLength(string propertyName)
  20. {
  21. var success = DDC.GetChannelStringPropertyLength(_SelfPtr, propertyName, out var length);
  22. return success == Error.NoError ? length : 0;
  23. }
  24. public override string[] GetPropertyNames()
  25. {
  26. var success = DDC.CountChannelProperties(_SelfPtr, out var count);
  27. if(success != Error.NoError || count ==0)return new string[0];
  28. var names = new IntPtr[count];
  29. success = DDC.GetChannelPropertyNames(_SelfPtr, names, count);
  30. if (success != Error.NoError) return new string[count];
  31. var result = new string[count];
  32. for (var i = 0; i < count; i++)
  33. {
  34. result[i] = Marshal.PtrToStringAnsi(names[i]) ?? string.Empty;
  35. }
  36. return result;
  37. }
  38. protected override Error DDCGetStringValue(string propertyName, StringBuilder chars, uint length)
  39. {
  40. var success = DDC.GetChannelPropertyString(_SelfPtr, propertyName, chars, length);
  41. return success;
  42. }
  43. public override byte GetByteValue(string propertyName)
  44. {
  45. var success = DDC.GetChannelPropertyUInt8(_SelfPtr, propertyName, out var value);
  46. return success == Error.NoError ? value : (byte)0;
  47. }
  48. public override short GetShortValue(string propertyName)
  49. {
  50. var success = DDC.GetChannelPropertyInt16(_SelfPtr, propertyName, out var value);
  51. return success == Error.NoError ? value : (short)0;
  52. }
  53. public override int GetIntValue(string propertyName)
  54. {
  55. var success = DDC.GetChannelPropertyInt32(_SelfPtr, propertyName, out var value);
  56. return success == Error.NoError ? value : 0;
  57. }
  58. public override float GetFloatValue(string propertyName)
  59. {
  60. var success = DDC.GetChannelPropertyFloat(_SelfPtr, propertyName, out var value);
  61. return success == Error.NoError ? value : 0;
  62. }
  63. public override double GetDoubleValue(string propertyName)
  64. {
  65. var success = DDC.GetChannelPropertyDouble(_SelfPtr, propertyName, out var value);
  66. return success == Error.NoError ? value : 0;
  67. }
  68. public override DateTime GetDateTimeValue(string propertyName)
  69. {
  70. var success = DDC.GetChannelPropertyTimestampComponents(_SelfPtr,
  71. propertyName,
  72. out var year,
  73. out var month,
  74. out var day,
  75. out var hour,
  76. out var minute,
  77. out var second,
  78. out var milliSecond,
  79. out var weekDay);
  80. return success == Error.NoError? new DateTime((int)year, (int)month, (int)day, (int)hour, (int)minute, (int)second, (int)milliSecond):DateTime.MinValue;
  81. }
  82. public override void SetStringValue(string propertyName, string propertyValue)
  83. {
  84. var success = DDC.SetChannelPropertyString(_SelfPtr, propertyName, propertyValue);
  85. }
  86. public override void SetByteValue(string propertyName, byte propertyValue)
  87. {
  88. var success = DDC.SetChannelPropertyUInt8(_SelfPtr, propertyName, propertyValue);
  89. }
  90. public override void SetShortValue(string propertyName, short propertyValue)
  91. {
  92. var success = DDC.SetChannelPropertyInt16(_SelfPtr, propertyName, propertyValue);
  93. }
  94. public override void SetIntValue(string propertyName, int propertyValue)
  95. {
  96. var success = DDC.SetChannelPropertyInt32(_SelfPtr, propertyName, propertyValue);
  97. }
  98. public override void SetFloatValue(string propertyName, float propertyValue)
  99. {
  100. var success = DDC.SetChannelPropertyFloat(_SelfPtr, propertyName, propertyValue);
  101. }
  102. public override void SetDoubleValue(string propertyName, double propertyValue)
  103. {
  104. var success = DDC.SetChannelPropertyDouble(_SelfPtr, propertyName, propertyValue);
  105. }
  106. public override void SetDateTimeValue(string propertyName, DateTime propertyValue)
  107. {
  108. var success = DDC.SetChannelPropertyTimestampComponents(_SelfPtr,
  109. propertyName,
  110. (uint)propertyValue.Year,
  111. (uint)propertyValue.Month,
  112. (uint)propertyValue.Day,
  113. (uint)propertyValue.Hour,
  114. (uint)propertyValue.Minute,
  115. (uint)propertyValue.Second,
  116. propertyValue.Millisecond);
  117. }
  118. public override void CreateStringValue(string propertyName, string propertyValue)
  119. {
  120. var success = DDC.CreateChannelPropertyString(_SelfPtr, propertyName, propertyValue);
  121. }
  122. public override void CreateByteValue(string propertyName, byte propertyValue)
  123. {
  124. var success = DDC.CreateChannelPropertyUInt8(_SelfPtr, propertyName, propertyValue);
  125. }
  126. public override void CreateShortValue(string propertyName, short propertyValue)
  127. {
  128. var success = DDC.CreateChannelPropertyInt16(_SelfPtr, propertyName, propertyValue);
  129. }
  130. public override void CreateIntValue(string propertyName, int propertyValue)
  131. {
  132. var success = DDC.CreateChannelPropertyInt32(_SelfPtr, propertyName, propertyValue);
  133. }
  134. public override void CreateFloatValue(string propertyName, float propertyValue)
  135. {
  136. var success = DDC.CreateChannelPropertyFloat(_SelfPtr, propertyName, propertyValue);
  137. }
  138. public override void CreateDoubleValue(string propertyName, double propertyValue)
  139. {
  140. var success = DDC.CreateChannelPropertyDouble(_SelfPtr, propertyName, propertyValue);
  141. }
  142. public override void CreateDateTimeValue(string propertyName, DateTime propertyValue)
  143. {
  144. var success = DDC.CreateChannelPropertyTimestampComponents(_SelfPtr,
  145. propertyName,
  146. (uint)propertyValue.Year,
  147. (uint)propertyValue.Month,
  148. (uint)propertyValue.Day,
  149. (uint)propertyValue.Hour,
  150. (uint)propertyValue.Minute,
  151. (uint)propertyValue.Second,
  152. propertyValue.Millisecond);
  153. }
  154. }
  155. internal class ChannelGroupPropertyOperator(IntPtr selfPtr) : PropertyOperator(selfPtr)
  156. {
  157. public override TDMSDataType GetDataType(string propertyName)
  158. {
  159. var success = DDC.GetChannelGroupPropertyType(_SelfPtr, propertyName, out var type);
  160. return type;
  161. }
  162. public override bool Exists(string propertyName)
  163. {
  164. var success = DDC.ChannelGroupPropertyExists(_SelfPtr, propertyName, out var exists);
  165. return exists;
  166. }
  167. public override uint GetStringLength(string propertyName)
  168. {
  169. var success = DDC.GetChannelGroupStringPropertyLength(_SelfPtr, propertyName, out var length);
  170. if (success != 0) return 0;
  171. return length;
  172. }
  173. public override string[] GetPropertyNames()
  174. {
  175. var success = DDC.CountChannelGroupProperties(_SelfPtr, out var count);
  176. if(success!= Error.NoError)return new string[0];
  177. var names = new IntPtr[count];
  178. success = DDC.GetChannelGroupPropertyNames(_SelfPtr, names, count);
  179. if (success != Error.NoError) return new string[count];
  180. var result = new string[count];
  181. for (var i = 0; i < count; i++)
  182. {
  183. result[i] = Marshal.PtrToStringAnsi(names[i]) ?? string.Empty;
  184. }
  185. return result;
  186. }
  187. protected override Error DDCGetStringValue(string propertyName, StringBuilder chars, uint length)
  188. {
  189. var success = DDC.GetChannelGroupPropertyString(_SelfPtr, propertyName, chars, length);
  190. return success;
  191. }
  192. public override byte GetByteValue(string propertyName)
  193. {
  194. var success = DDC.GetChannelGroupPropertyUInt8(_SelfPtr, propertyName, out var value);
  195. return value;
  196. }
  197. public override short GetShortValue(string propertyName)
  198. {
  199. var success = DDC.GetChannelGroupPropertyInt16(_SelfPtr, propertyName, out var value);
  200. return value;
  201. }
  202. public override int GetIntValue(string propertyName)
  203. {
  204. var success = DDC.GetChannelGroupPropertyInt32(_SelfPtr, propertyName, out var value);
  205. return value;
  206. }
  207. public override float GetFloatValue(string propertyName)
  208. {
  209. var success = DDC.GetChannelGroupPropertyFloat(_SelfPtr, propertyName, out var value);
  210. return value;
  211. }
  212. public override double GetDoubleValue(string propertyName)
  213. {
  214. var success = DDC.GetChannelGroupPropertyDouble(_SelfPtr, propertyName, out var value);
  215. return value;
  216. }
  217. public override DateTime GetDateTimeValue(string propertyName)
  218. {
  219. var success = DDC.GetChannelGroupPropertyTimestampComponents(_SelfPtr,
  220. propertyName,
  221. out var year,
  222. out var month,
  223. out var day,
  224. out var hour,
  225. out var minute,
  226. out var second,
  227. out var milliSecond,
  228. out var weekDay);
  229. return success == Error.NoError? new DateTime((int)year, (int)month, (int)day, (int)hour, (int)minute, (int)second, (int)milliSecond): DateTime.MinValue;
  230. }
  231. public override void SetStringValue(string propertyName, string propertyValue)
  232. {
  233. var success = DDC.SetChannelGroupPropertyString(_SelfPtr, propertyName, propertyValue);
  234. }
  235. public override void SetByteValue(string propertyName, byte propertyValue)
  236. {
  237. var success = DDC.SetChannelGroupPropertyUInt8(_SelfPtr, propertyName, propertyValue);
  238. }
  239. public override void SetShortValue(string propertyName, short propertyValue)
  240. {
  241. var success = DDC.SetChannelGroupPropertyInt16(_SelfPtr, propertyName, propertyValue);
  242. }
  243. public override void SetIntValue(string propertyName, int propertyValue)
  244. {
  245. var success = DDC.SetChannelGroupPropertyInt32(_SelfPtr, propertyName, propertyValue);
  246. }
  247. public override void SetFloatValue(string propertyName, float propertyValue)
  248. {
  249. var success = DDC.SetChannelGroupPropertyFloat(_SelfPtr, propertyName, propertyValue);
  250. }
  251. public override void SetDoubleValue(string propertyName, double propertyValue)
  252. {
  253. var success = DDC.SetChannelGroupPropertyDouble(_SelfPtr, propertyName, propertyValue);
  254. }
  255. public override void SetDateTimeValue(string propertyName, DateTime propertyValue)
  256. {
  257. var success = DDC.SetChannelGroupPropertyTimestampComponents(_SelfPtr,
  258. propertyName,
  259. (uint)propertyValue.Year,
  260. (uint)propertyValue.Month,
  261. (uint)propertyValue.Day,
  262. (uint)propertyValue.Hour,
  263. (uint)propertyValue.Minute,
  264. (uint)propertyValue.Second,
  265. propertyValue.Millisecond);
  266. }
  267. public override void CreateStringValue(string propertyName, string propertyValue)
  268. {
  269. var success = DDC.CreateChannelGroupPropertyString(_SelfPtr, propertyName, propertyValue);
  270. }
  271. public override void CreateByteValue(string propertyName, byte propertyValue)
  272. {
  273. var success = DDC.CreateChannelGroupPropertyUInt8(_SelfPtr, propertyName, propertyValue);
  274. }
  275. public override void CreateShortValue(string propertyName, short propertyValue)
  276. {
  277. var success = DDC.CreateChannelGroupPropertyInt16(_SelfPtr, propertyName, propertyValue);
  278. }
  279. public override void CreateIntValue(string propertyName, int propertyValue)
  280. {
  281. var success = DDC.CreateChannelGroupPropertyInt32(_SelfPtr, propertyName, propertyValue);
  282. }
  283. public override void CreateFloatValue(string propertyName, float propertyValue)
  284. {
  285. var success = DDC.CreateChannelGroupPropertyFloat(_SelfPtr, propertyName, propertyValue);
  286. }
  287. public override void CreateDoubleValue(string propertyName, double propertyValue)
  288. {
  289. var success = DDC.CreateChannelGroupPropertyDouble(_SelfPtr, propertyName, propertyValue);
  290. }
  291. public override void CreateDateTimeValue(string propertyName, DateTime propertyValue)
  292. {
  293. var success = DDC.CreateChannelGroupPropertyTimestampComponents(_SelfPtr,
  294. propertyName,
  295. (uint)propertyValue.Year,
  296. (uint)propertyValue.Month,
  297. (uint)propertyValue.Day,
  298. (uint)propertyValue.Hour,
  299. (uint)propertyValue.Minute,
  300. (uint)propertyValue.Second,
  301. propertyValue.Millisecond);
  302. }
  303. }
  304. internal class FilePropertyOperator(IntPtr selfPtr) : PropertyOperator(selfPtr)
  305. {
  306. public override TDMSDataType GetDataType(string propertyName)
  307. {
  308. var success = DDC.GetFilePropertyType(_SelfPtr, propertyName, out var type);
  309. return type;
  310. }
  311. public override bool Exists(string propertyName)
  312. {
  313. var success = DDC.FilePropertyExists(_SelfPtr, propertyName, out var exists);
  314. return exists;
  315. }
  316. public override uint GetStringLength(string propertyName)
  317. {
  318. var success = DDC.GetFileStringPropertyLength(_SelfPtr, propertyName, out var length);
  319. if (success != 0) return 0;
  320. return length;
  321. }
  322. public override string[] GetPropertyNames()
  323. {
  324. var success = DDC.CountFileProperties(_SelfPtr, out var count);
  325. if (success != Error.NoError) return new string[0];
  326. var names = new IntPtr[count];
  327. success = DDC.GetFilePropertyNames(_SelfPtr, names, count);
  328. if (success != Error.NoError) return new string[count];
  329. var result = new string[count];
  330. for (var i = 0; i < count; i++)
  331. {
  332. result[i] = Marshal.PtrToStringAnsi(names[i]) ?? string.Empty;
  333. }
  334. return result;
  335. }
  336. protected override Error DDCGetStringValue(string propertyName,StringBuilder chars, uint length)
  337. {
  338. var success = DDC.GetFilePropertyString(_SelfPtr, propertyName, chars, length);
  339. return success;
  340. }
  341. public override byte GetByteValue(string propertyName)
  342. {
  343. var success = DDC.GetFilePropertyUInt8(_SelfPtr, propertyName, out var value);
  344. return value;
  345. }
  346. public override short GetShortValue(string propertyName)
  347. {
  348. var success = DDC.GetFilePropertyInt16(_SelfPtr, propertyName, out var value);
  349. return value;
  350. }
  351. public override int GetIntValue(string propertyName)
  352. {
  353. var success = DDC.GetFilePropertyInt32(_SelfPtr, propertyName, out var value);
  354. return value;
  355. }
  356. public override float GetFloatValue(string propertyName)
  357. {
  358. var success = DDC.GetFilePropertyFloat(_SelfPtr, propertyName, out var value);
  359. return value;
  360. }
  361. public override double GetDoubleValue(string propertyName)
  362. {
  363. var success = DDC.GetFilePropertyDouble(_SelfPtr, propertyName, out var value);
  364. return value;
  365. }
  366. public override DateTime GetDateTimeValue(string propertyName)
  367. {
  368. var success = DDC.GetFilePropertyTimestampComponents(_SelfPtr,
  369. propertyName,
  370. out var year,
  371. out var month,
  372. out var day,
  373. out var hour,
  374. out var minute,
  375. out var second,
  376. out var milliSecond,
  377. out var weekDay);
  378. if (success != 0) return DateTime.MinValue;
  379. return new DateTime((int)year, (int)month, (int)day, (int)hour, (int)minute, (int)second, (int)milliSecond);
  380. }
  381. public override void SetStringValue(string propertyName, string propertyValue)
  382. {
  383. var success = DDC.SetFilePropertyString(_SelfPtr, propertyName, propertyValue);
  384. }
  385. public override void SetByteValue(string propertyName, byte propertyValue)
  386. {
  387. var success = DDC.SetFilePropertyUInt8(_SelfPtr, propertyName, propertyValue);
  388. }
  389. public override void SetShortValue(string propertyName, short propertyValue)
  390. {
  391. var success = DDC.SetFilePropertyInt16(_SelfPtr, propertyName, propertyValue);
  392. }
  393. public override void SetIntValue(string propertyName, int propertyValue)
  394. {
  395. var success = DDC.SetFilePropertyInt32(_SelfPtr, propertyName, propertyValue);
  396. }
  397. public override void SetFloatValue(string propertyName, float propertyValue)
  398. {
  399. var success = DDC.SetFilePropertyFloat(_SelfPtr, propertyName, propertyValue);
  400. }
  401. public override void SetDoubleValue(string propertyName, double propertyValue)
  402. {
  403. var success = DDC.SetFilePropertyDouble(_SelfPtr, propertyName, propertyValue);
  404. }
  405. public override void SetDateTimeValue(string propertyName, DateTime propertyValue)
  406. {
  407. var success = DDC.SetFilePropertyTimestampComponents(_SelfPtr,
  408. propertyName,
  409. (uint)propertyValue.Year,
  410. (uint)propertyValue.Month,
  411. (uint)propertyValue.Day,
  412. (uint)propertyValue.Hour,
  413. (uint)propertyValue.Minute,
  414. (uint)propertyValue.Second,
  415. propertyValue.Millisecond);
  416. }
  417. public override void CreateStringValue(string propertyName, string propertyValue)
  418. {
  419. var success = DDC.CreateFilePropertyString(_SelfPtr, propertyName, propertyValue);
  420. }
  421. public override void CreateByteValue(string propertyName, byte propertyValue)
  422. {
  423. var success = DDC.CreateFilePropertyUInt8(_SelfPtr, propertyName, propertyValue);
  424. }
  425. public override void CreateShortValue(string propertyName, short propertyValue)
  426. {
  427. var success = DDC.CreateFilePropertyInt16(_SelfPtr, propertyName, propertyValue);
  428. }
  429. public override void CreateIntValue(string propertyName, int propertyValue)
  430. {
  431. var success = DDC.CreateFilePropertyInt32(_SelfPtr, propertyName, propertyValue);
  432. }
  433. public override void CreateFloatValue(string propertyName, float propertyValue)
  434. {
  435. var success = DDC.CreateFilePropertyFloat(_SelfPtr, propertyName, propertyValue);
  436. }
  437. public override void CreateDoubleValue(string propertyName, double propertyValue)
  438. {
  439. var success = DDC.CreateFilePropertyDouble(_SelfPtr, propertyName, propertyValue);
  440. }
  441. public override void CreateDateTimeValue(string propertyName, DateTime propertyValue)
  442. {
  443. var success = DDC.CreateFilePropertyTimestampComponents(_SelfPtr,
  444. propertyName,
  445. (uint)propertyValue.Year,
  446. (uint)propertyValue.Month,
  447. (uint)propertyValue.Day,
  448. (uint)propertyValue.Hour,
  449. (uint)propertyValue.Minute,
  450. (uint)propertyValue.Second,
  451. propertyValue.Millisecond);
  452. }
  453. }
  454. internal abstract class PropertyOperator(IntPtr selfPtr) : IDisposable
  455. {
  456. protected IntPtr _SelfPtr = selfPtr;
  457. public abstract TDMSDataType GetDataType(string propertyName);
  458. public abstract bool Exists(string propertyName);
  459. public abstract uint GetStringLength(string propertyName);
  460. public abstract string[] GetPropertyNames();
  461. public string GetStringValue(string propertyName)
  462. {
  463. if (!Exists(propertyName))
  464. return string.Empty;
  465. var length = GetStringLength(propertyName);
  466. if (length <= 0)
  467. return string.Empty;
  468. length++;
  469. var chars = new StringBuilder((int)length);
  470. var success = DDCGetStringValue(propertyName, chars, length);
  471. return success == Error.NoError ? chars.ToString().TrimEnd('\0') : string.Empty;
  472. }
  473. protected abstract Error DDCGetStringValue(string propertyName, StringBuilder chars, uint length);
  474. public abstract byte GetByteValue(string propertyName);
  475. public abstract short GetShortValue(string propertyName);
  476. public abstract int GetIntValue(string propertyName);
  477. public abstract float GetFloatValue(string propertyName);
  478. public abstract double GetDoubleValue(string propertyName);
  479. public abstract DateTime GetDateTimeValue(string propertyName);
  480. public abstract void SetStringValue(string propertyName, string propertyValue);
  481. public abstract void SetByteValue(string propertyName, byte propertyValue);
  482. public abstract void SetShortValue(string propertyName, short propertyValue);
  483. public abstract void SetIntValue(string propertyName, int propertyValue);
  484. public abstract void SetFloatValue(string propertyName, float propertyValue);
  485. public abstract void SetDoubleValue(string propertyName, double propertyValue);
  486. public abstract void SetDateTimeValue(string propertyName, DateTime propertyValue);
  487. public abstract void CreateStringValue(string propertyName, string propertyValue);
  488. public abstract void CreateByteValue(string propertyName, byte propertyValue);
  489. public abstract void CreateShortValue(string propertyName, short propertyValue);
  490. public abstract void CreateIntValue(string propertyName, int propertyValue);
  491. public abstract void CreateFloatValue(string propertyName, float propertyValue);
  492. public abstract void CreateDoubleValue(string propertyName, double propertyValue);
  493. public abstract void CreateDateTimeValue(string propertyName, DateTime propertyValue);
  494. #region Implementation of IDisposable
  495. ~PropertyOperator()
  496. {
  497. Dispose(false);
  498. }
  499. protected virtual void Dispose(bool disposing)
  500. {
  501. _SelfPtr = IntPtr.Zero;
  502. if(disposing)
  503. {
  504. // 释放托管资源
  505. }
  506. }
  507. public void Dispose()
  508. {
  509. Dispose(true);
  510. GC.SuppressFinalize(this);
  511. }
  512. #endregion
  513. }
  514. }