DetectToolForm.cs 16 KB

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