ResampleForm.cs 5.3 KB

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