HistoryTaskI.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using DevExpress.Utils.Extensions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using XdCxRhDw;
  9. using XdCxRhDW.Dto;
  10. namespace XdCxRhDW.TaskServer.Task
  11. {
  12. public class HistoryTaskI
  13. {
  14. protected internal virtual string baseUrl => $"{ConfigurationManager.AppSettings["PosPlatformAddr"].Trim()}/api/";
  15. protected internal virtual bool IsRuning { get; set; } = false;
  16. //变采样
  17. protected internal virtual int OutFsHz { get; set; } = 96000;
  18. protected internal virtual HistoryTaskProcessingDto TaskDto { get; set; }
  19. public virtual void Start(HistoryTaskProcessingDto dto)
  20. {
  21. }
  22. /// <summary>
  23. /// 根据下行频点获取卫星Id
  24. /// </summary>
  25. /// <param name="freqdown"></param>
  26. /// <returns></returns>
  27. public int GetSatId(double freqdown)
  28. {
  29. int satId = 0;
  30. try
  31. {
  32. // (洋区固定大写字母aì,不是数字1,查不到结果给出日志方便排查
  33. string sql = $"select 卫星ID from freguencysatid where 下行 = '{freqdown}'and 洋区 = 'I' LIMIT 1";
  34. var res = MySqlTools.ExecuteScalar(System.Data.CommandType.Text, sql);
  35. bool isInt = int.TryParse($"{res}", out satId);
  36. if (!isInt)
  37. {
  38. LogHelper.Error($"下行频点{freqdown * 1e-6}未找到卫星编号");
  39. }
  40. }
  41. catch (Exception ex)
  42. {
  43. LogHelper.Error($"下行频点{freqdown}找卫星编号异常:{ex.Message}");
  44. }
  45. return satId;
  46. }
  47. public virtual void Stop()
  48. {
  49. IsRuning = false;
  50. }
  51. //检测
  52. public async Task<IEnumerable<DetectResDto>> DAMAAsync(EnumTaskCheckTypeDto dmc, double fsHz, string mFile)
  53. {
  54. try
  55. {
  56. //主星变采样
  57. var resampleRes = await ToResampleAsync((int)fsHz, mFile);
  58. DetectDto dto = new DetectDto();
  59. dto.dmcType = (DmcType)dmc;
  60. dto.fsHz = resampleRes.OutFsHz;
  61. dto.file1 = await UploadFileAsync("DAMA检测", resampleRes.File);
  62. var dmcResult = await HttpHelper.PostRequestAsync<IEnumerable<DetectResDto>>(baseUrl + "DetectCg/DetectCalc", dto);
  63. if (dmcResult.code != 200)
  64. {
  65. throw new Exception($"执行DAMA检测异常:{dmcResult.msg}");
  66. }
  67. else
  68. {
  69. dmcResult.data.ForEach(m => m.File1 = resampleRes.File);
  70. return dmcResult.data;
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. throw new Exception($"信号检测出错:{ex.Message}");
  76. }
  77. }
  78. public async Task<ResampleResponseDto> ToResampleAsync(int fsHz, string file)
  79. {
  80. ResampleResponseDto dtores = new ResampleResponseDto();
  81. dtores.File = file;
  82. dtores.OutFsHz = fsHz;
  83. string step = "变采样";
  84. //不需要变采样
  85. if (fsHz == OutFsHz)
  86. {
  87. return dtores;
  88. }
  89. string file1 = await UploadFileAsync(step, file);
  90. ResampleRequestDto dto = new ResampleRequestDto()
  91. {
  92. File = file1,
  93. FsHz = fsHz,
  94. };
  95. var response = await HttpHelper.PostRequestAsync<ResampleResponseDto>(baseUrl + "/DetectCg/Resample", dto, dto.TimeoutSeconds);
  96. if (response.code == 200)
  97. {
  98. string downloadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "download");
  99. string outFile = Path.Combine(downloadFolder, Path.GetFileNameWithoutExtension(file1) + $"_Resample{response.data.OutFsHz}K.dat");
  100. await DownloadFileAsync(step, response.data.File, outFile);
  101. dtores.OutFsHz = response.data.OutFsHz;
  102. dtores.File = outFile;
  103. return dtores;
  104. }
  105. else
  106. {
  107. throw new Exception($"采样率:{fsHz}变采样{file}异常:{response.msg}");
  108. }
  109. }
  110. public async Task<string> UploadFileAsync(string step, string file)
  111. {
  112. try
  113. {
  114. string file1 = await HttpHelper.UploadFileAsync(file, baseUrl + "File/UploadFileAsync");
  115. return file1;
  116. }
  117. catch (Exception ex)
  118. {
  119. throw new Exception($"执行{step}上传文件异常:{ex.Message}");
  120. }
  121. }
  122. public async Task<bool> DownloadFileAsync(string step, string Infile, string outFile)
  123. {
  124. try
  125. {
  126. return await HttpHelper.DownloadFileAsync(baseUrl, Infile, outFile);
  127. }
  128. catch (Exception ex)
  129. {
  130. throw new Exception($"执行{step}下载文件{Infile}异常:{ex.Message}");
  131. }
  132. }
  133. //CPU计算
  134. public async Task<CpuCgResDto> CPUCalcAsync(string file1, string file2, double fsHz, DetectResDto detect, double dtCenter, double dtRange)
  135. {
  136. string step = "CPU计算";
  137. CpuCgDto dto = new CpuCgDto();
  138. dto.file1 = await UploadFileAsync(step, file1);
  139. dto.file2 = await UploadFileAsync(step, file2);
  140. dto.smpCount = detect.Length;
  141. dto.samplingRate = fsHz;
  142. dto.dtCenter = dtCenter;
  143. dto.dtRange = dtRange;
  144. dto.smpStart = detect.Start;
  145. dto.snrThreshold = 14;
  146. try
  147. {
  148. var result = await HttpHelper.PostRequestAsync<CpuCgResDto>(baseUrl + "DetectCg/CpuCgCalc", dto);
  149. if (result.code != 200)
  150. {
  151. throw new Exception($"CPU文件参估出错,{result.msg}");
  152. }
  153. return result.data;
  154. }
  155. catch (Exception ex)
  156. {
  157. throw new Exception($"CPU文件参估出错,{ex.Message}");
  158. }
  159. }
  160. //CPU多检测计算
  161. public async Task<List<CpuCgResDto>> CPUCalcAsync(string file1, string file2, double fsHz, List<SmpPosition> smps, double dtCenter, double dtRange)
  162. {
  163. string step = "CPU计算";
  164. CpuCgMultiDto dto = new CpuCgMultiDto();
  165. dto.file1 = await UploadFileAsync(step, file1);
  166. dto.file2 = await UploadFileAsync(step, file2);
  167. dto.smpPositions = smps;
  168. dto.samplingRate = fsHz;
  169. dto.dtCenter = dtCenter;
  170. dto.dtRange = dtRange;
  171. dto.snrThreshold = 14;
  172. try
  173. {
  174. var result = await HttpHelper.PostRequestAsync<List<CpuCgResDto>>(baseUrl + "DetectCg/CpuCgMultiCalc", dto);
  175. if (result.code != 200)
  176. {
  177. throw new Exception($"CPU文件参估出错,{result.msg}");
  178. }
  179. return result.data;
  180. }
  181. catch (Exception ex)
  182. {
  183. throw new Exception($"CPU文件参估出错,{ex.Message}");
  184. }
  185. }
  186. //GPU计算
  187. public async Task<GpuCgResponseDto> GPUCalcAsync(string file1, string file2, double fsHz, double dtCenter, double dtRange)
  188. {
  189. string step = "GPU计算";
  190. GpuCgRequestDto dto = new GpuCgRequestDto();
  191. dto.file1 = await UploadFileAsync(step, file1);
  192. dto.file2 = await UploadFileAsync(step, file2);
  193. dto.samplingRate = fsHz;
  194. dto.dtCenter = dtCenter;
  195. dto.dtRange = dtRange;
  196. dto.snrThreshold = 14;
  197. try
  198. {
  199. var result = await HttpHelper.PostRequestAsync<List<GpuCgResponseDto>>(baseUrl + "DetectCg/GpuCgCalc", dto, dto.TimeoutSeconds);
  200. if (result.code != 200)
  201. {
  202. throw new Exception($"GPU文件参估出错,{result.msg}");
  203. }
  204. return result.data.Count > 0 ? result.data.First() : new GpuCgResponseDto();
  205. }
  206. catch (TaskCanceledException)
  207. {
  208. throw new Exception("GPU文件参估Http接口调用超时");
  209. }
  210. catch (Exception ex)
  211. {
  212. throw new Exception($"GPU文件参估出错{ex.Message}");
  213. }
  214. }
  215. /// <summary>
  216. /// 提取检测时隙数据
  217. /// </summary>
  218. /// <param name="file"></param>
  219. /// <param name="fsHz"></param>
  220. /// <param name="smps"></param>
  221. /// <param name="dtCenter"></param>
  222. /// <param name="dtRange"></param>
  223. /// <param name="Ch"></param>
  224. /// <returns></returns>
  225. public async Task<ExtractRes> ExtractMergeAsync(string file, double fsHz, List<SmpPosition> smps, double dtCenter, double dtRange, int Ch)
  226. {
  227. long offset = 0;
  228. long zero = 0;
  229. if (Ch == 2)//主星
  230. {
  231. zero = 0;
  232. }
  233. else if (Ch != 2 && dtCenter == 0)//三星信号文件
  234. {
  235. zero = Convert.ToInt64(dtRange * 1e-6 * fsHz) / 2;
  236. }
  237. else//地信号文件
  238. {
  239. zero = Convert.ToInt64(dtRange * 1e-6 * fsHz) / 2;
  240. offset = Convert.ToInt64(0.26 * fsHz);
  241. }
  242. string detectFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MultiDetect");
  243. Directory.CreateDirectory(detectFolder);
  244. string outfile = Path.Combine(detectFolder, Path.GetFileNameWithoutExtension(file) + "_de" + Path.GetExtension(file));
  245. ExtractRes res = new ExtractRes();
  246. res.file = outfile;
  247. List<SmpPosition> smpp = new List<SmpPosition>();
  248. // 从指定位置开始读取数据
  249. using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
  250. {
  251. byte[] bytes = new byte[fileStream.Length];
  252. foreach (var smp in smps)
  253. {
  254. long start = (smp.smpStart - zero - offset) * 4;
  255. int length = Convert.ToInt32(smp.smpCount + zero * 2 - offset) * 4;
  256. if (start < 0 || length < 0 || length > fileStream.Length)
  257. {
  258. continue;
  259. }
  260. // 移动到文件的指定位置
  261. fileStream.Seek(start, SeekOrigin.Begin);
  262. byte[] buffer = new byte[length];
  263. // 读取指定长度的数据
  264. int bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length);
  265. Array.ConstrainedCopy(buffer, 0, bytes, (int)start, bytesRead);
  266. smpp.Add(smp);
  267. }
  268. using (FileStream wrStream = new FileStream(outfile, FileMode.Create, FileAccess.ReadWrite))
  269. {
  270. await wrStream.WriteAsync(bytes, 0, bytes.Length);
  271. }
  272. }
  273. res.positions = smpp;
  274. return res;
  275. }
  276. }
  277. }