GpuCalcForm.cs 8.3 KB

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