GpuCalcForm.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 DxHelper;
  7. using ExtensionsDev;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Data.Entity;
  13. using System.Drawing;
  14. using System.IO;
  15. using System.IO.Compression;
  16. using System.Linq;
  17. using System.Net;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows.Forms;
  21. using XdCxRhDW.Api;
  22. using XdCxRhDW.Dto;
  23. using XdCxRhDW.Repostory;
  24. namespace XdCxRhDW.App.CorTools
  25. {
  26. public partial class GpuCalcForm : DevExpress.XtraEditors.XtraForm
  27. {
  28. static readonly string inifile = Path.Combine(Application.StartupPath, "parGpu.ini");
  29. List<GpuCafResult> gridSource = new List<GpuCafResult>();
  30. public GpuCalcForm()
  31. {
  32. InitializeComponent();
  33. }
  34. private void MainForm_Load(object sender, EventArgs e)
  35. {
  36. this.gridGpuCg.UseDefault(gridSource).UseExportXlsx().UseRowNumber()
  37. .UseExportCsv().UseClear<GpuCafResult>();
  38. this.btnFile1.UseChooseWaveFile((file, fsHz) =>
  39. {
  40. if (fsHz > 0)
  41. txtfs.Text = (fsHz / 1e6).ToString();
  42. else
  43. txtfs.Text = "0.096";
  44. }).UseDoubleClickToSelectAll();
  45. this.btnFile2.UseChooseFile().UseDoubleClickToSelectAll();
  46. ReadIni();
  47. }
  48. private async void btnCalc_Click(object sender, EventArgs e)
  49. {
  50. if (!ValidateFiles(btnFile1.Text, btnFile2.Text)) return;
  51. if (!ValidateParams()) return;
  52. layoutControl1.Enabled = false;
  53. string file1, file2;
  54. try
  55. {
  56. file1 = await HttpHelper.UploadFileAsync(btnFile1.Text, SysConfig.GetBaseUrl());
  57. file2 = await HttpHelper.UploadFileAsync(btnFile2.Text, SysConfig.GetBaseUrl());
  58. }
  59. catch (Exception ex)
  60. {
  61. layoutControl1.Enabled = true;
  62. Serilog.Log.Error(ex, ex.Message);
  63. DxHelper.MsgBoxHelper.ShowError(ex.Message);
  64. return;
  65. }
  66. var samplingRate = double.Parse(txtfs.Text) * 1e6;
  67. var dtCenter = double.Parse(txtDtoCenter.Text);
  68. var dtRange = double.Parse(txtDtoRange.Text);
  69. var dfRange = double.Parse(txtDfoRange.Text);
  70. var smpCount = double.Parse(txtSmpCount.Text);
  71. var snrThreshold = double.Parse(txtSnr.Text);
  72. var timeout = int.Parse(txtTimeout.Text);
  73. GpuCgRequestDto dto = new GpuCgRequestDto()
  74. {
  75. dfRange = dfRange,
  76. smpCount = smpCount,
  77. dtCenter = dtCenter,
  78. dtRange = dtRange,
  79. file1 = file1,
  80. file2 = file2,
  81. samplingRate = samplingRate,
  82. snrThreshold = snrThreshold,
  83. TimeoutSeconds = timeout,
  84. };
  85. WriteIni();
  86. gridSource.Clear();
  87. try
  88. {
  89. var result = await HttpHelper.PostRequestAsync<List<GpuCgResponseDto>>(SysConfig.GetUrl("DetectCg/GpuCgCalc"), dto, dto.TimeoutSeconds);
  90. if (result.code == 200)
  91. {
  92. foreach (var item in result.data)
  93. {
  94. gridSource.Add(new GpuCafResult()
  95. {
  96. file1 = btnFile1.Text,
  97. file2 = btnFile2.Text,
  98. dt = item.Dt,
  99. df = item.Df,
  100. snr = item.Snr,
  101. tm = item.TimeMs,
  102. });
  103. }
  104. }
  105. else
  106. {
  107. Serilog.Log.Error(result.msg);
  108. DxHelper.MsgBoxHelper.ShowError(result.msg);
  109. }
  110. }
  111. catch (TaskCanceledException)
  112. {
  113. Serilog.Log.Warning("GPU文件参估Http接口调用超时");
  114. DxHelper.MsgBoxHelper.ShowInfo("GPU文件参估Http接口调用超时");
  115. }
  116. catch (Exception ex)
  117. {
  118. Serilog.Log.Error(ex, "GPU文件参估出错");
  119. DxHelper.MsgBoxHelper.ShowError("GPU文件参估出错");
  120. }
  121. gridView1.RefreshData();
  122. layoutControl1.Enabled = true;
  123. }
  124. private bool ValidateFiles(params string[] files)
  125. {
  126. foreach (var file in files)
  127. {
  128. if (string.IsNullOrWhiteSpace(file))
  129. {
  130. DxHelper.MsgBoxHelper.ShowError($"请选择文件!");
  131. return false;
  132. }
  133. if (!File.Exists(file))
  134. {
  135. DxHelper.MsgBoxHelper.ShowError($"文件【{file}】不存在");
  136. return false;
  137. }
  138. }
  139. return true;
  140. }
  141. private bool ValidateParams()
  142. {
  143. if (!double.TryParse(txtfs.Text, out _))
  144. {
  145. DxHelper.MsgBoxHelper.ShowError($"采样率非有效数字");
  146. return false;
  147. }
  148. if (!double.TryParse(txtDtoCenter.Text, out _))
  149. {
  150. DxHelper.MsgBoxHelper.ShowError($"时差中心非有效数字");
  151. return false;
  152. }
  153. if (!double.TryParse(txtDtoRange.Text, out _))
  154. {
  155. DxHelper.MsgBoxHelper.ShowError($"时差范围非有效数字");
  156. return false;
  157. }
  158. if (!double.TryParse(txtDfoRange.Text, out _))
  159. {
  160. DxHelper.MsgBoxHelper.ShowError($"频差范围非有效数字");
  161. return false;
  162. }
  163. if (!double.TryParse(txtSnr.Text, out _))
  164. {
  165. DxHelper.MsgBoxHelper.ShowError($"信噪比门限非有效数字");
  166. return false;
  167. }
  168. if (!double.TryParse(txtSmpCount.Text, out _))
  169. {
  170. DxHelper.MsgBoxHelper.ShowError($"样点数非有效数字");
  171. return false;
  172. }
  173. if (!int.TryParse(txtTimeout.Text, out _))
  174. {
  175. DxHelper.MsgBoxHelper.ShowError($"超时时间非有效数字");
  176. return false;
  177. }
  178. return true;
  179. }
  180. //读取配置
  181. void ReadIni()
  182. {
  183. if (File.Exists(inifile))
  184. {
  185. try
  186. {
  187. var lines = File.ReadAllLines(inifile);
  188. btnFile1.Text = lines[0];
  189. btnFile2.Text = lines[1];
  190. txtSmpCount.Text = lines[2];
  191. txtfs.Text = lines[3];
  192. txtDtoCenter.Text = lines[4];
  193. txtDtoRange.Text = lines[5];
  194. txtSnr.Text = lines[6];
  195. txtDfoRange.Text = lines[7];
  196. txtTimeout.Text = lines[8];
  197. }
  198. catch
  199. {
  200. }
  201. }
  202. }
  203. // 写入配置
  204. void WriteIni()
  205. {
  206. var lines = new List<string>
  207. {
  208. btnFile1.Text,
  209. btnFile2.Text,
  210. txtSmpCount.Text,
  211. txtfs.Text,
  212. txtDtoCenter.Text,
  213. txtDtoRange.Text,
  214. txtSnr.Text,
  215. txtDfoRange.Text,
  216. txtTimeout.Text
  217. };
  218. File.WriteAllLines(inifile, lines);
  219. }
  220. }
  221. }