CNAS取数仪器端升级
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

203 líneas
8.3KB

  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. DirectoryInfo root = new DirectoryInfo(dataSourceInfo.Path);
  48. //if (root.GetFiles() == null)
  49. // AppLog.Info($"文件夹文件数量为空");
  50. //else
  51. // AppLog.Info($"文件夹文件数量{root.GetFiles().Count<FileInfo>()}");
  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("yyyyMMdd"))) //“今天”产生的数据,并且格式是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 == strDateColumn)
  134. {
  135. DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), normalFileFormat.NormalFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true));
  136. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss");
  137. }
  138. else
  139. {
  140. drNew[dc.ColumnName] = dr[dc.ColumnName];
  141. }
  142. }
  143. dtCompine.Rows.Add(drNew);
  144. }
  145. }
  146. }
  147. }
  148. catch (Exception ex)
  149. {
  150. AppLog.Info("读取数据时发生异常");
  151. AppLog.Error(ex.Message);
  152. }
  153. AppLog.Info("读取数据完成,准备返回");
  154. return dtCompine;
  155. }
  156. public override DataTable GetInstrumentDataStruct()
  157. {
  158. //读取数据
  159. List<DataTable> dataTables = new List<DataTable>();
  160. foreach (var item in LstFileFullName)
  161. {
  162. DataTable dt = txtDatabaseService.GetInstrumentData(new TXTOpenParams { StrPath = item, TxtFileMode = normalFileFormat.NormalFileDataMode })["txt"];
  163. dataTables.Add(dt);
  164. }
  165. //合并数据
  166. DataTable dtCompine = new DataTable();
  167. if (dataTables.Count > 0)
  168. {
  169. dtCompine = dataTables[0].Clone();
  170. foreach (DataTable dt1 in dataTables)
  171. {
  172. foreach (DataRow dr in dt1.Rows)
  173. {
  174. DataRow drNew = dtCompine.NewRow();
  175. drNew.ItemArray = dr.ItemArray;
  176. dtCompine.Rows.Add(drNew);
  177. }
  178. }
  179. }
  180. return dtCompine;
  181. }
  182. }
  183. }