DetectCgController.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Web.Http;
  10. using XdCxRhDW.Dto;
  11. using XdCxRhDW.WebApi;
  12. using System.ComponentModel.DataAnnotations;
  13. using XdCxRhDW.Api;
  14. namespace XdCxRhDW.App.Controllers
  15. {
  16. /// <summary>
  17. /// 检测参估功能相关接口
  18. /// </summary>
  19. public class DetectCgController : BaseController
  20. {
  21. /// <summary>
  22. /// CPU参估计算(需要先上传文件)
  23. /// </summary>
  24. /// <param name="dto">CPU参估参数</param>
  25. /// <returns></returns>
  26. [HttpPost]
  27. public async Task<AjaxResult<CpuCgResDto>> CpuCgCalc(CpuCgDto dto)
  28. {
  29. dto.file1 = GetLocalFile(dto.file1);
  30. dto.file2 = GetLocalFile(dto.file2);
  31. XcorrStruct xItem = new XcorrStruct();
  32. xItem.file1 = dto.file1;
  33. xItem.file2 = dto.file2;
  34. xItem.samplingRate = dto.samplingRate;
  35. xItem.dtCenter = dto.dtCenter;
  36. xItem.dtRange = dto.dtRange;
  37. xItem.dfRange = dto.dfRange;
  38. FileInfo file1 = new FileInfo(dto.file1);
  39. FileInfo file2 = new FileInfo(dto.file2);
  40. long totalsamp = file1.Length < file2.Length ? file1.Length / 4 : file2.Length / 4;
  41. //样点数为0时计算所有样本
  42. if (dto.smpCount == 0)
  43. {
  44. xItem.smpCount = (int)totalsamp - dto.smpStart;
  45. }
  46. else if (dto.smpCount > 0 && dto.smpCount < 1)
  47. {
  48. xItem.smpCount = (int)(totalsamp * dto.smpCount);
  49. }
  50. else
  51. {
  52. xItem.smpCount = dto.smpCount;
  53. }
  54. xItem.smpStart = dto.smpStart;
  55. xItem.snrThreshold = dto.snrThreshold;
  56. CpuCgResDto resDto = new CpuCgResDto();
  57. try
  58. {
  59. XcorrUtils xcorr = new XcorrUtils();
  60. var result = await xcorr.Calc(xItem);
  61. //开始计算
  62. if (result.flag == -2)
  63. {
  64. Serilog.Log.Error("参估计算内部错误!");
  65. return Error<CpuCgResDto>("参估计算内部错误!");
  66. }
  67. else if (result.flag == -1)
  68. {
  69. Serilog.Log.Error("参估计算所需数据超出文件范围!");
  70. return Error<CpuCgResDto>("参估计算所需数据超出文件范围!");
  71. }
  72. resDto.TimeMs = result.tm;
  73. resDto.Smpstart = result.smpstart;
  74. resDto.Smplen = result.smplen;
  75. if (result.flag == 1)
  76. {
  77. resDto.Dt = result.dt.Value;
  78. resDto.Df = result.df.Value;
  79. resDto.Snr = result.snr.Value;
  80. }
  81. }
  82. catch (Exception ex)
  83. {
  84. Serilog.Log.Error(ex, "执行CPU参估异常");
  85. return Error<CpuCgResDto>($"执行CPU参估异常");
  86. }
  87. finally
  88. {
  89. try
  90. {
  91. //删除计算得文件
  92. File.Delete(dto.file1);
  93. File.Delete(dto.file2);
  94. }
  95. catch
  96. {
  97. }
  98. }
  99. return Success(resDto);
  100. }
  101. /// <summary>
  102. /// GPU参估计算(需要先上传文件)
  103. /// </summary>
  104. /// <param name="dto">GPU参估参数</param>
  105. /// <returns></returns>
  106. [HttpPost]
  107. public async Task<AjaxResult<List<GpuCgResponseDto>>> GpuCgCalc(GpuCgRequestDto dto)
  108. {
  109. dto.file1 = GetLocalFile(dto.file1);
  110. dto.file2 = GetLocalFile(dto.file2);
  111. FileInfo file1 = new FileInfo(dto.file1);
  112. FileInfo file2 = new FileInfo(dto.file2);
  113. long totalsamp = file1.Length < file2.Length ? file1.Length / 4 : file2.Length / 4;
  114. //样点数为0时计算所有样本
  115. if (dto.smpCount == 0)
  116. {
  117. dto.smpCount = totalsamp;
  118. }
  119. else if (dto.smpCount > 0 && dto.smpCount < 1)
  120. {
  121. dto.smpCount = (long)(totalsamp * dto.smpCount);
  122. }
  123. CpuCgResDto resDto = new CpuCgResDto();
  124. try
  125. {
  126. var result = await Task.Run(() =>
  127. {
  128. return GpuCgHelper.Calc(dto.file1, dto.file2, dto.samplingRate, dto.smpCount,
  129. dto.dtCenter, dto.dtRange, dto.dfRange, dto.snrThreshold, dto.TimeoutSeconds);
  130. });
  131. return Success(result);
  132. }
  133. catch (Exception ex)
  134. {
  135. Serilog.Log.Error(ex, "执行GPU参估异常");
  136. return Error<List<GpuCgResponseDto>>($"执行GPU参估异常");
  137. }
  138. finally
  139. {
  140. try
  141. {
  142. //删除计算得文件
  143. File.Delete(dto.file1);
  144. File.Delete(dto.file2);
  145. }
  146. catch
  147. {
  148. }
  149. }
  150. }
  151. /// <summary>
  152. /// 信号检测(支持DAMA、IBS、能量检测)(需要先上传文件)
  153. /// </summary>
  154. /// <param name="dto">信号检测参数</param>
  155. /// <returns></returns>
  156. [HttpPost]
  157. public async Task<AjaxResult<IEnumerable<DetectResDto>>> DetectCalc(DetectDto dto)
  158. {
  159. dto.file1 = GetLocalFile(dto.file1);
  160. List<DetectResDto> list = new List<DetectResDto>();
  161. try
  162. {
  163. List<DmcResult> dmcResults = new List<DmcResult>();
  164. XcorrUtils xcorr = new XcorrUtils();
  165. if (dto.dmcType.HasFlag(DmcType.DAMA))
  166. {
  167. var dmcResult = await xcorr.DmcCheckAsync(dto.file1, dto.fsHz, DmcType.DAMA, dto.band);
  168. dmcResults.AddRange(dmcResult);
  169. }
  170. if (dto.dmcType.HasFlag(DmcType.IBS))
  171. {
  172. var dmcResult = await xcorr.DmcCheckAsync(dto.file1, dto.fsHz, DmcType.IBS, dto.band);
  173. dmcResults.AddRange(dmcResult);
  174. }
  175. if (dto.dmcType.HasFlag(DmcType.Ky5758))
  176. {
  177. var dmcResult = await xcorr.DmcCheckAsync(dto.file1, dto.fsHz, DmcType.Ky5758, dto.band);
  178. dmcResults.AddRange(dmcResult);
  179. }
  180. foreach (var dmcItem in dmcResults)
  181. {
  182. DetectResDto detectRes = new DetectResDto(dmcItem.Start, dmcItem.Length, dmcItem.UserName);
  183. detectRes.ModType = dmcItem.ModType;
  184. detectRes.DmcType = dmcItem.DmcType;
  185. detectRes.File1 = dto.file1;
  186. detectRes.TimeMs = dmcItem.Times;
  187. list.Add(detectRes);
  188. }
  189. }
  190. catch (Exception ex)
  191. {
  192. return Error<IEnumerable<DetectResDto>>($"执行检测计算异常,{ex.Message}");
  193. }
  194. finally
  195. {
  196. try
  197. {
  198. //删除检测的文件
  199. File.Delete(dto.file1);
  200. }
  201. catch
  202. {
  203. }
  204. }
  205. return Success<IEnumerable<DetectResDto>>(list);
  206. }
  207. /// <summary>
  208. /// 文件变采样(需要先上传文件)
  209. /// </summary>
  210. /// <param name="dto"></param>
  211. /// <returns></returns>
  212. public async Task<AjaxResult<ResampleResponseDto>> Resample(ResampleRequestDto dto)
  213. {
  214. var res = await Task.Run(() =>
  215. {
  216. string fileIn = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", dto.File);
  217. string fileOut = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", Guid.NewGuid() + ".dat");
  218. var val = Gcd(dto.FsHz, dto.OutFsHz);//最大公约数
  219. var insertFactor = dto.OutFsHz / val;
  220. var extFactor = dto.FsHz / val;
  221. var outFile = ReSampleHelper.Resample(fileIn, fileOut, insertFactor, extFactor,dto.TimeoutSeconds);
  222. if (!string.IsNullOrWhiteSpace(outFile))
  223. {
  224. if (!File.Exists(outFile))
  225. return Error<ResampleResponseDto>("变采样后的文件未能生成");
  226. else
  227. return Success(new ResampleResponseDto() { File = Path.GetFileName(outFile), OutFsHz = dto.OutFsHz });
  228. }
  229. else
  230. {
  231. return Error<ResampleResponseDto>("变采样算法调用失败");
  232. }
  233. });
  234. return res;
  235. }
  236. //求最大公约数
  237. private int Gcd(int M, int N)
  238. {
  239. int Rem;
  240. while (N > 0)
  241. {
  242. Rem = M % N;
  243. M = N;
  244. N = Rem;
  245. }
  246. return M;
  247. }
  248. }
  249. }