ResampleForm.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using DevExpress.XtraEditors;
  2. using DxHelper;
  3. using ExtensionsDev;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Data.Entity;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Documents;
  15. using System.Windows.Forms;
  16. using XdCxRhDw.Dto;
  17. using XdCxRhDW.App.App.Properties;
  18. using XdCxRhDW.Core;
  19. using XdCxRhDW.Repostory;
  20. using XdCxRhDW.Repostory.EFContext;
  21. using XdCxRhDW.Repostory.Model;
  22. namespace XdCxRhDW.App.CorTools
  23. {
  24. public partial class ResampleForm : DevExpress.XtraEditors.XtraForm
  25. {
  26. public SatInfo info;
  27. public ResampleForm()
  28. {
  29. InitializeComponent();
  30. txtOutFile.Text = "";
  31. txtFile.UseChooseFile((file, fsHz) =>
  32. {
  33. this.txtOutDir.Text = Path.GetDirectoryName(file);
  34. if (fsHz > 0)
  35. txtFs.Text = (fsHz / 1e3).ToString();
  36. else
  37. this.txtFs.Text = "";
  38. }).UseDoubleClickToSelectAll();
  39. txtOutDir.UseChooseDir().UseDoubleClickToSelectAll();
  40. this.txtOutFile.UseDoubleClickToSelectAll();
  41. this.layoutControl1.UseDefault();
  42. }
  43. private void btnCancel_Click(object sender, EventArgs e)
  44. {
  45. this.DialogResult = DialogResult.Cancel;
  46. }
  47. private async void btnOk_ClickAsync(object sender, EventArgs e)
  48. {
  49. double fsHz, outFsHz, timeout;
  50. if (!double.TryParse(txtFs.Text, out fsHz))
  51. {
  52. DxHelper.MsgBoxHelper.ShowError($"原始采样率非数字!");
  53. return;
  54. }
  55. if (!double.TryParse(txtOutFs.Text, out outFsHz))
  56. {
  57. DxHelper.MsgBoxHelper.ShowError($"变换后采样率非数字!");
  58. return;
  59. }
  60. if (!File.Exists(txtFile.Text))
  61. {
  62. DxHelper.MsgBoxHelper.ShowError($"文件[{txtFile.Text}]不存在");
  63. return;
  64. }
  65. if (!Directory.Exists(txtOutDir.Text))
  66. {
  67. DxHelper.MsgBoxHelper.ShowError($"输出目录[{txtOutDir.Text}]不存在");
  68. return;
  69. }
  70. if (!double.TryParse(txtTimeout.Text, out timeout))
  71. {
  72. DxHelper.MsgBoxHelper.ShowError($"超时时间非数字!");
  73. return;
  74. }
  75. if (timeout <= 0) timeout = 120;
  76. txtOutFile.Text = "";
  77. fsHz *= 1e3;
  78. outFsHz *= 1e3;
  79. try
  80. {
  81. layoutControl1.Enabled = false;
  82. string baseUrl = null;
  83. using (RHDWContext db = new RHDWContext())
  84. {
  85. var res = await db.SysSetings.FirstOrDefaultAsync();
  86. if (res != null)
  87. {
  88. baseUrl = $"http://{IpHelper.GetLocalIp()}:{res.HttpPort}/";
  89. }
  90. }
  91. if (baseUrl == null)
  92. {
  93. layoutControl1.Enabled = true;
  94. DxHelper.MsgBoxHelper.ShowWarning($"请在系统设置中配置Http端口");
  95. return;
  96. }
  97. string file = null;
  98. try
  99. {
  100. file = await HttpHelper.UploadFileAsync(txtFile.Text, baseUrl + "Api/File/UploadFileAsync");
  101. }
  102. catch (TaskCanceledException)
  103. {
  104. layoutControl1.Enabled = true;
  105. Serilog.Log.Warning($"文件[{txtFile.Text}]上传超时");
  106. DxHelper.MsgBoxHelper.ShowInfo($"文件[{txtFile.Text}]上传超时");
  107. }
  108. catch (Exception ex)
  109. {
  110. layoutControl1.Enabled = true;
  111. Serilog.Log.Error(ex, $"文件[{txtFile.Text}]上传异常");
  112. return;
  113. }
  114. ResampleRequestDto dto = new ResampleRequestDto()
  115. {
  116. File = file,
  117. FsHz = (int)fsHz,
  118. OutFsHz = (int)outFsHz,
  119. TimeoutSeconds = (int)timeout,
  120. };
  121. var response = await HttpHelper.PostRequestAsync<ResampleResponseDto>(baseUrl + "Api/DetectCg/Resample", dto, dto.TimeoutSeconds);
  122. if (response.code == 200)
  123. {
  124. string outFile = Path.Combine(txtOutDir.Text, Path.GetFileNameWithoutExtension(txtFile.Text) + $"_Resample{txtOutFs.Text}K.dat");
  125. if (await HttpHelper.DownloadFileAsync(baseUrl, response.data.File, outFile))
  126. this.txtOutFile.Text = outFile;
  127. else
  128. {
  129. Serilog.Log.Error($"下载文件[{response.data.File}]失败");
  130. MsgBoxHelper.ShowError($"下载文件[{response.data.File}]失败");
  131. }
  132. }
  133. else
  134. {
  135. MsgBoxHelper.ShowError(response.msg);
  136. }
  137. }
  138. catch (TaskCanceledException)
  139. {
  140. Serilog.Log.Warning("变采样Http接口调用超时");
  141. DxHelper.MsgBoxHelper.ShowInfo("变采样Http接口调用超时");
  142. }
  143. catch (Exception ex)
  144. {
  145. Serilog.Log.Error(ex, "变采样出错");
  146. DxHelper.MsgBoxHelper.ShowError("变采样出错");
  147. }
  148. finally
  149. {
  150. layoutControl1.Enabled = true;
  151. }
  152. }
  153. }
  154. }