DetectToolForm.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 Newtonsoft.Json;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Data.Entity;
  12. using System.Drawing;
  13. using System.IO;
  14. using System.IO.Compression;
  15. using System.Linq;
  16. using System.Net;
  17. using System.Net.Http;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Web.Http.Results;
  21. using System.Web.UI.WebControls;
  22. using System.Windows.Forms;
  23. using XdCxRhDW.App.EFContext;
  24. using XdCxRhDW.App.WebAPI;
  25. using static XdCxRhDW.App.WebAPI.BaseController;
  26. namespace XdCxRhDW.App.CorTools
  27. {
  28. public partial class DetectToolForm : DevExpress.XtraEditors.XtraForm
  29. {
  30. static readonly string inifile = Path.Combine(Application.StartupPath, "par.ini");
  31. volatile bool beRunning = false;
  32. private string uploadUrL;
  33. BindingList<CafResult> gridSource = new BindingList<CafResult>();
  34. public DetectToolForm()
  35. {
  36. InitializeComponent();
  37. }
  38. void openfile(object sender)
  39. {
  40. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  41. {
  42. if (sender == btnFile1)
  43. {
  44. btnFile1.Text = openFileDialog1.FileName;
  45. }
  46. else
  47. {
  48. btnFile2.Text = openFileDialog1.FileName;
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// 读取配置
  54. /// </summary>
  55. void ReadIni()
  56. {
  57. if (File.Exists(inifile))
  58. {
  59. var lines = File.ReadAllLines(inifile);
  60. if (lines.Length != 9)
  61. {
  62. return;
  63. }
  64. btnFile1.Text = lines[0];
  65. btnFile2.Text = lines[1];
  66. teCount.Text = lines[2];
  67. tefs.Text = lines[3];
  68. teCenter.Text = lines[4];
  69. teRange.Text = lines[5];
  70. teSnr.Text = lines[6];
  71. tePos.Text = lines[7];
  72. teDfRange.Text = lines[8];
  73. }
  74. }
  75. /// <summary>
  76. /// 写入配置
  77. /// </summary>
  78. void WriteIni()
  79. {
  80. var lines = new List<String>();
  81. lines.Add(btnFile1.Text);
  82. lines.Add(btnFile2.Text);
  83. lines.Add(teCount.Text);
  84. lines.Add(tefs.Text);
  85. lines.Add(teCenter.Text);
  86. lines.Add(teRange.Text);
  87. lines.Add(teSnr.Text);
  88. lines.Add(tePos.Text);
  89. lines.Add(teDfRange.Text);
  90. File.WriteAllLines(inifile, lines);
  91. }
  92. private async void MainForm_Load(object sender, EventArgs e)
  93. {
  94. this.gridControl1.DataSource = gridSource;
  95. ReadIni();
  96. }
  97. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  98. {
  99. //if (!DxHelper.MsgBoxHelper.ShowConfirm("是否退出当前程序"))
  100. //{
  101. // e.Cancel = true;
  102. //}
  103. }
  104. private bool ValidateFile(string filename)
  105. {
  106. if (string.IsNullOrWhiteSpace(filename))
  107. {
  108. DxHelper.MsgBoxHelper.ShowError($"请选择文件!");
  109. return false;
  110. }
  111. if (!File.Exists(filename))
  112. {
  113. DxHelper.MsgBoxHelper.ShowError($"文件【{filename}】不存在");
  114. return false;
  115. }
  116. return true;
  117. }
  118. /// <summary>
  119. /// 网格右键菜单弹出
  120. /// </summary>
  121. /// <param name="sender"></param>
  122. /// <param name="e"></param>
  123. private void gridView1_MouseDown(object sender, MouseEventArgs e)
  124. {
  125. if (e.Button == MouseButtons.Right && !beRunning)
  126. {
  127. popupMenu1.ShowPopup(Cursor.Position);
  128. }
  129. }
  130. /// <summary>
  131. /// 清除网格
  132. /// </summary>
  133. /// <param name="sender"></param>
  134. /// <param name="e"></param>
  135. private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  136. {
  137. gridSource.Clear();
  138. }
  139. /// <summary>
  140. /// 文件拖拽
  141. /// </summary>
  142. /// <param name="sender"></param>
  143. /// <param name="e"></param>
  144. private void btnFile_DragOver(object sender, DragEventArgs e)
  145. {
  146. e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Copy : DragDropEffects.None;
  147. }
  148. private void btnFile_DragDrop(object sender, DragEventArgs e)
  149. {
  150. string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
  151. if (files != null && files.Length != 0)
  152. {
  153. if (sender == btnFile1)
  154. {
  155. btnFile1.Text = files[0];
  156. }
  157. else if (sender == btnFile2)
  158. {
  159. btnFile2.Text = files[0];
  160. }
  161. }
  162. }
  163. private void btnFile_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
  164. {
  165. openfile(sender);
  166. }
  167. private void btnFile_DoubleClick(object sender, EventArgs e)
  168. {
  169. openfile(sender);
  170. }
  171. private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  172. {
  173. SaveFileDialog dialog = new SaveFileDialog();
  174. dialog.Filter = "csv|*.csv";
  175. if (dialog.ShowDialog() == DialogResult.OK)
  176. {
  177. gridView1.ExportToCsv(dialog.FileName);
  178. }
  179. }
  180. private async void btnCheck_Click(object sender, EventArgs e)
  181. {
  182. if (!ValidateFile(btnFile1.Text))
  183. {
  184. return;
  185. }
  186. var dmcResult = await XcorrUtils.DmcCheckAsync(btnFile1.Text, double.Parse(tefs.Text));
  187. gridSource.Clear();
  188. foreach (var dmcItem in dmcResult)
  189. {
  190. gridSource.Add(new CafResult()
  191. {
  192. file1 = btnFile1.Text,
  193. file2 = btnFile2.Text,
  194. smpstart = dmcItem.Start,
  195. smplen = dmcItem.Length
  196. });
  197. }
  198. WriteIni();
  199. }
  200. private void chkDama_CheckedChanged(object sender, EventArgs e)
  201. {
  202. lciStartPos.Visibility = lciSampleLength.Visibility = ckKY.Checked | ckIBS.Checked | chkDama.Checked ? LayoutVisibility.Never : LayoutVisibility.Always;
  203. btnCalc.Enabled = ckKY.Checked | ckIBS.Checked | chkDama.Checked ? false : true;
  204. if (chkDama.Checked)
  205. {
  206. ckIBS.Checked = false;
  207. ckKY.Checked = false;
  208. }
  209. }
  210. private void ckIBS_CheckedChanged(object sender, EventArgs e)
  211. {
  212. lciStartPos.Visibility = lciSampleLength.Visibility = ckKY.Checked | ckIBS.Checked | chkDama.Checked ? LayoutVisibility.Never : LayoutVisibility.Always;
  213. btnCalc.Enabled = ckKY.Checked | ckIBS.Checked | chkDama.Checked ? false : true;
  214. if (ckIBS.Checked)
  215. {
  216. chkDama.Checked = false;
  217. ckKY.Checked = false;
  218. }
  219. }
  220. private void ckKY_CheckedChanged(object sender, EventArgs e)
  221. {
  222. lciStartPos.Visibility = lciSampleLength.Visibility = ckKY.Checked | ckIBS.Checked | chkDama.Checked ? LayoutVisibility.Never : LayoutVisibility.Always;
  223. btnCalc.Enabled = ckKY.Checked | ckIBS.Checked | chkDama.Checked ? false : true;
  224. if (ckKY.Checked)
  225. {
  226. ckIBS.Checked = false;
  227. chkDama.Checked = false;
  228. }
  229. }
  230. private async void btnCalc_Click(object sender, EventArgs e)
  231. {
  232. if (btnCalc.Text == "停止")
  233. {
  234. XcorrUtils.Stop();
  235. return;
  236. }
  237. //开始计算
  238. //校验文件名称
  239. if (!ValidateFile(btnFile1.Text))
  240. {
  241. return;
  242. }
  243. if (!ValidateFile(btnFile2.Text))
  244. {
  245. return;
  246. }
  247. List<XcorrStruct> xcorrs = new List<XcorrStruct>();
  248. XcorrStruct xItem = new XcorrStruct();
  249. try
  250. {
  251. using (RHDWContext db = new RHDWContext())
  252. {
  253. var res = await db.SysSetings.FirstOrDefaultAsync();
  254. if (res != null)
  255. {
  256. uploadUrL = $"http://{res.ServerIp}:{res.HttpPort}/api/DetectCg";
  257. }
  258. }
  259. xItem.file1 = UpLoadFile(btnFile1.Text);
  260. xItem.file2 = UpLoadFile(btnFile2.Text);
  261. xItem.smpCount = int.Parse(teCount.Text);
  262. xItem.samplingRate = Convert.ToInt32(double.Parse(tefs.Text) * 1e6);
  263. xItem.dtCenter = int.Parse(teCenter.Text);
  264. xItem.dtRange = int.Parse(teRange.Text);
  265. xItem.dfRange = int.Parse(teDfRange.Text);
  266. xItem.smpStart = int.Parse(tePos.Text);
  267. xItem.snrThreshold = int.Parse(teSnr.Text);
  268. }
  269. catch (Exception ex)
  270. {
  271. DxHelper.MsgBoxHelper.ShowError($"参数错误{ex.Message}");
  272. return;
  273. }
  274. if (lciStartPos.Visibility == LayoutVisibility.Never)
  275. {
  276. foreach (var item in gridSource)
  277. {
  278. var corritem = xItem.Copy();
  279. corritem.smpCount = (int)item.smplen;
  280. corritem.smpStart = (int)item.smpstart;
  281. xcorrs.Add(corritem);
  282. }
  283. }
  284. else
  285. {
  286. xcorrs.Add(xItem);
  287. }
  288. WriteIni();
  289. btnCalc.Text = "停止";
  290. beRunning = true;
  291. gridSource.Clear();
  292. foreach (var xcitem in xcorrs)
  293. {
  294. // 构建上传请求的 Uri
  295. string uploadUri = $"{uploadUrL}/EstimationCalc";
  296. var content = new StringContent(JsonConvert.SerializeObject(xcitem), System.Text.Encoding.UTF8, "application/json");
  297. var result = PostRequest<CafResult>(uploadUri, content);
  298. // var result = await ExecuteCorAsync(xcitem);
  299. if (result != null)
  300. {
  301. //var smitemindex = gridSource.FindIndex(m => m.smpstart == xcitem.smpStart && m.smplen == xcitem.smpCount);
  302. //if (smitemindex == -1)
  303. //{
  304. gridSource.Add(result);
  305. gridView1.FocusedRowHandle = gridSource.Count - 1;
  306. //}
  307. //else
  308. //{
  309. // var smitem = gridSource[smitemindex];
  310. // smitem.dt = result.dt;
  311. // smitem.df = result.df;
  312. // smitem.snr = result.snr;
  313. // smitem.tm = result.tm;
  314. // gridView1.RefreshData();
  315. // gridView1.FocusedRowHandle = smitemindex;
  316. //}
  317. }
  318. }
  319. btnCalc.Text = "计算";
  320. beRunning = false;
  321. }
  322. private string UpLoadFile(string filePath)
  323. {
  324. string file = string.Empty;
  325. // 构建上传请求的 Uri
  326. string uploadUri = $"{uploadUrL}/UploadFile";
  327. try
  328. {
  329. // 添加文件内容到 MultipartFormDataContent
  330. byte[] fileBytes = File.ReadAllBytes(filePath);
  331. // 创建 MultipartFormDataContent 用于封装文件内容
  332. MultipartFormDataContent content = new MultipartFormDataContent();
  333. ByteArrayContent fileContent = new ByteArrayContent(fileBytes);
  334. content.Add(fileContent, "file", Path.GetFileName(filePath));
  335. // 发送上传请求
  336. file = PostRequest<string>(uploadUri, content);
  337. }
  338. catch (Exception ex)
  339. {
  340. throw new Exception($"上传文件{filePath}到{uploadUri}失败!");
  341. }
  342. return file;
  343. }
  344. private async Task<CafResult> ExecuteCorAsync(XcorrStruct xItem)
  345. {
  346. var result = await XcorrUtils.Calc(xItem);
  347. //开始计算
  348. if (result.flag == -2)
  349. {
  350. DxHelper.MsgBoxHelper.ShowError($"内部错误");
  351. btnCalc.Text = "计算";
  352. beRunning = false;
  353. return null;
  354. }
  355. else if (result.flag == -1)
  356. {
  357. DxHelper.MsgBoxHelper.ShowError($"计算所需数据超出文件范围");
  358. btnCalc.Text = "计算";
  359. beRunning = false;
  360. return null;
  361. }
  362. else if (result.flag == -3)
  363. {
  364. btnCalc.Text = "计算";
  365. beRunning = false;
  366. return null;
  367. }
  368. return result;
  369. }
  370. }
  371. }