CNAS取数仪器端升级
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

254 lines
11KB

  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(normalFileFormat.NormalFileNameFormat))) //“今天”产生的数据,并且格式是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(
  81. new TXTOpenParams
  82. {
  83. StrPath = item,
  84. TxtFileMode = normalFileFormat.NormalFileDataMode,
  85. SplitType = normalFileFormat.NormalFileSplitType,
  86. StartLineIndex = ChangeStringToInt(normalFileFormat.NormalFileStartLineIndex),
  87. Suffix = normalFileFormat.NormalFileSuffix,
  88. FileNameFormat = normalFileFormat.NormalFileNameFormat,
  89. IfCustomCreateDateColumn = normalFileFormat.IfCustomCreateDateColumn == "0" ? true : false
  90. })["txt"];
  91. dataTables.Add(dt);
  92. }
  93. //合并数据
  94. DataTable dtCompine = new DataTable();
  95. if (dataTables.Count > 0)
  96. {
  97. dtCompine = dataTables[0].Clone();
  98. foreach (DataTable dt1 in dataTables)
  99. {
  100. foreach (DataRow dr in dt1.Rows)
  101. {
  102. DataRow drNew = dtCompine.NewRow();
  103. drNew.ItemArray = dr.ItemArray;
  104. dtCompine.Rows.Add(drNew);
  105. }
  106. }
  107. }
  108. return new Dictionary<string, DataTable>() { { "TXT", dtCompine } };
  109. }
  110. public override DataTable GetInstrumentDataByDate()
  111. {
  112. DataTable dtCompine = new DataTable();
  113. try
  114. {
  115. //读取数据
  116. List<DataTable> dataTables = new List<DataTable>();
  117. foreach (var item in LstFileFullName)
  118. {
  119. DataTable dt = txtDatabaseService.GetInstrumentDataByDate
  120. (
  121. new TXTOpenParams
  122. {
  123. StrPath = item,
  124. TxtFileMode = normalFileFormat.NormalFileDataMode,
  125. SplitType = normalFileFormat.NormalFileSplitType,
  126. StartLineIndex = ChangeStringToInt(normalFileFormat.NormalFileStartLineIndex),
  127. Suffix = normalFileFormat.NormalFileSuffix,
  128. FileNameFormat = normalFileFormat.NormalFileNameFormat,
  129. IfCustomCreateDateColumn = normalFileFormat.IfCustomCreateDateColumn == "0" ? true : false
  130. },
  131. new ConditionParams
  132. {
  133. }
  134. );
  135. dataTables.Add(dt);
  136. }
  137. //合并数据
  138. if (dataTables.Count > 0)
  139. {
  140. dtCompine = dataTables[0].Clone();
  141. foreach (DataTable dt1 in dataTables)
  142. {
  143. foreach (DataRow dr in dt1.Rows)
  144. {
  145. DataRow drNew = dtCompine.NewRow();
  146. foreach (DataColumn dc in dtCompine.Columns)
  147. {
  148. if (dc.ColumnName.ToString().ToLower() == strDateColumn.ToLower())
  149. {
  150. DateTime dt = DateTime.Now;
  151. if (DateTime.TryParse(dr[dc.ColumnName].ToString(), out dt))
  152. {
  153. drNew[dc.ColumnName] = dr[dc.ColumnName].ToString();
  154. }
  155. else
  156. {
  157. DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), normalFileFormat.NormalFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true));
  158. if (dtTime.ToString("HH:mm:ss") == "00:00:00")
  159. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd");
  160. else
  161. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss");
  162. }
  163. }
  164. else
  165. {
  166. drNew[dc.ColumnName] = dr[dc.ColumnName];
  167. }
  168. }
  169. dtCompine.Rows.Add(drNew);
  170. }
  171. }
  172. }
  173. }
  174. catch (Exception ex)
  175. {
  176. AppLog.Info("读取数据时发生异常");
  177. AppLog.Error(ex.Message);
  178. }
  179. AppLog.Info("读取数据完成,准备返回");
  180. return dtCompine;
  181. }
  182. public override DataTable GetInstrumentDataStruct()
  183. {
  184. //读取数据
  185. List<DataTable> dataTables = new List<DataTable>();
  186. foreach (var item in LstFileFullName)
  187. {
  188. DataTable dt = txtDatabaseService.GetInstrumentData(
  189. new TXTOpenParams
  190. {
  191. StrPath = item,
  192. TxtFileMode = normalFileFormat.NormalFileDataMode,
  193. SplitType = normalFileFormat.NormalFileSplitType,
  194. StartLineIndex = ChangeStringToInt(normalFileFormat.NormalFileStartLineIndex),
  195. Suffix=normalFileFormat.NormalFileSuffix,
  196. FileNameFormat=normalFileFormat.NormalFileNameFormat,
  197. IfCustomCreateDateColumn = normalFileFormat.IfCustomCreateDateColumn == "0" ? true : false
  198. })["TXT"];
  199. dataTables.Add(dt);
  200. }
  201. //合并数据
  202. DataTable dtCompine = new DataTable();
  203. try
  204. {
  205. if (dataTables.Count > 0)
  206. {
  207. dtCompine = dataTables[0].Clone();
  208. foreach (DataTable dt1 in dataTables)
  209. {
  210. foreach (DataRow dr in dt1.Rows)
  211. {
  212. DataRow drNew = dtCompine.NewRow();
  213. drNew.ItemArray = dr.ItemArray;
  214. dtCompine.Rows.Add(drNew);
  215. }
  216. }
  217. }
  218. }
  219. catch (Exception ex)
  220. {
  221. AppLog.Error(ex.Message);
  222. }
  223. return dtCompine;
  224. }
  225. private int ChangeStringToInt(string strValue)
  226. {
  227. int value = 0;
  228. Int32.TryParse(strValue, out value);
  229. return value;
  230. }
  231. }
  232. }