HistoryTaskI.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using DevExpress.Utils.Extensions;
  2. using DevExpress.XtraPrinting.Native.Properties;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.Configuration;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using XdCxRhDw;
  12. using XdCxRhDW.Dto;
  13. namespace XdCxRhDW.TaskServer.Task
  14. {
  15. public class HistoryTaskI
  16. {
  17. protected internal virtual string baseUrl => $"{ConfigurationManager.AppSettings["PosPlatformAddr"].Trim()}/api/";
  18. protected internal virtual bool IsRuning { get; set; } = false;
  19. //变采样
  20. protected internal virtual int OutFsHz { get; set; } = 96000;
  21. public virtual void Start(HistoryTaskProcessingDto dto)
  22. {
  23. }
  24. /// <summary>
  25. /// 根据下行频点获取卫星Id
  26. /// </summary>
  27. /// <param name="freqdown"></param>
  28. /// <returns></returns>
  29. public int GetSatId(double freqdown)
  30. {
  31. // (洋区固定大写字母aì,不是数字1,查不到结果给出日志方便排查
  32. string sql = $"select 卫星ID from freguencysatid where 下行 = '{freqdown}'and 洋区 = 'I' LIMIT 1";
  33. sql = "SELECT id FROM role where id=1 LIMIT 1";
  34. var res = MySqlTools.ExecuteScalar(System.Data.CommandType.Text, sql);
  35. int satId = 0;
  36. bool isInt = int.TryParse($"{res}", out satId);
  37. if (!isInt)
  38. {
  39. LogHelper.Error($"下行频点{freqdown}未找到卫星编号");
  40. }
  41. return satId;
  42. }
  43. public virtual void Stop()
  44. {
  45. IsRuning = true;
  46. }
  47. //检测
  48. public async Task<IEnumerable<DetectResDto>> DAMAAsync(DmcTypeDto dmc, double fsHz, string mFile)
  49. {
  50. try
  51. {
  52. //主星变采样
  53. var resampleRes = await ToResampleAsync((int)fsHz, mFile);
  54. DetectDto dto = new DetectDto();
  55. dto.dmcType = (DmcType)dmc;
  56. dto.fsHz = resampleRes.OutFsHz;
  57. dto.file1 = await UploadFileAsync("DAMA检测", resampleRes.File);
  58. #warning 信号带宽怎么获取
  59. //dto.band = double.Parse(txtBand.Text);
  60. var dmcResult = await HttpHelper.PostRequestAsync<IEnumerable<DetectResDto>>(baseUrl + "DetectCg/DetectCalc", dto);
  61. if (dmcResult.code != 200)
  62. {
  63. throw new Exception($"执行DAMA检测异常:{dmcResult.msg}");
  64. }
  65. else
  66. {
  67. dmcResult.data.ForEach(m => m.File1 = resampleRes.File);
  68. return dmcResult.data;
  69. }
  70. }
  71. catch (Exception ex)
  72. {
  73. throw new Exception($"信号检测出错:{ex.Message}");
  74. }
  75. }
  76. public async Task<ResampleResponseDto> ToResampleAsync(int fsHz, string file)
  77. {
  78. ResampleResponseDto dtores = new ResampleResponseDto();
  79. dtores.File = file;
  80. dtores.OutFsHz = fsHz;
  81. string step = "变采样";
  82. //不需要变采样
  83. if (fsHz == OutFsHz)
  84. {
  85. return dtores;
  86. }
  87. string file1 = await UploadFileAsync(step, file);
  88. ResampleRequestDto dto = new ResampleRequestDto()
  89. {
  90. File = file1,
  91. FsHz = fsHz,
  92. };
  93. var response = await HttpHelper.PostRequestAsync<ResampleResponseDto>(baseUrl + "/DetectCg/Resample", dto, dto.TimeoutSeconds);
  94. if (response.code == 200)
  95. {
  96. string downloadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "download");
  97. string outFile = Path.Combine(downloadFolder, Path.GetFileNameWithoutExtension(file1) + $"_Resample{response.data.OutFsHz}K.dat");
  98. await DownloadFileAsync(step, response.data.File, outFile);
  99. dtores.OutFsHz = response.data.OutFsHz;
  100. dtores.File = outFile;
  101. return dtores;
  102. }
  103. else
  104. {
  105. throw new Exception($"采样率:{fsHz}变采样{file}异常:{response.msg}");
  106. }
  107. }
  108. public async Task<string> UploadFileAsync(string step, string file)
  109. {
  110. try
  111. {
  112. string file1 = await HttpHelper.UploadFileAsync(file, baseUrl + "File/UploadFileAsync");
  113. return file1;
  114. }
  115. catch (Exception ex)
  116. {
  117. throw new Exception($"执行{step}上传文件异常:{ex.Message}");
  118. }
  119. }
  120. public async Task<bool> DownloadFileAsync(string step, string Infile, string outFile)
  121. {
  122. try
  123. {
  124. return await HttpHelper.DownloadFileAsync(baseUrl, Infile, outFile);
  125. }
  126. catch (Exception ex)
  127. {
  128. throw new Exception($"执行{step}下载文件{Infile}异常:{ex.Message}");
  129. }
  130. }
  131. //CPU计算
  132. public async Task<CpuCgResDto> CPUCalcAsync(string file1, string file2, double fsHz, DetectResDto detect)
  133. {
  134. string step = "CPU计算";
  135. CpuCgDto dto = new CpuCgDto();
  136. dto.file1 = await UploadFileAsync(step, file1);
  137. dto.file2 = await UploadFileAsync(step, file2);
  138. dto.smpCount = detect.Length;
  139. dto.samplingRate = fsHz;
  140. dto.dtCenter = 0;
  141. dto.dtRange = 40000;
  142. dto.smpStart = detect.Start;
  143. dto.snrThreshold = 14;
  144. try
  145. {
  146. var result = await HttpHelper.PostRequestAsync<CpuCgResDto>(baseUrl + "DetectCg/CpuCgCalc", dto);
  147. if (result.code != 200)
  148. {
  149. throw new Exception($"CPU文件参估出错,{result.msg}");
  150. }
  151. return result.data;
  152. }
  153. catch (Exception ex)
  154. {
  155. throw new Exception($"CPU文件参估出错,{ex.Message}");
  156. }
  157. }
  158. //GPU计算
  159. public async Task<GpuCgResponseDto> GPUCalcAsync(string file1, string file2, double fsHz)
  160. {
  161. string step = "GPU计算";
  162. GpuCgRequestDto dto = new GpuCgRequestDto();
  163. dto.file1 = await UploadFileAsync(step, file1);
  164. dto.file2 = await UploadFileAsync(step, file2);
  165. dto.smpCount = 0;
  166. dto.samplingRate = fsHz;
  167. dto.dtCenter = 0;
  168. dto.dtRange = 10000;
  169. dto.snrThreshold = 14;
  170. try
  171. {
  172. var result = await HttpHelper.PostRequestAsync<List<GpuCgResponseDto>>(baseUrl + "DetectCg/GpuCgCalc", dto, dto.TimeoutSeconds);
  173. if (result.code != 200)
  174. {
  175. throw new Exception($"GPU文件参估出错,{result.msg}");
  176. }
  177. return result.data.Count > 0 ? result.data.First() : new GpuCgResponseDto();
  178. }
  179. catch (TaskCanceledException)
  180. {
  181. throw new Exception("GPU文件参估Http接口调用超时");
  182. }
  183. catch (Exception ex)
  184. {
  185. throw new Exception($"GPU文件参估出错{ex.Message}");
  186. }
  187. }
  188. }
  189. }