GpuCalcForm.cs 8.4 KB

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