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