HistoryTaskI.cs 7.5 KB

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