using DevExpress.Data.Extensions; using DevExpress.Internal.WinApi.Windows.UI.Notifications; using DevExpress.Mvvm.Native; using DevExpress.XtraEditors; using DevExpress.XtraLayout.Utils; using DxHelper; using ExtensionsDev; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Entity; using System.Drawing; using System.IO; using System.IO.Compression; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using XdCxRhDW.Api; using XdCxRhDW.Dto; using XdCxRhDW.Repostory; namespace XdCxRhDW.App.CorTools { public partial class GpuCalcForm : DevExpress.XtraEditors.XtraForm { static readonly string inifile = Path.Combine(Application.StartupPath, "parGpu.ini"); private string baseUrl; List gridSource = new List(); public GpuCalcForm() { InitializeComponent(); } private async void MainForm_Load(object sender, EventArgs e) { this.gridGpuCg.UseDefault(gridSource).UseExportXlsx().UseRowNumber() .UseExportCsv().UseClear(); this.btnFile1.UseChooseWaveFile((file, fsHz) => { if (fsHz > 0) txtfs.Text = (fsHz / 1e6).ToString(); else txtfs.Text = "0.096"; }).UseDoubleClickToSelectAll(); this.btnFile2.UseChooseFile().UseDoubleClickToSelectAll(); ReadIni(); using (RHDWContext db = new RHDWContext()) { var res = await db.SysSetings.FirstOrDefaultAsync(); if (res != null) { this.baseUrl = $"http://{IpHelper.GetLocalIp()}:{res.HttpPort}/api/"; } } } private async void btnCalc_Click(object sender, EventArgs e) { if (!ValidateFiles(btnFile1.Text, btnFile2.Text)) return; if (!ValidateParams()) return; layoutControl1.Enabled = false; string file1 = null, file2 = null; try { file1 = await HttpHelper.UploadFileAsync(btnFile1.Text, baseUrl + "File/UploadFileAsync"); file2 = await HttpHelper.UploadFileAsync(btnFile2.Text, baseUrl + "File/UploadFileAsync"); } catch (Exception ex) { layoutControl1.Enabled = true; Serilog.Log.Error(ex, ex.Message); DxHelper.MsgBoxHelper.ShowError(ex.Message); return; } var samplingRate = double.Parse(txtfs.Text) * 1e6; var dtCenter = double.Parse(txtDtoCenter.Text); var dtRange = double.Parse(txtDtoRange.Text); var dfRange = double.Parse(txtDfoRange.Text); var smpCount = double.Parse(txtSmpCount.Text); var snrThreshold = double.Parse(txtSnr.Text); var timeout = int.Parse(txtTimeout.Text); GpuCgRequestDto dto = new GpuCgRequestDto() { dfRange = dfRange, smpCount = smpCount, dtCenter = dtCenter, dtRange = dtRange, file1 = file1, file2 = file2, samplingRate = samplingRate, snrThreshold = snrThreshold, TimeoutSeconds = timeout, }; WriteIni(); gridSource.Clear(); try { var result = await HttpHelper.PostRequestAsync>(baseUrl + "DetectCg/GpuCgCalc", dto, dto.TimeoutSeconds); if (result.code == 200) { foreach (var item in result.data) { gridSource.Add(new GpuCafResult() { file1 = btnFile1.Text, file2 = btnFile2.Text, dt = item.Dt, df = item.Df, snr = item.Snr, tm = item.TimeMs, }); } } else { Serilog.Log.Error(result.msg); DxHelper.MsgBoxHelper.ShowError(result.msg); } } catch (TaskCanceledException) { Serilog.Log.Warning("GPU文件参估Http接口调用超时"); DxHelper.MsgBoxHelper.ShowInfo("GPU文件参估Http接口调用超时"); } catch (Exception ex) { Serilog.Log.Error(ex, "GPU文件参估出错"); DxHelper.MsgBoxHelper.ShowError("GPU文件参估出错"); } gridView1.RefreshData(); layoutControl1.Enabled = true; } private bool ValidateFiles(params string[] files) { foreach (var file in files) { if (string.IsNullOrWhiteSpace(file)) { DxHelper.MsgBoxHelper.ShowError($"请选择文件!"); return false; } if (!File.Exists(file)) { DxHelper.MsgBoxHelper.ShowError($"文件【{file}】不存在"); return false; } } return true; } private bool ValidateParams() { if (!double.TryParse(txtfs.Text, out _)) { DxHelper.MsgBoxHelper.ShowError($"采样率非有效数字"); return false; } if (!double.TryParse(txtDtoCenter.Text, out _)) { DxHelper.MsgBoxHelper.ShowError($"时差中心非有效数字"); return false; } if (!double.TryParse(txtDtoRange.Text, out _)) { DxHelper.MsgBoxHelper.ShowError($"时差范围非有效数字"); return false; } if (!double.TryParse(txtDfoRange.Text, out _)) { DxHelper.MsgBoxHelper.ShowError($"频差范围非有效数字"); return false; } if (!double.TryParse(txtSnr.Text, out _)) { DxHelper.MsgBoxHelper.ShowError($"信噪比门限非有效数字"); return false; } if (!double.TryParse(txtSmpCount.Text, out _)) { DxHelper.MsgBoxHelper.ShowError($"样点数非有效数字"); return false; } if (!int.TryParse(txtTimeout.Text, out _)) { DxHelper.MsgBoxHelper.ShowError($"超时时间非有效数字"); return false; } return true; } //读取配置 void ReadIni() { if (File.Exists(inifile)) { try { var lines = File.ReadAllLines(inifile); btnFile1.Text = lines[0]; btnFile2.Text = lines[1]; txtSmpCount.Text = lines[2]; txtfs.Text = lines[3]; txtDtoCenter.Text = lines[4]; txtDtoRange.Text = lines[5]; txtSnr.Text = lines[6]; txtDfoRange.Text = lines[7]; txtTimeout.Text = lines[8]; } catch { } } } // 写入配置 void WriteIni() { var lines = new List { btnFile1.Text, btnFile2.Text, txtSmpCount.Text, txtfs.Text, txtDtoCenter.Text, txtDtoRange.Text, txtSnr.Text, txtDfoRange.Text, txtTimeout.Text }; File.WriteAllLines(inifile, lines); } } }