HistoryTaskI.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. int satId = 0;
  32. try
  33. {
  34. // (洋区固定大写字母aì,不是数字1,查不到结果给出日志方便排查
  35. string sql = $"select 卫星ID from freguencysatid where 下行 = '{freqdown}'and 洋区 = 'I' LIMIT 1";
  36. var res = MySqlTools.ExecuteScalar(System.Data.CommandType.Text, sql);
  37. bool isInt = int.TryParse($"{res}", out satId);
  38. if (!isInt)
  39. {
  40. LogHelper.Error($"下行频点{freqdown * 1e-6}未找到卫星编号");
  41. }
  42. }
  43. catch (Exception ex)
  44. {
  45. LogHelper.Error($"下行频点{freqdown}找卫星编号异常:{ex.Message}");
  46. }
  47. return satId;
  48. }
  49. public virtual void Stop()
  50. {
  51. IsRuning = true;
  52. }
  53. //检测
  54. public async Task<IEnumerable<DetectResDto>> DAMAAsync(DmcTypeDto dmc, double fsHz, string mFile)
  55. {
  56. try
  57. {
  58. //主星变采样
  59. var resampleRes = await ToResampleAsync((int)fsHz, mFile);
  60. DetectDto dto = new DetectDto();
  61. dto.dmcType = (DmcType)dmc;
  62. dto.fsHz = resampleRes.OutFsHz;
  63. dto.file1 = await UploadFileAsync("DAMA检测", resampleRes.File);
  64. var dmcResult = await HttpHelper.PostRequestAsync<IEnumerable<DetectResDto>>(baseUrl + "DetectCg/DetectCalc", dto);
  65. if (dmcResult.code != 200)
  66. {
  67. throw new Exception($"执行DAMA检测异常:{dmcResult.msg}");
  68. }
  69. else
  70. {
  71. dmcResult.data.ForEach(m => m.File1 = resampleRes.File);
  72. return dmcResult.data;
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. throw new Exception($"信号检测出错:{ex.Message}");
  78. }
  79. }
  80. public async Task<ResampleResponseDto> ToResampleAsync(int fsHz, string file)
  81. {
  82. ResampleResponseDto dtores = new ResampleResponseDto();
  83. dtores.File = file;
  84. dtores.OutFsHz = fsHz;
  85. string step = "变采样";
  86. //不需要变采样
  87. if (fsHz == OutFsHz)
  88. {
  89. return dtores;
  90. }
  91. string file1 = await UploadFileAsync(step, file);
  92. ResampleRequestDto dto = new ResampleRequestDto()
  93. {
  94. File = file1,
  95. FsHz = fsHz,
  96. };
  97. var response = await HttpHelper.PostRequestAsync<ResampleResponseDto>(baseUrl + "/DetectCg/Resample", dto, dto.TimeoutSeconds);
  98. if (response.code == 200)
  99. {
  100. string downloadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "download");
  101. string outFile = Path.Combine(downloadFolder, Path.GetFileNameWithoutExtension(file1) + $"_Resample{response.data.OutFsHz}K.dat");
  102. await DownloadFileAsync(step, response.data.File, outFile);
  103. dtores.OutFsHz = response.data.OutFsHz;
  104. dtores.File = outFile;
  105. return dtores;
  106. }
  107. else
  108. {
  109. throw new Exception($"采样率:{fsHz}变采样{file}异常:{response.msg}");
  110. }
  111. }
  112. public async Task<string> UploadFileAsync(string step, string file)
  113. {
  114. try
  115. {
  116. string file1 = await HttpHelper.UploadFileAsync(file, baseUrl + "File/UploadFileAsync");
  117. return file1;
  118. }
  119. catch (Exception ex)
  120. {
  121. throw new Exception($"执行{step}上传文件异常:{ex.Message}");
  122. }
  123. }
  124. public async Task<bool> DownloadFileAsync(string step, string Infile, string outFile)
  125. {
  126. try
  127. {
  128. return await HttpHelper.DownloadFileAsync(baseUrl, Infile, outFile);
  129. }
  130. catch (Exception ex)
  131. {
  132. throw new Exception($"执行{step}下载文件{Infile}异常:{ex.Message}");
  133. }
  134. }
  135. //CPU计算
  136. public async Task<CpuCgResDto> CPUCalcAsync(string file1, string file2, double fsHz, DetectResDto detect, double dtCenter, double dtRange)
  137. {
  138. string step = "CPU计算";
  139. CpuCgDto dto = new CpuCgDto();
  140. dto.file1 = await UploadFileAsync(step, file1);
  141. dto.file2 = await UploadFileAsync(step, file2);
  142. dto.smpCount = detect.Length;
  143. dto.samplingRate = fsHz;
  144. dto.dtCenter = dtCenter;
  145. dto.dtRange = dtRange;
  146. dto.smpStart = detect.Start;
  147. dto.snrThreshold = 14;
  148. try
  149. {
  150. var result = await HttpHelper.PostRequestAsync<CpuCgResDto>(baseUrl + "DetectCg/CpuCgCalc", dto);
  151. if (result.code != 200)
  152. {
  153. throw new Exception($"CPU文件参估出错,{result.msg}");
  154. }
  155. return result.data;
  156. }
  157. catch (Exception ex)
  158. {
  159. throw new Exception($"CPU文件参估出错,{ex.Message}");
  160. }
  161. }
  162. //GPU计算
  163. public async Task<GpuCgResponseDto> GPUCalcAsync(string file1, string file2, double fsHz, double dtCenter, double dtRange)
  164. {
  165. string step = "GPU计算";
  166. GpuCgRequestDto dto = new GpuCgRequestDto();
  167. dto.file1 = await UploadFileAsync(step, file1);
  168. dto.file2 = await UploadFileAsync(step, file2);
  169. dto.samplingRate = fsHz;
  170. dto.dtCenter = dtCenter;
  171. dto.dtRange = dtRange;
  172. dto.snrThreshold = 14;
  173. try
  174. {
  175. var result = await HttpHelper.PostRequestAsync<List<GpuCgResponseDto>>(baseUrl + "DetectCg/GpuCgCalc", dto, dto.TimeoutSeconds);
  176. if (result.code != 200)
  177. {
  178. throw new Exception($"GPU文件参估出错,{result.msg}");
  179. }
  180. return result.data.Count > 0 ? result.data.First() : new GpuCgResponseDto();
  181. }
  182. catch (TaskCanceledException)
  183. {
  184. throw new Exception("GPU文件参估Http接口调用超时");
  185. }
  186. catch (Exception ex)
  187. {
  188. throw new Exception($"GPU文件参估出错{ex.Message}");
  189. }
  190. }
  191. }
  192. }