CNAS取数仪器端升级
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.

279 lines
12KB

  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. if (dataSourceInfo.Path == null)
  30. {
  31. return;
  32. }
  33. this.normalFileFormat = FileOperation.GetFormatConfigData<NormalFileFormatConfig>("NormalFileFormatConfig.xml");
  34. string strSuffix = normalFileFormat.NormalFileSuffix;
  35. try
  36. {
  37. LstFileFullName = new List<string>();
  38. if (normalFileFormat.ReadNormalFileMode == "0")
  39. {
  40. RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo);
  41. remoteFileCopy.CopyFileFromRemote(strSuffix);
  42. this.StrLocalPath = FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strSuffix;
  43. this.strTableName = vs[0].ToString();
  44. this.strDateColumn = vs[1].ToString();
  45. this.strDate = vs[2].ToString();
  46. LstFileFullName.Add(StrLocalPath);
  47. }
  48. //此时为读取“文件夹”模式,指定要读取的具体文件
  49. else
  50. {
  51. //遍历当前路径,找到所有“今天”的数据文件,复制形成缓冲文件
  52. //string strYear = DateTime.Now.Year.ToString();
  53. //string strMonth= DateTime.Now.Month.ToString();
  54. //string strDay = DateTime.Now.Day.ToString();
  55. //DirectoryInfo root = new DirectoryInfo($"{dataSourceInfo.Path}\\{strYear}\\{strMonth}\\{strDay}");
  56. DirectoryInfo root = new DirectoryInfo($"{dataSourceInfo.Path}");
  57. foreach (FileInfo f in root.GetFiles())
  58. {
  59. string name = f.Name;
  60. string fullName = f.FullName;
  61. string type = f.Extension;
  62. if (type.ToLower() == strSuffix.ToLower() && name.StartsWith(DateTime.Now.ToString(normalFileFormat.NormalFileNameFormat))) //“今天”产生的数据,并且格式是txt
  63. {
  64. RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo);
  65. remoteFileCopy.CopyFileFromRemote(strSuffix, $"{name}");
  66. LstFileFullName.Add(FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + $"{name}", "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strSuffix);
  67. }
  68. }
  69. this.strTableName = vs[0].ToString();
  70. this.strDateColumn = vs[1].ToString();
  71. this.strDate = vs[2].ToString();
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. AppLog.Error(ex.Message);
  77. }
  78. }
  79. public override Dictionary<string, DataTable> GetInstrumentData()
  80. {
  81. //读取数据
  82. List<DataTable> dataTables = new List<DataTable>();
  83. foreach (var item in LstFileFullName)
  84. {
  85. DataTable dt = txtDatabaseService.GetInstrumentData(
  86. new TXTOpenParams
  87. {
  88. StrPath = item,
  89. TxtFileMode = normalFileFormat.NormalFileDataMode,
  90. SplitType = normalFileFormat.NormalFileSplitType,
  91. StartLineIndex = ChangeStringToInt(normalFileFormat.NormalFileStartLineIndex),
  92. Suffix = normalFileFormat.NormalFileSuffix,
  93. FileNameFormat = normalFileFormat.NormalFileNameFormat,
  94. IfCustomCreateDateColumn = normalFileFormat.IfCustomCreateDateColumn == "0" ? true : false
  95. })["txt"];
  96. dataTables.Add(dt);
  97. }
  98. //合并数据
  99. DataTable dtCompine = new DataTable();
  100. if (dataTables.Count > 0)
  101. {
  102. dtCompine = dataTables[0].Clone();
  103. foreach (DataTable dt1 in dataTables)
  104. {
  105. foreach (DataRow dr in dt1.Rows)
  106. {
  107. DataRow drNew = dtCompine.NewRow();
  108. drNew.ItemArray = dr.ItemArray;
  109. dtCompine.Rows.Add(drNew);
  110. }
  111. }
  112. }
  113. return new Dictionary<string, DataTable>() { { "TXT", dtCompine } };
  114. }
  115. public override DataTable GetInstrumentDataByDate()
  116. {
  117. DataTable dtCompine = new DataTable();
  118. try
  119. {
  120. //读取数据
  121. List<DataTable> dataTables = new List<DataTable>();
  122. foreach (var item in LstFileFullName)
  123. {
  124. DataTable dt = txtDatabaseService.GetInstrumentDataByDate
  125. (
  126. new TXTOpenParams
  127. {
  128. StrPath = item,
  129. TxtFileMode = normalFileFormat.NormalFileDataMode,
  130. SplitType = normalFileFormat.NormalFileSplitType,
  131. StartLineIndex = ChangeStringToInt(normalFileFormat.NormalFileStartLineIndex),
  132. Suffix = normalFileFormat.NormalFileSuffix,
  133. FileNameFormat = normalFileFormat.NormalFileNameFormat,
  134. IfCustomCreateDateColumn = normalFileFormat.IfCustomCreateDateColumn == "0" ? true : false
  135. },
  136. new ConditionParams
  137. {
  138. }
  139. );
  140. dataTables.Add(dt);
  141. }
  142. //合并数据
  143. if (dataTables.Count > 0)
  144. {
  145. dtCompine = dataTables[0].Clone();
  146. foreach (DataTable dt1 in dataTables)
  147. {
  148. DataRow[] rows = dt1.Select(string.Format("{0}>'{1}'", strDateColumn, strDate));
  149. foreach (DataRow dr in rows)
  150. {
  151. DataRow drNew = dtCompine.NewRow();
  152. foreach (DataColumn dc in dtCompine.Columns)
  153. {
  154. try
  155. {
  156. if (dc.ColumnName.ToString().ToLower() == strDateColumn.ToLower())
  157. {
  158. DateTime dt = DateTime.Now;
  159. if (DateTime.TryParse(dr[dc.ColumnName].ToString(), out dt))
  160. {
  161. drNew[dc.ColumnName] = dr[dc.ColumnName].ToString();
  162. }
  163. else
  164. {
  165. DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), normalFileFormat.NormalFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true));
  166. if (dtTime.ToString("HH:mm:ss") == "00:00:00")
  167. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd");
  168. else
  169. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss");
  170. }
  171. }
  172. else
  173. {
  174. drNew[dc.ColumnName] = dr[dc.ColumnName];
  175. }
  176. }
  177. catch
  178. {
  179. }
  180. }
  181. if (0 < $"{drNew[strDateColumn]}".CompareTo(strDate))
  182. {
  183. dtCompine.Rows.Add(drNew);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. catch (Exception ex)
  190. {
  191. AppLog.Info("读取数据时发生异常");
  192. AppLog.Error(ex.Message);
  193. }
  194. AppLog.Info("读取数据完成,准备返回");
  195. return dtCompine;
  196. }
  197. public override DataTable GetInstrumentDataStruct()
  198. {
  199. //读取数据
  200. List<DataTable> dataTables = new List<DataTable>();
  201. foreach (var item in LstFileFullName)
  202. {
  203. DataTable dt = txtDatabaseService.GetInstrumentData(
  204. new TXTOpenParams
  205. {
  206. StrPath = item,
  207. TxtFileMode = normalFileFormat.NormalFileDataMode,
  208. SplitType = normalFileFormat.NormalFileSplitType,
  209. StartLineIndex = ChangeStringToInt(normalFileFormat.NormalFileStartLineIndex),
  210. Suffix=normalFileFormat.NormalFileSuffix,
  211. FileNameFormat=normalFileFormat.NormalFileNameFormat,
  212. IfCustomCreateDateColumn = normalFileFormat.IfCustomCreateDateColumn == "0" ? true : false
  213. })["TXT"];
  214. dataTables.Add(dt);
  215. }
  216. //合并数据
  217. DataTable dtCompine = new DataTable();
  218. try
  219. {
  220. if (dataTables.Count > 0)
  221. {
  222. dtCompine = dataTables[0].Clone();
  223. foreach (DataTable dt1 in dataTables)
  224. {
  225. foreach (DataRow dr in dt1.Rows)
  226. {
  227. DataRow drNew = dtCompine.NewRow();
  228. drNew.ItemArray = dr.ItemArray;
  229. dtCompine.Rows.Add(drNew);
  230. }
  231. }
  232. }
  233. }
  234. catch (Exception ex)
  235. {
  236. AppLog.Error(ex.Message);
  237. }
  238. return dtCompine;
  239. }
  240. private int ChangeStringToInt(string strValue)
  241. {
  242. int value = 0;
  243. Int32.TryParse(strValue, out value);
  244. return value;
  245. }
  246. public override DataTable GetInstrumentDataStruct(string Tablename)
  247. {
  248. throw new NotImplementedException();
  249. }
  250. }
  251. }