You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.5 KiB

8 months ago
  1. using Mirle.Component.MPLC.DataBlocks;
  2. using Mirle.Component.MPLC.Interfaces;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. namespace Mirle.Component.MPLC.FileData
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public class FileDataViewer : IMPLCProvider
  13. {
  14. /// <summary>
  15. /// 建構式
  16. /// </summary>
  17. /// <param name="fileReader">檔案讀取器</param>
  18. public FileDataViewer(FileReader fileReader)
  19. {
  20. _fileReader = fileReader;
  21. foreach (FileDataBlock fileReaderDataBlock in _fileReader.GetDataBlocks())
  22. {
  23. _dataBlocks.Add(new FileDataBlock(fileReaderDataBlock.DeviceRange, fileReaderDataBlock.ColumnIndex));
  24. }
  25. }
  26. /// <summary>
  27. /// 檔案讀取器
  28. /// </summary>
  29. private readonly FileReader _fileReader;
  30. /// <summary>
  31. /// 資料區塊
  32. /// </summary>
  33. private readonly List<FileDataBlock> _dataBlocks = new List<FileDataBlock>();
  34. /// <summary>
  35. /// 是否連線
  36. /// </summary>
  37. public bool IsConnected => true;
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. /// <param name="begin"></param>
  42. /// <param name="end"></param>
  43. /// <returns></returns>
  44. public IEnumerable<DateTime> Query(DateTime begin, DateTime end)
  45. {
  46. return _fileReader.GetDateTimeIndexes().Where(t => t.TimeOfDay >= begin.TimeOfDay && t.TimeOfDay <= end.TimeOfDay).OrderBy(i => i);
  47. }
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. /// <param name="index"></param>
  52. public void RefreshRawData(DateTime index)
  53. {
  54. foreach (FileDataBlock block in _dataBlocks)
  55. {
  56. try
  57. {
  58. byte[] newByteArray = _fileReader.GetRawDataByDateTimeIndex(index, block.ColumnIndex);
  59. if (newByteArray != null)
  60. block.SetRawData(newByteArray);
  61. }
  62. catch (Exception ex)
  63. {
  64. Debug.WriteLine($"{ex.Message}-{ex.StackTrace}");
  65. }
  66. }
  67. }
  68. /// <summary>
  69. /// 取得位元
  70. /// </summary>
  71. /// <param name="address">位置</param>
  72. /// <returns>True/False</returns>
  73. public bool GetBit(string address)
  74. {
  75. foreach (var block in _dataBlocks)
  76. {
  77. if (block.TryGetBit(address, out bool value))
  78. return value;
  79. }
  80. return false;
  81. }
  82. /// <summary>
  83. /// 設置位元開啟
  84. /// </summary>
  85. /// <param name="address">位置</param>
  86. public void SetBitOn(string address)
  87. {
  88. return;
  89. }
  90. /// <summary>
  91. /// 設置位元關閉
  92. /// </summary>
  93. /// <param name="address">位置</param>
  94. public void SetBitOff(string address)
  95. {
  96. return;
  97. }
  98. /// <summary>
  99. /// 讀取字元
  100. /// </summary>
  101. /// <param name="address">位置</param>
  102. /// <returns>字元</returns>
  103. public int ReadWord(string address)
  104. {
  105. foreach (var block in _dataBlocks)
  106. {
  107. if (block.TryGetWord(address, out int value))
  108. return value;
  109. }
  110. return 0;
  111. }
  112. /// <summary>
  113. /// 寫入字元
  114. /// </summary>
  115. /// <param name="address">位置</param>
  116. /// <param name="data">字元</param>
  117. public void WriteWord(string address, int data)
  118. {
  119. return;
  120. }
  121. /// <summary>
  122. /// 讀取多個字元
  123. /// </summary>
  124. /// <param name="startAddress">起始位置</param>
  125. /// <param name="length">長度</param>
  126. /// <returns>字元陣列</returns>
  127. public int[] ReadWords(string startAddress, int length)
  128. {
  129. foreach (var block in _dataBlocks)
  130. {
  131. if (block.TryGetWords(startAddress, out int[] data, length))
  132. return data;
  133. }
  134. return new int[length];
  135. }
  136. /// <summary>
  137. /// 寫入多個字元
  138. /// </summary>
  139. /// <param name="startAddress">起始位置</param>
  140. /// <param name="data">字元陣列</param>
  141. public void WriteWords(string startAddress, int[] data)
  142. {
  143. return;
  144. }
  145. }
  146. }