DetectToolForm.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. using DevExpress.Data.Extensions;
  2. using DevExpress.Internal.WinApi.Windows.UI.Notifications;
  3. using DevExpress.Mvvm.Native;
  4. using DevExpress.XtraEditors;
  5. using DevExpress.XtraLayout.Utils;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.IO.Compression;
  13. using System.Linq;
  14. using System.Net.Http;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Forms;
  18. namespace XdCxRhDW.App.CorTools
  19. {
  20. public partial class DetectToolForm : DevExpress.XtraEditors.XtraForm
  21. {
  22. static readonly string inifile = Path.Combine(Application.StartupPath, "par.ini");
  23. volatile bool beRunning = false;
  24. BindingList<CafResult> gridSource = new BindingList<CafResult>();
  25. public DetectToolForm()
  26. {
  27. InitializeComponent();
  28. }
  29. void openfile(object sender)
  30. {
  31. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  32. {
  33. if (sender == btnFile1)
  34. {
  35. btnFile1.Text = openFileDialog1.FileName;
  36. }
  37. else
  38. {
  39. btnFile2.Text = openFileDialog1.FileName;
  40. }
  41. }
  42. }
  43. /// <summary>
  44. /// 读取配置
  45. /// </summary>
  46. void ReadIni()
  47. {
  48. if (File.Exists(inifile))
  49. {
  50. var lines = File.ReadAllLines(inifile);
  51. if (lines.Length != 9)
  52. {
  53. return;
  54. }
  55. btnFile1.Text = lines[0];
  56. btnFile2.Text = lines[1];
  57. teCount.Text = lines[2];
  58. tefs.Text = lines[3];
  59. teCenter.Text = lines[4];
  60. teRange.Text = lines[5];
  61. teSnr.Text = lines[6];
  62. tePos.Text = lines[7];
  63. teDfRange.Text = lines[8];
  64. }
  65. }
  66. /// <summary>
  67. /// 写入配置
  68. /// </summary>
  69. void WriteIni()
  70. {
  71. var lines = new List<String>();
  72. lines.Add(btnFile1.Text);
  73. lines.Add(btnFile2.Text);
  74. lines.Add(teCount.Text);
  75. lines.Add(tefs.Text);
  76. lines.Add(teCenter.Text);
  77. lines.Add(teRange.Text);
  78. lines.Add(teSnr.Text);
  79. lines.Add(tePos.Text);
  80. lines.Add(teDfRange.Text);
  81. File.WriteAllLines(inifile, lines);
  82. }
  83. private void MainForm_Load(object sender, EventArgs e)
  84. {
  85. this.gridControl1.DataSource = gridSource;
  86. ReadIni();
  87. }
  88. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  89. {
  90. //if (!DxHelper.MsgBoxHelper.ShowConfirm("是否退出当前程序"))
  91. //{
  92. // e.Cancel = true;
  93. //}
  94. }
  95. private bool ValidateFile(string filename)
  96. {
  97. if (string.IsNullOrWhiteSpace(filename))
  98. {
  99. DxHelper.MsgBoxHelper.ShowError($"请选择文件!");
  100. return false;
  101. }
  102. if (!File.Exists(filename))
  103. {
  104. DxHelper.MsgBoxHelper.ShowError($"文件【{filename}】不存在");
  105. return false;
  106. }
  107. return true;
  108. }
  109. /// <summary>
  110. /// 网格右键菜单弹出
  111. /// </summary>
  112. /// <param name="sender"></param>
  113. /// <param name="e"></param>
  114. private void gridView1_MouseDown(object sender, MouseEventArgs e)
  115. {
  116. if (e.Button == MouseButtons.Right && !beRunning)
  117. {
  118. popupMenu1.ShowPopup(Cursor.Position);
  119. }
  120. }
  121. /// <summary>
  122. /// 清除网格
  123. /// </summary>
  124. /// <param name="sender"></param>
  125. /// <param name="e"></param>
  126. private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  127. {
  128. gridSource.Clear();
  129. }
  130. /// <summary>
  131. /// 文件拖拽
  132. /// </summary>
  133. /// <param name="sender"></param>
  134. /// <param name="e"></param>
  135. private void btnFile_DragOver(object sender, DragEventArgs e)
  136. {
  137. e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Copy : DragDropEffects.None;
  138. }
  139. private void btnFile_DragDrop(object sender, DragEventArgs e)
  140. {
  141. string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
  142. if (files != null && files.Length != 0)
  143. {
  144. if (sender == btnFile1)
  145. {
  146. btnFile1.Text = files[0];
  147. }
  148. else if (sender == btnFile2)
  149. {
  150. btnFile2.Text = files[0];
  151. }
  152. }
  153. }
  154. private void btnFile_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
  155. {
  156. openfile(sender);
  157. }
  158. private void btnFile_DoubleClick(object sender, EventArgs e)
  159. {
  160. openfile(sender);
  161. }
  162. private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  163. {
  164. SaveFileDialog dialog = new SaveFileDialog();
  165. dialog.Filter = "csv|*.csv";
  166. if (dialog.ShowDialog() == DialogResult.OK)
  167. {
  168. gridView1.ExportToCsv(dialog.FileName);
  169. }
  170. }
  171. private async void btnCheck_Click(object sender, EventArgs e)
  172. {
  173. if (!ValidateFile(btnFile1.Text))
  174. {
  175. return;
  176. }
  177. var dmcResult = await XcorrUtils.DmcCheckAsync(btnFile1.Text, double.Parse(tefs.Text));
  178. gridSource.Clear();
  179. foreach (var dmcItem in dmcResult)
  180. {
  181. gridSource.Add(new CafResult()
  182. {
  183. file1 = btnFile1.Text,
  184. file2 = btnFile2.Text,
  185. smpstart = dmcItem.Start,
  186. smplen = dmcItem.Length
  187. });
  188. }
  189. WriteIni();
  190. }
  191. private void chkDama_CheckedChanged(object sender, EventArgs e)
  192. {
  193. lciStartPos.Visibility = lciSampleLength.Visibility = ckKY.Checked | ckIBS.Checked | chkDama.Checked ? LayoutVisibility.Never : LayoutVisibility.Always;
  194. btnCalc.Enabled = ckKY.Checked | ckIBS.Checked | chkDama.Checked ? false : true;
  195. if (chkDama.Checked)
  196. {
  197. ckIBS.Checked = false;
  198. ckKY.Checked = false;
  199. }
  200. }
  201. private void ckIBS_CheckedChanged(object sender, EventArgs e)
  202. {
  203. lciStartPos.Visibility = lciSampleLength.Visibility = ckKY.Checked | ckIBS.Checked | chkDama.Checked ? LayoutVisibility.Never : LayoutVisibility.Always;
  204. btnCalc.Enabled = ckKY.Checked | ckIBS.Checked | chkDama.Checked ? false : true;
  205. if (ckIBS.Checked)
  206. {
  207. chkDama.Checked = false;
  208. ckKY.Checked = false;
  209. }
  210. }
  211. private void ckKY_CheckedChanged(object sender, EventArgs e)
  212. {
  213. lciStartPos.Visibility = lciSampleLength.Visibility = ckKY.Checked | ckIBS.Checked | chkDama.Checked ? LayoutVisibility.Never : LayoutVisibility.Always;
  214. btnCalc.Enabled = ckKY.Checked | ckIBS.Checked | chkDama.Checked ? false : true;
  215. if (ckKY.Checked)
  216. {
  217. ckIBS.Checked = false;
  218. chkDama.Checked = false;
  219. }
  220. }
  221. private async void btnCalc_Click(object sender, EventArgs e)
  222. {
  223. if (btnCalc.Text == "停止")
  224. {
  225. XcorrUtils.Stop();
  226. return;
  227. }
  228. //开始计算
  229. //校验文件名称
  230. if (!ValidateFile(btnFile1.Text))
  231. {
  232. return;
  233. }
  234. if (!ValidateFile(btnFile2.Text))
  235. {
  236. return;
  237. }
  238. // 压缩文件
  239. string compressedFilePath = CompressFile(btnFile1.Text);
  240. // 创建 HttpClient 对象
  241. using (HttpClient client = new HttpClient())
  242. {
  243. // 构建上传请求的 Uri
  244. string uploadUri = "http://localhost:8091/api/DetectCg/UploadFile";
  245. // 创建 MultipartFormDataContent 用于封装文件内容
  246. MultipartFormDataContent content = new MultipartFormDataContent();
  247. // 添加压缩后的文件内容到 MultipartFormDataContent
  248. byte[] fileBytes = File.ReadAllBytes(compressedFilePath);
  249. ByteArrayContent fileContent = new ByteArrayContent(fileBytes);
  250. content.Add(fileContent, "file", Path.GetFileName(compressedFilePath));
  251. try
  252. {
  253. // 发送上传请求
  254. HttpResponseMessage response = await client.PostAsync(uploadUri, content);
  255. // 检查响应状态码
  256. if (response.IsSuccessStatusCode)
  257. {
  258. MessageBox.Show("文件上传成功!");
  259. }
  260. else
  261. {
  262. MessageBox.Show("文件上传失败!");
  263. }
  264. }
  265. catch (Exception ex)
  266. {
  267. MessageBox.Show("文件上传失败:" + ex.Message);
  268. }
  269. }
  270. // 删除压缩后的临时文件
  271. File.Delete(compressedFilePath);
  272. List<XcorrStruct> xcorrs = new List<XcorrStruct>();
  273. XcorrStruct xItem = new XcorrStruct();
  274. xItem.file1 = btnFile1.Text;
  275. xItem.file2 = btnFile2.Text;
  276. try
  277. {
  278. xItem.smpCount = int.Parse(teCount.Text);
  279. xItem.samplingRate = Convert.ToInt32(double.Parse(tefs.Text) * 1e6);
  280. xItem.dtCenter = int.Parse(teCenter.Text);
  281. xItem.dtRange = int.Parse(teRange.Text);
  282. xItem.dfRange = int.Parse(teDfRange.Text);
  283. xItem.smpStart = int.Parse(tePos.Text);
  284. xItem.snrThreshold = int.Parse(teSnr.Text);
  285. }
  286. catch
  287. {
  288. DxHelper.MsgBoxHelper.ShowError($"参数错误");
  289. return;
  290. }
  291. if (lciStartPos.Visibility == LayoutVisibility.Never)
  292. {
  293. foreach (var item in gridSource)
  294. {
  295. var corritem = xItem.Copy();
  296. corritem.smpCount = (int)item.smplen;
  297. corritem.smpStart = (int)item.smpstart;
  298. xcorrs.Add(corritem);
  299. }
  300. }
  301. else
  302. {
  303. xcorrs.Add(xItem);
  304. }
  305. WriteIni();
  306. btnCalc.Text = "停止";
  307. beRunning = true;
  308. gridSource.Clear();
  309. foreach (var xcitem in xcorrs)
  310. {
  311. var result = await ExecuteCorAsync(xcitem);
  312. if (result != null)
  313. {
  314. //var smitemindex = gridSource.FindIndex(m => m.smpstart == xcitem.smpStart && m.smplen == xcitem.smpCount);
  315. //if (smitemindex == -1)
  316. //{
  317. gridSource.Add(result);
  318. gridView1.FocusedRowHandle = gridSource.Count - 1;
  319. //}
  320. //else
  321. //{
  322. // var smitem = gridSource[smitemindex];
  323. // smitem.dt = result.dt;
  324. // smitem.df = result.df;
  325. // smitem.snr = result.snr;
  326. // smitem.tm = result.tm;
  327. // gridView1.RefreshData();
  328. // gridView1.FocusedRowHandle = smitemindex;
  329. //}
  330. }
  331. }
  332. btnCalc.Text = "计算";
  333. beRunning = false;
  334. }
  335. private string CompressFile(string filePath)
  336. {
  337. string compressedFilePath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + ".zip");
  338. using (FileStream originalFileStream = File.OpenRead(filePath))
  339. {
  340. using (FileStream compressedFileStream = File.Create(compressedFilePath))
  341. {
  342. using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
  343. {
  344. originalFileStream.CopyTo(compressionStream);
  345. }
  346. }
  347. }
  348. return compressedFilePath;
  349. }
  350. private async Task<CafResult> ExecuteCorAsync(XcorrStruct xItem)
  351. {
  352. var result = await XcorrUtils.Calc(xItem);
  353. //开始计算
  354. if (result.flag == -2)
  355. {
  356. DxHelper.MsgBoxHelper.ShowError($"内部错误");
  357. btnCalc.Text = "计算";
  358. beRunning = false;
  359. return null;
  360. }
  361. else if (result.flag == -1)
  362. {
  363. DxHelper.MsgBoxHelper.ShowError($"计算所需数据超出文件范围");
  364. btnCalc.Text = "计算";
  365. beRunning = false;
  366. return null;
  367. }
  368. else if (result.flag == -3)
  369. {
  370. btnCalc.Text = "计算";
  371. beRunning = false;
  372. return null;
  373. }
  374. return result;
  375. }
  376. }
  377. }