GpuCalcForm.cs 8.1 KB

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