ResampleForm.cs 4.8 KB

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