GpuCalcForm.cs 7.9 KB

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