CNAS取数仪器端升级
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

262 rindas
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. 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. foreach (DataRow dr in dt1.Rows)
  149. {
  150. DataRow drNew = dtCompine.NewRow();
  151. foreach (DataColumn dc in dtCompine.Columns)
  152. {
  153. if (dc.ColumnName.ToString().ToLower() == strDateColumn.ToLower())
  154. {
  155. DateTime dt = DateTime.Now;
  156. if (DateTime.TryParse(dr[dc.ColumnName].ToString(), out dt))
  157. {
  158. drNew[dc.ColumnName] = dr[dc.ColumnName].ToString();
  159. }
  160. else
  161. {
  162. DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), normalFileFormat.NormalFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true));
  163. if (dtTime.ToString("HH:mm:ss") == "00:00:00")
  164. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd");
  165. else
  166. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss");
  167. }
  168. }
  169. else
  170. {
  171. drNew[dc.ColumnName] = dr[dc.ColumnName];
  172. }
  173. }
  174. dtCompine.Rows.Add(drNew);
  175. }
  176. }
  177. }
  178. }
  179. catch (Exception ex)
  180. {
  181. AppLog.Info("读取数据时发生异常");
  182. AppLog.Error(ex.Message);
  183. }
  184. AppLog.Info("读取数据完成,准备返回");
  185. return dtCompine;
  186. }
  187. public override DataTable GetInstrumentDataStruct()
  188. {
  189. //读取数据
  190. List<DataTable> dataTables = new List<DataTable>();
  191. foreach (var item in LstFileFullName)
  192. {
  193. DataTable dt = txtDatabaseService.GetInstrumentData(
  194. new TXTOpenParams
  195. {
  196. StrPath = item,
  197. TxtFileMode = normalFileFormat.NormalFileDataMode,
  198. SplitType = normalFileFormat.NormalFileSplitType,
  199. StartLineIndex = ChangeStringToInt(normalFileFormat.NormalFileStartLineIndex),
  200. Suffix=normalFileFormat.NormalFileSuffix,
  201. FileNameFormat=normalFileFormat.NormalFileNameFormat,
  202. IfCustomCreateDateColumn = normalFileFormat.IfCustomCreateDateColumn == "0" ? true : false
  203. })["TXT"];
  204. dataTables.Add(dt);
  205. }
  206. //合并数据
  207. DataTable dtCompine = new DataTable();
  208. try
  209. {
  210. if (dataTables.Count > 0)
  211. {
  212. dtCompine = dataTables[0].Clone();
  213. foreach (DataTable dt1 in dataTables)
  214. {
  215. foreach (DataRow dr in dt1.Rows)
  216. {
  217. DataRow drNew = dtCompine.NewRow();
  218. drNew.ItemArray = dr.ItemArray;
  219. dtCompine.Rows.Add(drNew);
  220. }
  221. }
  222. }
  223. }
  224. catch (Exception ex)
  225. {
  226. AppLog.Error(ex.Message);
  227. }
  228. return dtCompine;
  229. }
  230. private int ChangeStringToInt(string strValue)
  231. {
  232. int value = 0;
  233. Int32.TryParse(strValue, out value);
  234. return value;
  235. }
  236. }
  237. }