CNAS取数仪器端升级
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TXTInstrumentData.cs 8.8KB

5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using CnasSynchronousCommon;
  2. using CnasSynchronusDAL;
  3. using CnasSynchronusIDAL;
  4. using CnasSynchrousModel;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. namespace CnasSynchronusClient
  12. {
  13. internal class TXTInstrumentData : InstrumentData
  14. {
  15. public string StrLocalPath { get; set; }
  16. public string StrUser { get; set; }
  17. public string StrPwd { get; set; }
  18. public string strTableName { get; set; }
  19. public string strDateColumn { get; set; }
  20. public string strDate { get; set; }
  21. public ITXTService txtDatabaseService { get { return new TXTService(); } }
  22. public NormalFileFormatConfig normalFileFormat { get; set; }
  23. /// <summary>
  24. /// 如果配置读取的是文件夹,该属性用来记录需要读取所有文件的缓冲路径
  25. /// </summary>
  26. public List<string> LstFileFullName { get; set; }
  27. public TXTInstrumentData(InstrumentDataSourceInfo dataSourceInfo, object[] vs)
  28. {
  29. normalFileFormat= FileOperation.GetFormatConfigData<NormalFileFormatConfig>("NormalFileFormatConfig.xml");
  30. string strSuffix = normalFileFormat.NormalFileSuffix;
  31. try
  32. {
  33. LstFileFullName = new List<string>();
  34. if (normalFileFormat.ReadNormalFileMode == "0") //此时为读取“文件”模式,指定要读取的具体文件
  35. {
  36. RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo);
  37. remoteFileCopy.CopyFileFromRemote(strSuffix);
  38. this.StrLocalPath = FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strSuffix;
  39. this.strTableName = vs[0].ToString();
  40. this.strDateColumn = vs[1].ToString();
  41. this.strDate = vs[2].ToString();
  42. LstFileFullName.Add(StrLocalPath);
  43. }
  44. else //此时为读取“文件夹”模式,指定要读取的具体文件
  45. {
  46. //遍历当前路径,找到所有“今天”的数据文件,复制形成缓冲文件
  47. //string strYear = DateTime.Now.Year.ToString();
  48. //string strMonth= DateTime.Now.Month.ToString();
  49. //string strDay = DateTime.Now.Day.ToString();
  50. //DirectoryInfo root = new DirectoryInfo($"{dataSourceInfo.Path}\\{strYear}\\{strMonth}\\{strDay}");
  51. DirectoryInfo root = new DirectoryInfo($"{dataSourceInfo.Path}");
  52. foreach (FileInfo f in root.GetFiles())
  53. {
  54. string name = f.Name;
  55. string fullName = f.FullName;
  56. string type = f.Extension;
  57. if (type.ToLower() == strSuffix.ToLower() && name.StartsWith(DateTime.Now.ToString("yyMMdd"))) //“今天”产生的数据,并且格式是txt
  58. {
  59. RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo);
  60. remoteFileCopy.CopyFileFromRemote(strSuffix, $"{name}");
  61. LstFileFullName.Add(FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + $"{name}", "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strSuffix);
  62. }
  63. }
  64. this.strTableName = vs[0].ToString();
  65. this.strDateColumn = vs[1].ToString();
  66. this.strDate = vs[2].ToString();
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. AppLog.Error(ex.Message);
  72. }
  73. }
  74. public override Dictionary<string, DataTable> GetInstrumentData()
  75. {
  76. //读取数据
  77. List<DataTable> dataTables = new List<DataTable>();
  78. foreach (var item in LstFileFullName)
  79. {
  80. DataTable dt = txtDatabaseService.GetInstrumentData(new TXTOpenParams { StrPath = item, TxtFileMode = normalFileFormat.NormalFileDataMode })["txt"];
  81. dataTables.Add(dt);
  82. }
  83. //合并数据
  84. DataTable dtCompine = new DataTable();
  85. if (dataTables.Count > 0)
  86. {
  87. dtCompine = dataTables[0].Clone();
  88. foreach (DataTable dt1 in dataTables)
  89. {
  90. foreach (DataRow dr in dt1.Rows)
  91. {
  92. DataRow drNew = dtCompine.NewRow();
  93. drNew.ItemArray = dr.ItemArray;
  94. dtCompine.Rows.Add(drNew);
  95. }
  96. }
  97. }
  98. return new Dictionary<string, DataTable>() { { "txt", dtCompine } };
  99. }
  100. public override DataTable GetInstrumentDataByDate()
  101. {
  102. DataTable dtCompine = new DataTable();
  103. try
  104. {
  105. //读取数据
  106. List<DataTable> dataTables = new List<DataTable>();
  107. foreach (var item in LstFileFullName)
  108. {
  109. DataTable dt = txtDatabaseService.GetInstrumentDataByDate
  110. (
  111. new TXTOpenParams
  112. {
  113. StrPath = item,
  114. TxtFileMode = normalFileFormat.NormalFileDataMode
  115. },
  116. new ConditionParams
  117. {
  118. }
  119. );
  120. dataTables.Add(dt);
  121. }
  122. //合并数据
  123. if (dataTables.Count > 0)
  124. {
  125. dtCompine = dataTables[0].Clone();
  126. foreach (DataTable dt1 in dataTables)
  127. {
  128. foreach (DataRow dr in dt1.Rows)
  129. {
  130. DataRow drNew = dtCompine.NewRow();
  131. foreach (DataColumn dc in dtCompine.Columns)
  132. {
  133. if (dc.ColumnName.ToString().ToLower() == strDateColumn.ToLower())
  134. {
  135. DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), normalFileFormat.NormalFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true));
  136. if(dtTime.ToString("HH:mm:ss")=="00:00:00")
  137. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd");
  138. else
  139. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss");
  140. }
  141. else
  142. {
  143. drNew[dc.ColumnName] = dr[dc.ColumnName];
  144. }
  145. }
  146. dtCompine.Rows.Add(drNew);
  147. }
  148. }
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. AppLog.Info("读取数据时发生异常");
  154. AppLog.Error(ex.Message);
  155. }
  156. AppLog.Info("读取数据完成,准备返回");
  157. return dtCompine;
  158. }
  159. public override DataTable GetInstrumentDataStruct()
  160. {
  161. //读取数据
  162. List<DataTable> dataTables = new List<DataTable>();
  163. foreach (var item in LstFileFullName)
  164. {
  165. DataTable dt = txtDatabaseService.GetInstrumentData(new TXTOpenParams { StrPath = item, TxtFileMode = normalFileFormat.NormalFileDataMode })["txt"];
  166. dataTables.Add(dt);
  167. }
  168. //合并数据
  169. DataTable dtCompine = new DataTable();
  170. try
  171. {
  172. if (dataTables.Count > 0)
  173. {
  174. dtCompine = dataTables[0].Clone();
  175. foreach (DataTable dt1 in dataTables)
  176. {
  177. foreach (DataRow dr in dt1.Rows)
  178. {
  179. DataRow drNew = dtCompine.NewRow();
  180. drNew.ItemArray = dr.ItemArray;
  181. dtCompine.Rows.Add(drNew);
  182. }
  183. }
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. AppLog.Error(ex.Message);
  189. }
  190. return dtCompine;
  191. }
  192. }
  193. }